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-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-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-04-27 13:12:06 +02:00
|
|
|
void EndpointManager::SetIdentity(string identity)
|
2012-04-24 14:02:15 +02:00
|
|
|
{
|
2012-04-27 11:44:34 +02:00
|
|
|
m_Identity = identity;
|
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-04-27 11:44:34 +02:00
|
|
|
string EndpointManager::GetIdentity(void) const
|
|
|
|
{
|
|
|
|
return m_Identity;
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Sets the SSL context that is used for remote connections.
|
|
|
|
*
|
|
|
|
* @param sslContext The new SSL context.
|
|
|
|
*/
|
2012-04-27 13:12:06 +02:00
|
|
|
void EndpointManager::SetSSLContext(shared_ptr<SSL_CTX> sslContext)
|
|
|
|
{
|
|
|
|
m_SSLContext = sslContext;
|
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves the SSL context that is used for remote connections.
|
|
|
|
*
|
|
|
|
* @returns The SSL context.
|
|
|
|
*/
|
2012-04-27 13:12:06 +02:00
|
|
|
shared_ptr<SSL_CTX> EndpointManager::GetSSLContext(void) const
|
|
|
|
{
|
|
|
|
return m_SSLContext;
|
|
|
|
}
|
|
|
|
|
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-05-07 13:48:17 +02:00
|
|
|
void EndpointManager::AddListener(string service)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-05-15 11:08:04 +02:00
|
|
|
if (!GetSSLContext())
|
2012-05-26 21:30:04 +02:00
|
|
|
throw logic_error("SSL context is required for AddListener()");
|
2012-05-15 11:08:04 +02:00
|
|
|
|
2012-04-26 21:33:23 +02:00
|
|
|
stringstream s;
|
2012-05-07 13:48:17 +02:00
|
|
|
s << "Adding new listener: port " << service;
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogInformation, "icinga", s.str());
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
JsonRpcServer::Ptr server = boost::make_shared<JsonRpcServer>(m_SSLContext);
|
2012-04-06 08:56:52 +02:00
|
|
|
RegisterServer(server);
|
|
|
|
|
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-05-07 13:48:17 +02:00
|
|
|
void EndpointManager::AddConnection(string node, string service)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-04-26 16:45:00 +02:00
|
|
|
stringstream s;
|
2012-05-07 13:48:17 +02:00
|
|
|
s << "Adding new endpoint: [" << node << "]:" << service;
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogInformation, "icinga", s.str());
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
JsonRpcEndpoint::Ptr endpoint = boost::make_shared<JsonRpcEndpoint>();
|
2012-04-19 11:29:36 +02:00
|
|
|
RegisterEndpoint(endpoint);
|
2012-05-07 13:48:17 +02:00
|
|
|
endpoint->Connect(node, service, m_SSLContext);
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Registers a new JSON-RPC server with this endpoint manager.
|
|
|
|
*
|
|
|
|
* @param server The JSON-RPC server.
|
|
|
|
*/
|
2012-04-06 08:56:52 +02:00
|
|
|
void EndpointManager::RegisterServer(JsonRpcServer::Ptr server)
|
|
|
|
{
|
2012-05-13 10:44:03 +02:00
|
|
|
m_Servers.push_back(server);
|
2012-06-15 19:32:41 +02:00
|
|
|
server->OnNewClient.connect(boost::bind(&EndpointManager::NewClientHandler,
|
2012-06-16 03:42:54 +02:00
|
|
|
this, _2));
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Processes a new client connection.
|
|
|
|
*
|
|
|
|
* @param ncea Event arguments.
|
|
|
|
*/
|
2012-06-16 03:42:54 +02:00
|
|
|
void EndpointManager::NewClientHandler(const TcpClient::Ptr& client)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-07-10 12:21:19 +02:00
|
|
|
Logger::Write(LogInformation, "icinga", "Accepted new client from " + client->GetPeerAddress());
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
JsonRpcEndpoint::Ptr endpoint = boost::make_shared<JsonRpcEndpoint>();
|
2012-06-16 03:42:54 +02:00
|
|
|
endpoint->SetClient(static_pointer_cast<JsonRpcClient>(client));
|
2012-06-24 02:56:48 +02:00
|
|
|
client->Start();
|
2012-04-19 11:29:36 +02:00
|
|
|
RegisterEndpoint(endpoint);
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Unregisters a JSON-RPC server.
|
|
|
|
*
|
|
|
|
* @param server The JSON-RPC server.
|
|
|
|
*/
|
2012-04-18 15:22:25 +02:00
|
|
|
void EndpointManager::UnregisterServer(JsonRpcServer::Ptr server)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-05-13 10:44:03 +02:00
|
|
|
m_Servers.erase(
|
|
|
|
remove(m_Servers.begin(), m_Servers.end(), server),
|
|
|
|
m_Servers.end());
|
2012-04-18 15:22:25 +02:00
|
|
|
// TODO: unbind event
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Registers a new endpoint with this endpoint manager.
|
|
|
|
*
|
|
|
|
* @param endpoint The new endpoint.
|
|
|
|
*/
|
2012-04-06 08:56:52 +02:00
|
|
|
void EndpointManager::RegisterEndpoint(Endpoint::Ptr endpoint)
|
|
|
|
{
|
2012-06-16 13:06:06 +02:00
|
|
|
endpoint->SetEndpointManager(GetSelf());
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-06-20 15:22:39 +02:00
|
|
|
UnregisterEndpoint(endpoint);
|
|
|
|
|
|
|
|
string identity = endpoint->GetIdentity();
|
|
|
|
|
|
|
|
if (!identity.empty()) {
|
|
|
|
m_Endpoints[identity] = endpoint;
|
|
|
|
OnNewEndpoint(GetSelf(), endpoint);
|
|
|
|
} else {
|
|
|
|
m_PendingEndpoints.push_back(endpoint);
|
|
|
|
}
|
2012-07-02 11:19:14 +02:00
|
|
|
|
|
|
|
if (endpoint->IsLocal()) {
|
|
|
|
/* this endpoint might have introduced new subscriptions
|
|
|
|
* or publications which affect remote endpoints, we need
|
|
|
|
* to close all fully-connected remote endpoints to make sure
|
|
|
|
* these subscriptions/publications are kept up-to-date. */
|
|
|
|
Iterator prev, it;
|
|
|
|
for (it = m_Endpoints.begin(); it != m_Endpoints.end(); ) {
|
|
|
|
prev = it;
|
|
|
|
it++;
|
|
|
|
|
|
|
|
if (!prev->second->IsLocal())
|
|
|
|
m_Endpoints.erase(prev);
|
|
|
|
}
|
|
|
|
}
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Unregisters an endpoint.
|
|
|
|
*
|
|
|
|
* @param endpoint The endpoint.
|
|
|
|
*/
|
2012-04-06 08:56:52 +02:00
|
|
|
void EndpointManager::UnregisterEndpoint(Endpoint::Ptr endpoint)
|
|
|
|
{
|
2012-06-20 15:22:39 +02:00
|
|
|
m_PendingEndpoints.erase(
|
|
|
|
remove(m_PendingEndpoints.begin(), m_PendingEndpoints.end(), endpoint),
|
|
|
|
m_PendingEndpoints.end());
|
|
|
|
|
|
|
|
string identity = endpoint->GetIdentity();
|
|
|
|
if (!identity.empty())
|
|
|
|
m_Endpoints.erase(identity);
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
* @param message The request.
|
|
|
|
*/
|
2012-05-16 11:30:54 +02:00
|
|
|
void EndpointManager::SendUnicastMessage(Endpoint::Ptr sender,
|
|
|
|
Endpoint::Ptr recipient, const MessagePart& message)
|
2012-04-23 16:48:40 +02:00
|
|
|
{
|
|
|
|
/* don't forward messages between non-local endpoints */
|
2012-05-15 16:24:04 +02:00
|
|
|
if (!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-05-16 11:30:54 +02:00
|
|
|
void EndpointManager::SendAnycastMessage(Endpoint::Ptr sender,
|
2012-05-21 12:53:38 +02:00
|
|
|
const RequestMessage& message)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-06-14 11:18:20 +02:00
|
|
|
string method;
|
|
|
|
if (!message.GetMethod(&method))
|
|
|
|
throw invalid_argument("Message is missing the 'method' property.");
|
|
|
|
|
|
|
|
vector<Endpoint::Ptr> candidates;
|
2012-07-17 19:19:03 +02:00
|
|
|
Endpoint::Ptr endpoint;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, endpoint), m_Endpoints) {
|
2012-06-22 13:40:09 +02:00
|
|
|
/* don't forward messages between non-local endpoints */
|
|
|
|
if (!sender->IsLocal() && !endpoint->IsLocal())
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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-05-16 11:30:54 +02:00
|
|
|
void EndpointManager::SendMulticastMessage(Endpoint::Ptr sender,
|
2012-05-21 12:53:38 +02:00
|
|
|
const RequestMessage& message)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
|
|
|
string id;
|
2012-05-15 16:24:04 +02:00
|
|
|
if (message.GetID(&id))
|
2012-05-26 21:30:04 +02:00
|
|
|
throw invalid_argument("Multicast requests must not have an ID.");
|
2012-04-16 16:27:41 +02:00
|
|
|
|
|
|
|
string method;
|
2012-05-15 16:24:04 +02:00
|
|
|
if (!message.GetMethod(&method))
|
2012-05-26 21:30:04 +02:00
|
|
|
throw invalid_argument("Message is missing the 'method' property.");
|
2012-04-16 16:27:41 +02:00
|
|
|
|
2012-07-17 19:19:03 +02:00
|
|
|
Endpoint::Ptr recipient;
|
|
|
|
BOOST_FOREACH(tie(tuples::ignore, recipient), m_Endpoints) {
|
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-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Calls the specified callback function for each registered endpoint.
|
|
|
|
*
|
|
|
|
* @param callback The callback function.
|
|
|
|
*/
|
2012-06-16 13:06:06 +02:00
|
|
|
void EndpointManager::ForEachEndpoint(function<void (const EndpointManager::Ptr&, const Endpoint::Ptr&)> callback)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-06-20 15:22:39 +02:00
|
|
|
map<string, Endpoint::Ptr>::iterator prev, i;
|
2012-04-27 11:44:34 +02:00
|
|
|
for (i = m_Endpoints.begin(); i != m_Endpoints.end(); ) {
|
|
|
|
prev = i;
|
|
|
|
i++;
|
|
|
|
|
2012-06-20 15:22:39 +02:00
|
|
|
callback(GetSelf(), prev->second);
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
}
|
2012-05-07 13:48:17 +02:00
|
|
|
|
2012-05-15 16:24:04 +02:00
|
|
|
/**
|
|
|
|
* Retrieves an endpoint that has the specified identity.
|
|
|
|
*
|
|
|
|
* @param identity The identity of the endpoint.
|
|
|
|
*/
|
2012-05-07 14:52:49 +02:00
|
|
|
Endpoint::Ptr EndpointManager::GetEndpointByIdentity(string identity) const
|
2012-05-07 13:48:17 +02:00
|
|
|
{
|
2012-06-20 15:22:39 +02:00
|
|
|
map<string, Endpoint::Ptr>::const_iterator i;
|
|
|
|
i = m_Endpoints.find(identity);
|
|
|
|
if (i != m_Endpoints.end())
|
|
|
|
return i->second;
|
|
|
|
else
|
|
|
|
return Endpoint::Ptr();
|
2012-05-07 13:48:17 +02:00
|
|
|
}
|
2012-06-14 11:18:20 +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-06-16 13:06:06 +02:00
|
|
|
function<void(const EndpointManager::Ptr&, const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> callback, time_t timeout)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
|
|
|
m_NextMessageID++;
|
|
|
|
|
|
|
|
stringstream idstream;
|
|
|
|
idstream << m_NextMessageID;
|
|
|
|
|
|
|
|
string id = idstream.str();
|
|
|
|
message.SetID(id);
|
|
|
|
|
|
|
|
PendingRequest pr;
|
|
|
|
pr.Request = message;
|
|
|
|
pr.Callback = callback;
|
|
|
|
pr.Timeout = time(NULL) + timeout;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
bool EndpointManager::RequestTimeoutLessComparer(const pair<string, PendingRequest>& a,
|
|
|
|
const pair<string, PendingRequest>& b)
|
|
|
|
{
|
|
|
|
return a.second.Timeout < b.second.Timeout;
|
|
|
|
}
|
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
void EndpointManager::RequestTimerHandler(void)
|
2012-06-14 11:18:20 +02:00
|
|
|
{
|
|
|
|
map<string, PendingRequest>::iterator it;
|
|
|
|
for (it = m_Requests.begin(); it != m_Requests.end(); it++) {
|
|
|
|
if (it->second.HasTimedOut()) {
|
2012-06-16 13:06:06 +02:00
|
|
|
it->second.Callback(GetSelf(), Endpoint::Ptr(), it->second.Request, ResponseMessage(), true);
|
2012-06-14 11:18:20 +02:00
|
|
|
|
|
|
|
m_Requests.erase(it);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EndpointManager::ProcessResponseMessage(const Endpoint::Ptr& sender, const ResponseMessage& message)
|
|
|
|
{
|
|
|
|
string id;
|
|
|
|
if (!message.GetID(&id))
|
|
|
|
throw invalid_argument("Response message must have a message ID.");
|
|
|
|
|
|
|
|
map<string, PendingRequest>::iterator it;
|
|
|
|
it = m_Requests.find(id);
|
|
|
|
|
|
|
|
if (it == m_Requests.end())
|
|
|
|
return;
|
|
|
|
|
2012-06-16 13:06:06 +02:00
|
|
|
it->second.Callback(GetSelf(), 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
|
|
|
|
|
|
|
EndpointManager::Iterator EndpointManager::Begin(void)
|
|
|
|
{
|
|
|
|
return m_Endpoints.begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
EndpointManager::Iterator EndpointManager::End(void)
|
|
|
|
{
|
|
|
|
return m_Endpoints.end();
|
|
|
|
}
|
|
|
|
|
2012-06-27 18:43:34 +02:00
|
|
|
EndpointManager::Ptr EndpointManager::GetInstance(void)
|
|
|
|
{
|
|
|
|
static EndpointManager::Ptr instance;
|
|
|
|
|
|
|
|
if (!instance)
|
|
|
|
instance = boost::make_shared<EndpointManager>();
|
|
|
|
|
|
|
|
return instance;
|
|
|
|
}
|