1 /******************************************************************************
3 * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) *
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. *
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. *
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 ******************************************************************************/
20 #include "i2-configrpc.h"
22 using namespace icinga;
24 string ConfigRpcComponent::GetName(void) const
26 return "configcomponent";
29 void ConfigRpcComponent::Start(void)
31 EndpointManager::Ptr endpointManager = GetEndpointManager();
33 m_ConfigRpcEndpoint = make_shared<VirtualEndpoint>();
36 if (GetConfig()->GetProperty("configSource", &configSource) && configSource != 0) {
37 m_ConfigRpcEndpoint->RegisterTopicHandler("config::FetchObjects",
38 bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this()));
40 ConfigObject::GetAllObjects()->OnObjectAdded += bind_weak(&ConfigRpcComponent::LocalObjectCommittedHandler, shared_from_this());
41 ConfigObject::GetAllObjects()->OnObjectCommitted += bind_weak(&ConfigRpcComponent::LocalObjectCommittedHandler, shared_from_this());
42 ConfigObject::GetAllObjects()->OnObjectRemoved += bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this());
44 m_ConfigRpcEndpoint->RegisterPublication("config::ObjectCommitted");
45 m_ConfigRpcEndpoint->RegisterPublication("config::ObjectRemoved");
48 endpointManager->OnNewEndpoint += bind_weak(&ConfigRpcComponent::NewEndpointHandler, shared_from_this());
50 m_ConfigRpcEndpoint->RegisterPublication("config::FetchObjects");
51 m_ConfigRpcEndpoint->RegisterTopicHandler("config::ObjectCommitted",
52 bind_weak(&ConfigRpcComponent::RemoteObjectCommittedHandler, shared_from_this()));
53 m_ConfigRpcEndpoint->RegisterTopicHandler("config::ObjectRemoved",
54 bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this()));
56 endpointManager->RegisterEndpoint(m_ConfigRpcEndpoint);
59 void ConfigRpcComponent::Stop(void)
61 EndpointManager::Ptr mgr = GetEndpointManager();
64 mgr->UnregisterEndpoint(m_ConfigRpcEndpoint);
67 int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea)
69 ea.Endpoint->OnSessionEstablished += bind_weak(&ConfigRpcComponent::SessionEstablishedHandler, shared_from_this());
74 int ConfigRpcComponent::SessionEstablishedHandler(const EventArgs& ea)
76 RequestMessage request;
77 request.SetMethod("config::FetchObjects");
79 Endpoint::Ptr endpoint = static_pointer_cast<Endpoint>(ea.Source);
80 GetEndpointManager()->SendUnicastMessage(m_ConfigRpcEndpoint, endpoint, request);
85 RequestMessage ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties)
88 msg.SetMethod(method);
91 msg.SetParams(params);
93 params.SetProperty("name", object->GetName());
94 params.SetProperty("type", object->GetType());
96 if (includeProperties)
97 params.SetProperty("properties", object->GetProperties());
102 bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object)
104 return (!object->IsLocal());
107 int ConfigRpcComponent::FetchObjectsHandler(const NewRequestEventArgs& ea)
109 Endpoint::Ptr client = ea.Sender;
110 ConfigObject::Set::Ptr allObjects = ConfigObject::GetAllObjects();
112 for (ConfigObject::Set::Iterator ci = allObjects->Begin(); ci != allObjects->End(); ci++) {
113 ConfigObject::Ptr object = *ci;
115 if (!ShouldReplicateObject(object))
118 RequestMessage request = MakeObjectMessage(object, "config::ObjectCreated", true);
120 GetEndpointManager()->SendUnicastMessage(m_ConfigRpcEndpoint, client, request);
126 int ConfigRpcComponent::LocalObjectCommittedHandler(const ObjectSetEventArgs<ConfigObject::Ptr>& ea)
128 ConfigObject::Ptr object = ea.Target;
130 if (!ShouldReplicateObject(object))
133 GetEndpointManager()->SendMulticastMessage(m_ConfigRpcEndpoint,
134 MakeObjectMessage(object, "config::ObjectCreated", true));
139 int ConfigRpcComponent::LocalObjectRemovedHandler(const ObjectSetEventArgs<ConfigObject::Ptr>& ea)
141 ConfigObject::Ptr object = ea.Target;
143 if (!ShouldReplicateObject(object))
146 GetEndpointManager()->SendMulticastMessage(m_ConfigRpcEndpoint,
147 MakeObjectMessage(object, "config::ObjectRemoved", false));
152 int ConfigRpcComponent::RemoteObjectCommittedHandler(const NewRequestEventArgs& ea)
154 RequestMessage message = ea.Request;
157 if (!message.GetParams(¶ms))
161 if (!params.GetProperty("name", &name))
165 if (!params.GetProperty("type", &type))
168 MessagePart properties;
169 if (!params.GetProperty("properties", &properties))
172 ConfigObject::Ptr object = ConfigObject::GetObject(type, name);
175 object = make_shared<ConfigObject>(properties.GetDictionary());
177 object->SetProperties(properties.GetDictionary());
184 int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea)
186 RequestMessage message = ea.Request;
189 if (!message.GetParams(¶ms))
193 if (!params.GetProperty("name", &name))
197 if (!params.GetProperty("type", &type))
200 ConfigObject::Ptr object = ConfigObject::GetObject(type, name);
205 if (!object->IsLocal())
206 object->Unregister();
211 EXPORT_COMPONENT(configrpc, ConfigRpcComponent);