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-icinga.h"
22 using namespace icinga;
24 string JsonRpcEndpoint::GetAddress(void) const
27 return "<disconnected endpoint>";
29 return m_Client->GetPeerAddress();
32 JsonRpcClient::Ptr JsonRpcEndpoint::GetClient(void)
37 void JsonRpcEndpoint::Connect(string node, string service, shared_ptr<SSL_CTX> sslContext)
39 JsonRpcClient::Ptr client = make_shared<JsonRpcClient>(RoleOutbound, sslContext);
41 client->Connect(node, service);
45 void JsonRpcEndpoint::SetClient(JsonRpcClient::Ptr 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));
54 bool JsonRpcEndpoint::IsLocal(void) const
59 bool JsonRpcEndpoint::IsConnected(void) const
61 return (bool)m_Client;
64 void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const RequestMessage& message)
68 if (message.GetID(&id))
69 // TODO: remove calls after a certain timeout (and notify callers?)
70 m_PendingCalls[id] = sender;
72 m_Client->SendMessage(message);
74 // TODO: persist the event
78 void JsonRpcEndpoint::ProcessResponse(Endpoint::Ptr sender, const ResponseMessage& message)
80 m_Client->SendMessage(message);
83 int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea)
85 const MessagePart& message = nmea.Message;
86 Endpoint::Ptr sender = static_pointer_cast<Endpoint>(shared_from_this());
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);
96 if (!message.GetProperty("method", &method))
99 if (!HasPublication(method))
102 RequestMessage request = message;
105 if (request.GetID(&id))
106 GetEndpointManager()->SendAnycastMessage(sender, request);
108 GetEndpointManager()->SendMulticastMessage(sender, request);
113 int JsonRpcEndpoint::ClientClosedHandler(const EventArgs&)
115 Application::Log("Lost connection to endpoint: identity=" + GetIdentity());
117 m_PendingCalls.clear();
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();
124 // remove the endpoint if there are no more subscriptions */
125 if (BeginSubscriptions() == EndSubscriptions()) {
127 GetEndpointManager()->UnregisterEndpoint(static_pointer_cast<Endpoint>(shared_from_this()));
132 // TODO: persist events, etc., for now we just disable the endpoint
137 int JsonRpcEndpoint::ClientErrorHandler(const SocketErrorEventArgs& ea)
139 cerr << "Error occured for JSON-RPC socket: Message=" << ea.Exception.what() << endl;
144 int JsonRpcEndpoint::VerifyCertificateHandler(const VerifyCertificateEventArgs& ea)
146 if (ea.Certificate && ea.ValidCertificate) {
147 string identity = Utility::GetCertificateCN(ea.Certificate);
149 if (GetIdentity().empty() && !identity.empty())
150 SetIdentity(identity);
156 void JsonRpcEndpoint::Stop(void)