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-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.
|
|
|
|
*
|
|
|
|
* @param service The name of the service to listen on (@see getaddrinfo).
|
|
|
|
*/
|
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())
|
|
|
|
throw InvalidArgumentException("SSL context is required for AddListener()");
|
|
|
|
|
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-04-26 21:33:23 +02:00
|
|
|
Application::Log(s.str());
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-04-24 14:02:15 +02:00
|
|
|
JsonRpcServer::Ptr server = 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.
|
|
|
|
*
|
|
|
|
* @param node The remote host (@see getaddrinfo).
|
|
|
|
* @param service The remote port (@see getaddrinfo).
|
|
|
|
*/
|
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-04-26 16:45:00 +02:00
|
|
|
Application::Log(s.str());
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcEndpoint::Ptr endpoint = 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-05-16 11:30:54 +02:00
|
|
|
server->OnNewClient += bind_weak(&EndpointManager::NewClientHandler,
|
|
|
|
shared_from_this());
|
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-04-18 15:22:25 +02:00
|
|
|
int EndpointManager::NewClientHandler(const NewClientEventArgs& ncea)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-04-26 16:45:00 +02:00
|
|
|
string address = ncea.Client->GetPeerAddress();
|
|
|
|
Application::Log("Accepted new client from " + address);
|
2012-04-26 12:58:20 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcEndpoint::Ptr endpoint = make_shared<JsonRpcEndpoint>();
|
|
|
|
endpoint->SetClient(static_pointer_cast<JsonRpcClient>(ncea.Client));
|
2012-04-19 11:29:36 +02:00
|
|
|
RegisterEndpoint(endpoint);
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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-04-27 13:12:06 +02:00
|
|
|
if (!endpoint->IsLocal() && endpoint->GetIdentity() != "")
|
|
|
|
throw InvalidArgumentException("Identity must be empty.");
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
endpoint->SetEndpointManager(static_pointer_cast<EndpointManager>(shared_from_this()));
|
2012-05-13 10:44:03 +02:00
|
|
|
m_Endpoints.push_back(endpoint);
|
2012-04-18 15:22:25 +02:00
|
|
|
|
2012-04-19 11:29:36 +02:00
|
|
|
NewEndpointEventArgs neea;
|
|
|
|
neea.Source = shared_from_this();
|
|
|
|
neea.Endpoint = endpoint;
|
|
|
|
OnNewEndpoint(neea);
|
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-05-13 10:44:03 +02:00
|
|
|
m_Endpoints.erase(
|
|
|
|
remove(m_Endpoints.begin(), m_Endpoints.end(), endpoint),
|
|
|
|
m_Endpoints.end());
|
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
|
|
|
{
|
2012-05-15 16:24:04 +02:00
|
|
|
/* don't forward messages back to the sender */
|
2012-04-23 16:48:40 +02:00
|
|
|
if (sender == recipient)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* 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-05-15 16:24:04 +02:00
|
|
|
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,
|
|
|
|
const RpcRequest& message)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-04-16 16:27:41 +02:00
|
|
|
throw NotImplementedException();
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const RpcRequest& message)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
|
|
|
string id;
|
2012-05-15 16:24:04 +02:00
|
|
|
if (message.GetID(&id))
|
2012-04-16 16:27:41 +02:00
|
|
|
throw InvalidArgumentException("Multicast requests must not have an ID.");
|
|
|
|
|
|
|
|
string method;
|
2012-05-15 16:24:04 +02:00
|
|
|
if (!message.GetMethod(&method))
|
2012-04-30 08:22:30 +02:00
|
|
|
throw InvalidArgumentException("Message is missing the 'method' property.");
|
2012-04-16 16:27:41 +02:00
|
|
|
|
2012-05-13 10:44:03 +02:00
|
|
|
for (vector<Endpoint::Ptr>::iterator i = m_Endpoints.begin(); i != m_Endpoints.end(); i++)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
2012-05-15 16:24:04 +02:00
|
|
|
Endpoint::Ptr recipient = *i;
|
|
|
|
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-05-08 09:20:42 +02:00
|
|
|
void EndpointManager::ForEachEndpoint(function<int (const NewEndpointEventArgs&)> callback)
|
2012-04-18 15:22:25 +02:00
|
|
|
{
|
2012-04-19 11:29:36 +02:00
|
|
|
NewEndpointEventArgs neea;
|
|
|
|
neea.Source = shared_from_this();
|
2012-04-27 11:44:34 +02:00
|
|
|
|
2012-05-13 10:44:03 +02:00
|
|
|
vector<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++;
|
|
|
|
|
|
|
|
neea.Endpoint = *prev;
|
2012-04-19 11:29:36 +02:00
|
|
|
callback(neea);
|
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-05-13 10:44:03 +02:00
|
|
|
vector<Endpoint::Ptr>::const_iterator i;
|
2012-05-07 13:48:17 +02:00
|
|
|
for (i = m_Endpoints.begin(); i != m_Endpoints.end(); i++) {
|
|
|
|
if ((*i)->GetIdentity() == identity)
|
2012-05-07 14:52:49 +02:00
|
|
|
return *i;
|
2012-05-07 13:48:17 +02:00
|
|
|
}
|
|
|
|
|
2012-05-07 14:52:49 +02:00
|
|
|
return Endpoint::Ptr();
|
2012-05-07 13:48:17 +02:00
|
|
|
}
|