icinga2/lib/demo/demo.cpp

67 lines
2.5 KiB
C++
Raw Normal View History

/******************************************************************************
* Icinga 2 *
* Copyright (C) 2012-2014 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. *
******************************************************************************/
2014-05-25 16:23:35 +02:00
#include "demo/demo.hpp"
#include "remote/apilistener.hpp"
#include "remote/apifunction.hpp"
#include "base/dynamictype.hpp"
2014-10-19 14:21:12 +02:00
#include "base/logger.hpp"
2012-04-23 13:45:41 +02:00
using namespace icinga;
2013-09-25 09:39:31 +02:00
REGISTER_TYPE(Demo);
REGISTER_APIFUNCTION(HelloWorld, demo, &Demo::DemoMessageHandler);
/**
* Starts the component.
*/
2013-09-25 09:39:31 +02:00
void Demo::Start(void)
2012-04-23 13:45:41 +02:00
{
DynamicObject::Start();
2012-04-23 13:45:41 +02:00
m_DemoTimer = make_shared<Timer>();
m_DemoTimer->SetInterval(5);
2013-09-25 09:39:31 +02:00
m_DemoTimer->OnTimerExpired.connect(boost::bind(&Demo::DemoTimerHandler, this));
2012-04-23 13:45:41 +02:00
m_DemoTimer->Start();
}
/**
* Periodically broadcasts an API message.
*/
void Demo::DemoTimerHandler(void)
2012-04-23 13:45:41 +02:00
{
Dictionary::Ptr message = make_shared<Dictionary>();
message->Set("method", "demo::HelloWorld");
ApiListener::Ptr listener = ApiListener::GetInstance();
if (listener) {
listener->RelayMessage(MessageOrigin(), DynamicObject::Ptr(), message, true);
Log(LogInformation, "Demo", "Sent demo::HelloWorld message");
}
2012-04-23 13:45:41 +02:00
}
2014-05-22 09:02:44 +02:00
Value Demo::DemoMessageHandler(const MessageOrigin& origin, const Dictionary::Ptr&)
2012-04-23 13:45:41 +02:00
{
2014-10-20 10:09:57 +02:00
Log(LogInformation, "Demo")
<< "Got demo message from '" << origin.FromClient->GetEndpoint()->GetName() << "'";
return Empty;
2012-04-23 13:45:41 +02:00
}