From f7acf4ba3fddbe55c812bf6c2c7530b1ccd1acb9 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 23 Apr 2012 09:48:20 +0200 Subject: [PATCH] Code cleanups. Proper error handling for some *NIX functions. --- base/application.cpp | 8 ++++---- base/exception.cpp | 2 +- base/socket.cpp | 8 ++------ base/socket.h | 2 +- base/timer.cpp | 8 ++------ base/timer.h | 2 +- base/variant.h | 2 +- components/configrpc/configrpccomponent.cpp | 4 +--- icinga/authenticationcomponent.cpp | 6 ++---- icinga/endpointmanager.cpp | 2 -- icinga/icingaapplication.cpp | 1 - icinga/subscriptioncomponent.cpp | 1 - jsonrpc/jsonrpcrequest.h | 5 ++++- jsonrpc/jsonrpcresponse.h | 7 +++++-- jsonrpc/netstring.cpp | 2 +- 15 files changed, 25 insertions(+), 35 deletions(-) diff --git a/base/application.cpp b/base/application.cpp index 336788af3..def10cce6 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -254,7 +254,7 @@ string Application::GetExeDirectory(void) const const char *argv0 = m_Arguments[0].c_str(); if (getcwd(Cwd, sizeof(Cwd)) == NULL) - throw exception(/*"getcwd() failed"*/); + throw PosixException("getcwd failed", errno); if (argv0[0] != '/') snprintf(FullExePath, sizeof(FullExePath), "%s/%s", Cwd, argv0); @@ -271,7 +271,7 @@ string Application::GetExeDirectory(void) const for (Directory = strtok(PathEnv, ":"); Directory != NULL; Directory = strtok(NULL, ":")) { if (snprintf(PathTest, sizeof(PathTest), "%s/%s", Directory, argv0) < 0) - throw exception(/*"snprintf() failed"*/); + throw PosixException("snprintf failed", errno); if (access(PathTest, X_OK) == 0) { strncpy(FullExePath, PathTest, sizeof(FullExePath)); @@ -285,12 +285,12 @@ string Application::GetExeDirectory(void) const free(PathEnv); if (!FoundPath) - throw exception(/*"Could not determine executable path."*/); + throw Exception("Could not determine executable path."); } } if ((Buf = realpath(FullExePath, NULL)) == NULL) - throw exception(/*"realpath() failed"*/); + throw PosixException("realpath failed", errno); // remove filename char *LastSlash = strrchr(Buf, '/'); diff --git a/base/exception.cpp b/base/exception.cpp index 3dcd96bf9..f421a7b90 100644 --- a/base/exception.cpp +++ b/base/exception.cpp @@ -43,4 +43,4 @@ string Win32Exception::FormatErrorCode(int code) string PosixException::FormatErrorCode(int code) { return strerror(code); -} \ No newline at end of file +} diff --git a/base/socket.cpp b/base/socket.cpp index 135eb5250..77b497ff7 100644 --- a/base/socket.cpp +++ b/base/socket.cpp @@ -20,16 +20,12 @@ void Socket::Start(void) OnException += bind_weak(&Socket::ExceptionEventHandler, shared_from_this()); - Sockets.insert(static_pointer_cast(shared_from_this())); + Sockets.push_back(static_pointer_cast(shared_from_this())); } void Socket::Stop(void) { - Socket::Ptr self = static_pointer_cast(shared_from_this()); - Socket::CollectionType::iterator i = Sockets.find(self); - - if (i != Sockets.end()) - Sockets.erase(i); + Sockets.remove_if(weak_ptr_eq_raw(this)); } void Socket::SetFD(SOCKET fd) diff --git a/base/socket.h b/base/socket.h index 6ac7228f6..9be92459b 100644 --- a/base/socket.h +++ b/base/socket.h @@ -29,7 +29,7 @@ public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - typedef set< Socket::WeakPtr, owner_less > CollectionType; + typedef list CollectionType; static Socket::CollectionType Sockets; diff --git a/base/timer.cpp b/base/timer.cpp index d9d187285..6a94ac340 100644 --- a/base/timer.cpp +++ b/base/timer.cpp @@ -94,18 +94,14 @@ EventArgs Timer::GetUserArgs(void) const void Timer::Start(void) { - Timers.insert(static_pointer_cast(shared_from_this())); + Timers.push_back(static_pointer_cast(shared_from_this())); Reschedule(time(NULL) + m_Interval); } void Timer::Stop(void) { - Timer::Ptr self = static_pointer_cast(shared_from_this()); - Timer::CollectionType::iterator i = Timers.find(self); - - if (i != Timers.end()) - Timers.erase(i); + Timers.remove_if(weak_ptr_eq_raw(this)); } void Timer::Reschedule(time_t next) diff --git a/base/timer.h b/base/timer.h index cd57dae80..4983a2318 100644 --- a/base/timer.h +++ b/base/timer.h @@ -30,7 +30,7 @@ public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - typedef set< Timer::WeakPtr, owner_less > CollectionType; + typedef list CollectionType; static Timer::CollectionType Timers; diff --git a/base/variant.h b/base/variant.h index 5b1eb0eb4..1852b8fec 100644 --- a/base/variant.h +++ b/base/variant.h @@ -50,4 +50,4 @@ public: } -#endif /* VARIANT_H */ \ No newline at end of file +#endif /* VARIANT_H */ diff --git a/components/configrpc/configrpccomponent.cpp b/components/configrpc/configrpccomponent.cpp index 9a2f66f96..9fd60bec8 100644 --- a/components/configrpc/configrpccomponent.cpp +++ b/components/configrpc/configrpccomponent.cpp @@ -34,7 +34,7 @@ void ConfigRpcComponent::Start(void) m_ConfigRpcEndpoint->RegisterMethodSource("config::PropertyChanged"); } - m_ConfigRpcEndpoint->RegisterMethodHandler("message::Welcome", bind_weak(&ConfigRpcComponent::WelcomeMessageHandler, shared_from_this())); + m_ConfigRpcEndpoint->RegisterMethodHandler("auth::Welcome", bind_weak(&ConfigRpcComponent::WelcomeMessageHandler, shared_from_this())); m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectCreated", bind_weak(&ConfigRpcComponent::RemoteObjectUpdatedHandler, shared_from_this())); m_ConfigRpcEndpoint->RegisterMethodHandler("config::ObjectRemoved", bind_weak(&ConfigRpcComponent::RemoteObjectRemovedHandler, shared_from_this())); @@ -55,7 +55,6 @@ int ConfigRpcComponent::NewEndpointHandler(const NewEndpointEventArgs& ea) { if (ea.Endpoint->HasIdentity()) { JsonRpcRequest request; - request.SetVersion("2.0"); request.SetMethod("config::FetchObjects"); ea.Endpoint->ProcessRequest(m_ConfigRpcEndpoint, request); } @@ -76,7 +75,6 @@ int ConfigRpcComponent::WelcomeMessageHandler(const NewRequestEventArgs& ea) JsonRpcRequest ConfigRpcComponent::MakeObjectMessage(const ConfigObject::Ptr& object, string method, bool includeProperties) { JsonRpcRequest msg; - msg.SetVersion("2.0"); msg.SetMethod(method); Message params; diff --git a/icinga/authenticationcomponent.cpp b/icinga/authenticationcomponent.cpp index 85f3d0ae0..78b1c83f1 100644 --- a/icinga/authenticationcomponent.cpp +++ b/icinga/authenticationcomponent.cpp @@ -15,7 +15,7 @@ string AuthenticationComponent::GetName(void) const void AuthenticationComponent::Start(void) { m_AuthenticationEndpoint = make_shared(); - m_AuthenticationEndpoint->RegisterMethodHandler("message::SetIdentity", bind_weak(&AuthenticationComponent::IdentityMessageHandler, shared_from_this())); + m_AuthenticationEndpoint->RegisterMethodHandler("auth::SetIdentity", bind_weak(&AuthenticationComponent::IdentityMessageHandler, shared_from_this())); EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); mgr->OnNewEndpoint += bind_weak(&AuthenticationComponent::NewEndpointHandler, shared_from_this()); @@ -39,7 +39,6 @@ int AuthenticationComponent::NewEndpointHandler(const NewEndpointEventArgs& neea return 0; JsonRpcRequest request; - request.SetVersion("2.0"); request.SetMethod("message::SetIdentity"); IdentityMessage params; @@ -67,8 +66,7 @@ int AuthenticationComponent::IdentityMessageHandler(const NewRequestEventArgs& n /* there's no authentication for now, just tell them it's ok to send messages */ JsonRpcRequest request; - request.SetVersion("2.0"); - request.SetMethod("message::Welcome"); + request.SetMethod("auth::Welcome"); nrea.Sender->ProcessRequest(m_AuthenticationEndpoint, request); return 0; diff --git a/icinga/endpointmanager.cpp b/icinga/endpointmanager.cpp index a3d8d798b..3b655b3ae 100644 --- a/icinga/endpointmanager.cpp +++ b/icinga/endpointmanager.cpp @@ -114,7 +114,6 @@ int EndpointManager::NewMethodSinkHandler(const NewMethodEventArgs& ea) return 0; JsonRpcRequest request; - request.SetVersion("2.0"); request.SetMethod("message::Subscribe"); SubscriptionMessage subscriptionMessage; @@ -134,7 +133,6 @@ int EndpointManager::NewMethodSourceHandler(const NewMethodEventArgs& ea) return 0; JsonRpcRequest request; - request.SetVersion("2.0"); request.SetMethod("message::Provide"); SubscriptionMessage subscriptionMessage; diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index bc7632a8e..f31349582 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -87,7 +87,6 @@ int IcingaApplication::TestTimerHandler(const TimerEventArgs& tea) cout << "Problem?" << endl; JsonRpcRequest request; - request.SetVersion("2.0"); request.SetMethod("test"); for (int i = 0; i < 5; i++) diff --git a/icinga/subscriptioncomponent.cpp b/icinga/subscriptioncomponent.cpp index b1f3b45c3..78e881334 100644 --- a/icinga/subscriptioncomponent.cpp +++ b/icinga/subscriptioncomponent.cpp @@ -19,7 +19,6 @@ void SubscriptionComponent::Start(void) m_SubscriptionEndpoint->RegisterMethodHandler("message::Provide", bind_weak(&SubscriptionComponent::ProvideMessageHandler, shared_from_this())); m_SubscriptionEndpoint->RegisterMethodSource("message::Subscribe"); m_SubscriptionEndpoint->RegisterMethodSource("message::Provide"); - m_SubscriptionEndpoint->RegisterMethodSource("message::Welcome"); EndpointManager::Ptr mgr = GetIcingaApplication()->GetEndpointManager(); mgr->OnNewEndpoint += bind_weak(&SubscriptionComponent::NewEndpointHandler, shared_from_this()); diff --git a/jsonrpc/jsonrpcrequest.h b/jsonrpc/jsonrpcrequest.h index 45362d9e7..e3dd38ed7 100644 --- a/jsonrpc/jsonrpcrequest.h +++ b/jsonrpc/jsonrpcrequest.h @@ -8,7 +8,10 @@ class I2_JSONRPC_API JsonRpcRequest : public Message { public: - JsonRpcRequest(void) : Message() { } + JsonRpcRequest(void) : Message() { + SetVersion("2.0"); + } + JsonRpcRequest(const Message& message) : Message(message) { } inline bool GetVersion(string *value) const diff --git a/jsonrpc/jsonrpcresponse.h b/jsonrpc/jsonrpcresponse.h index ab001b973..deba28e92 100644 --- a/jsonrpc/jsonrpcresponse.h +++ b/jsonrpc/jsonrpcresponse.h @@ -7,7 +7,10 @@ namespace icinga class I2_JSONRPC_API JsonRpcResponse : public Message { public: - JsonRpcResponse(void) : Message() { } + JsonRpcResponse(void) : Message() { + SetVersion("2.0"); + } + JsonRpcResponse(const Message& message) : Message(message) { } inline bool GetVersion(string *value) const @@ -15,7 +18,7 @@ public: return GetPropertyString("jsonrpc", value); } - inline void SetJsonRpc(const string& value) + inline void SetVersion(const string& value) { SetPropertyString("jsonrpc", value); } diff --git a/jsonrpc/netstring.cpp b/jsonrpc/netstring.cpp index dd3e0e76f..3dc496e76 100644 --- a/jsonrpc/netstring.cpp +++ b/jsonrpc/netstring.cpp @@ -77,7 +77,7 @@ bool Netstring::ReadMessageFromFIFO(FIFO::Ptr fifo, Message *message) for (i = 0; i < buffer_length && isdigit(buffer[i]); i++) { /* length specifier must have at most 9 characters */ if (i >= 9) - return false; + throw InvalidArgumentException("Length specifier must not exceed 9 characters"); len = len * 10 + (buffer[i] - '0'); } -- 2.50.1