icinga2/components/demo/democomponent.cpp

96 lines
3.2 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License *
* as published by the Free Software Foundation; either version 2 *
* of the License, or (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software Foundation *
2012-05-11 13:33:57 +02:00
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. *
******************************************************************************/
2012-04-23 13:45:41 +02:00
#include "i2-demo.h"
using namespace icinga;
/**
* Returns the name of the component.
*
* @returns The name.
*/
2012-04-23 13:45:41 +02:00
string DemoComponent::GetName(void) const
{
return "democomponent";
}
/**
* Starts the component.
*/
2012-04-23 13:45:41 +02:00
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);
m_DemoTimer = make_shared<Timer>();
m_DemoTimer->SetInterval(5);
2012-04-23 13:45:41 +02:00
m_DemoTimer->OnTimerExpired += bind_weak(&DemoComponent::DemoTimerHandler, shared_from_this());
m_DemoTimer->Start();
}
/**
* Stops the component.
*/
2012-04-23 13:45:41 +02:00
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
}
/**
* Periodically sends a demo::HelloWorld message.
*
2012-05-10 13:46:04 +02:00
* @param - Event arguments for the timer.
* @returns 0
*/
2012-05-10 13:46:04 +02:00
int DemoComponent::DemoTimerHandler(const TimerEventArgs&)
2012-04-23 13:45:41 +02:00
{
Application::Log("Sending multicast 'hello world' message.");
2012-04-23 13:45:41 +02:00
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;
}
/**
* Processes demo::HelloWorld messages.
*/
int DemoComponent::HelloWorldRequestHandler(const NewRequestEventArgs& nrea)
2012-04-23 13:45:41 +02:00
{
2012-04-27 11:57:14 +02:00
Application::Log("Got 'hello world' from address=" + nrea.Sender->GetAddress() + ", identity=" + nrea.Sender->GetIdentity());
2012-04-23 13:45:41 +02:00
return 0;
}
2012-05-12 16:12:26 +02:00
EXPORT_COMPONENT(demo, DemoComponent);