]> granicus.if.org Git - icinga2/commitdiff
Added helper functions for commonly used IcingaApplication methods.
authorGunnar Beutner <gunnar.beutner@netways.de>
Tue, 24 Apr 2012 05:16:34 +0000 (07:16 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Tue, 24 Apr 2012 05:17:46 +0000 (07:17 +0200)
15 files changed:
components/configrpc/configrpccomponent.cpp
components/configrpc/configrpccomponent.h
components/demo/democomponent.cpp
components/demo/democomponent.h
icinga/Makefile.am
icinga/authenticationcomponent.cpp
icinga/authenticationcomponent.h
icinga/discoverycomponent.cpp
icinga/discoverycomponent.h
icinga/i2-icinga.h
icinga/icinga.vcxproj
icinga/icingacomponent.cpp [new file with mode: 0644]
icinga/icingacomponent.h [new file with mode: 0644]
icinga/subscriptioncomponent.cpp
icinga/subscriptioncomponent.h

index 062070f23a003b9581d4f7474890530a8cd19d9a..d516e70a012bd134ab7fd65bb3ec5d73c4b76436 100644 (file)
@@ -2,11 +2,6 @@
 
 using namespace icinga;
 
-IcingaApplication::Ptr ConfigRpcComponent::GetIcingaApplication(void)
-{
-       return static_pointer_cast<IcingaApplication>(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<VirtualEndpoint>();
 
@@ -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)
index 2f1085bcd8e325c51d1b17e627aab96c77d41c24..0efe8bec6ca5c1538a30e7cfc01eba30f8cd3eb6 100644 (file)
@@ -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);
 
index 31967a677fdd5a37fc2ad007b78c8a6ca9fa592c..dd370b08dc8efa3dfd8fc9cca11f4080e85ea9c7 100644 (file)
@@ -16,7 +16,7 @@ void DemoComponent::Start(void)
 {
        m_DemoEndpoint = make_shared<VirtualEndpoint>();
        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<Timer>();
-       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;
 }
index 3faaa6a378aa794782d40dc8f93289593272ea4e..51d88336a38d63f3f762467b49e7716821f78aa2 100644 (file)
@@ -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;
index b09174c9dbbd89f128a195d00198c09a98ae6a74..73acd1beaa22a94272fb0946f34a0fdd1e883eff 100644 (file)
@@ -15,6 +15,8 @@ libicinga_la_SOURCES =  \
        endpointmanager.h \
        icingaapplication.cpp \
        icingaapplication.h \
+       icingacomponent.cpp \
+       icingacomponent.h \
        identitymessage.cpp \
        identitymessage.h \
        i2-icinga.h \
index a7c3888777b2f2435f6c2d3626427a4c53cddb32..14520036eeaceea0c81d832f56679498906bcebb 100644 (file)
@@ -2,11 +2,6 @@
 
 using namespace icinga;
 
-IcingaApplication::Ptr AuthenticationComponent::GetIcingaApplication(void) const
-{
-       return static_pointer_cast<IcingaApplication>(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;
 }
index 5d03e4a9e673ca16636bc1eb20ee71bdb43a16b7..60442b84ef5ac0a7f4cb64b355e32e398d3d3a76 100644 (file)
@@ -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);
 
index 31667bd39333f234ce0ba29a5f7dc70476b80ca6..88b7bbb0fce27482c358651691a71de85883bea0 100644 (file)
@@ -2,11 +2,6 @@
 
 using namespace icinga;
 
-IcingaApplication::Ptr DiscoveryComponent::GetIcingaApplication(void) const
-{
-       return static_pointer_cast<IcingaApplication>(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;
 }
index 20ec3b560f97845b7f9897cfbc2d131e2d412e26..76d1b148f4376cf1d24b9de0f066cd67692ccc50 100644 (file)
@@ -4,7 +4,7 @@
 namespace icinga
 {
 
-class DiscoveryComponent : public Component
+class DiscoveryComponent : public IcingaComponent
 {
 private:
        VirtualEndpoint::Ptr m_DiscoveryEndpoint;
index 0cd6c511e3fb0ec9bc89e48e06b9cae253fadb6a..6115bf4fbf11aef6d5fe5f2481bf9bce4305d62e 100644 (file)
@@ -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"
index 829cc38ac16b790ef3a7ea02c007f7e8429e1d27..b081daadaa858fac5d40e5bf35db1c6dedd19a13 100644 (file)
@@ -16,6 +16,7 @@
     <ClCompile Include="endpoint.cpp" />
     <ClCompile Include="endpointmanager.cpp" />
     <ClCompile Include="icingaapplication.cpp" />
+    <ClCompile Include="icingacomponent.cpp" />
     <ClCompile Include="identitymessage.cpp" />
     <ClCompile Include="jsonrpcendpoint.cpp" />
     <ClCompile Include="subscriptioncomponent.cpp" />
@@ -29,6 +30,7 @@
     <ClInclude Include="endpointmanager.h" />
     <ClInclude Include="i2-icinga.h" />
     <ClInclude Include="icingaapplication.h" />
+    <ClInclude Include="icingacomponent.h" />
     <ClInclude Include="identitymessage.h" />
     <ClInclude Include="jsonrpcendpoint.h" />
     <ClInclude Include="subscriptioncomponent.h" />
diff --git a/icinga/icingacomponent.cpp b/icinga/icingacomponent.cpp
new file mode 100644 (file)
index 0000000..776c7f8
--- /dev/null
@@ -0,0 +1,28 @@
+#include "i2-icinga.h"
+
+using namespace icinga;
+
+IcingaApplication::Ptr IcingaComponent::GetIcingaApplication(void) const
+{
+       return static_pointer_cast<IcingaApplication>(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 (file)
index 0000000..a716735
--- /dev/null
@@ -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 */
index ad00ffe0a47c900a9cd7256917b64c8f0eef4462..190b45ea094f504f90fa45d2d0c509f22f1ab617 100644 (file)
@@ -2,11 +2,6 @@
 
 using namespace icinga;
 
-IcingaApplication::Ptr SubscriptionComponent::GetIcingaApplication(void) const
-{
-       return static_pointer_cast<IcingaApplication>(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;
 }
index 3c5a185e8689532e1bb5449feff8fd2ab9167e4a..8c9e60c237abc2831d554045fe2b1729d67da467 100644 (file)
@@ -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);