]> granicus.if.org Git - icinga2/blob - icinga/jsonrpcendpoint.cpp
Replaced custom event code with Boost.Signals.
[icinga2] / icinga / jsonrpcendpoint.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-icinga.h"
21
22 using namespace icinga;
23
24 string JsonRpcEndpoint::GetAddress(void) const
25 {
26         if (!m_Client)
27                 return "<disconnected endpoint>";
28
29         return m_Client->GetPeerAddress();
30 }
31
32 JsonRpcClient::Ptr JsonRpcEndpoint::GetClient(void)
33 {
34         return m_Client;
35 }
36
37 void JsonRpcEndpoint::Connect(string node, string service, shared_ptr<SSL_CTX> sslContext)
38 {
39         JsonRpcClient::Ptr client = make_shared<JsonRpcClient>(RoleOutbound, sslContext);
40         SetClient(client);
41         client->Connect(node, service);
42         client->Start();
43 }
44
45 void JsonRpcEndpoint::SetClient(JsonRpcClient::Ptr client)
46 {
47         m_Client = client;
48         client->OnNewMessage.connect(bind(&JsonRpcEndpoint::NewMessageHandler, this, _1));
49         client->OnClosed.connect(bind(&JsonRpcEndpoint::ClientClosedHandler, this, _1));
50         client->OnError.connect(bind(&JsonRpcEndpoint::ClientErrorHandler, this, _1));
51         client->OnVerifyCertificate.connect(bind(&JsonRpcEndpoint::VerifyCertificateHandler, this, _1));
52 }
53
54 bool JsonRpcEndpoint::IsLocal(void) const
55 {
56         return false;
57 }
58
59 bool JsonRpcEndpoint::IsConnected(void) const
60 {
61         return (bool)m_Client;
62 }
63
64 void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const RequestMessage& message)
65 {
66         if (IsConnected()) {
67                 string id;
68                 if (message.GetID(&id))
69                         // TODO: remove calls after a certain timeout (and notify callers?)
70                         m_PendingCalls[id] = sender;
71
72                 m_Client->SendMessage(message);
73         } else {
74                 // TODO: persist the event
75         }
76 }
77
78 void JsonRpcEndpoint::ProcessResponse(Endpoint::Ptr sender, const ResponseMessage& message)
79 {
80         m_Client->SendMessage(message);
81 }
82
83 int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea)
84 {
85         const MessagePart& message = nmea.Message;
86         Endpoint::Ptr sender = static_pointer_cast<Endpoint>(shared_from_this());
87
88         if (ResponseMessage::IsResponseMessage(message)) {
89                 /* rather than routing the message to the right virtual
90                  * endpoint we just process it here right away. */
91                 GetEndpointManager()->ProcessResponseMessage(sender, message);
92                 return 0;
93         }
94
95         string method;
96         if (!message.GetProperty("method", &method))
97                 return 0;
98
99         if (!HasPublication(method))
100                 return 0;
101
102         RequestMessage request = message;
103
104         string id;
105         if (request.GetID(&id))
106                 GetEndpointManager()->SendAnycastMessage(sender, request);
107         else
108                 GetEndpointManager()->SendMulticastMessage(sender, request);
109
110         return 0;
111 }
112
113 int JsonRpcEndpoint::ClientClosedHandler(const EventArgs&)
114 {
115         Application::Log("Lost connection to endpoint: identity=" + GetIdentity());
116
117         m_PendingCalls.clear();
118
119         // TODO: _only_ clear non-persistent publications/subscriptions
120         // unregister ourselves if no persistent publications/subscriptions are left (use a timer for that, once we have a TTL property for the topics)
121         ClearSubscriptions();
122         ClearPublications();
123
124         // remove the endpoint if there are no more subscriptions */
125         if (BeginSubscriptions() == EndSubscriptions()) {
126                 Hold();
127                 GetEndpointManager()->UnregisterEndpoint(static_pointer_cast<Endpoint>(shared_from_this()));
128         }
129
130         m_Client.reset();
131
132         // TODO: persist events, etc., for now we just disable the endpoint
133
134         return 0;
135 }
136
137 int JsonRpcEndpoint::ClientErrorHandler(const SocketErrorEventArgs& ea)
138 {
139         cerr << "Error occured for JSON-RPC socket: Message=" << ea.Exception.what() << endl;
140
141         return 0;
142 }
143
144 int JsonRpcEndpoint::VerifyCertificateHandler(const VerifyCertificateEventArgs& ea)
145 {
146         if (ea.Certificate && ea.ValidCertificate) {
147                 string identity = Utility::GetCertificateCN(ea.Certificate);
148
149                 if (GetIdentity().empty() && !identity.empty())
150                         SetIdentity(identity);
151         }
152
153         return 0;
154 }
155
156 void JsonRpcEndpoint::Stop(void)
157 {
158         if (m_Client)
159                 m_Client->Close();
160 }