2012-04-06 08:56:52 +02:00
|
|
|
#include "i2-icinga.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
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-04-27 11:44:34 +02:00
|
|
|
string EndpointManager::GetIdentity(void) const
|
|
|
|
{
|
|
|
|
return m_Identity;
|
|
|
|
}
|
|
|
|
|
2012-04-27 13:12:06 +02:00
|
|
|
void EndpointManager::SetSSLContext(shared_ptr<SSL_CTX> sslContext)
|
|
|
|
{
|
|
|
|
m_SSLContext = sslContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
shared_ptr<SSL_CTX> EndpointManager::GetSSLContext(void) const
|
|
|
|
{
|
|
|
|
return m_SSLContext;
|
|
|
|
}
|
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
void EndpointManager::AddListener(unsigned short port)
|
|
|
|
{
|
2012-04-26 21:33:23 +02:00
|
|
|
stringstream s;
|
|
|
|
s << "Adding new listener: port " << port;
|
|
|
|
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-04-27 09:54:07 +02:00
|
|
|
server->Bind(port, AF_INET6);
|
2012-04-06 08:56:52 +02:00
|
|
|
server->Listen();
|
|
|
|
server->Start();
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
void EndpointManager::AddConnection(string host, unsigned short port)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-04-26 16:45:00 +02:00
|
|
|
stringstream s;
|
2012-04-27 09:54:07 +02:00
|
|
|
s << "Adding new endpoint: [" << host << "]:" << port;
|
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-24 14:02:15 +02:00
|
|
|
endpoint->Connect(host, port, m_SSLContext);
|
2012-04-19 11:29:36 +02:00
|
|
|
RegisterEndpoint(endpoint);
|
2012-04-06 08:56:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void EndpointManager::RegisterServer(JsonRpcServer::Ptr server)
|
|
|
|
{
|
|
|
|
m_Servers.push_front(server);
|
|
|
|
server->OnNewClient += bind_weak(&EndpointManager::NewClientHandler, shared_from_this());
|
|
|
|
}
|
|
|
|
|
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-04-18 15:22:25 +02:00
|
|
|
void EndpointManager::UnregisterServer(JsonRpcServer::Ptr server)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
m_Servers.remove(server);
|
|
|
|
// TODO: unbind event
|
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-04-06 08:56:52 +02:00
|
|
|
m_Endpoints.push_front(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
|
|
|
}
|
|
|
|
|
|
|
|
void EndpointManager::UnregisterEndpoint(Endpoint::Ptr endpoint)
|
|
|
|
{
|
|
|
|
m_Endpoints.remove(endpoint);
|
|
|
|
}
|
|
|
|
|
2012-04-23 16:48:40 +02:00
|
|
|
void EndpointManager::SendUnicastRequest(Endpoint::Ptr sender, Endpoint::Ptr recipient, const JsonRpcRequest& request, bool fromLocal)
|
|
|
|
{
|
|
|
|
if (sender == recipient)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* don't forward messages between non-local endpoints */
|
|
|
|
if (!fromLocal && !recipient->IsLocal())
|
|
|
|
return;
|
|
|
|
|
|
|
|
string method;
|
|
|
|
if (!request.GetMethod(&method))
|
|
|
|
throw InvalidArgumentException("Missing 'method' parameter.");
|
|
|
|
|
2012-04-30 15:30:45 +02:00
|
|
|
if (recipient->IsMethodSink(method))
|
2012-04-23 16:48:40 +02:00
|
|
|
recipient->ProcessRequest(sender, request);
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
void EndpointManager::SendAnycastRequest(Endpoint::Ptr sender, const JsonRpcRequest& request, bool fromLocal)
|
2012-04-06 08:56:52 +02:00
|
|
|
{
|
2012-04-16 16:27:41 +02:00
|
|
|
throw NotImplementedException();
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
void EndpointManager::SendMulticastRequest(Endpoint::Ptr sender, const JsonRpcRequest& request, bool fromLocal)
|
2012-04-16 16:27:41 +02:00
|
|
|
{
|
|
|
|
#ifdef _DEBUG
|
|
|
|
string id;
|
2012-04-18 15:22:25 +02:00
|
|
|
if (request.GetID(&id))
|
2012-04-16 16:27:41 +02:00
|
|
|
throw InvalidArgumentException("Multicast requests must not have an ID.");
|
|
|
|
#endif /* _DEBUG */
|
|
|
|
|
|
|
|
string method;
|
2012-04-18 15:22:25 +02:00
|
|
|
if (!request.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
|
|
|
|
|
|
|
for (list<Endpoint::Ptr>::iterator i = m_Endpoints.begin(); i != m_Endpoints.end(); i++)
|
|
|
|
{
|
2012-04-23 16:48:40 +02:00
|
|
|
SendUnicastRequest(sender, *i, request, fromLocal);
|
2012-04-18 15:22:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-19 11:29:36 +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
|
|
|
|
|
|
|
list<Endpoint::Ptr>::iterator prev, i;
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|