icinga2/icinga/endpointmanager.cpp

152 lines
3.7 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>();
RegisterEndpoint(endpoint);
2012-04-18 15:22:25 +02:00
endpoint->Connect(host, port);
}
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>();
RegisterEndpoint(endpoint);
2012-04-18 15:22:25 +02:00
endpoint->SetClient(static_pointer_cast<JsonRpcClient>(ncea.Client));
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));
}
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 (Endpoint::Ptr)> callback)
{
for (list<Endpoint::Ptr>::iterator i = m_Endpoints.begin(); i != m_Endpoints.end(); i++) {
callback(*i);
}
}