2012-04-19 11:29:36 +02:00
|
|
|
#include "i2-icinga.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
|
|
|
string SubscriptionComponent::GetName(void) const
|
|
|
|
{
|
|
|
|
return "subscriptioncomponent";
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubscriptionComponent::Start(void)
|
|
|
|
{
|
|
|
|
m_SubscriptionEndpoint = make_shared<VirtualEndpoint>();
|
|
|
|
m_SubscriptionEndpoint->RegisterMethodHandler("message::Subscribe", bind_weak(&SubscriptionComponent::SubscribeMessageHandler, shared_from_this()));
|
|
|
|
m_SubscriptionEndpoint->RegisterMethodHandler("message::Provide", bind_weak(&SubscriptionComponent::ProvideMessageHandler, shared_from_this()));
|
|
|
|
m_SubscriptionEndpoint->RegisterMethodSource("message::Subscribe");
|
|
|
|
m_SubscriptionEndpoint->RegisterMethodSource("message::Provide");
|
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
EndpointManager::Ptr mgr = GetEndpointManager();
|
2012-04-19 11:29:36 +02:00
|
|
|
mgr->OnNewEndpoint += bind_weak(&SubscriptionComponent::NewEndpointHandler, shared_from_this());
|
|
|
|
mgr->ForeachEndpoint(bind(&SubscriptionComponent::NewEndpointHandler, this, _1));
|
|
|
|
mgr->RegisterEndpoint(m_SubscriptionEndpoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SubscriptionComponent::Stop(void)
|
|
|
|
{
|
2012-04-24 07:16:34 +02:00
|
|
|
EndpointManager::Ptr mgr = GetEndpointManager();
|
2012-04-20 16:10:38 +02:00
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
if (mgr)
|
2012-04-20 16:10:38 +02:00
|
|
|
mgr->UnregisterEndpoint(m_SubscriptionEndpoint);
|
2012-04-19 11:29:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int SubscriptionComponent::SyncSubscription(Endpoint::Ptr target, string type, const NewMethodEventArgs& nmea)
|
|
|
|
{
|
|
|
|
JsonRpcRequest request;
|
|
|
|
request.SetVersion("2.0");
|
|
|
|
request.SetMethod(type);
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
SubscriptionMessage subscriptionMessage;
|
|
|
|
subscriptionMessage.SetMethod(nmea.Method);
|
|
|
|
request.SetParams(subscriptionMessage);
|
2012-04-19 11:29:36 +02:00
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
GetEndpointManager()->SendUnicastRequest(m_SubscriptionEndpoint, target, request);
|
2012-04-19 11:29:36 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SubscriptionComponent::SyncSubscriptions(Endpoint::Ptr target, const NewEndpointEventArgs& neea)
|
|
|
|
{
|
|
|
|
Endpoint::Ptr source = neea.Endpoint;
|
|
|
|
|
|
|
|
if (!source->IsLocal())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
source->ForeachMethodSink(bind(&SubscriptionComponent::SyncSubscription, this, target, "message::Subscribe", _1));
|
|
|
|
source->ForeachMethodSource(bind(&SubscriptionComponent::SyncSubscription, this, target, "message::Provide", _1));
|
|
|
|
|
|
|
|
// TODO: bind to endpoint's events
|
|
|
|
//endpoint->OnNewMethodSink...
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SubscriptionComponent::NewEndpointHandler(const NewEndpointEventArgs& neea)
|
|
|
|
{
|
|
|
|
if (neea.Endpoint->IsLocal())
|
|
|
|
return 0;
|
|
|
|
|
2012-04-23 13:45:41 +02:00
|
|
|
neea.Endpoint->AddAllowedMethodSinkPrefix("message::");
|
|
|
|
neea.Endpoint->AddAllowedMethodSourcePrefix("message::");
|
|
|
|
|
2012-04-23 17:00:39 +02:00
|
|
|
neea.Endpoint->RegisterMethodSink("message::Subscribe");
|
|
|
|
neea.Endpoint->RegisterMethodSink("message::Provide");
|
|
|
|
|
2012-04-24 07:16:34 +02:00
|
|
|
GetEndpointManager()->ForeachEndpoint(bind(&SubscriptionComponent::SyncSubscriptions, this, neea.Endpoint, _1));
|
2012-04-23 17:00:39 +02:00
|
|
|
|
2012-04-19 11:29:36 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SubscriptionComponent::SubscribeMessageHandler(const NewRequestEventArgs& nrea)
|
|
|
|
{
|
|
|
|
Message params;
|
|
|
|
if (!nrea.Request.GetParams(¶ms))
|
|
|
|
return 0;
|
|
|
|
|
2012-04-20 10:38:11 +02:00
|
|
|
SubscriptionMessage subscriptionMessage = params;
|
|
|
|
|
2012-04-19 11:29:36 +02:00
|
|
|
string method;
|
2012-04-20 10:38:11 +02:00
|
|
|
if (!subscriptionMessage.GetMethod(&method))
|
2012-04-19 11:29:36 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
nrea.Sender->RegisterMethodSink(method);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int SubscriptionComponent::ProvideMessageHandler(const NewRequestEventArgs& nrea)
|
|
|
|
{
|
|
|
|
Message params;
|
|
|
|
if (!nrea.Request.GetParams(¶ms))
|
|
|
|
return 0;
|
|
|
|
|
2012-04-20 10:38:11 +02:00
|
|
|
SubscriptionMessage subscriptionMessage = params;
|
|
|
|
|
2012-04-19 11:29:36 +02:00
|
|
|
string method;
|
2012-04-20 10:38:11 +02:00
|
|
|
if (!subscriptionMessage.GetMethod(&method))
|
2012-04-19 11:29:36 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
nrea.Sender->RegisterMethodSource(method);
|
|
|
|
return 0;
|
|
|
|
}
|