2012-03-28 13:24:49 +02:00
|
|
|
#include "i2-jsonrpc.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
JsonRpcClient::JsonRpcClient(TCPClientRole role, shared_ptr<SSL_CTX> sslContext)
|
|
|
|
: TLSClient(role, sslContext) { }
|
|
|
|
|
2012-03-28 13:24:49 +02:00
|
|
|
void JsonRpcClient::Start(void)
|
|
|
|
{
|
2012-04-24 14:02:15 +02:00
|
|
|
TLSClient::Start();
|
2012-03-28 13:24:49 +02:00
|
|
|
|
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
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
try {
|
2012-04-30 08:22:30 +02:00
|
|
|
Message message;
|
|
|
|
|
|
|
|
if (Netstring::ReadMessageFromFIFO(GetRecvQueue(), &message)) {
|
|
|
|
NewMessageEventArgs nea;
|
|
|
|
nea.Source = shared_from_this();
|
|
|
|
nea.Message = message;
|
|
|
|
OnNewMessage(nea);
|
|
|
|
}
|
2012-04-18 15:22:25 +02:00
|
|
|
} catch (const Exception& ex) {
|
2012-04-30 08:22:30 +02:00
|
|
|
Application::Log("Exception while processing message from JSON-RPC client: " + ex.GetMessage());
|
2012-03-28 13:24:49 +02:00
|
|
|
Close();
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2012-04-24 14:02:15 +02:00
|
|
|
|
|
|
|
TCPClient::Ptr icinga::JsonRpcClientFactory(TCPClientRole role, shared_ptr<SSL_CTX> sslContext)
|
|
|
|
{
|
|
|
|
return make_shared<JsonRpcClient>(role, sslContext);
|
|
|
|
}
|