[Runner] Add handler interface
This commit is contained in:
parent
0c8ba82fba
commit
0221d24f07
@ -37,6 +37,11 @@ namespace Worker
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Runner::attachHandler(std::shared_ptr<Handler> handler)
|
||||||
|
{
|
||||||
|
this->handlers[handler->getCommand()] = handler;
|
||||||
|
}
|
||||||
|
|
||||||
void Runner::worker()
|
void Runner::worker()
|
||||||
{
|
{
|
||||||
printf("Worker started\n");
|
printf("Worker started\n");
|
||||||
@ -49,8 +54,20 @@ namespace Worker
|
|||||||
std::string buffer;
|
std::string buffer;
|
||||||
if (channel.receive(&buffer))
|
if (channel.receive(&buffer))
|
||||||
{
|
{
|
||||||
printf("Data received: %s\n", buffer.data());
|
Proto::IPC::Command command;
|
||||||
channel.send("OK " + buffer);
|
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);
|
std::this_thread::sleep_for(1ms);
|
||||||
|
@ -5,15 +5,26 @@ namespace Worker
|
|||||||
class Runner
|
class Runner
|
||||||
{
|
{
|
||||||
public:
|
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(int pid);
|
||||||
~Runner();
|
~Runner();
|
||||||
|
|
||||||
void run();
|
void run();
|
||||||
|
|
||||||
|
void attachHandler(std::shared_ptr<Handler> handler);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void worker();
|
void worker();
|
||||||
|
|
||||||
int processId;
|
int processId;
|
||||||
bool terminate;
|
bool terminate;
|
||||||
|
std::map<std::string, std::shared_ptr<Handler>> handlers;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user