M module-sys/Service/Service.cpp => module-sys/Service/Service.cpp +11 -10
@@ 89,22 89,23 @@ namespace sys
void Service::Run()
{
while (enableRunLoop) {
- auto msg = mailbox.pop();
- if (!msg) {
- continue;
- }
+ processBus();
+ }
+ CloseService();
+ };
- const bool respond = msg->type != Message::Type::Response && GetName() != msg->sender;
+ void Service::processBus()
+ {
+ if (auto msg = mailbox.pop(); msg) {
+ const bool respond = msg->type != Message::Type::Response && GetName() != msg->sender;
currentlyProcessing = msg;
- auto response = msg->Execute(this);
+ auto response = msg->Execute(this);
if (response == nullptr || !respond) {
- continue;
+ return;
}
-
bus.sendResponse(response, msg);
}
- CloseService();
- };
+ }
auto Service::MessageEntry(Message *message, ResponseMessage *response) -> MessagePointer
{
M module-sys/Service/include/Service/Service.hpp => module-sys/Service/include/Service/Service.hpp +9 -5
@@ 30,7 30,7 @@ namespace sys
class async_fail : public std::runtime_error
{
public:
- async_fail() : runtime_error("async failure")
+ explicit async_fail(const std::string &reason) : runtime_error(reason)
{}
};
@@ 161,7 161,7 @@ namespace sys
auto request = std::make_shared<Request>(arg...);
if (isConnected(std::type_index(typeid(Response)))) {
async.setState(Async<Request, Response>::State::Error);
- throw async_fail();
+ throw async_fail("connection failure");
}
auto meta = async.metadata;
meta->request = request;
@@ 174,10 174,10 @@ namespace sys
};
if (!connect(typeid(Response), foo)) {
- throw async_fail();
+ throw async_fail("cant connect");
}
if (!bus.sendUnicast(request, whom)) {
- throw async_fail();
+ throw async_fail("cant send to " + whom);
}
return async;
}
@@ 188,7 188,7 @@ namespace sys
static_assert(std::is_base_of<sys::ResponseMessage, Response>::value,
"Response has to be based on system message");
if (async.metadata->request == nullptr) {
- throw async_fail();
+ throw async_fail("there was no request");
}
if (async.getState() != Async<Request, Response>::State::Pending) {
return;
@@ 209,6 209,10 @@ namespace sys
bool enableRunLoop;
void Run() override;
+ /// one and only buss processing function to run in the loop
+ /// please __always__ call it in Run() method in loop instead
+ /// creating different implementations in other services
+ virtual void processBus() final;
std::map<std::type_index, MessageHandler> message_handlers;
M module-sys/SystemManager/SystemManagerCommon.cpp => module-sys/SystemManager/SystemManagerCommon.cpp +1 -3
@@ 152,9 152,7 @@ namespace sys
// in shutdown we need to wait till event manager tells us that it's ok to stfu
while (state == State::Running) {
- if (auto msg = mailbox.pop(); msg) {
- msg->Execute(this);
- }
+ processBus();
}
while (state == State::Shutdown) {