icinga2/icinga/endpointmanager.cpp

158 lines
3.9 KiB
C++
Raw Normal View History

#include "i2-icinga.h"
using namespace icinga;
void EndpointManager::SetIdentity(string identity)
{
m_Identity = identity;
}
string EndpointManager::GetIdentity(void) const
{
return m_Identity;
}
void EndpointManager::AddListener(unsigned short port)
{
JsonRpcServer::Ptr server = make_shared<JsonRpcServer>();
RegisterServer(server);
server->MakeSocket();
server->Bind(port);
server->Listen();
server->Start();
}
2012-04-18 15:22:25 +02:00
void EndpointManager::AddConnection(string host, unsigned short port)
{
2012-04-18 15:22:25 +02:00
JsonRpcEndpoint::Ptr endpoint = make_shared<JsonRpcEndpoint>();
endpoint->Connect(host, port);
RegisterEndpoint(endpoint);
}
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-18 15:22:25 +02:00
JsonRpcEndpoint::Ptr endpoint = make_shared<JsonRpcEndpoint>();
endpoint->SetClient(static_pointer_cast<JsonRpcClient>(ncea.Client));
RegisterEndpoint(endpoint);
return 0;
}
2012-04-18 15:22:25 +02:00
void EndpointManager::UnregisterServer(JsonRpcServer::Ptr server)
{
2012-04-18 15:22:25 +02:00
m_Servers.remove(server);
// TODO: unbind event
}
void EndpointManager::RegisterEndpoint(Endpoint::Ptr endpoint)
{
2012-04-18 15:22:25 +02:00
endpoint->SetEndpointManager(static_pointer_cast<EndpointManager>(shared_from_this()));
m_Endpoints.push_front(endpoint);
2012-04-18 15:22:25 +02:00
endpoint->OnNewMethodSink += bind_weak(&EndpointManager::NewMethodSinkHandler, shared_from_this());
endpoint->ForeachMethodSink(bind(&EndpointManager::NewMethodSinkHandler, this, _1));
endpoint->OnNewMethodSource += bind_weak(&EndpointManager::NewMethodSourceHandler, shared_from_this());
endpoint->ForeachMethodSource(bind(&EndpointManager::NewMethodSourceHandler, this, _1));
NewEndpointEventArgs neea;
neea.Source = shared_from_this();
neea.Endpoint = endpoint;
OnNewEndpoint(neea);
}
void EndpointManager::UnregisterEndpoint(Endpoint::Ptr endpoint)
{
m_Endpoints.remove(endpoint);
}
2012-04-18 15:22:25 +02:00
void EndpointManager::SendAnycastRequest(Endpoint::Ptr sender, const JsonRpcRequest& request, bool fromLocal)
{
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-16 16:27:41 +02:00
throw InvalidArgumentException();
for (list<Endpoint::Ptr>::iterator i = m_Endpoints.begin(); i != m_Endpoints.end(); i++)
{
Endpoint::Ptr endpoint = *i;
2012-04-16 16:27:41 +02:00
if (endpoint == sender)
continue;
2012-04-18 15:22:25 +02:00
/* send non-local messages to just the local endpoints */
if (!fromLocal && !endpoint->IsLocal())
continue;
2012-04-16 16:27:41 +02:00
if (endpoint->IsMethodSink(method))
2012-04-18 15:22:25 +02:00
endpoint->ProcessRequest(sender, request);
}
}
int EndpointManager::NewMethodSinkHandler(const NewMethodEventArgs& ea)
{
Endpoint::Ptr sender = static_pointer_cast<Endpoint>(ea.Source);
if (!sender->IsLocal())
return 0;
JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("message::Subscribe");
Message params;
params.GetDictionary()->SetValueString("method", ea.Method);
request.SetParams(params);
SendMulticastRequest(sender, request);
return 0;
}
int EndpointManager::NewMethodSourceHandler(const NewMethodEventArgs& ea)
{
Endpoint::Ptr sender = static_pointer_cast<Endpoint>(ea.Source);
if (!sender->IsLocal())
return 0;
JsonRpcRequest request;
request.SetVersion("2.0");
request.SetMethod("message::Provide");
Message params;
params.GetDictionary()->SetValueString("method", ea.Method);
request.SetParams(params);
SendMulticastRequest(sender, request);
return 0;
}
void EndpointManager::ForeachEndpoint(function<int (const NewEndpointEventArgs&)> callback)
2012-04-18 15:22:25 +02:00
{
NewEndpointEventArgs neea;
neea.Source = shared_from_this();
2012-04-18 15:22:25 +02:00
for (list<Endpoint::Ptr>::iterator i = m_Endpoints.begin(); i != m_Endpoints.end(); i++) {
neea.Endpoint = *i;
callback(neea);
}
}