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
|
|
|
******************************************************************************/
|
|
|
|
|
2013-03-17 20:19:29 +01:00
|
|
|
#include "remoting/endpointmanager.h"
|
2013-03-16 21:18:53 +01:00
|
|
|
#include "base/dynamictype.h"
|
|
|
|
#include "base/objectlock.h"
|
|
|
|
#include "base/logger_fwd.h"
|
|
|
|
#include "base/convert.h"
|
2013-03-18 11:02:18 +01:00
|
|
|
#include "base/utility.h"
|
2013-03-18 19:02:42 +01:00
|
|
|
#include "base/tlsutility.h"
|
2013-03-15 18:21:29 +01:00
|
|
|
#include <boost/tuple/tuple.hpp>
|
2013-03-16 21:18:53 +01:00
|
|
|
#include <boost/foreach.hpp>
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-06-21 12:51:50 +02:00
|
|
|
/**
|
|
|
|
* Constructor for the EndpointManager class.
|
|
|
|
*/
|
|
|
|
EndpointManager::EndpointManager(void)
|
|
|
|
: m_NextMessageID(0)
|
|
|
|
{
|
|
|
|
m_RequestTimer = boost::make_shared<Timer>();
|
|
|
|
m_RequestTimer->OnTimerExpired.connect(boost::bind(&EndpointManager::RequestTimerHandler, this));
|
|
|
|
m_RequestTimer->SetInterval(5);
|
|
|
|
m_RequestTimer->Start();
|
2012-09-03 10:28:14 +02:00
|
|
|
|
|
|
|
m_SubscriptionTimer = boost::make_shared<Timer>();
|
|
|
|
m_SubscriptionTimer->OnTimerExpired.connect(boost::bind(&EndpointManager::SubscriptionTimerHandler, this));
|
2013-02-13 12:22:18 +01:00
|
|
|
m_SubscriptionTimer->SetInterval(5);
|
2012-09-03 10:28:14 +02:00
|
|
|
m_SubscriptionTimer->Start();
|
|
|
|
|
|
|
|
m_ReconnectTimer = boost::make_shared<Timer>();
|
|
|
|
m_ReconnectTimer->OnTimerExpired.connect(boost::bind(&EndpointManager::ReconnectTimerHandler, this));
|
2013-02-13 12:22:18 +01:00
|
|
|
m_ReconnectTimer->SetInterval(5);
|
2012-09-03 10:28:14 +02:00
|
|
|
m_ReconnectTimer->Start();
|
2012-06-21 12:51:50 +02:00
|
|
|
}
|
|
|
|
|
2012-09-10 14:07:32 +02:00
|
|
|
/**
|
|
|
|
* Sets the SSL context.
|
|
|
|
*
|
|
|
|
* @param sslContext The new SSL context.
|
|
|
|
*/
|
|
|
|
void EndpointManager::SetSSLContext(const shared_ptr<SSL_CTX>& sslContext)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-09-10 14:07:32 +02:00
|
|
|
m_SSLContext = sslContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the SSL context.
|
|
|
|
*
|
|
|
|
* @returns The SSL context.
|
|
|
|
*/
|
|
|
|
shared_ptr<SSL_CTX> EndpointManager::GetSSLContext(void) const
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-09-10 14:07:32 +02:00
|
|
|
return m_SSLContext;
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Sets the identity of the endpoint manager. This identity is used when
|
|
|
|
* connecting to remote peers.
|
|
|
|
*
|
|
|
|
* @param identity The new identity.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
void EndpointManager::SetIdentity(const String& identity)
|
2012-04-24 14:02:15 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-04-27 11:44:34 +02:00
|
|
|
m_Identity = identity;
|
2012-09-03 10:28:14 +02:00
|
|
|
|
|
|
|
if (m_Endpoint)
|
|
|
|
m_Endpoint->Unregister();
|
|
|
|
|
|
|
|
DynamicObject::Ptr object = DynamicObject::GetObject("Endpoint", identity);
|
|
|
|
|
|
|
|
if (object)
|
|
|
|
m_Endpoint = dynamic_pointer_cast<Endpoint>(object);
|
|
|
|
else
|
2013-01-18 09:36:28 +01:00
|
|
|
m_Endpoint = Endpoint::MakeEndpoint(identity, true, true);
|
2012-04-24 14:02:15 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the identity for the endpoint manager.
|
|
|
|
*
|
|
|
|
* @returns The identity.
|
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
String EndpointManager::GetIdentity(void) const
|
2012-04-27 11:44:34 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-04-27 11:44:34 +02:00
|
|
|
return m_Identity;
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Creates a new JSON-RPC listener on the specified port.
|
|
|
|
*
|
2012-05-18 23:25:06 +02:00
|
|
|
* @param service The port to listen on.
|
2012-05-15 16:24:04 +02:00
|
|
|
*/
|
2012-08-02 09:38:08 +02:00
|
|
|
void EndpointManager::AddListener(const String& service)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-03-12 11:56:14 +01:00
|
|
|
shared_ptr<SSL_CTX> sslContext = m_SSLContext;
|
2012-09-03 10:28:14 +02:00
|
|
|
|
|
|
|
if (!sslContext)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::logic_error("SSL context is required for AddListener()"));
|
2012-05-15 11:08:04 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::ostringstream s;
|
2012-05-07 13:48:17 +02:00
|
|
|
s << "Adding new listener: port " << service;
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "icinga", s.str());
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
TcpSocket::Ptr server = boost::make_shared<TcpSocket>();
|
2012-09-03 10:28:14 +02:00
|
|
|
|
|
|
|
m_Servers.insert(server);
|
|
|
|
server->OnNewClient.connect(boost::bind(&EndpointManager::NewClientHandler,
|
2012-11-22 12:04:32 +01:00
|
|
|
this, _2, TlsRoleServer));
|
2012-04-06 08:56:52 +02:00
|
|
|
|
2012-05-07 13:48:17 +02:00
|
|
|
server->Bind(service, AF_INET6);
|
2012-04-06 08:56:52 +02:00
|
|
|
server->Listen();
|
|
|
|
server->Start();
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Creates a new JSON-RPC client and connects to the specified host and port.
|
|
|
|
*
|
2012-05-18 23:25:06 +02:00
|
|
|
* @param node The remote host.
|
|
|
|
* @param service The remote port.
|
2012-05-15 16:24:04 +02:00
|
|
|
*/
|
2012-09-03 10:28:14 +02:00
|
|
|
void EndpointManager::AddConnection(const String& node, const String& service) {
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-03-12 11:56:14 +01:00
|
|
|
shared_ptr<SSL_CTX> sslContext = m_SSLContext;
|
2012-09-10 14:07:32 +02:00
|
|
|
|
|
|
|
if (!sslContext)
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::logic_error("SSL context is required for AddConnection()"));
|
2012-09-10 14:07:32 +02:00
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
TcpSocket::Ptr client = boost::make_shared<TcpSocket>();
|
2012-09-03 10:28:14 +02:00
|
|
|
client->Connect(node, service);
|
2012-11-22 12:04:32 +01:00
|
|
|
NewClientHandler(client, TlsRoleClient);
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Processes a new client connection.
|
|
|
|
*
|
2012-09-03 10:28:14 +02:00
|
|
|
* @param client The new client.
|
2012-05-15 16:24:04 +02:00
|
|
|
*/
|
2012-11-22 12:04:32 +01:00
|
|
|
void EndpointManager::NewClientHandler(const Socket::Ptr& client, TlsRole role)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
String peerAddress = client->GetPeerAddress();
|
2013-03-12 11:56:14 +01:00
|
|
|
TlsStream::Ptr tlsStream = boost::make_shared<TlsStream>(client, role, m_SSLContext);
|
2012-11-22 12:04:32 +01:00
|
|
|
tlsStream->Start();
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
m_PendingClients.insert(tlsStream);
|
|
|
|
tlsStream->OnConnected.connect(boost::bind(&EndpointManager::ClientConnectedHandler, this, _1, peerAddress));
|
2012-12-04 08:42:24 +01:00
|
|
|
tlsStream->OnClosed.connect(boost::bind(&EndpointManager::ClientClosedHandler, this, _1));
|
2012-11-22 12:04:32 +01:00
|
|
|
|
|
|
|
client->Start();
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
void EndpointManager::ClientConnectedHandler(const Stream::Ptr& client, const String& peerAddress)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
TlsStream::Ptr tlsStream = static_pointer_cast<TlsStream>(client);
|
|
|
|
JsonRpcConnection::Ptr jclient = boost::make_shared<JsonRpcConnection>(tlsStream);
|
2012-10-11 14:17:40 +02:00
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
m_PendingClients.erase(tlsStream);
|
2012-06-20 15:22:39 +02:00
|
|
|
|
2012-11-22 12:04:32 +01:00
|
|
|
shared_ptr<X509> cert = tlsStream->GetPeerCertificate();
|
2013-03-18 19:02:42 +01:00
|
|
|
String identity = GetCertificateCN(cert);
|
2012-07-02 11:19:14 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogInformation, "icinga", "New client connection at " + peerAddress + " for identity '" + identity + "'");
|
2012-11-22 12:04:32 +01:00
|
|
|
|
2013-02-27 15:23:25 +01:00
|
|
|
Endpoint::Ptr endpoint = Endpoint::GetByName(identity);
|
2012-04-06 08:56:52 +02:00
|
|
|
|
2013-02-27 15:23:25 +01:00
|
|
|
if (!endpoint)
|
2013-01-18 09:36:28 +01:00
|
|
|
endpoint = Endpoint::MakeEndpoint(identity, true);
|
2012-06-20 15:22:39 +02:00
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
endpoint->SetClient(jclient);
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
2012-12-04 08:42:24 +01:00
|
|
|
void EndpointManager::ClientClosedHandler(const Stream::Ptr& client)
|
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-12-04 08:42:24 +01:00
|
|
|
TlsStream::Ptr tlsStream = static_pointer_cast<TlsStream>(client);
|
|
|
|
m_PendingClients.erase(tlsStream);
|
|
|
|
}
|
|
|
|
|
2013-01-22 10:14:52 +01:00
|
|
|
/**
|
|
|
|
* Sends an anonymous unicast message to the specified recipient.
|
|
|
|
*
|
|
|
|
* @param recipient The recipient of the message.
|
|
|
|
* @param message The message.
|
|
|
|
*/
|
|
|
|
void EndpointManager::SendUnicastMessage(const Endpoint::Ptr& recipient,
|
|
|
|
const MessagePart& message)
|
|
|
|
{
|
|
|
|
SendUnicastMessage(Endpoint::Ptr(), recipient, message);
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Sends a unicast message to the specified recipient.
|
|
|
|
*
|
|
|
|
* @param sender The sender of the message.
|
|
|
|
* @param recipient The recipient of the message.
|
2013-01-22 10:14:52 +01:00
|
|
|
* @param message The message.
|
2012-05-15 16:24:04 +02:00
|
|
|
*/
|
2012-07-18 11:43:41 +02:00
|
|
|
void EndpointManager::SendUnicastMessage(const Endpoint::Ptr& sender,
|
|
|
|
const Endpoint::Ptr& recipient, const MessagePart& message)
|
2012-04-23 16:48:40 +02:00
|
|
|
{
|
2013-01-22 10:14:52 +01:00
|
|
|
/* don't forward messages between non-local endpoints, assume that
|
|
|
|
* anonymous senders (sender == null) are local */
|
|
|
|
if ((sender && !sender->IsLocal()) && !recipient->IsLocal())
|
2012-04-23 16:48:40 +02:00
|
|
|
return;
|
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
if (ResponseMessage::IsResponseMessage(message))
|
|
|
|
recipient->ProcessResponse(sender, message);
|
|
|
|
else
|
|
|
|
recipient->ProcessRequest(sender, message);
|
2012-04-23 16:48:40 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Sends a message to exactly one recipient out of all recipients who have a
|
|
|
|
* subscription for the message's topic.
|
|
|
|
*
|
|
|
|
* @param sender The sender of the message.
|
|
|
|
* @param message The message.
|
|
|
|
*/
|
2012-07-18 11:43:41 +02:00
|
|
|
void EndpointManager::SendAnycastMessage(const Endpoint::Ptr& sender,
|
2012-05-21 12:53:38 +02:00
|
|
|
const RequestMessage& message)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
String method;
|
2012-06-14 11:18:20 +02:00
|
|
|
if (!message.GetMethod(&method))
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Message is missing the 'method' property."));
|
2012-06-14 11:18:20 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::vector<Endpoint::Ptr> candidates;
|
2013-02-18 23:44:24 +01:00
|
|
|
|
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Endpoint")) {
|
2012-09-03 10:28:14 +02:00
|
|
|
Endpoint::Ptr endpoint = dynamic_pointer_cast<Endpoint>(object);
|
2012-06-22 13:40:09 +02:00
|
|
|
/* don't forward messages between non-local endpoints */
|
2013-02-09 15:20:10 +01:00
|
|
|
if ((sender && !sender->IsLocal()) && !endpoint->IsLocal())
|
2012-06-22 13:40:09 +02:00
|
|
|
continue;
|
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
if (endpoint->HasSubscription(method))
|
|
|
|
candidates.push_back(endpoint);
|
|
|
|
}
|
|
|
|
|
2012-06-19 09:38:20 +02:00
|
|
|
if (candidates.empty())
|
2012-06-14 11:18:20 +02:00
|
|
|
return;
|
|
|
|
|
|
|
|
Endpoint::Ptr recipient = candidates[rand() % candidates.size()];
|
|
|
|
SendUnicastMessage(sender, recipient, message);
|
2012-04-16 16:27:41 +02:00
|
|
|
}
|
|
|
|
|
2013-01-22 10:14:52 +01:00
|
|
|
/**
|
|
|
|
* Sends an anonymous message to all recipients who have a subscription for the
|
|
|
|
* message#s topic.
|
|
|
|
*
|
|
|
|
* @param message The message.
|
|
|
|
*/
|
|
|
|
void EndpointManager::SendMulticastMessage(const RequestMessage& message)
|
|
|
|
{
|
|
|
|
SendMulticastMessage(Endpoint::Ptr(), message);
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Sends a message to all recipients who have a subscription for the
|
|
|
|
* message's topic.
|
|
|
|
*
|
|
|
|
* @param sender The sender of the message.
|
|
|
|
* @param message The message.
|
|
|
|
*/
|
2012-07-18 11:43:41 +02:00
|
|
|
void EndpointManager::SendMulticastMessage(const Endpoint::Ptr& sender,
|
2012-05-21 12:53:38 +02:00
|
|
|
const RequestMessage& message)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
2012-08-02 09:38:08 +02:00
|
|
|
String id;
|
2012-05-15 16:24:04 +02:00
|
|
|
if (message.GetID(&id))
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Multicast requests must not have an ID."));
|
2012-04-16 16:27:41 +02:00
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String method;
|
2012-05-15 16:24:04 +02:00
|
|
|
if (!message.GetMethod(&method))
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Message is missing the 'method' property."));
|
2012-04-16 16:27:41 +02:00
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Endpoint")) {
|
2012-09-03 10:28:14 +02:00
|
|
|
Endpoint::Ptr recipient = dynamic_pointer_cast<Endpoint>(object);
|
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
/* don't forward messages back to the sender */
|
|
|
|
if (sender == recipient)
|
|
|
|
continue;
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
if (recipient->HasSubscription(method))
|
|
|
|
SendUnicastMessage(sender, recipient, message);
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-21 00:10:10 +02:00
|
|
|
void EndpointManager::SendAPIMessage(const Endpoint::Ptr& sender, const Endpoint::Ptr& recipient,
|
2012-06-14 11:18:20 +02:00
|
|
|
RequestMessage& message,
|
2012-09-21 09:43:06 +02:00
|
|
|
const EndpointManager::APICallback& callback, double timeout)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-06-14 11:18:20 +02:00
|
|
|
m_NextMessageID++;
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
String id = Convert::ToString(m_NextMessageID);
|
2012-06-14 11:18:20 +02:00
|
|
|
message.SetID(id);
|
|
|
|
|
|
|
|
PendingRequest pr;
|
|
|
|
pr.Request = message;
|
|
|
|
pr.Callback = callback;
|
2012-08-03 18:17:47 +02:00
|
|
|
pr.Timeout = Utility::GetTime() + timeout;
|
2012-06-14 11:18:20 +02:00
|
|
|
|
|
|
|
m_Requests[id] = pr;
|
|
|
|
|
2012-06-21 00:10:10 +02:00
|
|
|
if (!recipient)
|
|
|
|
SendAnycastMessage(sender, message);
|
|
|
|
else
|
|
|
|
SendUnicastMessage(sender, recipient, message);
|
2012-06-14 11:18:20 +02:00
|
|
|
}
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
bool EndpointManager::RequestTimeoutLessComparer(const std::pair<String, PendingRequest>& a,
|
|
|
|
const std::pair<String, PendingRequest>& b)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
|
|
|
return a.second.Timeout < b.second.Timeout;
|
|
|
|
}
|
|
|
|
|
2012-09-03 10:28:14 +02:00
|
|
|
void EndpointManager::SubscriptionTimerHandler(void)
|
|
|
|
{
|
|
|
|
Dictionary::Ptr subscriptions = boost::make_shared<Dictionary>();
|
|
|
|
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Endpoint")) {
|
2012-09-03 10:28:14 +02:00
|
|
|
Endpoint::Ptr endpoint = dynamic_pointer_cast<Endpoint>(object);
|
|
|
|
|
2013-01-24 10:21:26 +01:00
|
|
|
/* don't copy subscriptions from non-local endpoints or the identity endpoint */
|
|
|
|
if (!endpoint->IsLocalEndpoint() || endpoint == m_Endpoint)
|
2012-09-03 10:28:14 +02:00
|
|
|
continue;
|
|
|
|
|
2013-03-01 12:07:52 +01:00
|
|
|
Dictionary::Ptr endpointSubscriptions = endpoint->GetSubscriptions();
|
|
|
|
|
|
|
|
if (endpointSubscriptions) {
|
|
|
|
ObjectLock olock(endpointSubscriptions);
|
|
|
|
|
2013-01-18 09:36:28 +01:00
|
|
|
String topic;
|
2013-03-15 18:21:29 +01:00
|
|
|
BOOST_FOREACH(boost::tie(boost::tuples::ignore, topic), endpointSubscriptions) {
|
2013-01-18 09:36:28 +01:00
|
|
|
subscriptions->Set(topic, topic);
|
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-24 01:10:34 +01:00
|
|
|
subscriptions->Seal();
|
|
|
|
|
2012-09-17 09:47:02 +02:00
|
|
|
if (m_Endpoint)
|
|
|
|
m_Endpoint->SetSubscriptions(subscriptions);
|
2012-09-03 10:28:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EndpointManager::ReconnectTimerHandler(void)
|
|
|
|
{
|
2013-02-18 23:44:24 +01:00
|
|
|
BOOST_FOREACH(const DynamicObject::Ptr& object, DynamicType::GetObjects("Endpoint")) {
|
2012-09-03 10:28:14 +02:00
|
|
|
Endpoint::Ptr endpoint = dynamic_pointer_cast<Endpoint>(object);
|
|
|
|
|
2012-09-03 12:20:04 +02:00
|
|
|
if (endpoint->IsConnected() || endpoint == m_Endpoint)
|
2012-09-03 10:28:14 +02:00
|
|
|
continue;
|
|
|
|
|
|
|
|
String node, service;
|
|
|
|
node = endpoint->GetNode();
|
|
|
|
service = endpoint->GetService();
|
|
|
|
|
2012-09-03 12:20:04 +02:00
|
|
|
if (node.IsEmpty() || service.IsEmpty()) {
|
2013-03-16 21:18:53 +01:00
|
|
|
Log(LogWarning, "icinga", "Can't reconnect "
|
2012-09-03 12:20:04 +02:00
|
|
|
"to endpoint '" + endpoint->GetName() + "': No "
|
|
|
|
"node/service information.");
|
2012-09-03 10:28:14 +02:00
|
|
|
continue;
|
2012-09-03 12:20:04 +02:00
|
|
|
}
|
2012-09-03 10:28:14 +02:00
|
|
|
|
|
|
|
AddConnection(node, service);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
void EndpointManager::RequestTimerHandler(void)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::map<String, PendingRequest>::iterator it;
|
2013-03-06 15:41:13 +01:00
|
|
|
for (it = m_Requests.begin(); it != m_Requests.end(); ++it) {
|
2012-06-14 11:18:20 +02:00
|
|
|
if (it->second.HasTimedOut()) {
|
2013-03-15 11:19:52 +01:00
|
|
|
it->second.Callback(Endpoint::Ptr(), it->second.Request,
|
|
|
|
ResponseMessage(), true);
|
2012-06-14 11:18:20 +02:00
|
|
|
|
|
|
|
m_Requests.erase(it);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-21 09:43:06 +02:00
|
|
|
void EndpointManager::ProcessResponseMessage(const Endpoint::Ptr& sender,
|
|
|
|
const ResponseMessage& message)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
2013-03-02 09:07:47 +01:00
|
|
|
ObjectLock olock(this);
|
|
|
|
|
2012-08-02 09:38:08 +02:00
|
|
|
String id;
|
2012-06-14 11:18:20 +02:00
|
|
|
if (!message.GetID(&id))
|
2013-03-16 21:18:53 +01:00
|
|
|
BOOST_THROW_EXCEPTION(std::invalid_argument("Response message must have a message ID."));
|
2012-06-14 11:18:20 +02:00
|
|
|
|
2013-03-16 21:18:53 +01:00
|
|
|
std::map<String, PendingRequest>::iterator it;
|
2012-06-14 11:18:20 +02:00
|
|
|
it = m_Requests.find(id);
|
|
|
|
|
|
|
|
if (it == m_Requests.end())
|
|
|
|
return;
|
|
|
|
|
2013-03-15 11:19:52 +01:00
|
|
|
it->second.Callback(sender, it->second.Request, message, false);
|
2012-06-14 11:18:20 +02:00
|
|
|
|
|
|
|
m_Requests.erase(it);
|
|
|
|
}
|
2012-06-20 16:52:56 +02:00
|
|
|
|
2013-03-15 11:19:52 +01:00
|
|
|
EndpointManager *EndpointManager::GetInstance(void)
|
2012-06-27 18:43:34 +02:00
|
|
|
{
|
2013-03-15 11:19:52 +01:00
|
|
|
return Singleton<EndpointManager>::GetInstance();
|
2012-06-27 18:43:34 +02:00
|
|
|
}
|