Implemented demo component.

This commit is contained in:
Gunnar Beutner 2012-04-23 13:45:41 +02:00
parent ce6272d306
commit d3798c4bf4
19 changed files with 354 additions and 29 deletions

View File

@ -2,4 +2,5 @@
## Created by Anjuta
SUBDIRS = configfile \
configrpc
configrpc \
demo

View File

@ -0,0 +1,20 @@
## Process this file with automake to produce Makefile.in
pkglib_LTLIBRARIES = \
demo.la
demo_la_SOURCES = \
democomponent.cpp \
democomponent.h \
i2-demo.h
demo_la_CXXFLAGS = -I${top_srcdir}/base \
-I${top_srcdir}/jsonrpc \
-I${top_srcdir}/cJSON \
-I${top_srcdir}/icinga
demo_la_LDFLAGS = -module -version-info 0:0:0 -no-undefined -pthread
demo_la_LIBADD = ${top_builddir}/base/libbase.la \
${top_builddir}/jsonrpc/libjsonrpc.la \
${top_builddir}/cJSON/libcJSON.la \
${top_builddir}/icinga/libicinga.la

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>{2E6C1133-730F-4875-A72C-B455B1DD4C5C}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>demo</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</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>$(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(IncludePath)</IncludePath>
<LibraryPath>$(OutDir);$(LibraryPath)</LibraryPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<IncludePath>$(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\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;DEMO_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies>base.lib;jsonrpc.lib;icinga.lib;cJSON.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;DEMO_EXPORTS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalDependencies>base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies)</AdditionalDependencies>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="democomponent.h" />
<ClInclude Include="i2-demo.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="democomponent.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>

View File

@ -0,0 +1,69 @@
#include "i2-demo.h"
using namespace icinga;
IcingaApplication::Ptr DemoComponent::GetIcingaApplication(void)
{
return static_pointer_cast<IcingaApplication>(GetApplication());
}
string DemoComponent::GetName(void) const
{
return "democomponent";
}
void DemoComponent::Start(void)
{
m_DemoEndpoint = make_shared<VirtualEndpoint>();
m_DemoEndpoint->RegisterMethodSink("demo::HelloWorld");
m_DemoEndpoint->RegisterMethodSource("demo::HelloWorld");
EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager();
endpointManager->RegisterEndpoint(m_DemoEndpoint);
endpointManager->OnNewEndpoint += bind_weak(&DemoComponent::NewEndpointHandler, shared_from_this());
endpointManager->ForeachEndpoint(bind(&DemoComponent::NewEndpointHandler, this, _1));
m_DemoTimer = make_shared<Timer>();
m_DemoTimer->SetInterval(1);
m_DemoTimer->OnTimerExpired += bind_weak(&DemoComponent::DemoTimerHandler, shared_from_this());
m_DemoTimer->Start();
}
void DemoComponent::Stop(void)
{
EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager();
endpointManager->UnregisterEndpoint(m_DemoEndpoint);
}
int AuthenticationComponent::NewEndpointHandler(const NewEndpointEventArgs& neea)
{
neea.Endpoint->AddAllowedMethodSinkPrefix("demo");
neea.Endpoint->AddAllowedMethodSourcePrefix("demo");
return 0;
}
int DemoComponent::DemoTimerHandler(const TimerEventArgs& tea)
{
cout << "Sending multicast 'hello world' message." << endl;
JsonRpcRequest request;
request.SetMethod("test");
EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager();
for (int i = 0; i < 5; i++)
endpointManager->SendMulticastRequest(m_DemoEndpoint, request);
return 0;
}
int DemoComponent::HelloWorldRequestHAndler(const NewRequestEventArgs& nrea)
{
cout << "Got Hello World from " << nrea.Sender->GetAddress();
return 0;
}
EXPORT_COMPONENT(DemoComponent);

View File

@ -0,0 +1,27 @@
#ifndef DEMOCOMPONENT_H
#define DEMOCOMPONENT_H
namespace icinga
{
class DemoComponent : public Component
{
private:
Timer::Ptr m_DemoTimer;
VirtualEndpoint::Ptr m_DemoEndpoint;
IcingaApplication::Ptr GetIcingaApplication(void);
int DemoTimerHandler(const TimerEventArgs& tea);
int NewEndpointHandler(const NewEndpointEventArgs& neea);
int HelloWorldRequestHAndler(const NewRequestEventArgs& nrea);
public:
virtual string GetName(void) const;
virtual void Start(void);
virtual void Stop(void);
};
}
#endif /* DEMOCOMPONENT_H */

10
components/demo/i2-demo.h Normal file
View File

@ -0,0 +1,10 @@
#ifndef I2DEMO_H
#define I2DEMO_H
#include <i2-base.h>
#include <i2-jsonrpc.h>
#include <i2-icinga.h>
#include "democomponent.h"
#endif /* I2DEMO_H */

View File

@ -47,6 +47,7 @@ cJSON/Makefile
components/Makefile
components/configfile/Makefile
components/configrpc/Makefile
components/demo/Makefile
icinga/Makefile
icinga-app/Makefile
jsonrpc/Makefile

View File

@ -1,8 +1,8 @@
{
"component": {
"configrpc": { "replicate": "0", "configSource": "1" }
"configrpc": { "replicate": "0", "configSource": "1" },
"demo": { "replicate": "0" }
},
"rpclistener": {
"kekslistener": { "replicate": "0", "port": "7777" }
},

View File

@ -32,6 +32,11 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "configrpc", "components\con
{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8} = {C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "demo", "components\demo\demo.vcxproj", "{2E6C1133-730F-4875-A72C-B455B1DD4C5C}"
ProjectSection(ProjectDependencies) = postProject
{C1FC77E1-04A4-481B-A78B-2F7AF489C2F8} = {C1FC77E1-04A4-481B-A78B-2F7AF489C2F8}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
@ -66,6 +71,10 @@ Global
{697C6D7E-3109-484C-A7AF-384D28711610}.Debug|Win32.Build.0 = Debug|Win32
{697C6D7E-3109-484C-A7AF-384D28711610}.Release|Win32.ActiveCfg = Release|Win32
{697C6D7E-3109-484C-A7AF-384D28711610}.Release|Win32.Build.0 = Release|Win32
{2E6C1133-730F-4875-A72C-B455B1DD4C5C}.Debug|Win32.ActiveCfg = Debug|Win32
{2E6C1133-730F-4875-A72C-B455B1DD4C5C}.Debug|Win32.Build.0 = Debug|Win32
{2E6C1133-730F-4875-A72C-B455B1DD4C5C}.Release|Win32.ActiveCfg = Release|Win32
{2E6C1133-730F-4875-A72C-B455B1DD4C5C}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -38,6 +38,9 @@ int AuthenticationComponent::NewEndpointHandler(const NewEndpointEventArgs& neea
if (neea.Endpoint->IsLocal() || neea.Endpoint->HasIdentity())
return 0;
neea.Endpoint->AddAllowedMethodSinkPrefix("auth::");
neea.Endpoint->AddAllowedMethodSourcePrefix("auth::");
JsonRpcRequest request;
request.SetMethod("message::SetIdentity");

View File

@ -24,6 +24,8 @@ public:
typedef shared_ptr<Endpoint> Ptr;
typedef weak_ptr<Endpoint> WeakPtr;
virtual string GetAddress(void) const = 0;
string GetIdentity(void) const;
void SetIdentity(string identity);
bool HasIdentity(void) const;
@ -38,6 +40,13 @@ public:
void RegisterMethodSource(string method);
void UnregisterMethodSource(string method);
bool IsMethodSource(string method) const;
virtual void AddAllowedMethodSinkPrefix(string method) = 0;
virtual void RemoveAllowedMethodSinkPrefix(string method) = 0;
virtual bool IsAllowedMethodSink(string method) const = 0;
virtual void AddAllowedMethodSourcePrefix(string method) = 0;
virtual void RemoveAllowedMethodSourcePrefix(string method) = 0;
virtual bool IsAllowedMethodSource(string method) const = 0;
virtual bool IsLocal(void) const = 0;

View File

@ -101,7 +101,7 @@ void EndpointManager::SendMulticastRequest(Endpoint::Ptr sender, const JsonRpcRe
if (!fromLocal && !endpoint->IsLocal())
continue;
if (endpoint->IsMethodSink(method))
if (endpoint->IsMethodSink(method) && endpoint->IsAllowedMethodSink(method))
endpoint->ProcessRequest(sender, request);
}
}

View File

@ -67,34 +67,11 @@ int IcingaApplication::Main(const vector<string>& args)
ConfigCollection::Ptr collection = GetConfigHive()->GetCollection("rpclistener");
m_TestEndpoint = make_shared<VirtualEndpoint>();
m_EndpointManager->RegisterEndpoint(m_TestEndpoint);
m_TestEndpoint->RegisterMethodSink("test");
m_TestEndpoint->RegisterMethodSource("test");
m_TestTimer = make_shared<Timer>();
m_TestTimer->SetInterval(1);
m_TestTimer->OnTimerExpired += bind_weak(&IcingaApplication::TestTimerHandler, shared_from_this());
m_TestTimer->Start();
RunEventLoop();
return EXIT_SUCCESS;
}
int IcingaApplication::TestTimerHandler(const TimerEventArgs& tea)
{
cout << "Problem?" << endl;
JsonRpcRequest request;
request.SetMethod("test");
for (int i = 0; i < 5; i++)
m_EndpointManager->SendMulticastRequest(m_TestEndpoint, request);
return 0;
}
void IcingaApplication::PrintUsage(const string& programPath)
{
cout << "Syntax: " << programPath << " <config-file>" << endl;

View File

@ -8,8 +8,6 @@ class I2_ICINGA_API IcingaApplication : public Application
{
private:
EndpointManager::Ptr m_EndpointManager;
Timer::Ptr m_TestTimer;
VirtualEndpoint::Ptr m_TestEndpoint;
int NewComponentHandler(const EventArgs& ea);
int DeletedComponentHandler(const EventArgs& ea);

View File

@ -2,13 +2,57 @@
using namespace icinga;
void JsonRpcEndpoint::SetAddress(string address)
{
m_Address = address;
}
string JsonRpcEndpoint::GetAddress(void) const
{
return m_Address;
}
JsonRpcClient::Ptr JsonRpcEndpoint::GetClient(void)
{
return m_Client;
}
void JsonRpcEndpoint::AddAllowedMethodSinkPrefix(string method)
{
m_AllowedMethodSinkPrefixes.insert(method);
}
void JsonRpcEndpoint::RemoveAllowedMethodSinkPrefix(string method)
{
m_AllowedMethodSinkPrefixes.erase(method);
}
bool JsonRpcEndpoint::IsAllowedMethodSink(string method) const
{
return (m_AllowedMethodSinkPrefixes.find(method) != m_AllowedMethodSinkPrefixes.end());
}
void JsonRpcEndpoint::AddAllowedMethodSourcePrefix(string method)
{
m_AllowedMethodSourcePrefixes.insert(method);
}
void JsonRpcEndpoint::RemoveAllowedMethodSourcePrefix(string method)
{
m_AllowedMethodSourcePrefixes.erase(method);
}
bool JsonRpcEndpoint::IsAllowedMethodSource(string method) const
{
return (m_AllowedMethodSourcePrefixes.find(method) != m_AllowedMethodSourcePrefixes.end());
}
void JsonRpcEndpoint::Connect(string host, unsigned short port)
{
char portStr[20];
sprintf(portStr, "%d", port);
SetAddress("jsonrpc-tcp://" + host + ":" + portStr);
JsonRpcClient::Ptr client = make_shared<JsonRpcClient>();
client->MakeSocket();
client->Connect(host, port);
@ -62,6 +106,9 @@ int JsonRpcEndpoint::NewMessageHandler(const NewMessageEventArgs& nmea)
string method;
if (message.GetPropertyString("method", &method)) {
if (!IsAllowedMethodSource(method))
return 0;
JsonRpcRequest request = message;
string id;

View File

@ -7,9 +7,12 @@ namespace icinga
class I2_ICINGA_API JsonRpcEndpoint : public Endpoint
{
private:
string m_Address;
JsonRpcClient::Ptr m_Client;
map<string, Endpoint::Ptr> m_PendingCalls;
Timer::Ptr m_ReconnectTimer;
set<string> m_AllowedMethodSinkPrefixes;
set<string> m_AllowedMethodSourcePrefixes;
bool IsConnected(void) const;
@ -27,6 +30,16 @@ public:
JsonRpcClient::Ptr GetClient(void);
void SetClient(JsonRpcClient::Ptr client);
void SetAddress(string address);
virtual string GetAddress(void) const;
virtual void AddAllowedMethodSinkPrefix(string method);
virtual void RemoveAllowedMethodSinkPrefix(string method);
virtual bool IsAllowedMethodSink(string method) const;
virtual void AddAllowedMethodSourcePrefix(string method);
virtual void RemoveAllowedMethodSourcePrefix(string method);
virtual bool IsAllowedMethodSource(string method) const;
virtual bool IsLocal(void) const;
virtual void ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& message);

View File

@ -75,6 +75,9 @@ int SubscriptionComponent::NewEndpointHandler(const NewEndpointEventArgs& neea)
EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager();
mgr->ForeachEndpoint(bind(&SubscriptionComponent::SyncSubscriptions, this, neea.Endpoint, _1));
neea.Endpoint->AddAllowedMethodSinkPrefix("message::");
neea.Endpoint->AddAllowedMethodSourcePrefix("message::");
return 0;
}

View File

@ -2,6 +2,13 @@
using namespace icinga;
string VirtualEndpoint::GetAddress(void) const
{
char address[50];
sprintf(address, "virtual:%p", this);
return address;
}
bool VirtualEndpoint::IsLocal(void) const
{
return true;
@ -46,3 +53,33 @@ void VirtualEndpoint::ProcessResponse(Endpoint::Ptr sender, const JsonRpcRespons
// TODO: figure out which request this response belongs to and notify the caller
throw NotImplementedException();
}
void VirtualEndpoint::AddAllowedMethodSinkPrefix(string method)
{
/* Nothing to do here. */
}
void VirtualEndpoint::RemoveAllowedMethodSinkPrefix(string method)
{
/* Nothing to do here. */
}
void VirtualEndpoint::AddAllowedMethodSourcePrefix(string method)
{
/* Nothing to do here. */
}
void VirtualEndpoint::RemoveAllowedMethodSourcePrefix(string method)
{
/* Nothing to do here. */
}
bool VirtualEndpoint::IsAllowedMethodSink(string method) const
{
return true;
}
bool VirtualEndpoint::IsAllowedMethodSource(string method) const
{
return true;
}

View File

@ -25,6 +25,15 @@ public:
void RegisterMethodHandler(string method, function<int (const NewRequestEventArgs&)> callback);
void UnregisterMethodHandler(string method, function<int (const NewRequestEventArgs&)> callback);
virtual void AddAllowedMethodSinkPrefix(string method);
virtual void RemoveAllowedMethodSinkPrefix(string method);
virtual bool IsAllowedMethodSink(string method) const;
virtual void AddAllowedMethodSourcePrefix(string method);
virtual void RemoveAllowedMethodSourcePrefix(string method);
virtual bool IsAllowedMethodSource(string method) const;
virtual string GetAddress(void) const;
virtual bool IsLocal(void) const;
virtual void ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& message);