From 3da08ca9b6ac0a6ca16f12896a8bfc3668a70485 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 3 Apr 2012 11:13:17 +0200 Subject: [PATCH] Refactored event handling. --- base/Makefile.am | 1 - base/event.h | 16 ++++++++++++++-- base/tcpclient.cpp | 7 ++----- base/tcpserver.cpp | 3 +-- configrpccomponent/Makefile.am | 2 +- configrpccomponent/configrpccomponent.cpp | 6 +++--- icinga/icingaapplication.cpp | 4 ++-- jsonrpc/connectionmanager.cpp | 6 +++--- jsonrpc/jsonrpcclient.cpp | 2 +- 9 files changed, 27 insertions(+), 20 deletions(-) diff --git a/base/Makefile.am b/base/Makefile.am index a9b83c8fb..c8f50b602 100644 --- a/base/Makefile.am +++ b/base/Makefile.am @@ -44,7 +44,6 @@ libbase_la_SOURCES = \ win32.h libbase_la_LIBADD=$(LIBLTDL) -libbase_la_LDFLAGS=-pthread AM_CFLAGS=$(LTDLINCL) AM_CXXFLAGS=$(LTDLINCL) diff --git a/base/event.h b/base/event.h index 5dd048dec..60d9ecf19 100644 --- a/base/event.h +++ b/base/event.h @@ -22,16 +22,28 @@ private: list m_Delegates; public: - void bind(const DelegateType& delegate) + void hook(const DelegateType& delegate) { m_Delegates.push_front(delegate); } - void unbind(const DelegateType& delegate) + void unhook(const DelegateType& delegate) { m_Delegates.remove(delegate); } + event& operator +=(const DelegateType& rhs) + { + hook(rhs); + return *this; + } + + event& operator -=(const DelegateType& rhs) + { + unhook(rhs); + return *this; + } + void operator()(const TArgs& args) { typename list::iterator prev, i; diff --git a/base/tcpclient.cpp b/base/tcpclient.cpp index 1d1e80a9f..bdcca575a 100644 --- a/base/tcpclient.cpp +++ b/base/tcpclient.cpp @@ -12,11 +12,8 @@ void TCPClient::Start(void) { TCPSocket::Start(); - function rd = bind_weak(&TCPClient::ReadableEventHandler, shared_from_this()); - OnReadable.bind(rd); - - function wd = bind_weak(&TCPClient::WritableEventHandler, shared_from_this()); - OnWritable.bind(wd); + OnReadable += bind_weak(&TCPClient::ReadableEventHandler, shared_from_this()); + OnWritable += bind_weak(&TCPClient::WritableEventHandler, shared_from_this()); } FIFO::Ptr TCPClient::GetSendQueue(void) diff --git a/base/tcpserver.cpp b/base/tcpserver.cpp index c94e64cda..4c8e5b6b1 100644 --- a/base/tcpserver.cpp +++ b/base/tcpserver.cpp @@ -21,8 +21,7 @@ void TCPServer::Start(void) { TCPSocket::Start(); - function dr = bind_weak(&TCPServer::ReadableEventHandler, shared_from_this()); - OnReadable.bind(dr); + OnReadable += bind_weak(&TCPServer::ReadableEventHandler, shared_from_this()); } void TCPServer::Listen(void) diff --git a/configrpccomponent/Makefile.am b/configrpccomponent/Makefile.am index 06bbf3d25..f76aec842 100644 --- a/configrpccomponent/Makefile.am +++ b/configrpccomponent/Makefile.am @@ -10,5 +10,5 @@ libconfigrpccomponent_la_SOURCES = \ libconfigrpccomponent_la_CXXFLAGS = -I${top_srcdir}/base -I${top_srcdir}/jsonrpc -I${top_srcdir}/icinga -libconfigrpccomponent_la_LDFLAGS = -module -version-info 0:0:0 -no-undefined +libconfigrpccomponent_la_LDFLAGS = -module -version-info 0:0:0 -no-undefined -pthread libconfigrpccomponent_la_LIBADD = ${top_builddir}/base/libbase.la ${top_builddir}/jsonrpc/libjsonrpc.la diff --git a/configrpccomponent/configrpccomponent.cpp b/configrpccomponent/configrpccomponent.cpp index d767d6d26..20a425502 100644 --- a/configrpccomponent/configrpccomponent.cpp +++ b/configrpccomponent/configrpccomponent.cpp @@ -23,9 +23,9 @@ void ConfigRpcComponent::Start(void) if (GetConfig()->GetPropertyInteger("configSource", &configSource) && configSource != 0) { connectionManager->RegisterMethod("config::FetchObjects", bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this())); - configHive->OnObjectCreated.bind(bind_weak(&ConfigRpcComponent::LocalObjectCreatedHandler, shared_from_this())); - configHive->OnObjectRemoved.bind(bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this())); - configHive->OnPropertyChanged.bind(bind_weak(&ConfigRpcComponent::LocalPropertyChangedHandler, shared_from_this())); + configHive->OnObjectCreated += bind_weak(&ConfigRpcComponent::LocalObjectCreatedHandler, shared_from_this()); + configHive->OnObjectRemoved += bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this()); + configHive->OnPropertyChanged += bind_weak(&ConfigRpcComponent::LocalPropertyChangedHandler, shared_from_this()); } connectionManager->RegisterMethod("config::ObjectCreated", bind_weak(&ConfigRpcComponent::RemoteObjectCreatedHandler, shared_from_this())); diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index 2f85e65a3..f882d7027 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -30,8 +30,8 @@ int IcingaApplication::Main(const vector& args) string componentDirectory = GetExeDirectory() + "/../lib/icinga"; AddComponentSearchDir(componentDirectory); - GetConfigHive()->OnObjectCreated.bind(bind_weak(&IcingaApplication::ConfigObjectCreatedHandler, shared_from_this())); - GetConfigHive()->OnObjectRemoved.bind(bind_weak(&IcingaApplication::ConfigObjectRemovedHandler, shared_from_this())); + GetConfigHive()->OnObjectCreated += bind_weak(&IcingaApplication::ConfigObjectCreatedHandler, shared_from_this()); + GetConfigHive()->OnObjectRemoved += bind_weak(&IcingaApplication::ConfigObjectRemovedHandler, shared_from_this()); ConfigObject::Ptr fileComponentConfig = new_object(); fileComponentConfig->SetName("configfilecomponent"); diff --git a/jsonrpc/connectionmanager.cpp b/jsonrpc/connectionmanager.cpp index 803b5d70a..67151bef0 100644 --- a/jsonrpc/connectionmanager.cpp +++ b/jsonrpc/connectionmanager.cpp @@ -5,7 +5,7 @@ using namespace icinga; void ConnectionManager::RegisterServer(JsonRpcServer::Ptr server) { m_Servers.push_front(server); - server->OnNewClient.bind(bind_weak(&ConnectionManager::NewClientHandler, shared_from_this())); + server->OnNewClient += bind_weak(&ConnectionManager::NewClientHandler, shared_from_this()); } void ConnectionManager::UnregisterServer(JsonRpcServer::Ptr server) @@ -17,7 +17,7 @@ void ConnectionManager::UnregisterServer(JsonRpcServer::Ptr server) void ConnectionManager::RegisterClient(JsonRpcClient::Ptr client) { m_Clients.push_front(client); - client->OnNewMessage.bind(bind_weak(&ConnectionManager::NewMessageHandler, shared_from_this())); + client->OnNewMessage += bind_weak(&ConnectionManager::NewMessageHandler, shared_from_this()); } void ConnectionManager::UnregisterClient(JsonRpcClient::Ptr client) @@ -74,7 +74,7 @@ void ConnectionManager::RegisterMethod(string method, functionsecond.bind(callback); + i->second += callback; } void ConnectionManager::UnregisterMethod(string method, function function) diff --git a/jsonrpc/jsonrpcclient.cpp b/jsonrpc/jsonrpcclient.cpp index 52cf3d75c..34db54c07 100644 --- a/jsonrpc/jsonrpcclient.cpp +++ b/jsonrpc/jsonrpcclient.cpp @@ -6,7 +6,7 @@ void JsonRpcClient::Start(void) { TCPClient::Start(); - OnDataAvailable.bind(bind_weak(&JsonRpcClient::DataAvailableHandler, shared_from_this())); + OnDataAvailable += bind_weak(&JsonRpcClient::DataAvailableHandler, shared_from_this()); } void JsonRpcClient::SendMessage(JsonRpcMessage::Ptr message) -- 2.40.0