]> granicus.if.org Git - icinga2/commitdiff
Code cleanups.
authorGunnar Beutner <gunnar.beutner@netways.de>
Mon, 23 Apr 2012 07:48:20 +0000 (09:48 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Mon, 23 Apr 2012 07:48:20 +0000 (09:48 +0200)
Proper error handling for some *NIX functions.

15 files changed:
base/application.cpp
base/exception.cpp
base/socket.cpp
base/socket.h
base/timer.cpp
base/timer.h
base/variant.h
components/configrpc/configrpccomponent.cpp
icinga/authenticationcomponent.cpp
icinga/endpointmanager.cpp
icinga/icingaapplication.cpp
icinga/subscriptioncomponent.cpp
jsonrpc/jsonrpcrequest.h
jsonrpc/jsonrpcresponse.h
jsonrpc/netstring.cpp

index 336788af3fdfb96798fe9925c8fbb17a922aa68b..def10cce6fd4f3d8241d2b60435a12fd70abaffe 100644 (file)
@@ -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, '/');
index 3dcd96bf9bb5b797b614db378fe5ab3e9cc121f8..f421a7b90457988689eb9ad13ab0b2b54274aac1 100644 (file)
@@ -43,4 +43,4 @@ string Win32Exception::FormatErrorCode(int code)
 string PosixException::FormatErrorCode(int code)
 {
        return strerror(code);
-}
\ No newline at end of file
+}
index 135eb52506cafe1bd7b8eb2d094b8b4f920ff255..77b497ff7ed0a88d2142418bc7ec1bc1b2c351ce 100644 (file)
@@ -20,16 +20,12 @@ void Socket::Start(void)
 
        OnException += bind_weak(&Socket::ExceptionEventHandler, shared_from_this());
 
-       Sockets.insert(static_pointer_cast<Socket>(shared_from_this()));
+       Sockets.push_back(static_pointer_cast<Socket>(shared_from_this()));
 }
 
 void Socket::Stop(void)
 {
-       Socket::Ptr self = static_pointer_cast<Socket>(shared_from_this());
-       Socket::CollectionType::iterator i = Sockets.find(self);
-
-       if (i != Sockets.end())
-               Sockets.erase(i);
+       Sockets.remove_if(weak_ptr_eq_raw<Socket>(this));
 }
 
 void Socket::SetFD(SOCKET fd)
index 6ac7228f6b9c2123d2f5b5c496eb2227de8526c7..9be92459b1f3d644b747a1c843ed8fd3141c199e 100644 (file)
@@ -29,7 +29,7 @@ public:
        typedef shared_ptr<Socket> Ptr;
        typedef weak_ptr<Socket> WeakPtr;
 
-       typedef set< Socket::WeakPtr, owner_less<Socket::WeakPtr> > CollectionType;
+       typedef list<Socket::WeakPtr> CollectionType;
 
        static Socket::CollectionType Sockets;
 
index d9d1872857080d96711b00b6c64fda8f652e0ffe..6a94ac340ca6c67085e504690191f6d74cf97b2f 100644 (file)
@@ -94,18 +94,14 @@ EventArgs Timer::GetUserArgs(void) const
 
 void Timer::Start(void)
 {
-       Timers.insert(static_pointer_cast<Timer>(shared_from_this()));
+       Timers.push_back(static_pointer_cast<Timer>(shared_from_this()));
 
        Reschedule(time(NULL) + m_Interval);
 }
 
 void Timer::Stop(void)
 {
-       Timer::Ptr self = static_pointer_cast<Timer>(shared_from_this());
-       Timer::CollectionType::iterator i = Timers.find(self);
-
-       if (i != Timers.end())
-               Timers.erase(i);
+       Timers.remove_if(weak_ptr_eq_raw<Timer>(this));
 }
 
 void Timer::Reschedule(time_t next)
index cd57dae80929ac46fc1f5d9ff041b1a71fd424f8..4983a23180f08817a04b0efb9f642bc8dad05b2e 100644 (file)
@@ -30,7 +30,7 @@ public:
        typedef shared_ptr<Timer> Ptr;
        typedef weak_ptr<Timer> WeakPtr;
 
-       typedef set< Timer::WeakPtr, owner_less<Timer::WeakPtr> > CollectionType;
+       typedef list<Timer::WeakPtr> CollectionType;
 
        static Timer::CollectionType Timers;
 
index 5b1eb0eb4669cf508ee6c5d15ea41ce58424ead7..1852b8fec89c906d61c0e6111c5fbc23ff6d7d6f 100644 (file)
@@ -50,4 +50,4 @@ public:
 
 }
 
-#endif /* VARIANT_H */
\ No newline at end of file
+#endif /* VARIANT_H */
index 9a2f66f96e078f67b014fd73c31f1febfc381caf..9fd60bec88a940c8039ad6f2c2992bc95edc3f53 100644 (file)
@@ -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;
index 85f3d0ae06703a64376b26d6a572aa0df4964e67..78b1c83f1c460af81dd9e01ae66a4fd7ba224f39 100644 (file)
@@ -15,7 +15,7 @@ string AuthenticationComponent::GetName(void) const
 void AuthenticationComponent::Start(void)
 {
        m_AuthenticationEndpoint = make_shared<VirtualEndpoint>();
-       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;
index a3d8d798befd5e352c2993f955d7f77dd4d5b2db..3b655b3ae599c6d7adea7b5106ad6252ff6e58e9 100644 (file)
@@ -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;
index bc7632a8eceb8c95ed28a763849601c648d70c39..f31349582a972af1f4a5ae5b8b7cb743f26fadca 100644 (file)
@@ -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++)
index b1f3b45c3b8d8d34a98e61e63395b525d53a7d81..78e881334e44894d6104c257fc526dcb4abfac84 100644 (file)
@@ -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());
index 45362d9e7a7ae03552b8ed48eeafbd913208852a..e3dd38ed7181815d3d25d49e94eaf52436de89cf 100644 (file)
@@ -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
index ab001b973e839eab34e2e9fdcfdf2926d74c9281..deba28e92814a5d5b4072a79e0a29a5122299190 100644 (file)
@@ -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);
        }
index dd3e0e76f283993e34a22e81919e1a166fbc891c..3dc496e7669b677e51a075343adfed91d0df8e9f 100644 (file)
@@ -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');
        }