Added helper functions for commonly used IcingaApplication methods.

This commit is contained in:
Gunnar Beutner 2012-04-24 07:16:34 +02:00
parent bd3ceb8edd
commit 576181f304
15 changed files with 85 additions and 78 deletions

View 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)

View 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);

View 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;
}

View 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;

View 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 \

View 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;
}

View 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);

View 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;
}

View File

@ -4,7 +4,7 @@
namespace icinga
{
class DiscoveryComponent : public Component
class DiscoveryComponent : public IcingaComponent
{
private:
VirtualEndpoint::Ptr m_DiscoveryEndpoint;

View 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"

View 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" />

View File

@ -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();
}

17
icinga/icingacomponent.h Normal file
View File

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

View 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;
}

View 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);