2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* 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-05-10 12:06:41 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
2012-04-23 13:45:41 +02:00
|
|
|
#include "i2-demo.h"
|
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Returns the name of the component.
|
|
|
|
*
|
|
|
|
* @returns The name.
|
|
|
|
*/
|
2012-04-23 13:45:41 +02:00
|
|
|
string DemoComponent::GetName(void) const
|
|
|
|
{
|
2012-06-27 18:43:34 +02:00
|
|
|
return "demo";
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
2012-04-23 13:45:41 +02:00
|
|
|
void DemoComponent::Start(void)
|
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
m_DemoEndpoint = boost::make_shared<VirtualEndpoint>();
|
2012-05-15 16:24:04 +02:00
|
|
|
m_DemoEndpoint->RegisterTopicHandler("demo::HelloWorld",
|
2012-06-16 03:42:54 +02:00
|
|
|
boost::bind(&DemoComponent::HelloWorldRequestHandler, this, _2, _3));
|
2012-05-15 16:24:04 +02:00
|
|
|
m_DemoEndpoint->RegisterPublication("demo::HelloWorld");
|
2012-06-27 18:43:34 +02:00
|
|
|
EndpointManager::GetInstance()->RegisterEndpoint(m_DemoEndpoint);
|
2012-04-23 13:45:41 +02:00
|
|
|
|
2012-06-15 19:32:41 +02:00
|
|
|
m_DemoTimer = boost::make_shared<Timer>();
|
2012-04-24 07:16:34 +02:00
|
|
|
m_DemoTimer->SetInterval(5);
|
2012-06-15 19:32:41 +02:00
|
|
|
m_DemoTimer->OnTimerExpired.connect(boost::bind(&DemoComponent::DemoTimerHandler, this));
|
2012-04-23 13:45:41 +02:00
|
|
|
m_DemoTimer->Start();
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Stops the component.
|
|
|
|
*/
|
2012-04-23 13:45:41 +02:00
|
|
|
void DemoComponent::Stop(void)
|
|
|
|
{
|
2012-06-27 18:43:34 +02:00
|
|
|
EndpointManager::Ptr endpointManager = EndpointManager::GetInstance();
|
2012-04-23 14:01:31 +02:00
|
|
|
|
2012-06-27 18:43:34 +02:00
|
|
|
if (endpointManager)
|
2012-04-23 14:01:31 +02:00
|
|
|
endpointManager->UnregisterEndpoint(m_DemoEndpoint);
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Periodically sends a demo::HelloWorld message.
|
|
|
|
*
|
2012-05-10 13:46:04 +02:00
|
|
|
* @param - Event arguments for the timer.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2012-06-15 19:32:41 +02:00
|
|
|
void DemoComponent::DemoTimerHandler(void)
|
2012-04-23 13:45:41 +02:00
|
|
|
{
|
2012-06-15 19:32:41 +02:00
|
|
|
Application::Log(LogInformation, "demo", "Sending multicast 'hello world' message.");
|
2012-04-23 13:45:41 +02:00
|
|
|
|
2012-05-21 12:53:38 +02:00
|
|
|
RequestMessage request;
|
2012-04-23 14:01:31 +02:00
|
|
|
request.SetMethod("demo::HelloWorld");
|
2012-04-23 13:45:41 +02:00
|
|
|
|
2012-06-27 18:43:34 +02:00
|
|
|
EndpointManager::GetInstance()->SendMulticastMessage(m_DemoEndpoint, request);
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Processes demo::HelloWorld messages.
|
|
|
|
*/
|
2012-06-16 03:42:54 +02:00
|
|
|
void DemoComponent::HelloWorldRequestHandler(const Endpoint::Ptr& sender, const RequestMessage& request)
|
2012-04-23 13:45:41 +02:00
|
|
|
{
|
2012-06-16 03:42:54 +02:00
|
|
|
Application::Log(LogInformation, "demo", "Got 'hello world' from address=" + sender->GetAddress() + ", identity=" + sender->GetIdentity());
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
2012-05-12 16:12:26 +02:00
|
|
|
EXPORT_COMPONENT(demo, DemoComponent);
|