icinga2/jsonrpc/jsonrpcclient.cpp

43 lines
829 B
C++
Raw Normal View History

2012-03-28 13:24:49 +02:00
#include "i2-jsonrpc.h"
using namespace icinga;
void JsonRpcClient::Start(void)
{
TCPClient::Start();
2012-04-03 11:13:17 +02:00
OnDataAvailable += bind_weak(&JsonRpcClient::DataAvailableHandler, shared_from_this());
2012-03-28 13:24:49 +02:00
}
2012-04-18 15:22:25 +02:00
void JsonRpcClient::SendMessage(const Message& message)
2012-03-28 13:24:49 +02:00
{
2012-04-16 16:27:41 +02:00
Netstring::WriteMessageToFIFO(GetSendQueue(), message);
2012-03-28 13:24:49 +02:00
}
2012-04-18 15:22:25 +02:00
int JsonRpcClient::DataAvailableHandler(const EventArgs& ea)
2012-03-28 13:24:49 +02:00
{
2012-04-18 15:22:25 +02:00
Message message;
bool message_read;
2012-03-28 13:24:49 +02:00
while (true) {
try {
2012-04-18 15:22:25 +02:00
message_read = Netstring::ReadMessageFromFIFO(GetRecvQueue(), &message);
} catch (const Exception& ex) {
cerr << "Exception while reading from JSON-RPC client: " << ex.GetMessage() << endl;
2012-03-28 13:24:49 +02:00
Close();
return 1;
}
2012-04-18 15:22:25 +02:00
if (!message_read)
2012-03-28 13:24:49 +02:00
break;
2012-04-18 15:22:25 +02:00
NewMessageEventArgs nea;
nea.Source = shared_from_this();
nea.Message = message;
2012-03-28 13:24:49 +02:00
OnNewMessage(nea);
}
return 0;
}