1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
5 * This program is free software; you can redistribute it and/or *
6 * modify it under the terms of the GNU General Public License *
7 * as published by the Free Software Foundation; either version 2 *
8 * of the License, or (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software Foundation *
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ******************************************************************************/
20 #include "i2-jsonrpc.h"
22 using namespace icinga;
25 * Constructor for the JsonRpcClient class.
27 * @param role The role of the underlying TCP client.
28 * @param sslContext SSL context for the TLS connection.
30 JsonRpcClient::JsonRpcClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
31 : TlsClient(role, sslContext) { }
33 void JsonRpcClient::Start(void)
37 OnDataAvailable.connect(bind(&JsonRpcClient::DataAvailableHandler, this, _1));
41 * Sends a message to the connected peer.
43 * @param message The message.
45 void JsonRpcClient::SendMessage(const MessagePart& message)
47 Netstring::WriteStringToFIFO(GetSendQueue(), message.ToJsonString());
51 * Processes inbound data.
53 * @param - Event arguments for the event.
56 int JsonRpcClient::DataAvailableHandler(const EventArgs&)
63 if (!Netstring::ReadStringFromFIFO(GetRecvQueue(), &jsonString))
66 message = MessagePart(jsonString);
68 NewMessageEventArgs nea;
69 nea.Source = shared_from_this();
70 nea.Message = message;
72 } catch (const Exception& ex) {
73 Application::Log("Exception while processing message from JSON-RPC client: " + string(ex.GetMessage()));
84 * Factory function for JSON-RPC clients.
86 * @param role The role of the underlying TCP client.
87 * @param sslContext SSL context for the TLS connection.
88 * @returns A new JSON-RPC client.
90 JsonRpcClient::Ptr icinga::JsonRpcClientFactory(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
92 return make_shared<JsonRpcClient>(role, sslContext);