Browse Source

just cosmetics (less error prone to read, makes you think about the missing else case)

pull/5/head
heck 4 years ago
parent
commit
b0a57da9fa
  1. 11
      Adapter.cc
  2. 18
      Adapter.hxx
  3. 34
      callback_dispatcher.cc
  4. 16
      callback_dispatcher.hh

11
Adapter.cc

@ -78,8 +78,9 @@ namespace pEp {
SYNC_EVENT syncEvent = nullptr;
const bool success = sync_evt_q.try_pop_front(syncEvent, std::chrono::seconds(threshold));
if (!success)
if (!success) {
return new_sync_timeout_event();
}
return syncEvent;
}
@ -98,8 +99,9 @@ namespace pEp {
switch (action) {
case release:
if (_session.get())
if (_session.get()) {
_session = nullptr;
}
break;
case init:
@ -143,10 +145,11 @@ namespace pEp {
catch (std::underflow_error&) {
return false;
}
if (ev)
if (ev) {
return false;
else
} else {
return true;
}
}
}
}

18
Adapter.hxx

@ -30,8 +30,9 @@ namespace pEp {
_ex = nullptr;
assert(_messageToSend);
assert(_notifyHandshake);
if (obj && _startup)
if (obj && _startup) {
_startup(obj);
}
session();
@ -56,9 +57,10 @@ namespace pEp {
session(release);
if (obj && _shutdown)
if (obj && _shutdown) {
_shutdown(obj);
}
}
template< class T >
void startup(
@ -68,23 +70,27 @@ namespace pEp {
function< void(T *) > _startup,
function< void(T *) > _shutdown)
{
if (messageToSend)
if (messageToSend) {
_messageToSend = messageToSend;
}
if (notifyHandshake)
if (notifyHandshake) {
_notifyHandshake = notifyHandshake;
}
session();
if (!_sync_thread.joinable()) {
register_done = false;
_sync_thread = std::thread(sync_thread<T>, obj, _startup, _shutdown);
while (!register_done)
while (!register_done) {
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
if (_ex)
if (_ex) {
std::rethrow_exception(_ex);
}
}
}
}
}

34
callback_dispatcher.cc

@ -25,8 +25,9 @@ namespace pEp {
)
{
assert(messageToSend);
if (!messageToSend)
if (!messageToSend) {
throw std::invalid_argument("messageToSend must be set");
}
targets.push_back({messageToSend, notifyHandshake, on_startup, shutdown});
}
@ -34,8 +35,9 @@ namespace pEp {
void CallbackDispatcher::remove(::messageToSend_t messageToSend)
{
assert(messageToSend);
if (!messageToSend)
if (!messageToSend) {
throw std::invalid_argument("messageToSend argument needed");
}
for (auto target = targets.begin(); target != targets.end(); ++target) {
if (target->messageToSend == messageToSend) {
@ -43,25 +45,29 @@ namespace pEp {
break;
}
}
if (targets.empty())
if (targets.empty()) {
stop_sync();
}
}
void CallbackDispatcher::on_startup()
{
for (auto target : targets) {
if (target.on_startup)
if (target.on_startup) {
target.on_startup();
}
}
}
void CallbackDispatcher::on_shutdown()
{
for (auto target : targets) {
if (target.on_shutdown)
if (target.on_shutdown) {
target.on_shutdown();
}
}
}
void CallbackDispatcher::start_sync()
{
@ -73,10 +79,11 @@ namespace pEp {
&CallbackDispatcher::on_shutdown);
for (auto target : callback_dispatcher.targets) {
if (target.notifyHandshake)
if (target.notifyHandshake) {
target.notifyHandshake(nullptr, nullptr, SYNC_NOTIFY_START);
}
}
}
void CallbackDispatcher::stop_sync()
{
@ -85,24 +92,27 @@ namespace pEp {
callback_dispatcher.semaphore.go();
for (auto target : callback_dispatcher.targets) {
if (target.notifyHandshake)
if (target.notifyHandshake) {
target.notifyHandshake(nullptr, nullptr, SYNC_NOTIFY_STOP);
}
}
}
PEP_STATUS CallbackDispatcher::_messageToSend(::message *msg)
{
if (Adapter::on_sync_thread() && !msg) {
semaphore.try_wait();
if (Adapter::in_shutdown())
if (Adapter::in_shutdown()) {
return PEP_SYNC_NO_CHANNEL;
}
PEP_STATUS status = PassphraseCache::config_next_passphrase();
// if the cache has no valid passphrase ask the app
if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE)
if (status == PEP_PASSPHRASE_REQUIRED || status == PEP_WRONG_PASSPHRASE) {
semaphore.stop();
}
// the pEp engine must try again
return status;
@ -117,9 +127,10 @@ namespace pEp {
::message *_msg = nullptr;
if (msg) {
_msg = ::message_dup(msg);
if (!_msg)
if (!_msg) {
return PEP_OUT_OF_MEMORY;
}
}
assert(target.messageToSend);
target.messageToSend(_msg);
}
@ -135,9 +146,10 @@ namespace pEp {
::pEp_identity *_me = nullptr;
if (me) {
_me = ::identity_dup(me);
if (!_me)
if (!_me) {
return PEP_OUT_OF_MEMORY;
}
}
::pEp_identity *_partner = nullptr;
if (partner) {
_partner = ::identity_dup(partner);

16
callback_dispatcher.hh

@ -38,19 +38,25 @@ namespace pEp {
static void stop_sync();
static PEP_STATUS messageToSend(::message *msg);
static PEP_STATUS notifyHandshake(::pEp_identity *me,
::pEp_identity *partner, ::sync_handshake_signal signal);
static PEP_STATUS notifyHandshake(
::pEp_identity *me,
::pEp_identity *partner,
::sync_handshake_signal signal
);
protected:
void on_startup();
void on_shutdown();
PEP_STATUS _messageToSend(::message *msg);
PEP_STATUS _notifyHandshake(::pEp_identity *me,
::pEp_identity *partner, ::sync_handshake_signal signal);
PEP_STATUS _notifyHandshake(
::pEp_identity *me,
::pEp_identity *partner,
::sync_handshake_signal signal
);
friend const char *PassphraseCache::add(const std::string& passphrase);
};
extern CallbackDispatcher callback_dispatcher;
};
}

Loading…
Cancel
Save