From: Gunnar Beutner Date: Sat, 31 Mar 2012 14:26:51 +0000 (+0200) Subject: Implemented rudimentary config file parser. X-Git-Tag: v0.0.1~682 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=6e441a26e9f5136b4916ad89a1d1e9f01df68232;p=icinga2 Implemented rudimentary config file parser. --- diff --git a/configcomponent/configcomponent.cpp b/configcomponent/configcomponent.cpp deleted file mode 100644 index c5d467d7e..000000000 --- a/configcomponent/configcomponent.cpp +++ /dev/null @@ -1,170 +0,0 @@ -#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 deleted file mode 100644 index 191e942fc..000000000 --- a/configcomponent/configcomponent.h +++ /dev/null @@ -1,32 +0,0 @@ -#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/i2-configcomponent.h b/configcomponent/i2-configcomponent.h deleted file mode 100644 index 4076b52d9..000000000 --- a/configcomponent/i2-configcomponent.h +++ /dev/null @@ -1,10 +0,0 @@ -#ifndef I2_I2CONFIGCOMPONENT_H -#define I2_I2CONFIGCOMPONENT_H - -#include -#include -#include - -#include "configcomponent.h" - -#endif /* I2_I2CONFIGCOMPONENT_H */ diff --git a/configfilecomponent/configfilecomponent.cpp b/configfilecomponent/configfilecomponent.cpp new file mode 100644 index 000000000..dbdea10f5 --- /dev/null +++ b/configfilecomponent/configfilecomponent.cpp @@ -0,0 +1,75 @@ +#include "i2-configfilecomponent.h" +#include +#include +#include + +using namespace icinga; +using std::ifstream; + +string ConfigFileComponent::GetName(void) +{ + return "configfilecomponent"; +} + +void ConfigFileComponent::Start(void) +{ + ifstream fp; + FIFO::RefType fifo = new_object(); + + fp.open(GetConfig()->GetProperty("filename"), ifstream::in); + if (fp.fail()) + throw exception(/*"Could not open config file"*/); + + while (!fp.eof()) { + size_t bufferSize = 1024; + char *buffer = (char *)fifo->GetWriteBuffer(&bufferSize); + fp.read(buffer, bufferSize); + if (fp.bad()) + throw exception(/*"Could not read from config file"*/); + fifo->Write(NULL, fp.gcount()); + } + + fp.close(); + + fifo->Write("\0", 1); + + /* TODO: implement config parsing, for now we just use JSON */ + cJSON *jsonobj = cJSON_Parse((const char *)fifo->GetReadBuffer()); + fifo->Read(NULL, fifo->GetSize()); + + if (jsonobj == NULL) + throw exception(/*"Could not parse config file."*/); + + for (cJSON *typeobj = jsonobj->child; typeobj != NULL; typeobj = typeobj->next) { + string type = typeobj->string; + + for (cJSON *object = typeobj->child; object != NULL; object = object->next) { + string name = object->string; + + ConfigObject::RefType cfgobj = new_object(); + cfgobj->SetName(name); + cfgobj->SetType(type); + + for (cJSON *property = object->child; property != NULL; property = property->next) { + string key = property->string; + + if (property->type != cJSON_String) + continue; + + string value = property->valuestring; + + cfgobj->SetProperty(key, value); + } + + GetApplication()->GetConfigHive()->AddObject(cfgobj); + } + } + + cJSON_Delete(jsonobj); +} + +void ConfigFileComponent::Stop(void) +{ +} + +EXPORT_COMPONENT(ConfigFileComponent); diff --git a/configfilecomponent/configfilecomponent.h b/configfilecomponent/configfilecomponent.h new file mode 100644 index 000000000..0039d8fda --- /dev/null +++ b/configfilecomponent/configfilecomponent.h @@ -0,0 +1,20 @@ +#ifndef I2_CONFIGFILECOMPONENT_H +#define I2_CONFIGFILECOMPONENT_H + +namespace icinga +{ + +class ConfigFileComponent : public Component +{ +public: + typedef shared_ptr RefType; + typedef weak_ptr WeakRefType; + + virtual string GetName(void); + virtual void Start(void); + virtual void Stop(void); +}; + +} + +#endif /* I2_CONFIGFILECOMPONENT_H */ \ No newline at end of file diff --git a/configcomponent/configcomponent.vcxproj b/configfilecomponent/configfilecomponent.vcxproj similarity index 79% rename from configcomponent/configcomponent.vcxproj rename to configfilecomponent/configfilecomponent.vcxproj index 87eb8cd9d..fa0f9ff5f 100644 --- a/configcomponent/configcomponent.vcxproj +++ b/configfilecomponent/configfilecomponent.vcxproj @@ -10,8 +10,15 @@ Win32 + + + + + + + - {697C6D7E-3109-484C-A7AF-384D28711610} + {E58F1DA7-B723-412B-B2B7-7FF58E2A944E} Win32Proj icinga @@ -38,13 +45,11 @@ - true - $(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\config;$(ProjectDir)\..\icinga;$(IncludePath) + $(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(IncludePath) $(OutDir);$(LibraryPath) - false - $(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\config;$(ProjectDir)\..\componentloader;$(IncludePath) + $(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(IncludePath) $(OutDir);$(LibraryPath) @@ -53,13 +58,17 @@ Level3 Disabled - WIN32;_DEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions) + WIN32;_DEBUG;_LIB;%(PreprocessorDefinitions) Windows true - base.lib;jsonrpc.lib;config.lib;%(AdditionalDependencies) + base.lib;jsonrpc.lib;%(AdditionalDependencies) + + + + @@ -69,23 +78,19 @@ MaxSpeed true true - WIN32;NDEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions) + WIN32;NDEBUG;_LIB;%(PreprocessorDefinitions) Windows true true true - base.lib;jsonrpc.lib;config.lib;%(AdditionalDependencies) + base.lib;jsonrpc.lib;%(AdditionalDependencies) + + $(OutDir)\base.lib;$(OutDir)\jsonrpc.lib + - - - - - - - diff --git a/configfilecomponent/i2-configfilecomponent.h b/configfilecomponent/i2-configfilecomponent.h new file mode 100644 index 000000000..64de42294 --- /dev/null +++ b/configfilecomponent/i2-configfilecomponent.h @@ -0,0 +1,8 @@ +#ifndef I2_I2CONFIGFILECOMPONENT_H +#define I2_I2CONFIGFILECOMPONENT_H + +#include + +#include "configfilecomponent.h" + +#endif /* I2_I2CONFIGFILECOMPONENT_H */