Implemented JSON-RPC config component.

This commit is contained in:
Gunnar Beutner 2012-03-31 16:02:59 +02:00
parent 3158767ac6
commit 0a435bf891
4 changed files with 304 additions and 0 deletions

View File

@ -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<IcingaApplication>(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<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 ConfigComponent::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 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);

View File

@ -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 */

View File

@ -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>
<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)\..\config;$(ProjectDir)\..\icinga;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(ProjectDir)\..\base;$(ProjectDir)\..\jsonrpc;$(ProjectDir)\..\config;$(ProjectDir)\..\componentloader;$(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;config.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;config.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="configcomponent.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="configcomponent.h" />
<ClInclude Include="i2-configcomponent.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -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 "configcomponent.h"
#endif /* I2_I2CONFIGCOMPONENT_H */