From 576181f304b598e27ffa8249e672cbeb05e2c3de Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 24 Apr 2012 07:16:34 +0200 Subject: [PATCH] Added helper functions for commonly used IcingaApplication methods. --- components/configrpc/configrpccomponent.cpp | 34 ++++++++------------- components/configrpc/configrpccomponent.h | 4 +-- components/demo/democomponent.cpp | 8 ++--- components/demo/democomponent.h | 2 +- icinga/Makefile.am | 2 ++ icinga/authenticationcomponent.cpp | 19 +++--------- icinga/authenticationcomponent.h | 4 +-- icinga/discoverycomponent.cpp | 17 +++-------- icinga/discoverycomponent.h | 2 +- icinga/i2-icinga.h | 1 + icinga/icinga.vcxproj | 2 ++ icinga/icingacomponent.cpp | 28 +++++++++++++++++ icinga/icingacomponent.h | 17 +++++++++++ icinga/subscriptioncomponent.cpp | 19 +++--------- icinga/subscriptioncomponent.h | 4 +-- 15 files changed, 85 insertions(+), 78 deletions(-) create mode 100644 icinga/icingacomponent.cpp create mode 100644 icinga/icingacomponent.h diff --git a/components/configrpc/configrpccomponent.cpp b/components/configrpc/configrpccomponent.cpp index 062070f23..d516e70a0 100644 --- a/components/configrpc/configrpccomponent.cpp +++ b/components/configrpc/configrpccomponent.cpp @@ -2,11 +2,6 @@ using namespace icinga; -IcingaApplication::Ptr ConfigRpcComponent::GetIcingaApplication(void) -{ - return static_pointer_cast(GetApplication()); -} - string ConfigRpcComponent::GetName(void) const { return "configcomponent"; @@ -14,10 +9,8 @@ string ConfigRpcComponent::GetName(void) const void ConfigRpcComponent::Start(void) { - IcingaApplication::Ptr icingaApp = GetIcingaApplication(); - - EndpointManager::Ptr endpointManager = icingaApp->GetEndpointManager(); - ConfigHive::Ptr configHive = icingaApp->GetConfigHive(); + EndpointManager::Ptr endpointManager = GetEndpointManager(); + ConfigHive::Ptr configHive = GetConfigHive(); m_ConfigRpcEndpoint = make_shared(); @@ -57,8 +50,7 @@ int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea) JsonRpcRequest request; request.SetMethod("config::FetchObjects"); - EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); - endpointManager->SendUnicastRequest(m_ConfigRpcEndpoint, ea.Endpoint, request); + GetEndpointManager()->SendUnicastRequest(m_ConfigRpcEndpoint, ea.Endpoint, request); } return 0; @@ -102,7 +94,7 @@ bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object) int ConfigRpcComponent::FetchObjectsHandler(const NewRequestEventArgs& ea) { Endpoint::Ptr client = ea.Sender; - ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive(); + ConfigHive::Ptr configHive = GetConfigHive(); for (ConfigHive::CollectionIterator ci = configHive->Collections.begin(); ci != configHive->Collections.end(); ci++) { ConfigCollection::Ptr collection = ci->second; @@ -115,8 +107,7 @@ int ConfigRpcComponent::FetchObjectsHandler(const NewRequestEventArgs& ea) JsonRpcRequest request = MakeObjectMessage(object, "config::ObjectCreated", true); - EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); - endpointManager->SendUnicastRequest(m_ConfigRpcEndpoint, client, request); + GetEndpointManager()->SendUnicastRequest(m_ConfigRpcEndpoint, client, request); } } @@ -130,8 +121,8 @@ int ConfigRpcComponent::LocalObjectCreatedHandler(const EventArgs& ea) if (!ShouldReplicateObject(object)) return 0; - EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); - mgr->SendMulticastRequest(m_ConfigRpcEndpoint, MakeObjectMessage(object, "config::ObjectCreated", true)); + GetEndpointManager()->SendMulticastRequest(m_ConfigRpcEndpoint, + MakeObjectMessage(object, "config::ObjectCreated", true)); return 0; } @@ -143,8 +134,8 @@ int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea) if (!ShouldReplicateObject(object)) return 0; - EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); - mgr->SendMulticastRequest(m_ConfigRpcEndpoint, MakeObjectMessage(object, "config::ObjectRemoved", false)); + GetEndpointManager()->SendMulticastRequest(m_ConfigRpcEndpoint, + MakeObjectMessage(object, "config::ObjectRemoved", false)); return 0; } @@ -169,8 +160,7 @@ int ConfigRpcComponent::LocalPropertyChangedHandler(const PropertyChangedEventAr properties.GetDictionary()->SetPropertyString(ea.Property, value); - EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); - mgr->SendMulticastRequest(m_ConfigRpcEndpoint, msg); + GetEndpointManager()->SendMulticastRequest(m_ConfigRpcEndpoint, msg); return 0; } @@ -192,7 +182,7 @@ int ConfigRpcComponent::RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea if (!params.GetDictionary()->GetPropertyString("type", &type)) return 0; - ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive(); + ConfigHive::Ptr configHive = GetConfigHive(); ConfigObject::Ptr object = configHive->GetObject(type, name); if (!object) { @@ -232,7 +222,7 @@ int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea if (!params.GetDictionary()->GetPropertyString("type", &type)) return 0; - ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive(); + ConfigHive::Ptr configHive = GetConfigHive(); ConfigObject::Ptr object = configHive->GetObject(type, name); if (!object) diff --git a/components/configrpc/configrpccomponent.h b/components/configrpc/configrpccomponent.h index 2f1085bcd..0efe8bec6 100644 --- a/components/configrpc/configrpccomponent.h +++ b/components/configrpc/configrpccomponent.h @@ -4,13 +4,11 @@ namespace icinga { -class ConfigRpcComponent : public Component +class ConfigRpcComponent : public IcingaComponent { private: VirtualEndpoint::Ptr m_ConfigRpcEndpoint; - IcingaApplication::Ptr GetIcingaApplication(void); - int NewEndpointHandler(const NewEndpointEventArgs& ea); int WelcomeMessageHandler(const NewRequestEventArgs& ea); diff --git a/components/demo/democomponent.cpp b/components/demo/democomponent.cpp index 31967a677..dd370b08d 100644 --- a/components/demo/democomponent.cpp +++ b/components/demo/democomponent.cpp @@ -16,7 +16,7 @@ void DemoComponent::Start(void) { m_DemoEndpoint = make_shared(); m_DemoEndpoint->RegisterMethodHandler("demo::HelloWorld", - bind_weak(&DemoComponent::HelloWorldRequestHAndler, shared_from_this())); + bind_weak(&DemoComponent::HelloWorldRequestHandler, shared_from_this())); m_DemoEndpoint->RegisterMethodSource("demo::HelloWorld"); EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); @@ -26,7 +26,7 @@ void DemoComponent::Start(void) endpointManager->ForeachEndpoint(bind(&DemoComponent::NewEndpointHandler, this, _1)); m_DemoTimer = make_shared(); - m_DemoTimer->SetInterval(1); + m_DemoTimer->SetInterval(5); m_DemoTimer->OnTimerExpired += bind_weak(&DemoComponent::DemoTimerHandler, shared_from_this()); m_DemoTimer->Start(); } @@ -63,9 +63,9 @@ int DemoComponent::DemoTimerHandler(const TimerEventArgs& tea) return 0; } -int DemoComponent::HelloWorldRequestHAndler(const NewRequestEventArgs& nrea) +int DemoComponent::HelloWorldRequestHandler(const NewRequestEventArgs& nrea) { - cout << "Got Hello World from " << nrea.Sender->GetAddress() << endl; + cout << "Got 'hello world' from " << nrea.Sender->GetAddress() << endl; return 0; } diff --git a/components/demo/democomponent.h b/components/demo/democomponent.h index 3faaa6a37..51d88336a 100644 --- a/components/demo/democomponent.h +++ b/components/demo/democomponent.h @@ -14,7 +14,7 @@ private: int DemoTimerHandler(const TimerEventArgs& tea); int NewEndpointHandler(const NewEndpointEventArgs& neea); - int HelloWorldRequestHAndler(const NewRequestEventArgs& nrea); + int HelloWorldRequestHandler(const NewRequestEventArgs& nrea); public: virtual string GetName(void) const; diff --git a/icinga/Makefile.am b/icinga/Makefile.am index b09174c9d..73acd1bea 100644 --- a/icinga/Makefile.am +++ b/icinga/Makefile.am @@ -15,6 +15,8 @@ libicinga_la_SOURCES = \ endpointmanager.h \ icingaapplication.cpp \ icingaapplication.h \ + icingacomponent.cpp \ + icingacomponent.h \ identitymessage.cpp \ identitymessage.h \ i2-icinga.h \ diff --git a/icinga/authenticationcomponent.cpp b/icinga/authenticationcomponent.cpp index a7c388877..14520036e 100644 --- a/icinga/authenticationcomponent.cpp +++ b/icinga/authenticationcomponent.cpp @@ -2,11 +2,6 @@ using namespace icinga; -IcingaApplication::Ptr AuthenticationComponent::GetIcingaApplication(void) const -{ - return static_pointer_cast(GetApplication()); -} - string AuthenticationComponent::GetName(void) const { return "authenticationcomponent"; @@ -18,7 +13,7 @@ void AuthenticationComponent::Start(void) m_AuthenticationEndpoint->RegisterMethodHandler("auth::SetIdentity", bind_weak(&AuthenticationComponent::IdentityMessageHandler, shared_from_this())); m_AuthenticationEndpoint->RegisterMethodSource("auth::Welcome"); - EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); + EndpointManager::Ptr mgr = GetEndpointManager(); mgr->OnNewEndpoint += bind_weak(&AuthenticationComponent::NewEndpointHandler, shared_from_this()); mgr->ForeachEndpoint(bind(&AuthenticationComponent::NewEndpointHandler, this, _1)); mgr->RegisterEndpoint(m_AuthenticationEndpoint); @@ -26,12 +21,10 @@ void AuthenticationComponent::Start(void) void AuthenticationComponent::Stop(void) { - IcingaApplication::Ptr app = GetIcingaApplication(); + EndpointManager::Ptr mgr = GetEndpointManager(); - if (app) { - EndpointManager::Ptr mgr = app->GetEndpointManager(); + if (mgr) mgr->UnregisterEndpoint(m_AuthenticationEndpoint); - } } int AuthenticationComponent::NewEndpointHandler(const NewEndpointEventArgs& neea) @@ -51,8 +44,7 @@ int AuthenticationComponent::NewEndpointHandler(const NewEndpointEventArgs& neea params.SetIdentity("keks"); request.SetParams(params); - EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); - endpointManager->SendUnicastRequest(m_AuthenticationEndpoint, neea.Endpoint, request); + GetEndpointManager()->SendUnicastRequest(m_AuthenticationEndpoint, neea.Endpoint, request); return 0; } @@ -75,8 +67,7 @@ int AuthenticationComponent::IdentityMessageHandler(const NewRequestEventArgs& n JsonRpcRequest request; request.SetMethod("auth::Welcome"); - EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); - endpointManager->SendUnicastRequest(m_AuthenticationEndpoint, nrea.Sender, request); + GetEndpointManager()->SendUnicastRequest(m_AuthenticationEndpoint, nrea.Sender, request); return 0; } diff --git a/icinga/authenticationcomponent.h b/icinga/authenticationcomponent.h index 5d03e4a9e..60442b84e 100644 --- a/icinga/authenticationcomponent.h +++ b/icinga/authenticationcomponent.h @@ -4,13 +4,11 @@ namespace icinga { -class AuthenticationComponent : public Component +class AuthenticationComponent : public IcingaComponent { private: VirtualEndpoint::Ptr m_AuthenticationEndpoint; - IcingaApplication::Ptr GetIcingaApplication(void) const; - int NewEndpointHandler(const NewEndpointEventArgs& neea); int IdentityMessageHandler(const NewRequestEventArgs& nrea); diff --git a/icinga/discoverycomponent.cpp b/icinga/discoverycomponent.cpp index 31667bd39..88b7bbb0f 100644 --- a/icinga/discoverycomponent.cpp +++ b/icinga/discoverycomponent.cpp @@ -2,11 +2,6 @@ using namespace icinga; -IcingaApplication::Ptr DiscoveryComponent::GetIcingaApplication(void) const -{ - return static_pointer_cast(GetApplication()); -} - string DiscoveryComponent::GetName(void) const { return "discoverycomponent"; @@ -21,18 +16,15 @@ void DiscoveryComponent::Start(void) m_DiscoveryEndpoint->RegisterMethodHandler("discovery::GetPeers", bind_weak(&DiscoveryComponent::GetPeersMessageHandler, shared_from_this())); - EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); - mgr->RegisterEndpoint(m_DiscoveryEndpoint); + GetEndpointManager()->RegisterEndpoint(m_DiscoveryEndpoint); } void DiscoveryComponent::Stop(void) { - IcingaApplication::Ptr app = GetIcingaApplication(); + EndpointManager::Ptr mgr = GetEndpointManager(); - if (app) { - EndpointManager::Ptr mgr = app->GetEndpointManager(); + if (mgr) mgr->UnregisterEndpoint(m_DiscoveryEndpoint); - } } int DiscoveryComponent::NewEndpointHandler(const NewEndpointEventArgs& neea) @@ -56,8 +48,7 @@ int DiscoveryComponent::WelcomeMessageHandler(const NewRequestEventArgs& nrea) JsonRpcRequest request; request.SetMethod("discovery::GetPeers"); - EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); - endpointManager->SendUnicastRequest(m_DiscoveryEndpoint, nrea.Sender, request); + GetEndpointManager()->SendUnicastRequest(m_DiscoveryEndpoint, nrea.Sender, request); return 0; } diff --git a/icinga/discoverycomponent.h b/icinga/discoverycomponent.h index 20ec3b560..76d1b148f 100644 --- a/icinga/discoverycomponent.h +++ b/icinga/discoverycomponent.h @@ -4,7 +4,7 @@ namespace icinga { -class DiscoveryComponent : public Component +class DiscoveryComponent : public IcingaComponent { private: VirtualEndpoint::Ptr m_DiscoveryEndpoint; diff --git a/icinga/i2-icinga.h b/icinga/i2-icinga.h index 0cd6c511e..6115bf4fb 100644 --- a/icinga/i2-icinga.h +++ b/icinga/i2-icinga.h @@ -16,6 +16,7 @@ #include "virtualendpoint.h" #include "endpointmanager.h" #include "icingaapplication.h" +#include "icingacomponent.h" #include "subscriptioncomponent.h" #include "subscriptionmessage.h" #include "authenticationcomponent.h" diff --git a/icinga/icinga.vcxproj b/icinga/icinga.vcxproj index 829cc38ac..b081daada 100644 --- a/icinga/icinga.vcxproj +++ b/icinga/icinga.vcxproj @@ -16,6 +16,7 @@ + @@ -29,6 +30,7 @@ + diff --git a/icinga/icingacomponent.cpp b/icinga/icingacomponent.cpp new file mode 100644 index 000000000..776c7f8a6 --- /dev/null +++ b/icinga/icingacomponent.cpp @@ -0,0 +1,28 @@ +#include "i2-icinga.h" + +using namespace icinga; + +IcingaApplication::Ptr IcingaComponent::GetIcingaApplication(void) const +{ + return static_pointer_cast(GetApplication()); +} + +EndpointManager::Ptr IcingaComponent::GetEndpointManager(void) const +{ + IcingaApplication::Ptr app = GetIcingaApplication(); + + if (!app) + return EndpointManager::Ptr(); + + return app->GetEndpointManager(); +} + +ConfigHive::Ptr IcingaComponent::GetConfigHive(void) const +{ + IcingaApplication::Ptr app = GetIcingaApplication(); + + if (!app) + return ConfigHive::Ptr(); + + return app->GetConfigHive(); +} diff --git a/icinga/icingacomponent.h b/icinga/icingacomponent.h new file mode 100644 index 000000000..a716735df --- /dev/null +++ b/icinga/icingacomponent.h @@ -0,0 +1,17 @@ +#ifndef ICINGACOMPONENT_H +#define ICINGACOMPONENT_H + +namespace icinga +{ + +class I2_ICINGA_API IcingaComponent : public Component +{ +protected: + IcingaApplication::Ptr GetIcingaApplication(void) const; + EndpointManager::Ptr GetEndpointManager(void) const; + ConfigHive::Ptr GetConfigHive(void) const; +}; + +} + +#endif /* ICINGACOMPONENT_H */ diff --git a/icinga/subscriptioncomponent.cpp b/icinga/subscriptioncomponent.cpp index ad00ffe0a..190b45ea0 100644 --- a/icinga/subscriptioncomponent.cpp +++ b/icinga/subscriptioncomponent.cpp @@ -2,11 +2,6 @@ using namespace icinga; -IcingaApplication::Ptr SubscriptionComponent::GetIcingaApplication(void) const -{ - return static_pointer_cast(GetApplication()); -} - string SubscriptionComponent::GetName(void) const { return "subscriptioncomponent"; @@ -20,7 +15,7 @@ void SubscriptionComponent::Start(void) m_SubscriptionEndpoint->RegisterMethodSource("message::Subscribe"); m_SubscriptionEndpoint->RegisterMethodSource("message::Provide"); - EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); + EndpointManager::Ptr mgr = GetEndpointManager(); mgr->OnNewEndpoint += bind_weak(&SubscriptionComponent::NewEndpointHandler, shared_from_this()); mgr->ForeachEndpoint(bind(&SubscriptionComponent::NewEndpointHandler, this, _1)); mgr->RegisterEndpoint(m_SubscriptionEndpoint); @@ -28,12 +23,10 @@ void SubscriptionComponent::Start(void) void SubscriptionComponent::Stop(void) { - IcingaApplication::Ptr app = GetIcingaApplication(); + EndpointManager::Ptr mgr = GetEndpointManager(); - if (app) { - EndpointManager::Ptr mgr = app->GetEndpointManager(); + if (mgr) mgr->UnregisterEndpoint(m_SubscriptionEndpoint); - } } int SubscriptionComponent::SyncSubscription(Endpoint::Ptr target, string type, const NewMethodEventArgs& nmea) @@ -46,8 +39,7 @@ int SubscriptionComponent::SyncSubscription(Endpoint::Ptr target, string type, c subscriptionMessage.SetMethod(nmea.Method); request.SetParams(subscriptionMessage); - EndpointManager::Ptr endpointManager = GetIcingaApplication()->GetEndpointManager(); - endpointManager->SendUnicastRequest(m_SubscriptionEndpoint, target, request); + GetEndpointManager()->SendUnicastRequest(m_SubscriptionEndpoint, target, request); return 0; } @@ -79,8 +71,7 @@ int SubscriptionComponent::NewEndpointHandler(const NewEndpointEventArgs& neea) neea.Endpoint->RegisterMethodSink("message::Subscribe"); neea.Endpoint->RegisterMethodSink("message::Provide"); - EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); - mgr->ForeachEndpoint(bind(&SubscriptionComponent::SyncSubscriptions, this, neea.Endpoint, _1)); + GetEndpointManager()->ForeachEndpoint(bind(&SubscriptionComponent::SyncSubscriptions, this, neea.Endpoint, _1)); return 0; } diff --git a/icinga/subscriptioncomponent.h b/icinga/subscriptioncomponent.h index 3c5a185e8..8c9e60c23 100644 --- a/icinga/subscriptioncomponent.h +++ b/icinga/subscriptioncomponent.h @@ -4,13 +4,11 @@ namespace icinga { -class SubscriptionComponent : public Component +class SubscriptionComponent : public IcingaComponent { private: VirtualEndpoint::Ptr m_SubscriptionEndpoint; - IcingaApplication::Ptr GetIcingaApplication(void) const; - int NewEndpointHandler(const NewEndpointEventArgs& neea); int SubscribeMessageHandler(const NewRequestEventArgs& nrea); int ProvideMessageHandler(const NewRequestEventArgs& nrea); -- 2.40.0