icinga2/jsonrpc/jsonrpcclient.cpp

41 lines
756 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-16 16:27:41 +02:00
void JsonRpcClient::SendMessage(Message::Ptr 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
}
int JsonRpcClient::DataAvailableHandler(EventArgs::Ptr ea)
2012-03-28 13:24:49 +02:00
{
2012-04-16 16:27:41 +02:00
Message::Ptr message;
2012-03-28 13:24:49 +02:00
while (true) {
try {
2012-04-16 16:27:41 +02:00
message = Netstring::ReadMessageFromFIFO(GetRecvQueue());
2012-03-28 13:24:49 +02:00
} catch (const exception&) {
Close();
return 1;
}
2012-04-16 16:27:41 +02:00
if (message.get() == NULL)
2012-03-28 13:24:49 +02:00
break;
2012-04-03 15:16:11 +02:00
NewMessageEventArgs::Ptr nea = make_shared<NewMessageEventArgs>();
2012-03-28 13:24:49 +02:00
nea->Source = shared_from_this();
2012-04-16 16:27:41 +02:00
nea->Message = message;
2012-03-28 13:24:49 +02:00
OnNewMessage(nea);
}
return 0;
}