icinga2/components/configrpc/configrpccomponent.cpp

238 lines
6.7 KiB
C++
Raw Normal View History

2012-04-13 13:16:54 +02:00
#include "i2-configrpc.h"
2012-03-31 16:28:11 +02:00
using namespace icinga;
string ConfigRpcComponent::GetName(void) const
2012-03-31 16:28:11 +02:00
{
return "configcomponent";
}
void ConfigRpcComponent::Start(void)
{
EndpointManager::Ptr endpointManager = GetEndpointManager();
ConfigHive::Ptr configHive = GetConfigHive();
2012-03-31 16:28:11 +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) {
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());
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectCreated");
m_ConfigRpcEndpoint->RegisterMethodSource("config::ObjectRemoved");
m_ConfigRpcEndpoint->RegisterMethodSource("config::PropertyChanged");
2012-03-31 16:28:11 +02:00
}
m_ConfigRpcEndpoint->RegisterMethodHandler("auth::Welcome", bind_weak(&ConfigRpcComponent::WelcomeMessageHandler, shared_from_this()));
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);
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
}
int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea)
{
if (ea.Endpoint->HasIdentity()) {
JsonRpcRequest request;
request.SetMethod("config::FetchObjects");
2012-04-23 16:49:02 +02:00
GetEndpointManager()->SendUnicastRequest(m_ConfigRpcEndpoint, ea.Endpoint, request);
}
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;
}
bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object)
{
long replicate;
if (!object->GetPropertyInteger("replicate", &replicate))
2012-04-20 15:49:12 +02:00
return true;
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;
ConfigHive::Ptr configHive = GetConfigHive();
2012-03-31 16:28:11 +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++) {
ConfigObject::Ptr object = oi->second;
if (!ShouldReplicateObject(object))
continue;
2012-04-23 16:49:02 +02:00
JsonRpcRequest request = MakeObjectMessage(object, "config::ObjectCreated", true);
GetEndpointManager()->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);
if (!ShouldReplicateObject(object))
return 0;
GetEndpointManager()->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);
if (!ShouldReplicateObject(object))
return 0;
GetEndpointManager()->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);
if (!ShouldReplicateObject(object))
return 0;
JsonRpcRequest msg = MakeObjectMessage(object, "config::PropertyChanged", false);
Message params;
msg.SetParams(params);
Message properties;
params.GetDictionary()->SetPropertyDictionary("properties", properties.GetDictionary());
string value;
if (!object->GetPropertyString(ea.Property, &value))
return 0;
properties.GetDictionary()->SetPropertyString(ea.Property, value);
GetEndpointManager()->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;
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(&params))
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;
ConfigHive::Ptr configHive = GetConfigHive();
ConfigObject::Ptr object = configHive->GetObject(type, name);
2012-03-31 16:28: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-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);
}
if (was_null) {
object->SetReplicated(true);
configHive->AddObject(object);
}
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-18 15:22:25 +02:00
Message params;
if (!message.GetParams(&params))
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;
ConfigHive::Ptr configHive = GetConfigHive();
ConfigObject::Ptr object = configHive->GetObject(type, name);
2012-03-31 16:28:11 +02:00
if (!object)
2012-03-31 16:28:11 +02:00
return 0;
if (object->GetReplicated())
configHive->RemoveObject(object);
2012-03-31 16:28:11 +02:00
return 0;
}
EXPORT_COMPONENT(ConfigRpcComponent);