]> granicus.if.org Git - icinga2/blob - components/demo/democomponent.cpp
5bf073cc50a3c711edca3b6d5be055b68030c9ad
[icinga2] / components / demo / democomponent.cpp
1 /******************************************************************************
2  * Icinga 2                                                                   *
3  * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/)        *
4  *                                                                            *
5  * This program is free software; you can redistribute it and/or              *
6  * modify it under the terms of the GNU General Public License                *
7  * as published by the Free Software Foundation; either version 2             *
8  * of the License, or (at your option) any later version.                     *
9  *                                                                            *
10  * This program is distributed in the hope that it will be useful,            *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
13  * GNU General Public License for more details.                               *
14  *                                                                            *
15  * You should have received a copy of the GNU General Public License          *
16  * along with this program; if not, write to the Free Software Foundation     *
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.             *
18  ******************************************************************************/
19
20 #include "i2-demo.h"
21
22 using namespace icinga;
23
24 /**
25  * Returns the name of the component.
26  *
27  * @returns The name.
28  */
29 string DemoComponent::GetName(void) const
30 {
31         return "democomponent";
32 }
33
34 /**
35  * Starts the component.
36  */
37 void DemoComponent::Start(void)
38 {
39         m_DemoEndpoint = make_shared<VirtualEndpoint>();
40         m_DemoEndpoint->RegisterTopicHandler("demo::HelloWorld",
41             bind_weak(&DemoComponent::HelloWorldRequestHandler, shared_from_this()));
42         m_DemoEndpoint->RegisterPublication("demo::HelloWorld");
43         GetEndpointManager()->RegisterEndpoint(m_DemoEndpoint);
44
45         m_DemoTimer = make_shared<Timer>();
46         m_DemoTimer->SetInterval(5);
47         m_DemoTimer->OnTimerExpired += bind_weak(&DemoComponent::DemoTimerHandler, shared_from_this());
48         m_DemoTimer->Start();
49 }
50
51 /**
52  * Stops the component.
53  */
54 void DemoComponent::Stop(void)
55 {
56         IcingaApplication::Ptr app = GetIcingaApplication();
57
58         if (app) {
59                 EndpointManager::Ptr endpointManager = app->GetEndpointManager();
60                 endpointManager->UnregisterEndpoint(m_DemoEndpoint);
61         }
62 }
63
64 /**
65  * Periodically sends a demo::HelloWorld message.
66  *
67  * @param - Event arguments for the timer.
68  * @returns 0
69  */
70 int DemoComponent::DemoTimerHandler(const TimerEventArgs&)
71 {
72         Application::Log("Sending multicast 'hello world' message.");
73
74         RequestMessage request;
75         request.SetMethod("demo::HelloWorld");
76
77         EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager();
78         endpointManager->SendMulticastMessage(m_DemoEndpoint, request);
79
80         return 0;
81 }
82
83 /**
84  * Processes demo::HelloWorld messages.
85  */
86 int DemoComponent::HelloWorldRequestHandler(const NewRequestEventArgs& nrea)
87 {
88         Application::Log("Got 'hello world' from address=" + nrea.Sender->GetAddress() + ", identity=" + nrea.Sender->GetIdentity());
89
90         return 0;
91 }
92
93 EXPORT_COMPONENT(demo, DemoComponent);