From c11231f4e7832b15b7afcd605eb40a1c797badf5 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Thu, 19 Apr 2012 12:16:52 +0200 Subject: [PATCH] Added placeholders for event persistance handling. --- base/delegate.h | 2 +- components/configrpc/configrpccomponent.cpp | 2 +- icinga/endpoint.cpp | 24 +++++++++++++++++++-- icinga/endpoint.h | 10 +++++++-- icinga/jsonrpcendpoint.cpp | 16 ++++++++++---- jsonrpc/netstring.cpp | 2 +- 6 files changed, 45 insertions(+), 11 deletions(-) diff --git a/base/delegate.h b/base/delegate.h index a52815550..beae350fe 100644 --- a/base/delegate.h +++ b/base/delegate.h @@ -9,7 +9,7 @@ int delegate_fwd(int (TObject::*function)(TArgs), weak_ptr wref, const { shared_ptr ref = wref.lock(); - if (ref.get() == NULL) + if (!ref) return -1; return (ref.get()->*function)(args); diff --git a/components/configrpc/configrpccomponent.cpp b/components/configrpc/configrpccomponent.cpp index 0e0194d3a..15d1cc035 100644 --- a/components/configrpc/configrpccomponent.cpp +++ b/components/configrpc/configrpccomponent.cpp @@ -201,7 +201,7 @@ int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea ConfigHive::Ptr configHive = GetIcingaApplication()->GetConfigHive(); ConfigObject::Ptr object = configHive->GetObject(type, name); - if (object.get() == NULL) + if (!object) return 0; configHive->RemoveObject(object); diff --git a/icinga/endpoint.cpp b/icinga/endpoint.cpp index d95b80807..7fd8e791f 100644 --- a/icinga/endpoint.cpp +++ b/icinga/endpoint.cpp @@ -4,10 +4,10 @@ using namespace icinga; EndpointManager::Ptr Endpoint::GetEndpointManager(void) const { - return m_EndpointManager; + return m_EndpointManager.lock(); } -void Endpoint::SetEndpointManager(EndpointManager::Ptr manager) +void Endpoint::SetEndpointManager(EndpointManager::WeakPtr manager) { m_EndpointManager = manager; } @@ -61,3 +61,23 @@ void Endpoint::ForeachMethodSource(function cal callback(nmea); } } + +void Endpoint::ClearMethodSinks(void) +{ + m_MethodSinks.clear(); +} + +void Endpoint::ClearMethodSources(void) +{ + m_MethodSources.clear(); +} + +int Endpoint::CountMethodSinks(void) const +{ + return m_MethodSinks.size(); +} + +int Endpoint::CountMethodSources(void) const +{ + return m_MethodSources.size(); +} diff --git a/icinga/endpoint.h b/icinga/endpoint.h index 1a7f8258e..efe509934 100644 --- a/icinga/endpoint.h +++ b/icinga/endpoint.h @@ -17,14 +17,14 @@ private: set m_MethodSinks; set m_MethodSources; - shared_ptr m_EndpointManager; + weak_ptr m_EndpointManager; public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; shared_ptr GetEndpointManager(void) const; - void SetEndpointManager(shared_ptr manager); + void SetEndpointManager(weak_ptr manager); void RegisterMethodSink(string method); void UnregisterMethodSink(string method); @@ -44,6 +44,12 @@ public: void ForeachMethodSink(function callback); void ForeachMethodSource(function callback); + + void ClearMethodSinks(void); + void ClearMethodSources(void); + + int CountMethodSinks(void) const; + int CountMethodSources(void) const; }; } diff --git a/icinga/jsonrpcendpoint.cpp b/icinga/jsonrpcendpoint.cpp index 604686c3d..cc48665ac 100644 --- a/icinga/jsonrpcendpoint.cpp +++ b/icinga/jsonrpcendpoint.cpp @@ -32,7 +32,7 @@ bool JsonRpcEndpoint::IsLocal(void) const bool JsonRpcEndpoint::IsConnected(void) const { - return (m_Client.get() != NULL); + return m_Client; } void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& message) @@ -44,6 +44,8 @@ void JsonRpcEndpoint::ProcessRequest(Endpoint::Ptr sender, const JsonRpcRequest& m_PendingCalls[id] = sender; m_Client->SendMessage(message); + } else { + // TODO: persist the event } } @@ -94,8 +96,6 @@ int JsonRpcEndpoint::ClientClosedHandler(const EventArgs& ea) { m_PendingCalls.clear(); - // TODO: clear method sources/sinks - if (m_Client->GetPeerHost() != string()) { Timer::Ptr timer = make_shared(); timer->SetInterval(30); @@ -105,6 +105,14 @@ int JsonRpcEndpoint::ClientClosedHandler(const EventArgs& ea) m_ReconnectTimer = timer; } + // TODO: _only_ clear non-persistent method sources/sinks + // unregister ourselves if no persistent sources/sinks are left (use a timer for that, once we have a TTL property for the methods) + ClearMethodSinks(); + ClearMethodSources(); + + if (CountMethodSinks() == 0) + GetEndpointManager()->UnregisterEndpoint(static_pointer_cast(shared_from_this())); + m_Client.reset(); // TODO: persist events, etc., for now we just disable the endpoint @@ -124,7 +132,7 @@ int JsonRpcEndpoint::ClientReconnectHandler(const TimerEventArgs& ea) JsonRpcClient::Ptr client = static_pointer_cast(ea.UserArgs.Source); Timer::Ptr timer = static_pointer_cast(ea.Source); - m_Client = client; + GetEndpointManager()->AddConnection(client->GetPeerHost(), client->GetPeerPort()); timer->Stop(); m_ReconnectTimer.reset(); diff --git a/jsonrpc/netstring.cpp b/jsonrpc/netstring.cpp index 48ca07419..85347cc0e 100644 --- a/jsonrpc/netstring.cpp +++ b/jsonrpc/netstring.cpp @@ -47,7 +47,7 @@ json_t *Netstring::GetJsonFromDictionary(const Dictionary::Ptr& dictionary) case VariantObject: valueDictionary = dynamic_pointer_cast(i->second.GetObject()); - if (valueDictionary.get() != NULL) + if (valueDictionary) cJSON_AddItemToObject(json, i->first.c_str(), GetJsonFromDictionary(valueDictionary)); default: break;