2012-05-10 12:06:41 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Icinga 2 *
|
2016-01-12 08:29:59 +01:00
|
|
|
* Copyright (C) 2012-2016 Icinga Development Team (https://www.icinga.org/) *
|
2012-05-10 12:06:41 +02:00
|
|
|
* *
|
|
|
|
* 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
|
|
|
******************************************************************************/
|
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "demo/demo.hpp"
|
2015-03-28 11:04:42 +01:00
|
|
|
#include "demo/demo.tcpp"
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/apilistener.hpp"
|
|
|
|
#include "remote/apifunction.hpp"
|
2015-08-15 20:28:05 +02:00
|
|
|
#include "base/configtype.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);
|
2013-03-12 13:45:54 +01:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
REGISTER_APIFUNCTION(HelloWorld, demo, &Demo::DemoMessageHandler);
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
|
|
|
* Starts the component.
|
|
|
|
*/
|
2015-08-20 17:18:48 +02:00
|
|
|
void Demo::Start(bool runtimeCreated)
|
2012-04-23 13:45:41 +02:00
|
|
|
{
|
2015-08-20 17:18:48 +02:00
|
|
|
ObjectImpl<Demo>::Start(runtimeCreated);
|
2012-04-23 13:45:41 +02:00
|
|
|
|
2014-11-08 21:17:16 +01:00
|
|
|
m_DemoTimer = new Timer();
|
2012-04-24 07:16:34 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2012-05-08 15:14:20 +02:00
|
|
|
/**
|
2014-05-03 20:02:22 +02:00
|
|
|
* Periodically broadcasts an API message.
|
2012-05-08 15:14:20 +02:00
|
|
|
*/
|
2014-05-03 20:02:22 +02:00
|
|
|
void Demo::DemoTimerHandler(void)
|
2012-04-23 13:45:41 +02:00
|
|
|
{
|
2014-11-08 21:17:16 +01:00
|
|
|
Dictionary::Ptr message = new Dictionary();
|
2014-05-03 20:02:22 +02:00
|
|
|
message->Set("method", "demo::HelloWorld");
|
|
|
|
|
|
|
|
ApiListener::Ptr listener = ApiListener::GetInstance();
|
|
|
|
if (listener) {
|
2015-08-04 14:47:44 +02:00
|
|
|
MessageOrigin::Ptr origin = new MessageOrigin();
|
2015-08-15 20:28:05 +02:00
|
|
|
listener->RelayMessage(origin, ConfigObject::Ptr(), message, true);
|
2014-05-28 13:03:44 +02:00
|
|
|
Log(LogInformation, "Demo", "Sent demo::HelloWorld message");
|
2014-05-03 20:02:22 +02:00
|
|
|
}
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|
|
|
|
|
2015-08-30 13:26:07 +02:00
|
|
|
Value Demo::DemoMessageHandler(const MessageOrigin::Ptr& origin, const Dictionary::Ptr&)
|
2012-04-23 13:45:41 +02:00
|
|
|
{
|
2014-10-20 10:09:57 +02:00
|
|
|
Log(LogInformation, "Demo")
|
2015-08-30 13:26:07 +02:00
|
|
|
<< "Got demo message from '" << origin->FromClient->GetEndpoint()->GetName() << "'";
|
2014-05-03 20:02:22 +02:00
|
|
|
|
|
|
|
return Empty;
|
2012-04-23 13:45:41 +02:00
|
|
|
}
|