]> granicus.if.org Git - icinga2/blob - jsonrpc/jsonrpcclient.cpp
Replaced custom event code with Boost.Signals.
[icinga2] / jsonrpc / jsonrpcclient.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
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.                     *
9  *                                                                            *
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.                               *
14  *                                                                            *
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  ******************************************************************************/
19
20 #include "i2-jsonrpc.h"
21
22 using namespace icinga;
23
24 /**
25  * Constructor for the JsonRpcClient class.
26  *
27  * @param role The role of the underlying TCP client.
28  * @param sslContext SSL context for the TLS connection.
29  */
30 JsonRpcClient::JsonRpcClient(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
31     : TlsClient(role, sslContext) { }
32
33 void JsonRpcClient::Start(void)
34 {
35         TlsClient::Start();
36
37         OnDataAvailable.connect(bind(&JsonRpcClient::DataAvailableHandler, this, _1));
38 }
39
40 /**
41  * Sends a message to the connected peer.
42  *
43  * @param message The message.
44  */
45 void JsonRpcClient::SendMessage(const MessagePart& message)
46 {
47         Netstring::WriteStringToFIFO(GetSendQueue(), message.ToJsonString());
48 }
49
50 /**
51  * Processes inbound data.
52  *
53  * @param - Event arguments for the event.
54  * @returns 0
55  */
56 int JsonRpcClient::DataAvailableHandler(const EventArgs&)
57 {
58         for (;;) {
59                 try {
60                         string jsonString;
61                         MessagePart message;
62
63                         if (!Netstring::ReadStringFromFIFO(GetRecvQueue(), &jsonString))
64                                 break;
65
66                         message = MessagePart(jsonString);
67
68                         NewMessageEventArgs nea;
69                         nea.Source = shared_from_this();
70                         nea.Message = message;
71                         OnNewMessage(nea);
72                 } catch (const Exception& ex) {
73                         Application::Log("Exception while processing message from JSON-RPC client: " + string(ex.GetMessage()));
74                         Close();
75
76                         return 0;
77                 }
78         }
79
80         return 0;
81 }
82
83 /**
84  * Factory function for JSON-RPC clients.
85  *
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.
89  */
90 JsonRpcClient::Ptr icinga::JsonRpcClientFactory(TcpClientRole role, shared_ptr<SSL_CTX> sslContext)
91 {
92         return make_shared<JsonRpcClient>(role, sslContext);
93 }