2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
|
|
|
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
|
|
|
|
* *
|
|
|
|
* This program is free software; you can redistribute it and/or *
|
|
|
|
* modify it under the terms of the GNU General Public License *
|
|
|
|
* as published by the Free Software Foundation; either version 2 *
|
|
|
|
* of the License, or (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* This program is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software Foundation *
|
2012-05-11 13:33:57 +02:00
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
|
2012-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
#include "i2-icinga.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
2012-04-16 16:27:41 +02:00
|
|
|
|
2012-04-23 13:45:41 +02:00
|
|
|
string JsonRpcEndpoint::GetAddress(void) const
|
|
|
|
{
|
2012-04-26 16:45:00 +02:00
|
|
|
if (!m_Client)
|
|
|
|
return "<disconnected endpoint>";
|
|
|
|
|
|
|
|
return m_Client->GetPeerAddress();
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
2012-04-16 16:27:41 +02:00
|
|
|
JsonRpcClient::Ptr JsonRpcEndpoint::GetClient(void)
|
|
|
|
{
|
|
|
|
return m_Client;
|
|
|
|
}
|
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
void JsonRpcEndpoint::Connect(string node, string service, shared_ptr<SSL_CTX> sslContext)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
JsonRpcClient::Ptr client = boost::make_shared<JsonRpcClient>(RoleOutbound, sslContext);
|
2012-04-18 15:22:25 +02:00
|
|
|
SetClient(client);
|
2012-05-07 13:48:17 +02:00
|
|
|
client->Connect(node, service);
|
|
|
|
client->Start();
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-04-16 16:27:41 +02:00
|
|
|
void JsonRpcEndpoint::SetClient(JsonRpcClient::Ptr client)
|
|
|
|
{
|
|
|
|
m_Client = client;
|
2012-06-16 03:42:54 +02:00
|
|
|
client->OnNewMessage.connect(boost::bind(&JsonRpcEndpoint::NewMessageHandler, this, _2));
|
|
|
|
client->OnClosed.connect(boost::bind(&JsonRpcEndpoint::ClientClosedHandler, this));
|
|
|
|
client->OnError.connect(boost::bind(&JsonRpcEndpoint::ClientErrorHandler, this, _2));
|
|
|
|
client->OnVerifyCertificate.connect(boost::bind(&JsonRpcEndpoint::VerifyCertificateHandler, this, _2, _4));
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonRpcEndpoint::IsLocal(void) const
|
|
|
|
{
|
|
|
|
return false;
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool JsonRpcEndpoint::IsConnected(void) const
|
|
|
|
{
|
2012-04-19 12:20:03 +02:00
|
|
|
return (bool)m_Client;
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 12:53:38 +02:00
|
|
|
void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const RequestMessage& message)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
if (IsConnected()) {
|
|
|
|
string id;
|
|
|
|
if (message.GetID(&id))
|
|
|
|
// TODO: remove calls after a certain timeout (and notify callers?)
|
|
|
|
m_PendingCalls[id] = sender;
|
|
|
|
|
2012-04-16 16:27:41 +02:00
|
|
|
m_Client->SendMessage(message);
|
2012-04-19 12:16:52 +02:00
|
|
|
} else {
|
|
|
|
// TODO: persist the event
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-21 12:53:38 +02:00
|
|
|
void JsonRpcEndpoint::ProcessResponse(Endpoint::Ptr sender, const ResponseMessage& message)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
2012-06-14 11:18:20 +02:00
|
|
|
m_Client->SendMessage(message);
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void JsonRpcEndpoint::NewMessageHandler(const MessagePart& message)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
|
|
|
Endpoint::Ptr sender = static_pointer_cast<Endpoint>(shared_from_this());
|
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
if (ResponseMessage::IsResponseMessage(message)) {
|
|
|
|
/* rather than routing the message to the right virtual
|
|
|
|
* endpoint we just process it here right away. */
|
|
|
|
GetEndpointManager()->ProcessResponseMessage(sender, message);
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
2012-06-14 11:18:20 +02:00
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
string method;
|
2012-06-14 11:18:20 +02:00
|
|
|
if (!message.GetProperty("method", &method))
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
2012-04-23 13:45:41 +02:00
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
if (!HasPublication(method))
|
2012-06-15 19:32:41 +02:00
|
|
|
return;
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
RequestMessage request = message;
|
2012-04-20 10:38:11 +02:00
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
string id;
|
|
|
|
if (request.GetID(&id))
|
|
|
|
GetEndpointManager()->SendAnycastMessage(sender, request);
|
|
|
|
else
|
|
|
|
GetEndpointManager()->SendMulticastMessage(sender, request);
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void JsonRpcEndpoint::ClientClosedHandler(void)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
Application::Log(LogWarning, "jsonrpc", "Lost connection to endpoint: identity=" + GetIdentity());
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
m_PendingCalls.clear();
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
// TODO: _only_ clear non-persistent publications/subscriptions
|
|
|
|
// unregister ourselves if no persistent publications/subscriptions are left (use a timer for that, once we have a TTL property for the topics)
|
|
|
|
ClearSubscriptions();
|
|
|
|
ClearPublications();
|
2012-04-19 12:16:52 +02:00
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
// remove the endpoint if there are no more subscriptions */
|
2012-06-14 15:16:41 +02:00
|
|
|
if (BeginSubscriptions() == EndSubscriptions()) {
|
|
|
|
Hold();
|
2012-04-19 12:16:52 +02:00
|
|
|
GetEndpointManager()->UnregisterEndpoint(static_pointer_cast<Endpoint>(shared_from_this()));
|
2012-06-14 15:16:41 +02:00
|
|
|
}
|
2012-04-19 12:16:52 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
m_Client.reset();
|
|
|
|
|
|
|
|
// TODO: persist events, etc., for now we just disable the endpoint
|
|
|
|
}
|
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void JsonRpcEndpoint::ClientErrorHandler(const std::exception& ex)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
stringstream message;
|
2012-06-16 03:42:54 +02:00
|
|
|
message << "Error occured for JSON-RPC socket: Message=" << ex.what();
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
Application::Log(LogWarning, "jsonrpc", message.str());
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
|
2012-06-16 03:42:54 +02:00
|
|
|
void JsonRpcEndpoint::VerifyCertificateHandler(bool& valid, const shared_ptr<X509>& certificate)
|
2012-04-24 15:56:48 +02:00
|
|
|
{
|
2012-06-16 03:42:54 +02:00
|
|
|
if (certificate && valid) {
|
|
|
|
string identity = Utility::GetCertificateCN(certificate);
|
2012-04-24 16:27:23 +02:00
|
|
|
|
|
|
|
if (GetIdentity().empty() && !identity.empty())
|
|
|
|
SetIdentity(identity);
|
|
|
|
}
|
2012-04-24 15:56:48 +02:00
|
|
|
}
|
2012-04-27 11:44:34 +02:00
|
|
|
|
|
|
|
void JsonRpcEndpoint::Stop(void)
|
|
|
|
{
|
2012-04-27 13:44:53 +02:00
|
|
|
if (m_Client)
|
2012-04-27 11:44:34 +02:00
|
|
|
m_Client->Close();
|
|
|
|
}
|