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. 26
      callback_dispatcher.hh

11
Adapter.cc

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

18
Adapter.hxx

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

34
callback_dispatcher.cc

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

26
callback_dispatcher.hh

@ -27,30 +27,36 @@ namespace pEp {
public: public:
void add( void add(
::messageToSend_t messageToSend, ::messageToSend_t messageToSend,
::notifyHandshake_t notifyHandshake, ::notifyHandshake_t notifyHandshake,
proc on_startup = nullptr, proc on_startup = nullptr,
proc on_shutdown = nullptr proc on_shutdown = nullptr
); );
void remove(::messageToSend_t messageToSend); void remove(::messageToSend_t messageToSend);
static void start_sync(); static void start_sync();
static void stop_sync(); static void stop_sync();
static PEP_STATUS messageToSend(::message *msg); static PEP_STATUS messageToSend(::message *msg);
static PEP_STATUS notifyHandshake(::pEp_identity *me, static PEP_STATUS notifyHandshake(
::pEp_identity *partner, ::sync_handshake_signal signal); ::pEp_identity *me,
::pEp_identity *partner,
::sync_handshake_signal signal
);
protected: protected:
void on_startup(); void on_startup();
void on_shutdown(); void on_shutdown();
PEP_STATUS _messageToSend(::message *msg); PEP_STATUS _messageToSend(::message *msg);
PEP_STATUS _notifyHandshake(::pEp_identity *me, PEP_STATUS _notifyHandshake(
::pEp_identity *partner, ::sync_handshake_signal signal); ::pEp_identity *me,
::pEp_identity *partner,
::sync_handshake_signal signal
);
friend const char *PassphraseCache::add(const std::string& passphrase); friend const char *PassphraseCache::add(const std::string& passphrase);
}; };
extern CallbackDispatcher callback_dispatcher; extern CallbackDispatcher callback_dispatcher;
}; }

Loading…
Cancel
Save