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()));
|
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +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
|
|
|
}
|
|
|
|
|
2012-04-02 20:50:35 +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-02 20:50:35 +02:00
|
|
|
JsonRpcMessage::Ptr msg = new_object<JsonRpcMessage>();
|
2012-03-28 21:20:13 +02:00
|
|
|
msg->SetJSON(json);
|
2012-04-02 20:50:35 +02:00
|
|
|
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;
|
|
|
|
}
|