icinga2/jsonrpc/jsonrpcclient.cpp

43 lines
824 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
}
void JsonRpcClient::SendMessage(JsonRpcMessage::Ptr message)
2012-03-28 13:24:49 +02:00
{
2012-03-28 21:20:13 +02:00
Netstring::WriteJSONToFIFO(GetSendQueue(), message->GetJSON());
2012-03-28 13:24:49 +02:00
}
int JsonRpcClient::DataAvailableHandler(EventArgs::Ptr ea)
2012-03-28 13:24:49 +02:00
{
2012-03-28 21:20:13 +02:00
cJSON *json;
2012-03-28 13:24:49 +02:00
while (true) {
try {
2012-03-28 21:20:13 +02:00
json = Netstring::ReadJSONFromFIFO(GetRecvQueue());
2012-03-28 13:24:49 +02:00
} catch (const exception&) {
Close();
return 1;
}
2012-03-28 21:20:13 +02:00
if (json == NULL)
2012-03-28 13:24:49 +02:00
break;
2012-04-03 15:16:11 +02:00
JsonRpcMessage::Ptr msg = make_shared<JsonRpcMessage>();
2012-03-28 21:20:13 +02:00
msg->SetJSON(json);
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();
nea->Message = msg;
OnNewMessage(nea);
}
return 0;
}