icinga2/jsonrpc/jsonrpcclient.cpp

43 lines
825 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();
OnDataAvailable.bind(bind_weak(&JsonRpcClient::DataAvailableHandler, shared_from_this()));
}
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;
JsonRpcMessage::Ptr msg = new_object<JsonRpcMessage>();
2012-03-28 21:20:13 +02:00
msg->SetJSON(json);
NewMessageEventArgs::Ptr nea = new_object<NewMessageEventArgs>();
2012-03-28 13:24:49 +02:00
nea->Source = shared_from_this();
nea->Message = msg;
OnNewMessage(nea);
}
return 0;
}