]> granicus.if.org Git - icinga2/commitdiff
Renamed configcomponent module.
authorGunnar Beutner <gunnar.beutner@netways.de>
Sat, 31 Mar 2012 14:28:11 +0000 (16:28 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sat, 31 Mar 2012 14:28:11 +0000 (16:28 +0200)
configrpccomponent/configrpccomponent.cpp [new file with mode: 0644]
configrpccomponent/configrpccomponent.h [new file with mode: 0644]
configrpccomponent/configrpccomponent.vcxproj [new file with mode: 0644]
configrpccomponent/i2-configrpccomponent.h [new file with mode: 0644]

diff --git a/configrpccomponent/configrpccomponent.cpp b/configrpccomponent/configrpccomponent.cpp
new file mode 100644 (file)
index 0000000..ce74169
--- /dev/null
@@ -0,0 +1,170 @@
+#include "i2-configrpccomponent.h"
+
+using namespace icinga;
+using std::dynamic_pointer_cast;
+
+IcingaApplication::RefType ConfigRpcComponent::GetIcingaApplication(void)
+{
+       return dynamic_pointer_cast<IcingaApplication>(GetApplication());
+}
+
+string ConfigRpcComponent::GetName(void)
+{
+       return "configcomponent";
+}
+
+void ConfigRpcComponent::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(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this()));
+
+               configHive->OnObjectCreated.bind(bind_weak(&ConfigRpcComponent::LocalObjectCreatedHandler, shared_from_this()));
+               configHive->OnObjectRemoved.bind(bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this()));
+               configHive->OnPropertyChanged.bind(bind_weak(&ConfigRpcComponent::LocalPropertyChangedHandler, shared_from_this()));
+       }
+
+       connectionManager->RegisterMethod("config::ObjectCreated", bind_weak(&ConfigRpcComponent::RemoteObjectCreatedHandler, shared_from_this()));
+       connectionManager->RegisterMethod("config::ObjectRemoved", bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this()));
+       connectionManager->RegisterMethod("config::PropertyChanged", bind_weak(&ConfigRpcComponent::RemotePropertyChangedHandler, shared_from_this()));
+}
+
+void ConfigRpcComponent::Stop(void)
+{
+       // TODO: implement
+}
+
+JsonRpcMessage::RefType ConfigRpcComponent::MakeObjectMessage(const ConfigObject::RefType& object, string method, bool includeProperties)
+{
+       JsonRpcMessage::RefType msg = new_object<JsonRpcMessage>();
+       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 ConfigRpcComponent::FetchObjectsHandler(NewMessageEventArgs::RefType ea)
+{
+       JsonRpcClient::RefType client = static_pointer_cast<JsonRpcClient>(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 ConfigRpcComponent::LocalObjectCreatedHandler(ConfigHiveEventArgs::RefType ea)
+{
+       ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager();
+       connectionManager->SendMessage(MakeObjectMessage(ea->ConfigObject, "config::ObjectCreated", true));
+
+       return 0;
+}
+
+int ConfigRpcComponent::LocalObjectRemovedHandler(ConfigHiveEventArgs::RefType ea)
+{
+       ConnectionManager::RefType connectionManager = GetIcingaApplication()->GetConnectionManager();
+       connectionManager->SendMessage(MakeObjectMessage(ea->ConfigObject, "config::ObjectRemoved", false));
+
+       return 0;
+}
+
+int ConfigRpcComponent::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 ConfigRpcComponent::RemoteObjectCreatedHandler(NewMessageEventArgs::RefType ea)
+{
+       JsonRpcMessage::RefType message = ea->Message;
+
+       // TODO: update hive
+       return 0;
+}
+
+int ConfigRpcComponent::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 ConfigRpcComponent::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(ConfigRpcComponent);
diff --git a/configrpccomponent/configrpccomponent.h b/configrpccomponent/configrpccomponent.h
new file mode 100644 (file)
index 0000000..ee0905e
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef I2_CONFIGRPCCOMPONENT_H
+#define I2_CONFIGRPCCOMPONENT_H
+
+namespace icinga
+{
+
+class ConfigRpcComponent : 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_CONFIGRPCCOMPONENT_H */
diff --git a/configrpccomponent/configrpccomponent.vcxproj b/configrpccomponent/configrpccomponent.vcxproj
new file mode 100644 (file)
index 0000000..61feb47
--- /dev/null
@@ -0,0 +1,92 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <ItemGroup Label="ProjectConfigurations">
+    <ProjectConfiguration Include="Debug|Win32">
+      <Configuration>Debug</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+    <ProjectConfiguration Include="Release|Win32">
+      <Configuration>Release</Configuration>
+      <Platform>Win32</Platform>
+    </ProjectConfiguration>
+  </ItemGroup>
+  <ItemGroup>
+    <ClCompile Include="configrpccomponent.cpp" />
+  </ItemGroup>
+  <ItemGroup>
+    <ClInclude Include="configrpccomponent.h" />
+    <ClInclude Include="i2-configrpccomponent.h" />
+  </ItemGroup>
+  <PropertyGroup Label="Globals">
+    <ProjectGuid>{697C6D7E-3109-484C-A7AF-384D28711610}</ProjectGuid>
+    <Keyword>Win32Proj</Keyword>
+    <RootNamespace>icinga</RootNamespace>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
+    <ConfigurationType>DynamicLibrary</ConfigurationType>
+    <UseDebugLibraries>true</UseDebugLibraries>
+    <CharacterSet>MultiByte</CharacterSet>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
+    <ConfigurationType>DynamicLibrary</ConfigurationType>
+    <UseDebugLibraries>false</UseDebugLibraries>
+    <WholeProgramOptimization>true</WholeProgramOptimization>
+    <CharacterSet>MultiByte</CharacterSet>
+  </PropertyGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
+  <ImportGroup Label="ExtensionSettings">
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
+  </ImportGroup>
+  <PropertyGroup Label="UserMacros" />
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <LinkIncremental>true</LinkIncremental>
+    <IncludePath>$(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\icinga;$(IncludePath)</IncludePath>
+    <LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <LinkIncremental>false</LinkIncremental>
+    <IncludePath>$(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\icinga;$(IncludePath)</IncludePath>
+    <LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
+  </PropertyGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
+    <ClCompile>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <WarningLevel>Level3</WarningLevel>
+      <Optimization>Disabled</Optimization>
+      <PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+    </ClCompile>
+    <Link>
+      <SubSystem>Windows</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <AdditionalDependencies>base.lib;jsonrpc.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
+    <ClCompile>
+      <WarningLevel>Level3</WarningLevel>
+      <PrecompiledHeader>
+      </PrecompiledHeader>
+      <Optimization>MaxSpeed</Optimization>
+      <FunctionLevelLinking>true</FunctionLevelLinking>
+      <IntrinsicFunctions>true</IntrinsicFunctions>
+      <PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;_USRDLL;CONFIGCOMPONENT_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
+    </ClCompile>
+    <Link>
+      <SubSystem>Windows</SubSystem>
+      <GenerateDebugInformation>true</GenerateDebugInformation>
+      <EnableCOMDATFolding>true</EnableCOMDATFolding>
+      <OptimizeReferences>true</OptimizeReferences>
+      <AdditionalDependencies>base.lib;jsonrpc.lib;%(AdditionalDependencies)</AdditionalDependencies>
+    </Link>
+  </ItemDefinitionGroup>
+  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
+  <ImportGroup Label="ExtensionTargets">
+  </ImportGroup>
+</Project>
\ No newline at end of file
diff --git a/configrpccomponent/i2-configrpccomponent.h b/configrpccomponent/i2-configrpccomponent.h
new file mode 100644 (file)
index 0000000..514ea31
--- /dev/null
@@ -0,0 +1,10 @@
+#ifndef I2_I2CONFIGCOMPONENT_H
+#define I2_I2CONFIGCOMPONENT_H
+
+#include <i2-base.h>
+#include <i2-jsonrpc.h>
+#include <i2-icinga.h>
+
+#include "configrpccomponent.h"
+
+#endif /* I2_I2CONFIGCOMPONENT_H */