[Runner] Add handler interface

This commit is contained in:
momo5502 2017-01-28 14:20:28 +01:00
parent 0c8ba82fba
commit 0221d24f07
2 changed files with 30 additions and 2 deletions

View File

@ -37,6 +37,11 @@ namespace Worker
}
}
void Runner::attachHandler(std::shared_ptr<Handler> handler)
{
this->handlers[handler->getCommand()] = handler;
}
void Runner::worker()
{
printf("Worker started\n");
@ -49,8 +54,20 @@ namespace Worker
std::string buffer;
if (channel.receive(&buffer))
{
printf("Data received: %s\n", buffer.data());
channel.send("OK " + buffer);
Proto::IPC::Command command;
if(command.ParseFromString(buffer))
{
auto handler = this->handlers.find(command.command());
if (handler != this->handlers.end())
{
printf("Handling command %s\n", command.command().data());
handler->second->handle(&channel, command.data());
}
else
{
printf("No handler found for command %s\n", command.command().data());
}
}
}
std::this_thread::sleep_for(1ms);

View File

@ -5,15 +5,26 @@ namespace Worker
class Runner
{
public:
class Handler
{
public:
virtual ~Handler() {};
virtual std::string getCommand() = 0;
virtual void handle(Utils::IPC::BidirectionalChannel* channel, std::string data) = 0;
};
Runner(int pid);
~Runner();
void run();
void attachHandler(std::shared_ptr<Handler> handler);
private:
void worker();
int processId;
bool terminate;
std::map<std::string, std::shared_ptr<Handler>> handlers;
};
}