#include "IPC.h"
|
#include <iostream>
|
|
IPC::IPC()
|
{
|
DLOG(INFO) << "IPC ready.. queue size at init: " << messageQueue.size();
|
}
|
|
IPC::~IPC()
|
{
|
while (!messageQueue.empty())
|
{
|
messageQueue.pop();
|
}
|
}
|
|
void IPC::registerHandler(std::shared_ptr<IPlugin> handler)
|
{
|
handlers.push_back(handler);
|
}
|
|
void IPC::sendMessage(const Command &cmd)
|
{
|
std::lock_guard lock(queueMutex);
|
std::string val;
|
tser::Serialize(cmd, val);
|
messageQueue.push(std::move(val));
|
queueCondVar.notify_one();
|
}
|
|
std::optional<Command> IPC::receiveMessage()
|
{
|
std::unique_lock<std::mutex> lock(queueMutex);
|
queueCondVar.wait(lock, [this]
|
{ return !messageQueue.empty(); });
|
|
if (!messageQueue.empty())
|
{
|
std::string a = std::move(messageQueue.front());
|
messageQueue.pop();
|
|
Command c;
|
tser::DeSerialize(a, c);
|
return c;
|
}
|
return std::nullopt;
|
}
|