From 6a42ac0fe5cc06bdc0b64e3fa6df76b99bb27cd6 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Wed, 4 Apr 2012 15:09:59 +0200 Subject: [PATCH] Moved ConnectionManager class to icinga subproject. --- base/socket.cpp | 3 +- icinga/Makefile.am | 2 ++ {jsonrpc => icinga}/connectionmanager.cpp | 35 +++++++++++++++++------ {jsonrpc => icinga}/connectionmanager.h | 5 ++++ icinga/i2-icinga.h | 1 + icinga/icinga.vcxproj | 2 ++ jsonrpc/Makefile.am | 2 -- jsonrpc/i2-jsonrpc.h | 1 - jsonrpc/jsonrpc.vcxproj | 2 -- 9 files changed, 39 insertions(+), 14 deletions(-) rename {jsonrpc => icinga}/connectionmanager.cpp (80%) rename {jsonrpc => icinga}/connectionmanager.h (89%) diff --git a/base/socket.cpp b/base/socket.cpp index 8c6b22600..aba6d054c 100644 --- a/base/socket.cpp +++ b/base/socket.cpp @@ -100,8 +100,9 @@ int Socket::ExceptionEventHandler(EventArgs::Ptr ea) SocketErrorEventArgs::Ptr ea = make_shared(); ea->Code = opt; ea->Message = FormatErrorCode(opt); - Close(); + OnError(ea); + Close(); } return 0; diff --git a/icinga/Makefile.am b/icinga/Makefile.am index 849f877c5..bd090f8cd 100644 --- a/icinga/Makefile.am +++ b/icinga/Makefile.am @@ -5,6 +5,8 @@ bin_PROGRAMS = \ icinga icinga_SOURCES = \ + connectionmanager.cpp \ + connectionmanager.h \ icingaapplication.cpp \ icingaapplication.h \ i2-icinga.h diff --git a/jsonrpc/connectionmanager.cpp b/icinga/connectionmanager.cpp similarity index 80% rename from jsonrpc/connectionmanager.cpp rename to icinga/connectionmanager.cpp index c9dd0ae59..3688efffc 100644 --- a/jsonrpc/connectionmanager.cpp +++ b/icinga/connectionmanager.cpp @@ -1,7 +1,17 @@ -#include "i2-jsonrpc.h" +#include "i2-icinga.h" using namespace icinga; +void ConnectionManager::SetIdentity(string identity) +{ + m_Identity = identity; +} + +string ConnectionManager::GetIdentity(void) +{ + return m_Identity; +} + void ConnectionManager::AddListener(unsigned short port) { JsonRpcServer::Ptr server = make_shared(); @@ -23,7 +33,6 @@ void ConnectionManager::AddConnection(string host, short port) client->Start(); } - void ConnectionManager::RegisterServer(JsonRpcServer::Ptr server) { m_Servers.push_front(server); @@ -41,6 +50,7 @@ void ConnectionManager::RegisterClient(JsonRpcClient::Ptr client) m_Clients.push_front(client); client->OnNewMessage += bind_weak(&ConnectionManager::NewMessageHandler, shared_from_this()); client->OnClosed += bind_weak(&ConnectionManager::CloseClientHandler, shared_from_this()); + client->OnError += bind_weak(&ConnectionManager::ErrorClientHandler, shared_from_this()); } void ConnectionManager::UnregisterClient(JsonRpcClient::Ptr client) @@ -62,12 +72,21 @@ int ConnectionManager::CloseClientHandler(EventArgs::Ptr ea) JsonRpcClient::Ptr client = static_pointer_cast(ea->Source); UnregisterClient(client); - Timer::Ptr timer = make_shared(); - timer->SetInterval(30); - timer->SetUserArgs(ea); - timer->OnTimerExpired += bind_weak(&ConnectionManager::ReconnectClientHandler, shared_from_this()); - timer->Start(); - m_ReconnectTimers.push_front(timer); + if (client->GetPeerHost() != string()) { + Timer::Ptr timer = make_shared(); + timer->SetInterval(30); + timer->SetUserArgs(ea); + timer->OnTimerExpired += bind_weak(&ConnectionManager::ReconnectClientHandler, shared_from_this()); + timer->Start(); + m_ReconnectTimers.push_front(timer); + } + + return 0; +} + +int ConnectionManager::ErrorClientHandler(SocketErrorEventArgs::Ptr ea) +{ + cout << "Error occured for JSON-RPC socket: Code=" << ea->Code << "; Message=" << ea->Message << endl; return 0; } diff --git a/jsonrpc/connectionmanager.h b/icinga/connectionmanager.h similarity index 89% rename from jsonrpc/connectionmanager.h rename to icinga/connectionmanager.h index fe3999e7e..2cd2f2fd8 100644 --- a/jsonrpc/connectionmanager.h +++ b/icinga/connectionmanager.h @@ -10,9 +10,11 @@ class ConnectionManager : public Object list m_Clients; map< string, event > m_Methods; list m_ReconnectTimers; + string m_Identity; int NewClientHandler(NewClientEventArgs::Ptr ncea); int CloseClientHandler(EventArgs::Ptr ea); + int ErrorClientHandler(SocketErrorEventArgs::Ptr ea); int ReconnectClientHandler(TimerEventArgs::Ptr ea); int NewMessageHandler(NewMessageEventArgs::Ptr nmea); @@ -25,6 +27,9 @@ public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; + void SetIdentity(string identity); + string GetIdentity(void); + void AddListener(unsigned short port); void AddConnection(string host, short port); diff --git a/icinga/i2-icinga.h b/icinga/i2-icinga.h index fd61e6fca..61d091e8a 100644 --- a/icinga/i2-icinga.h +++ b/icinga/i2-icinga.h @@ -4,6 +4,7 @@ #include #include +#include "connectionmanager.h" #include "icingaapplication.h" #endif /* I2ICINGA_H */ diff --git a/icinga/icinga.vcxproj b/icinga/icinga.vcxproj index e1b775aa5..748c46120 100644 --- a/icinga/icinga.vcxproj +++ b/icinga/icinga.vcxproj @@ -81,9 +81,11 @@ + + diff --git a/jsonrpc/Makefile.am b/jsonrpc/Makefile.am index a74d76e4a..7ec7558c2 100644 --- a/jsonrpc/Makefile.am +++ b/jsonrpc/Makefile.am @@ -7,8 +7,6 @@ noinst_LTLIBRARIES = \ libjsonrpc_la_SOURCES = \ cJSON.c \ cJSON.h \ - connectionmanager.cpp \ - connectionmanager.h \ i2-jsonrpc.h \ jsonrpcclient.cpp \ jsonrpcclient.h \ diff --git a/jsonrpc/i2-jsonrpc.h b/jsonrpc/i2-jsonrpc.h index dfbec61c2..1a2b43110 100644 --- a/jsonrpc/i2-jsonrpc.h +++ b/jsonrpc/i2-jsonrpc.h @@ -9,6 +9,5 @@ #include "jsonrpcmessage.h" #include "jsonrpcclient.h" #include "jsonrpcserver.h" -#include "connectionmanager.h" #endif /* I2JSONRPC_H */ diff --git a/jsonrpc/jsonrpc.vcxproj b/jsonrpc/jsonrpc.vcxproj index ccb112e08..2a321fab9 100644 --- a/jsonrpc/jsonrpc.vcxproj +++ b/jsonrpc/jsonrpc.vcxproj @@ -12,7 +12,6 @@ - @@ -21,7 +20,6 @@ - -- 2.50.1