From d3798c4bf47b697e4e42b23eb8ecc59bf37a20d8 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 23 Apr 2012 13:45:41 +0200 Subject: [PATCH] Implemented demo component. --- components/Makefile.am | 3 +- components/demo/Makefile.am | 20 +++++++ components/demo/demo.vcxproj | 92 ++++++++++++++++++++++++++++++ components/demo/democomponent.cpp | 69 ++++++++++++++++++++++ components/demo/democomponent.h | 27 +++++++++ components/demo/i2-demo.h | 10 ++++ configure.ac | 1 + icinga-app/icinga.conf | 4 +- icinga.sln | 9 +++ icinga/authenticationcomponent.cpp | 3 + icinga/endpoint.h | 9 +++ icinga/endpointmanager.cpp | 2 +- icinga/icingaapplication.cpp | 23 -------- icinga/icingaapplication.h | 2 - icinga/jsonrpcendpoint.cpp | 47 +++++++++++++++ icinga/jsonrpcendpoint.h | 13 +++++ icinga/subscriptioncomponent.cpp | 3 + icinga/virtualendpoint.cpp | 37 ++++++++++++ icinga/virtualendpoint.h | 9 +++ 19 files changed, 354 insertions(+), 29 deletions(-) create mode 100644 components/demo/Makefile.am create mode 100644 components/demo/demo.vcxproj create mode 100644 components/demo/democomponent.cpp create mode 100644 components/demo/democomponent.h create mode 100644 components/demo/i2-demo.h diff --git a/components/Makefile.am b/components/Makefile.am index e32fb4b85..da8b0020f 100644 --- a/components/Makefile.am +++ b/components/Makefile.am @@ -2,4 +2,5 @@ ## Created by Anjuta SUBDIRS = configfile \ - configrpc + configrpc \ + demo diff --git a/components/demo/Makefile.am b/components/demo/Makefile.am new file mode 100644 index 000000000..d49b52c82 --- /dev/null +++ b/components/demo/Makefile.am @@ -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 diff --git a/components/demo/demo.vcxproj b/components/demo/demo.vcxproj new file mode 100644 index 000000000..5a96b64ed --- /dev/null +++ b/components/demo/demo.vcxproj @@ -0,0 +1,92 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + + {2E6C1133-730F-4875-A72C-B455B1DD4C5C} + Win32Proj + demo + + + + DynamicLibrary + true + Unicode + + + DynamicLibrary + false + true + Unicode + + + + + + + + + + + + + true + $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(IncludePath) + $(OutDir);$(LibraryPath) + + + false + $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(IncludePath) + $(OutDir);$(LibraryPath) + + + + + + Level3 + Disabled + WIN32;_DEBUG;_WINDOWS;_USRDLL;DEMO_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies) + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_WINDOWS;_USRDLL;DEMO_EXPORTS;%(PreprocessorDefinitions) + + + Windows + true + true + true + base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies) + + + + + + + + + + + + + \ No newline at end of file diff --git a/components/demo/democomponent.cpp b/components/demo/democomponent.cpp new file mode 100644 index 000000000..7d326c6f5 --- /dev/null +++ b/components/demo/democomponent.cpp @@ -0,0 +1,69 @@ +#include "i2-demo.h" + +using namespace icinga; + +IcingaApplication::Ptr DemoComponent::GetIcingaApplication(void) +{ + return static_pointer_cast(GetApplication()); +} + +string DemoComponent::GetName(void) const +{ + return "democomponent"; +} + +void DemoComponent::Start(void) +{ + m_DemoEndpoint = make_shared(); + 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(); + 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); diff --git a/components/demo/democomponent.h b/components/demo/democomponent.h new file mode 100644 index 000000000..3faaa6a37 --- /dev/null +++ b/components/demo/democomponent.h @@ -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 */ diff --git a/components/demo/i2-demo.h b/components/demo/i2-demo.h new file mode 100644 index 000000000..f42701779 --- /dev/null +++ b/components/demo/i2-demo.h @@ -0,0 +1,10 @@ +#ifndef I2DEMO_H +#define I2DEMO_H + +#include +#include +#include + +#include "democomponent.h" + +#endif /* I2DEMO_H */ diff --git a/configure.ac b/configure.ac index 70e549fa8..24849cde6 100644 --- a/configure.ac +++ b/configure.ac @@ -47,6 +47,7 @@ cJSON/Makefile components/Makefile components/configfile/Makefile components/configrpc/Makefile +components/demo/Makefile icinga/Makefile icinga-app/Makefile jsonrpc/Makefile diff --git a/icinga-app/icinga.conf b/icinga-app/icinga.conf index 050bf4df6..440c71715 100644 --- a/icinga-app/icinga.conf +++ b/icinga-app/icinga.conf @@ -1,8 +1,8 @@ { "component": { - "configrpc": { "replicate": "0", "configSource": "1" } + "configrpc": { "replicate": "0", "configSource": "1" }, + "demo": { "replicate": "0" } }, - "rpclistener": { "kekslistener": { "replicate": "0", "port": "7777" } }, diff --git a/icinga.sln b/icinga.sln index b165bd7f1..b8ac15f90 100644 --- a/icinga.sln +++ b/icinga.sln @@ -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 diff --git a/icinga/authenticationcomponent.cpp b/icinga/authenticationcomponent.cpp index 78b1c83f1..3adff23cf 100644 --- a/icinga/authenticationcomponent.cpp +++ b/icinga/authenticationcomponent.cpp @@ -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"); diff --git a/icinga/endpoint.h b/icinga/endpoint.h index 96591a2e2..317cbd956 100644 --- a/icinga/endpoint.h +++ b/icinga/endpoint.h @@ -24,6 +24,8 @@ public: typedef shared_ptr Ptr; typedef weak_ptr 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; diff --git a/icinga/endpointmanager.cpp b/icinga/endpointmanager.cpp index 3b655b3ae..1f6172eb2 100644 --- a/icinga/endpointmanager.cpp +++ b/icinga/endpointmanager.cpp @@ -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); } } diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index f31349582..228da63cb 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -67,34 +67,11 @@ int IcingaApplication::Main(const vector& args) ConfigCollection::Ptr collection = GetConfigHive()->GetCollection("rpclistener"); - m_TestEndpoint = make_shared(); - m_EndpointManager->RegisterEndpoint(m_TestEndpoint); - m_TestEndpoint->RegisterMethodSink("test"); - m_TestEndpoint->RegisterMethodSource("test"); - - m_TestTimer = make_shared(); - 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 << " " << endl; diff --git a/icinga/icingaapplication.h b/icinga/icingaapplication.h index 6d86e55d4..3205d6897 100644 --- a/icinga/icingaapplication.h +++ b/icinga/icingaapplication.h @@ -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); diff --git a/icinga/jsonrpcendpoint.cpp b/icinga/jsonrpcendpoint.cpp index c6019d920..88597372a 100644 --- a/icinga/jsonrpcendpoint.cpp +++ b/icinga/jsonrpcendpoint.cpp @@ -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(); 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; diff --git a/icinga/jsonrpcendpoint.h b/icinga/jsonrpcendpoint.h index 306ca9329..7d6b0a1b8 100644 --- a/icinga/jsonrpcendpoint.h +++ b/icinga/jsonrpcendpoint.h @@ -7,9 +7,12 @@ namespace icinga class I2_ICINGA_API JsonRpcEndpoint : public Endpoint { private: + string m_Address; JsonRpcClient::Ptr m_Client; map m_PendingCalls; Timer::Ptr m_ReconnectTimer; + set m_AllowedMethodSinkPrefixes; + set 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); diff --git a/icinga/subscriptioncomponent.cpp b/icinga/subscriptioncomponent.cpp index 78e881334..6af4bffbd 100644 --- a/icinga/subscriptioncomponent.cpp +++ b/icinga/subscriptioncomponent.cpp @@ -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; } diff --git a/icinga/virtualendpoint.cpp b/icinga/virtualendpoint.cpp index f1a203d27..b22f2420e 100644 --- a/icinga/virtualendpoint.cpp +++ b/icinga/virtualendpoint.cpp @@ -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; +} diff --git a/icinga/virtualendpoint.h b/icinga/virtualendpoint.h index ecf420225..e363dd025 100644 --- a/icinga/virtualendpoint.h +++ b/icinga/virtualendpoint.h @@ -25,6 +25,15 @@ public: void RegisterMethodHandler(string method, function callback); void UnregisterMethodHandler(string method, function 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); -- 2.40.0