icinga2/components/demo/democomponent.cpp

74 lines
2.1 KiB
C++
Raw Normal View History

2012-04-23 13:45:41 +02:00
#include "i2-demo.h"
using namespace icinga;
IcingaApplication::Ptr DemoComponent::GetIcingaApplication(void)
{
return static_pointer_cast<IcingaApplication>(GetApplication());
}
string DemoComponent::GetName(void) const
{
return "democomponent";
}
void DemoComponent::Start(void)
{
m_DemoEndpoint = make_shared<VirtualEndpoint>();
2012-04-23 14:06:13 +02:00
m_DemoEndpoint->RegisterMethodHandler("demo::HelloWorld",
bind_weak(&DemoComponent::HelloWorldRequestHAndler, shared_from_this()));
2012-04-23 13:45:41 +02:00
m_DemoEndpoint->RegisterMethodSource("demo::HelloWorld");
EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager();
endpointManager->RegisterEndpoint(m_DemoEndpoint);
endpointManager->OnNewEndpoint += bind_weak(&DemoComponent::NewEndpointHandler, shared_from_this());
endpointManager->ForeachEndpoint(bind(&DemoComponent::NewEndpointHandler, this, _1));
m_DemoTimer = make_shared<Timer>();
m_DemoTimer->SetInterval(1);
m_DemoTimer->OnTimerExpired += bind_weak(&DemoComponent::DemoTimerHandler, shared_from_this());
m_DemoTimer->Start();
}
void DemoComponent::Stop(void)
{
2012-04-23 14:01:31 +02:00
IcingaApplication::Ptr app = GetIcingaApplication();
if (app) {
EndpointManager::Ptr endpointManager = app->GetEndpointManager();
endpointManager->UnregisterEndpoint(m_DemoEndpoint);
}
2012-04-23 13:45:41 +02:00
}
2012-04-23 14:01:31 +02:00
int DemoComponent::NewEndpointHandler(const NewEndpointEventArgs& neea)
2012-04-23 13:45:41 +02:00
{
2012-04-23 14:06:13 +02:00
/* Allow sending/receiving demo messages without authentication */
2012-04-23 14:01:31 +02:00
neea.Endpoint->AddAllowedMethodSinkPrefix("demo::");
neea.Endpoint->AddAllowedMethodSourcePrefix("demo::");
2012-04-23 13:45:41 +02:00
return 0;
}
int DemoComponent::DemoTimerHandler(const TimerEventArgs& tea)
{
cout << "Sending multicast 'hello world' message." << endl;
JsonRpcRequest request;
2012-04-23 14:01:31 +02:00
request.SetMethod("demo::HelloWorld");
2012-04-23 13:45:41 +02:00
EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager();
2012-04-23 14:01:31 +02:00
endpointManager->SendMulticastRequest(m_DemoEndpoint, request);
2012-04-23 13:45:41 +02:00
return 0;
}
int DemoComponent::HelloWorldRequestHAndler(const NewRequestEventArgs& nrea)
{
2012-04-23 14:01:31 +02:00
cout << "Got Hello World from " << nrea.Sender->GetAddress() << endl;
2012-04-23 13:45:41 +02:00
return 0;
}
EXPORT_COMPONENT(DemoComponent);