From: Gunnar Beutner Date: Sat, 31 Mar 2012 14:02:59 +0000 (+0200) Subject: Implemented JSON-RPC config component. X-Git-Tag: v0.0.1~684 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0a435bf891c7c90ea7ff6e870cf17e8cd67566d4;p=icinga2 Implemented JSON-RPC config component. --- diff --git a/configcomponent/configcomponent.cpp b/configcomponent/configcomponent.cpp new file mode 100644 index 000000000..c5d467d7e --- /dev/null +++ b/configcomponent/configcomponent.cpp @@ -0,0 +1,170 @@ +#include "i2-configcomponent.h" + +using namespace icinga; +using std::dynamic_pointer_cast; + +IcingaApplication::RefType ConfigComponent::GetIcingaApplication(void) +{ + return dynamic_pointer_cast(GetApplication()); +} + +string ConfigComponent::GetName(void) +{ + return "configcomponent"; +} + +void ConfigComponent::Start(void) +{ + IcingaApplication::RefType icingaApp = GetIcingaApplication(); + + if (icingaApp.get() == NULL) + throw exception(/*"Component loaded by incompatible application."*/); + + ConnectionManager::RefType connectionManager = icingaApp->GetConnectionManager(); + ConfigHive::RefType configHive = icingaApp->GetConfigHive(); + + if (GetConfig()->GetPropertyInteger("configSource") != 0) { + connectionManager->RegisterMethod("config::FetchObjects", bind_weak(&ConfigComponent::FetchObjectsHandler, shared_from_this())); + + configHive->OnObjectCreated.bind(bind_weak(&ConfigComponent::LocalObjectCreatedHandler, shared_from_this())); + configHive->OnObjectRemoved.bind(bind_weak(&ConfigComponent::LocalObjectRemovedHandler, shared_from_this())); + configHive->OnPropertyChanged.bind(bind_weak(&ConfigComponent::LocalPropertyChangedHandler, shared_from_this())); + } + + connectionManager->RegisterMethod("config::ObjectCreated", bind_weak(&ConfigComponent::RemoteObjectCreatedHandler, shared_from_this())); + connectionManager->RegisterMethod("config::ObjectRemoved", bind_weak(&ConfigComponent::RemoteObjectRemovedHandler, shared_from_this())); + connectionManager->RegisterMethod("config::PropertyChanged", bind_weak(&ConfigComponent::RemotePropertyChangedHandler, shared_from_this())); +} + +void ConfigComponent::Stop(void) +{ + // TODO: implement +} + +JsonRpcMessage::RefType ConfigComponent::MakeObjectMessage(const ConfigObject::RefType& object, string method, bool includeProperties) +{ + JsonRpcMessage::RefType msg = new_object(); + msg->SetVersion("2.0"); + msg->SetMethod(method); + cJSON *params = msg->GetParams(); + + string name = object->GetName(); + cJSON_AddStringToObject(params, "name", name.c_str()); + + string type = object->GetType(); + cJSON_AddStringToObject(params, "type", type.c_str()); + + if (includeProperties) { + for (ConfigObject::ParameterIterator pi = object->Properties.begin(); pi != object->Properties.end(); pi++) { + cJSON_AddStringToObject(params, pi->first.c_str(), pi->second.c_str()); + } + } + + return msg; +} + +int ConfigComponent::FetchObjectsHandler(NewMessageEventArgs::RefType ea) +{ + JsonRpcClient::RefType client = static_pointer_cast(ea->Source); + ConfigHive::RefType configHive = GetIcingaApplication()->GetConfigHive(); + + for (ConfigHive::TypeIterator ti = configHive->Objects.begin(); ti != configHive->Objects.end(); ti++) { + for (ConfigHive::ObjectIterator oi = ti->second.begin(); oi != ti->second.end(); oi++) { + JsonRpcMessage::RefType msg = MakeObjectMessage(oi->second, "config::ObjectCreated", true); + client->SendMessage(msg); + } + } + + return 0; +} + +int ConfigComponent::LocalObjectCreatedHandler(ConfigHiveEventArgs::RefType ea) +{ + ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager(); + connectionManager->SendMessage(MakeObjectMessage(ea->ConfigObject, "config::ObjectCreated", true)); + + return 0; +} + +int ConfigComponent::LocalObjectRemovedHandler(ConfigHiveEventArgs::RefType ea) +{ + ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager(); + connectionManager->SendMessage(MakeObjectMessage(ea->ConfigObject, "config::ObjectRemoved", false)); + + return 0; +} + +int ConfigComponent::LocalPropertyChangedHandler(ConfigHiveEventArgs::RefType ea) +{ + JsonRpcMessage::RefType msg = MakeObjectMessage(ea->ConfigObject, "config::ObjectRemoved", false); + cJSON *params = msg->GetParams(); + cJSON_AddStringToObject(params, "property", ea->Property.c_str()); + string value = ea->ConfigObject->GetProperty(ea->Property); + cJSON_AddStringToObject(params, "value", value.c_str()); + + ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager(); + connectionManager->SendMessage(msg); + + return 0; +} + +int ConfigComponent::RemoteObjectCreatedHandler(NewMessageEventArgs::RefType ea) +{ + JsonRpcMessage::RefType message = ea->Message; + + // TODO: update hive + return 0; +} + +int ConfigComponent::RemoteObjectRemovedHandler(NewMessageEventArgs::RefType ea) +{ + JsonRpcMessage::RefType message = ea->Message; + string name, type; + + if (!message->GetParamString("name", &name)) + return 0; + + if (!message->GetParamString("type", &type)) + return 0; + + ConfigHive::RefType configHive = GetIcingaApplication()->GetConfigHive(); + ConfigObject::RefType object = configHive->GetObject(type, name); + + if (object.get() == NULL) + return 0; + + configHive->RemoveObject(object); + + return 0; +} + +int ConfigComponent::RemotePropertyChangedHandler(NewMessageEventArgs::RefType ea) +{ + JsonRpcMessage::RefType message = ea->Message; + string name, type, property, value; + + if (!message->GetParamString("name", &name)) + return 0; + + if (!message->GetParamString("type", &type)) + return 0; + + if (!message->GetParamString("property", &property)) + return 0; + + if (!message->GetParamString("value", &value)) + return 0; + + ConfigHive::RefType configHive = GetIcingaApplication()->GetConfigHive(); + ConfigObject::RefType object = configHive->GetObject(type, name); + + if (object.get() == NULL) + return 0; + + object->SetProperty(property, value); + + return 0; +} + + +EXPORT_COMPONENT(ConfigComponent); diff --git a/configcomponent/configcomponent.h b/configcomponent/configcomponent.h new file mode 100644 index 000000000..191e942fc --- /dev/null +++ b/configcomponent/configcomponent.h @@ -0,0 +1,32 @@ +#ifndef I2_CONFIGCOMPONENT_H +#define I2_CONFIGCOMPONENT_H + +namespace icinga +{ + +class ConfigComponent : public Component +{ +private: + IcingaApplication::RefType GetIcingaApplication(void); + + int FetchObjectsHandler(NewMessageEventArgs::RefType ea); + + int LocalObjectCreatedHandler(ConfigHiveEventArgs::RefType ea); + int LocalObjectRemovedHandler(ConfigHiveEventArgs::RefType ea); + int LocalPropertyChangedHandler(ConfigHiveEventArgs::RefType ea); + + int RemoteObjectCreatedHandler(NewMessageEventArgs::RefType ea); + int RemoteObjectRemovedHandler(NewMessageEventArgs::RefType ea); + int RemotePropertyChangedHandler(NewMessageEventArgs::RefType ea); + + JsonRpcMessage::RefType MakeObjectMessage(const ConfigObject::RefType& object, string method, bool includeProperties); + +public: + virtual string GetName(void); + virtual void Start(void); + virtual void Stop(void); +}; + +} + +#endif /* I2_CONFIGCOMPONENT_H */ diff --git a/configcomponent/configcomponent.vcxproj b/configcomponent/configcomponent.vcxproj new file mode 100644 index 000000000..87eb8cd9d --- /dev/null +++ b/configcomponent/configcomponent.vcxproj @@ -0,0 +1,92 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {697C6D7E-3109-484C-A7AF-384D28711610} + Win32Proj + icinga + + + + DynamicLibrary + true + MultiByte + + + DynamicLibrary + false + true + MultiByte + + + + + + + + + + + + + true + $(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\config;$(ProjectDir)\..\icinga;$(IncludePath) + $(OutDir);$(LibraryPath) + + + false + $(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\config;$(ProjectDir)\..\componentloader;$(IncludePath) + $(OutDir);$(LibraryPath) + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + base.lib;jsonrpc.lib;config.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + true + true + base.lib;jsonrpc.lib;config.lib;%(AdditionalDependencies) + + + + + + + + + + + + + \ No newline at end of file diff --git a/configcomponent/i2-configcomponent.h b/configcomponent/i2-configcomponent.h new file mode 100644 index 000000000..4076b52d9 --- /dev/null +++ b/configcomponent/i2-configcomponent.h @@ -0,0 +1,10 @@ +#ifndef I2_I2CONFIGCOMPONENT_H +#define I2_I2CONFIGCOMPONENT_H + +#include +#include +#include + +#include "configcomponent.h" + +#endif /* I2_I2CONFIGCOMPONENT_H */