2012-04-13 13:16:54 +02:00
|
|
|
#include "i2-configrpc.h"
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
using namespace icinga;
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
IcingaApplication::Ptr ConfigRpcComponent::GetIcingaApplication(void)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-02 16:25:43 +02:00
|
|
|
return static_pointer_cast<IcingaApplication>(GetApplication());
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-19 11:29:36 +02:00
|
|
|
string ConfigRpcComponent::GetName(void) const
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
|
|
|
return "configcomponent";
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigRpcComponent::Start(void)
|
|
|
|
{
|
2012-04-02 20:50:35 +02:00
|
|
|
IcingaApplication::Ptr icingaApp = GetIcingaApplication();
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
EndpointManager::Ptr endpointManager = icingaApp->GetEndpointManager();
|
2012-04-02 20:50:35 +02:00
|
|
|
ConfigHive::Ptr configHive = icingaApp->GetConfigHive();
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
m_ConfigRpcEndpoint = make_shared<VirtualEndpoint>();
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
long configSource;
|
2012-04-01 19:32:41 +02:00
|
|
|
if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) {
|
2012-04-06 08:56:52 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodHandler("config::FetchObjects", bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this()));
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-03 11:13:17 +02:00
|
|
|
configHive->OnObjectCreated += bind_weak(&ConfigRpcComponent::LocalObjectCreatedHandler, shared_from_this());
|
|
|
|
configHive->OnObjectRemoved += bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this());
|
|
|
|
configHive->OnPropertyChanged += bind_weak(&ConfigRpcComponent::LocalPropertyChangedHandler, shared_from_this());
|
2012-04-06 08:56:52 +02:00
|
|
|
|
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectCreated");
|
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectRemoved");
|
|
|
|
m_ConfigRpcEndpoint->RegisterMethodSource("config::PropertyChanged");
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-23 09:48:20 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodHandler("auth::Welcome", bind_weak(&ConfigRpcComponent::WelcomeMessageHandler, shared_from_this()));
|
2012-04-20 10:38:11 +02:00
|
|
|
|
2012-04-06 08:56:52 +02:00
|
|
|
m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectCreated", bind_weak(&ConfigRpcComponent::RemoteObjectUpdatedHandler, shared_from_this()));
|
|
|
|
m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectRemoved", bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this()));
|
|
|
|
m_ConfigRpcEndpoint->RegisterMethodHandler("config::PropertyChanged", bind_weak(&ConfigRpcComponent::RemoteObjectUpdatedHandler, shared_from_this()));
|
|
|
|
|
|
|
|
endpointManager->RegisterEndpoint(m_ConfigRpcEndpoint);
|
2012-04-20 10:38:11 +02:00
|
|
|
|
|
|
|
endpointManager->OnNewEndpoint += bind_weak(&ConfigRpcComponent::NewEndpointHandler, shared_from_this());
|
|
|
|
endpointManager->ForeachEndpoint(bind(&ConfigRpcComponent::NewEndpointHandler, this, _1));
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ConfigRpcComponent::Stop(void)
|
|
|
|
{
|
|
|
|
// TODO: implement
|
|
|
|
}
|
|
|
|
|
2012-04-20 10:38:11 +02:00
|
|
|
int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea)
|
|
|
|
{
|
|
|
|
if (ea.Endpoint->HasIdentity()) {
|
|
|
|
JsonRpcRequest request;
|
|
|
|
request.SetMethod("config::FetchObjects");
|
2012-04-23 16:49:02 +02:00
|
|
|
|
|
|
|
EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager();
|
|
|
|
endpointManager->SendUnicastRequest(m_ConfigRpcEndpoint, ea.Endpoint, request);
|
2012-04-20 10:38:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int ConfigRpcComponent::WelcomeMessageHandler(const NewRequestEventArgs& ea)
|
|
|
|
{
|
|
|
|
NewEndpointEventArgs neea;
|
|
|
|
neea.Source = shared_from_this();
|
|
|
|
neea.Endpoint = ea.Sender;
|
|
|
|
NewEndpointHandler(neea);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest msg;
|
|
|
|
msg.SetMethod(method);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Message params;
|
|
|
|
msg.SetParams(params);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
params.GetDictionary()->SetPropertyString("name", object->GetName());
|
|
|
|
params.GetDictionary()->SetPropertyString("type", object->GetType());
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-20 15:49:12 +02:00
|
|
|
if (includeProperties)
|
|
|
|
params.SetPropertyMessage("properties", Message(object));
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return msg;
|
|
|
|
}
|
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object)
|
|
|
|
{
|
|
|
|
long replicate;
|
|
|
|
if (!object->GetPropertyInteger("replicate", &replicate))
|
2012-04-20 15:49:12 +02:00
|
|
|
return true;
|
2012-04-20 14:20:25 +02:00
|
|
|
return (replicate != 0);
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
int ConfigRpcComponent::FetchObjectsHandler(const NewRequestEventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
Endpoint::Ptr client = ea.Sender;
|
2012-04-02 20:50:35 +02:00
|
|
|
ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive();
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-04 10:04:38 +02:00
|
|
|
for (ConfigHive::CollectionIterator ci = configHive->Collections.begin(); ci != configHive->Collections.end(); ci++) {
|
|
|
|
ConfigCollection::Ptr collection = ci->second;
|
|
|
|
|
|
|
|
for (ConfigCollection::ObjectIterator oi = collection->Objects.begin(); oi != collection->Objects.end(); oi++) {
|
2012-04-20 14:20:25 +02:00
|
|
|
ConfigObject::Ptr object = oi->second;
|
|
|
|
|
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
continue;
|
|
|
|
|
2012-04-23 16:49:02 +02:00
|
|
|
JsonRpcRequest request = MakeObjectMessage(object, "config::ObjectCreated", true);
|
|
|
|
|
|
|
|
EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager();
|
|
|
|
endpointManager->SendUnicastRequest(m_ConfigRpcEndpoint, client, request);
|
2012-03-31 16:28:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
int ConfigRpcComponent::LocalObjectCreatedHandler(const EventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return 0;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
|
|
|
|
mgr->SendMulticastRequest(m_ConfigRpcEndpoint, MakeObjectMessage(object, "config::ObjectCreated", true));
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-20 13:49:04 +02:00
|
|
|
int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return 0;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
|
|
|
|
mgr->SendMulticastRequest(m_ConfigRpcEndpoint, MakeObjectMessage(object, "config::ObjectRemoved", false));
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-22 16:45:31 +02:00
|
|
|
int ConfigRpcComponent::LocalPropertyChangedHandler(const PropertyChangedEventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (!ShouldReplicateObject(object))
|
|
|
|
return 0;
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
JsonRpcRequest msg = MakeObjectMessage(object, "config::PropertyChanged", false);
|
|
|
|
Message params;
|
|
|
|
msg.SetParams(params);
|
2012-04-04 14:30:11 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
Message properties;
|
|
|
|
params.GetDictionary()->SetPropertyDictionary("properties", properties.GetDictionary());
|
2012-04-04 14:30:11 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
string value;
|
|
|
|
if (!object->GetPropertyString(ea.Property, &value))
|
|
|
|
return 0;
|
2012-04-04 14:30:11 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
properties.GetDictionary()->SetPropertyString(ea.Property, value);
|
2012-04-04 12:22:46 +02:00
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
|
|
|
|
mgr->SendMulticastRequest(m_ConfigRpcEndpoint, msg);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
int ConfigRpcComponent::RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest message = ea.Request;
|
2012-04-04 14:30:11 +02:00
|
|
|
bool was_null = false;
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Message params;
|
|
|
|
if (!message.GetParams(¶ms))
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
string name;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyString("name", &name))
|
2012-04-18 15:22:25 +02:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
string type;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyString("type", &type))
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive();
|
|
|
|
ConfigObject::Ptr object = configHive->GetObject(type, name);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-04 14:30:11 +02:00
|
|
|
if (!object) {
|
|
|
|
was_null = true;
|
|
|
|
object = make_shared<ConfigObject>(type, name);
|
|
|
|
}
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Dictionary::Ptr properties;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyDictionary("properties", &properties))
|
2012-04-18 15:22:25 +02:00
|
|
|
return 0;
|
2012-04-04 14:30:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
for (DictionaryIterator i = properties->Begin(); i != properties->End(); i++) {
|
2012-04-20 13:49:04 +02:00
|
|
|
object->SetPropertyString(i->first, i->second);
|
2012-04-04 14:30:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (was_null) {
|
|
|
|
object->SetReplicated(true);
|
2012-04-04 14:30:11 +02:00
|
|
|
configHive->AddObject(object);
|
2012-04-20 14:20:25 +02:00
|
|
|
}
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea)
|
2012-03-31 16:28:11 +02:00
|
|
|
{
|
2012-04-18 15:22:25 +02:00
|
|
|
JsonRpcRequest message = ea.Request;
|
2012-04-04 14:30:11 +02:00
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
Message params;
|
|
|
|
if (!message.GetParams(¶ms))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
string name;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyString("name", &name))
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-18 15:22:25 +02:00
|
|
|
string type;
|
2012-04-20 13:49:04 +02:00
|
|
|
if (!params.GetDictionary()->GetPropertyString("type", &type))
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-02 20:50:35 +02:00
|
|
|
ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive();
|
|
|
|
ConfigObject::Ptr object = configHive->GetObject(type, name);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
2012-04-19 12:16:52 +02:00
|
|
|
if (!object)
|
2012-03-31 16:28:11 +02:00
|
|
|
return 0;
|
|
|
|
|
2012-04-20 14:20:25 +02:00
|
|
|
if (object->GetReplicated())
|
|
|
|
configHive->RemoveObject(object);
|
2012-03-31 16:28:11 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_COMPONENT(ConfigRpcComponent);
|