]> granicus.if.org Git - icinga2/commitdiff
Code cleanup
authorGunnar Beutner <gunnar.beutner@netways.de>
Sun, 22 Apr 2012 14:45:31 +0000 (16:45 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sun, 22 Apr 2012 14:45:31 +0000 (16:45 +0200)
36 files changed:
base/Makefile.am
base/application.cpp
base/application.h
base/base.vcxproj
base/component.h
base/condvar.h
base/configcollection.h
base/confighive.cpp
base/confighive.h
base/configobject.cpp
base/configobject.h
base/delegate.h
base/dictionary.cpp
base/dictionary.h
base/event.h
base/exception.cpp
base/exception.h
base/i2-base.h
base/memory.cpp
base/memory.h
base/mutex.h
base/object.cpp
base/object.h
base/socket.cpp
base/socket.h
base/tcpclient.cpp
base/tcpserver.cpp
base/tcpsocket.cpp
base/thread.cpp
base/thread.h
base/timer.cpp
base/timer.h
base/utility.cpp [new file with mode: 0644]
base/utility.h [new file with mode: 0644]
components/configrpc/configrpccomponent.cpp
components/configrpc/configrpccomponent.h

index ef7f2ee0e1abc1c5684103556c1590e9d20c055f..c67020e5127146065b633cd4798b02879fc28f8e 100644 (file)
@@ -47,6 +47,8 @@ libbase_la_SOURCES =  \
        timer.h \
        unix.cpp \
        unix.h \
+       utility.cpp \
+       utility.h \
        variant.cpp \
        variant.h \
        win32.cpp \
index aab9f8e8f42ba5dfa12c08eb3f6e5c2e6d8891b7..336788af3fdfb96798fe9925c8fbb17a922aa68b 100644 (file)
@@ -31,10 +31,8 @@ Application::Application(void)
 
 Application::~Application(void)
 {
-       Timer::StopAllTimers();
-       Socket::CloseAllSockets();
-
-       for (map<string, Component::Ptr>::iterator i = m_Components.begin(); i != m_Components.end(); i++) {
+       for (map<string, Component::Ptr>::iterator i = m_Components.begin();
+           i != m_Components.end(); i++) {
                i->second->Stop();
        }
 
@@ -60,7 +58,8 @@ void Application::RunEventLoop(void)
                FD_ZERO(&exceptfds);
 
                Socket::CollectionType::iterator prev, i;
-               for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) {
+               for (i = Socket::Sockets.begin();
+                   i != Socket::Sockets.end(); ) {
                        Socket::Ptr socket = i->lock();
 
                        prev = i;
@@ -102,7 +101,8 @@ void Application::RunEventLoop(void)
                        Sleep(tv.tv_sec * 1000 + tv.tv_usec);
                        ready = 0;
                } else
-                       ready = select(nfds + 1, &readfds, &writefds, &exceptfds, &tv);
+                       ready = select(nfds + 1, &readfds, &writefds,
+                           &exceptfds, &tv);
 
                if (ready < 0)
                        break;
@@ -112,7 +112,8 @@ void Application::RunEventLoop(void)
                EventArgs ea;
                ea.Source = shared_from_this();
 
-               for (i = Socket::Sockets.begin(); i != Socket::Sockets.end(); ) {
+               for (i = Socket::Sockets.begin();
+                   i != Socket::Sockets.end(); ) {
                        Socket::Ptr socket = i->lock();
 
                        prev = i;
@@ -123,75 +124,35 @@ void Application::RunEventLoop(void)
                                continue;
                        }
 
-                       int fd = socket->GetFD();
+                       int fd;
 
-                       if (FD_ISSET(fd, &writefds) && socket->GetFD() != INVALID_SOCKET)
+                       fd = socket->GetFD();
+                       if (fd != INVALID_SOCKET && FD_ISSET(fd, &writefds))
                                socket->OnWritable(ea);
 
-                       if (FD_ISSET(fd, &readfds) && socket->GetFD() != INVALID_SOCKET)
+                       fd = socket->GetFD();
+                       if (fd != INVALID_SOCKET && FD_ISSET(fd, &readfds))
                                socket->OnReadable(ea);
 
-                       if (FD_ISSET(fd, &exceptfds) && socket->GetFD() != INVALID_SOCKET)
+                       fd = socket->GetFD();
+                       if (fd != INVALID_SOCKET && FD_ISSET(fd, &exceptfds))
                                socket->OnException(ea);
                }
        }
 }
 
-bool Application::Daemonize(void) {
-#ifndef _WIN32
-       pid_t pid;
-       pid_t sid;
-       int fd;
-
-       pid = fork();
-       if (pid == -1) {
-               return false;
-       }
-
-       if (pid) {
-               fprintf(stdout, "DONE\n");
-               exit(0);
-       }
-
-       fd = open("/dev/null", O_RDWR);
-       if (fd) {
-               if (fd != 0) {
-                       dup2(fd, 0);
-               }
-
-               if (fd != 1) {
-                       dup2(fd, 1);
-               }
-
-               if (fd != 2) {
-                       dup2(fd, 2);
-               }
-
-               if (fd > 2) {
-                       close(fd);
-               }
-       }
-
-       sid = setsid();
-       if (sid == -1) {
-               return false;
-       }
-#endif
-
-       return true;
-}
-
 void Application::Shutdown(void)
 {
        m_ShuttingDown = true;
 }
 
-ConfigHive::Ptr Application::GetConfigHive(void)
+ConfigHive::Ptr Application::GetConfigHive(void) const
 {
        return m_ConfigHive;
 }
 
-Component::Ptr Application::LoadComponent(const string& path, const ConfigObject::Ptr& componentConfig)
+Component::Ptr Application::LoadComponent(const string& path,
+    const ConfigObject::Ptr& componentConfig)
 {
        Component::Ptr component;
        Component *(*pCreateComponent)();
@@ -208,13 +169,16 @@ Component::Ptr Application::LoadComponent(const string& path, const ConfigObject
                throw ComponentLoadException("Could not load module");
 
 #ifdef _WIN32
-       pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
+       pCreateComponent = (CreateComponentFunction)GetProcAddress(hModule,
+           "CreateComponent");
 #else /* _WIN32 */
-       pCreateComponent = (Component *(*)())lt_dlsym(hModule, "CreateComponent");
+       pCreateComponent = (CreateComponentFunction)lt_dlsym(hModule,
+           "CreateComponent");
 #endif /* _WIN32 */
 
        if (pCreateComponent == NULL)
-               throw ComponentLoadException("Loadable module does not contain CreateComponent function");
+               throw ComponentLoadException("Loadable module does not "
+                   "contain CreateComponent function");
 
        component = Component::Ptr(pCreateComponent());
        component->SetConfig(componentConfig);
@@ -270,12 +234,12 @@ void Application::SetArguments(const vector<string>& arguments)
        m_Arguments = arguments;
 }
 
-const vector<string>& Application::GetArguments(void)
+const vector<string>& Application::GetArguments(void) const
 {
        return m_Arguments;
 }
 
-const string& Application::GetExeDirectory(void)
+string Application::GetExeDirectory(void) const
 {
        static string ExePath;
 
@@ -364,24 +328,19 @@ bool Application::IsDebugging(void) const
        return m_Debugging;
 }
 
-void Application::SigIntHandler(int signum)
+#ifndef _WIN32
+static void ApplicationSigIntHandler(int signum)
 {
        Application::Instance->Shutdown();
 
-#ifndef _WIN32
        struct sigaction sa;
        memset(&sa, 0, sizeof(sa));
        sa.sa_handler = SIG_DFL;
        sigaction(SIGINT, &sa, NULL);
-#endif /* _WIN32 */
-}
-
-static void application_sigint_handler(int signum)
-{
-       Application::Instance->SigIntHandler(signum);
 }
+#endif /* _WIN32 */
 
-int application_main(int argc, char **argv, Application *instance)
+int icinga::RunApplication(int argc, char **argv, Application *instance)
 {
        int result;
 
@@ -390,7 +349,7 @@ int application_main(int argc, char **argv, Application *instance)
 #ifndef _WIN32
        struct sigaction sa;
        memset(&sa, 0, sizeof(sa));
-       sa.sa_handler = application_sigint_handler;
+       sa.sa_handler = ApplicationSigIntHandler;
        sigaction(SIGINT, &sa, NULL);
 #endif /* _WIN32 */
 
@@ -408,29 +367,12 @@ int application_main(int argc, char **argv, Application *instance)
                        result = Application::Instance->Main(args);
                } catch (const Exception& ex) {
                        cerr << "---" << endl;
-
-                       string klass = typeid(ex).name();
-
-#ifdef HAVE_GCC_ABI_DEMANGLE
-                       int status;
-                       char *realname = abi::__cxa_demangle(klass.c_str(), 0, 0, &status);
-
-                       if (realname != NULL) {
-                               klass = string(realname);
-                               free(realname);
-                       }
-#endif /* HAVE_GCC_ABI_DEMANGLE */
-
-                       cerr << "Exception: " << klass << endl;
+                       cerr << "Exception: " << Utility::GetTypeName(ex) << endl;
                        cerr << "Message: " << ex.GetMessage() << endl;
 
                        return EXIT_FAILURE;
                }
        }
 
-       Application::Instance.reset();
-
-       assert(Object::ActiveObjects == 0);
-
        return result;
 }
index 48acddf83baca7c82d1d1d14acec4c4c5ab005a2..164c0d47c7dcb865d3664b9e7089f94dad7984c0 100644 (file)
@@ -15,6 +15,10 @@ private:
        vector<string> m_Arguments;
        bool m_Debugging;
 
+protected:
+       void RunEventLoop(void);
+       string GetExeDirectory(void) const;
+
 public:
        typedef shared_ptr<Application> Ptr;
        typedef weak_ptr<Application> WeakPtr;
@@ -27,36 +31,32 @@ public:
        virtual int Main(const vector<string>& args) = 0;
 
        void SetArguments(const vector<string>& arguments);
-       const vector<string>& GetArguments(void);
+       const vector<string>& GetArguments(void) const;
 
-       void RunEventLoop(void);
-       bool Daemonize(void);
        void Shutdown(void);
 
        void Log(const char *format, ...);
 
-       ConfigHive::Ptr GetConfigHive(void);
+       ConfigHive::Ptr GetConfigHive(void) const;
 
-       shared_ptr<Component> LoadComponent(const string& path, const ConfigObject::Ptr& componentConfig);
+       shared_ptr<Component> LoadComponent(const string& path,
+           const ConfigObject::Ptr& componentConfig);
        void RegisterComponent(shared_ptr<Component> component);
        void UnregisterComponent(shared_ptr<Component> component);
        shared_ptr<Component> GetComponent(const string& name);
        void AddComponentSearchDir(const string& componentDirectory);
 
-       const string& GetExeDirectory(void);
-
        bool IsDebugging(void) const;
-       void SigIntHandler(int signum);
 };
 
-}
+int I2_EXPORT RunApplication(int argc, char **argv, Application *instance);
 
-int I2_EXPORT application_main(int argc, char **argv, icinga::Application *instance);
+}
 
 #define IMPLEMENT_ENTRY_POINT(klass)                                   \
-       int main(int argc, char **argv) {                                       \
-               klass *instance = new klass();                                  \
-               return application_main(argc, argv, instance);  \
+       int main(int argc, char **argv) {                               \
+               klass *instance = new klass();                          \
+               return icinga::RunApplication(argc, argv, instance);    \
        }
 
 #endif /* APPLICATION_H */
index 9cc9a0e79acecc3ba946cf388ff29b31eed57d5b..96447590695f4f18ece3719f2d03365fcae2259c 100644 (file)
@@ -30,6 +30,7 @@
     <ClCompile Include="thread.cpp" />
     <ClCompile Include="timer.cpp" />
     <ClCompile Include="unix.cpp" />
+    <ClCompile Include="utility.cpp" />
     <ClCompile Include="variant.cpp" />
     <ClCompile Include="win32.cpp" />
   </ItemGroup>
@@ -56,6 +57,7 @@
     <ClInclude Include="thread.h" />
     <ClInclude Include="timer.h" />
     <ClInclude Include="unix.h" />
+    <ClInclude Include="utility.h" />
     <ClInclude Include="variant.h" />
     <ClInclude Include="win32.h" />
   </ItemGroup>
index 97f1dd87067c8feebb7baf231986a39606daeff1..ccae322219edee99aa2f6700470f95b5feee862f 100644 (file)
@@ -25,10 +25,12 @@ public:
        virtual void Stop(void) = 0;
 };
 
+typedef Component *(*CreateComponentFunction)(void);
+
 #define EXPORT_COMPONENT(klass) \
        extern "C" I2_EXPORT icinga::Component *CreateComponent(void)   \
-       {                                                                                                               \
-               return new klass();                                                                     \
+       {                                                               \
+               return new klass();                                     \
        }
 
 }
index 03c92b332d1903bfd9ca6b121b57cfe524743dc2..b515dadfeecdb2416fe7966fb220010c5394216d 100644 (file)
@@ -4,6 +4,11 @@
 namespace icinga
 {
 
+/**
+ * CondVar
+ *
+ * A wrapper around OS-specific condition variable functionality.
+ */
 class I2_BASE_API CondVar
 {
 private:
index dfcb3c84be8f7e11e7209bae168415008c4d2c00..dd9658c2f2f6e1ba953ed76f359ad9b3431f98c4 100644 (file)
@@ -29,7 +29,7 @@ public:
 
        Event<EventArgs> OnObjectCreated;
        Event<EventArgs> OnObjectRemoved;
-       Event<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
+       Event<PropertyChangedEventArgs> OnPropertyChanged;
 };
 
 }
index fb43ce3af0efb5d2b2c7fb161d8141663c92b19e..b51c15d8c21a0a0ea1679c16e2fe4704c7e0744e 100644 (file)
@@ -30,7 +30,8 @@ ConfigCollection::Ptr ConfigHive::GetCollection(const string& collection)
        return ci->second;
 }
 
-void ConfigHive::ForEachObject(const string& type, function<int (const EventArgs&)> callback)
+void ConfigHive::ForEachObject(const string& type,
+    function<int (const EventArgs&)> callback)
 {
        CollectionIterator ci = Collections.find(type);
 
index a8654cbd0bdddfa009cc4be49c105dfe7429ce42..e5b3bfb6dd1b00c4e8849f076bbe3b09d7c61542 100644 (file)
@@ -15,14 +15,16 @@ public:
 
        void AddObject(const ConfigObject::Ptr& object);
        void RemoveObject(const ConfigObject::Ptr& object);
-       ConfigObject::Ptr GetObject(const string& collection, const string& name = string());
+       ConfigObject::Ptr GetObject(const string& collection,
+           const string& name = string());
        ConfigCollection::Ptr GetCollection(const string& collection);
 
-       void ForEachObject(const string& type, function<int (const EventArgs&)> callback);
+       void ForEachObject(const string& type,
+           function<int (const EventArgs&)> callback);
 
        Event<EventArgs> OnObjectCreated;
        Event<EventArgs> OnObjectRemoved;
-       Event<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
+       Event<PropertyChangedEventArgs> OnPropertyChanged;
 };
 
 }
index f256f0ec46fe90649aafcc5d69df55ea0a958002..2999a6aa9185a597f4252649ea0d1e809e317068 100644 (file)
@@ -53,7 +53,7 @@ bool ConfigObject::GetReplicated(void) const
        return m_Replicated;
 }
 
-int ConfigObject::PropertyChangedHandler(const DictionaryPropertyChangedEventArgs dpcea)
+int ConfigObject::PropertyChangedHandler(const PropertyChangedEventArgs& dpcea)
 {
        ConfigHive::Ptr hive = m_Hive.lock();
        if (hive) {
index d35e1513b69e7ce913d09377072722044e3d2da4..bea58f0d9affeebc54c7cfb682051deb0bb41a58 100644 (file)
@@ -17,7 +17,7 @@ private:
        string m_Type;
        bool m_Replicated;
 
-       int PropertyChangedHandler(const DictionaryPropertyChangedEventArgs dpcea);
+       int PropertyChangedHandler(const PropertyChangedEventArgs& dpcea);
 
 public:
        typedef shared_ptr<ConfigObject> Ptr;
index beae350fe5e4846c996cf7b2c6b7fa0ceadb2e7f..86ded1fb265a47284f8e5c7d4a840e83dea6ceac 100644 (file)
@@ -18,7 +18,7 @@ int delegate_fwd(int (TObject::*function)(TArgs), weak_ptr<TObject> wref, const
 template<class TObject, class TArgs>
 function<int (TArgs)> bind_weak(int (TObject::*function)(TArgs), const weak_ptr<TObject>& wref)
 {
-       return bind<int>(delegate_fwd<TObject, TArgs>, function, wref, _1);
+       return bind(delegate_fwd<TObject, TArgs>, function, wref, _1);
 }
 
 template<class TObject, class TArgs>
index 965278519b285edfbd3b44a3168cfe74b0fca3a7..6db6d6a5989fe726ebbbe6d46b27ad18b6a17b12 100644 (file)
@@ -25,7 +25,7 @@ void Dictionary::SetProperty(string key, const Variant& value)
 
        m_Data[key] = value;
 
-       DictionaryPropertyChangedEventArgs dpce;
+       PropertyChangedEventArgs dpce;
        dpce.Source = shared_from_this();
        dpce.Property = key;
        dpce.OldValue = oldValue;
index 127f9d6a6c5a67e00f8cb264940619223b37564d..5c16d1dcbce3f8dd45e20f3ced69c864d114c9cc 100644 (file)
@@ -7,7 +7,7 @@ namespace icinga
 typedef map<string, Variant>::const_iterator ConstDictionaryIterator;
 typedef map<string, Variant>::iterator DictionaryIterator;
 
-struct I2_BASE_API DictionaryPropertyChangedEventArgs : public EventArgs
+struct I2_BASE_API PropertyChangedEventArgs : public EventArgs
 {
        string Property;
        Variant OldValue;
@@ -41,7 +41,7 @@ public:
        DictionaryIterator Begin(void);
        DictionaryIterator End(void);
 
-       Event<DictionaryPropertyChangedEventArgs> OnPropertyChanged;
+       Event<PropertyChangedEventArgs> OnPropertyChanged;
 };
 
 }
index a2dbd5e115529621d97bc11a150835a72f514233..725b4a03fc9b7a4de885d057526d39b83c0bf106 100644 (file)
@@ -16,34 +16,24 @@ public:
        typedef function<int (const TArgs&)> DelegateType;
 
 private:
-       list<DelegateType> m_Delegates;
+       vector<DelegateType> m_Delegates;
 
 public:
-       void Hook(const DelegateType& delegate)
-       {
-               m_Delegates.push_front(delegate);
-       }
-
-       void Unhook(const DelegateType& delegate)
-       {
-               m_Delegates.remove(delegate);
-       }
-
        Event<TArgs>& operator +=(const DelegateType& rhs)
        {
-               Hook(rhs);
+               m_Delegates.push_back(rhs);
                return *this;
        }
 
        Event<TArgs>& operator -=(const DelegateType& rhs)
        {
-               Unhook(rhs);
+               m_Delegates.erase(rhs);
                return *this;
        }
 
        void operator()(const TArgs& args)
        {
-               typename list<DelegateType>::iterator prev, i;
+               typename vector<DelegateType>::iterator prev, i;
 
                for (i = m_Delegates.begin(); i != m_Delegates.end(); ) {
                        prev = i;
index f120c5f23a46219d19e76b824cc61a2ab7183502..3dcd96bf9bb5b797b614db378fe5ab3e9cc121f8 100644 (file)
@@ -8,14 +8,39 @@ Exception::Exception(void)
 
 Exception::Exception(const string& message)
 {
-       m_Message = message;
+       SetMessage(message);
 }
 
-Exception::~Exception(void)
+string Exception::GetMessage(void) const
 {
+       return m_Message;
 }
 
-string Exception::GetMessage(void) const
+void Exception::SetMessage(string message)
 {
-       return m_Message;
+       m_Message = message;
+}
+
+#ifdef _WIN32
+string Win32Exception::FormatErrorCode(int code)
+{
+       char *message;
+       string result = "Unknown error.";
+
+       DWORD rc = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
+               FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message,
+               0, NULL);
+
+       if (rc != 0) {
+               result = string(message);
+               LocalFree(message);
+       }
+
+       return result;
 }
+#endif /* _WIN32 */
+
+string PosixException::FormatErrorCode(int code)
+{
+       return strerror(code);
+}
\ No newline at end of file
index 5cb7f41fe22be2cda243a14aa888e10840fcc1ba..b97846c92109808770b43ac024ce1afe2d5afd19 100644 (file)
@@ -4,19 +4,31 @@
 namespace icinga
 {
 
+/**
+ * Exception
+ *
+ * Base class for all exceptions.
+ */
 class I2_BASE_API Exception
 {
 private:
        string m_Message;
 
-public:
-       typedef shared_ptr<Exception> Ptr;
-       typedef weak_ptr<Exception> WeakPtr;
+protected:
+       void SetMessage(string message);
 
+public:
        Exception(void);
        Exception(const string& message);
 
-       virtual ~Exception(void);
+       /**
+        * ~Exception
+        *
+        * Required for RTTI.
+        */
+       virtual ~Exception(void)
+       {
+       }
 
        string GetMessage(void) const;
 };
@@ -25,9 +37,6 @@ public:
        class klass : public Exception                                                                  \
        {                                                                                                                               \
        public:                                                                                                                 \
-               typedef shared_ptr<klass> Ptr;                                                          \
-               typedef weak_ptr<klass> WeakPtr;                                                        \
-                                                                                                                                       \
                inline klass(void) : Exception()                                                        \
                {                                                                                                                       \
                }                                                                                                                       \
@@ -40,6 +49,30 @@ public:
 DEFINE_EXCEPTION_CLASS(NotImplementedException);
 DEFINE_EXCEPTION_CLASS(InvalidArgumentException);
 
+#ifdef _WIN32
+class Win32Exception : public Exception
+{
+public:
+       inline Win32Exception(const string& message, int errorCode)
+       {
+               SetMessage(message + ": " + FormatErrorCode(errorCode));
+       }
+
+       static string FormatErrorCode(int code);
+};
+#endif /* _WIN32 */
+
+class PosixException : public Exception
+{
+public:
+       inline PosixException(const string& message, int errorCode)
+       {
+               SetMessage(message + ": " + FormatErrorCode(errorCode));
+       }
+
+       static string FormatErrorCode(int code);
+};
+
 }
 
 #endif /* EXCEPTION_H */
index 3b0e2a63232ec5f2f339b2af140f01c66de8b88b..96dcbb6b8b657d82103f22d5455d66113b5d024e 100644 (file)
@@ -67,6 +67,7 @@ using namespace std::tr1::placeholders;
 #include "mutex.h"
 #include "condvar.h"
 #include "thread.h"
+#include "utility.h"
 #include "object.h"
 #include "exception.h"
 #include "memory.h"
index d41d8a5a778f5e90d3241dd8418a9995beb8b0a5..b14505ad6303b29d8d466b428461dd4a8abe9b23 100644 (file)
@@ -11,7 +11,7 @@ void *Memory::Allocate(size_t size)
        void *ptr = malloc(size);
 
        if (size != 0 && ptr == NULL)
-               throw OutOfMemoryException();
+               throw OutOfMemoryException("malloc failed.");
 
        return ptr;
 }
@@ -21,7 +21,7 @@ void *Memory::Reallocate(void *ptr, size_t size)
        void *new_ptr = realloc(ptr, size);
 
        if (size != 0 && new_ptr == NULL)
-               throw OutOfMemoryException();
+               throw OutOfMemoryException("realloc failed.");
        
        return new_ptr;
 }
@@ -31,7 +31,7 @@ char *Memory::StrDup(const char *str)
        char *new_str = strdup(str);
 
        if (str == NULL)
-               throw OutOfMemoryException();
+               throw OutOfMemoryException("strdup failed.");
 
        return new_str;
 }
index 70d5d4e25a1f247de024c470265f3d5fde159c9f..6930877c7e8ac579379050137be3800e862b131d 100644 (file)
@@ -6,6 +6,11 @@ namespace icinga
 
 DEFINE_EXCEPTION_CLASS(OutOfMemoryException);
 
+/**
+ * Memory
+ *
+ * Singleton class which implements memory allocation helpers.
+ */
 class I2_BASE_API Memory
 {
 private:
index a0ed1c024e353a8d73f2e456021456c931039baf..8d6c2d11b62a0dd189d6fc54086090aa1b59862e 100644 (file)
@@ -4,6 +4,11 @@
 namespace icinga
 {
 
+/**
+ * Mutex
+ *
+ * A wrapper around OS-specific mutex functionality.
+ */
 class I2_BASE_API Mutex
 {
 private:
index f0d72ee7c095ded7b30f55f9cb6e4bf4ddafcf08..ab402195129821492ec943375bacdf23ff2a1f72 100644 (file)
@@ -1,13 +1,3 @@
 #include "i2-base.h"
 
 using namespace icinga;
-
-unsigned long Object::ActiveObjects;
-
-Object::Object(void) {
-       ActiveObjects++;
-}
-
-Object::~Object(void) {
-       ActiveObjects--;
-}
index ffb609bb87ff071c584403675c3762663feabb65..cfb5e15f5f93d3de0d12b44f5aa92ad277edcacc 100644 (file)
@@ -4,20 +4,29 @@
 namespace icinga
 {
 
+/**
+ * Object
+ *
+ * Base class for all heap-allocated objects. At least one of its methods
+ * has to be virtual for RTTI to work.
+ */
 class I2_BASE_API Object : public enable_shared_from_this<Object>
 {
 private:
-       Object(const Object &other);
+       Object(const Objectother);
 
 protected:
-       Object(void);
-       virtual ~Object(void);
+       inline Object(void)
+       {
+       }
+
+       inline virtual ~Object(void)
+       {
+       }
 
 public:
        typedef shared_ptr<Object> Ptr;
        typedef weak_ptr<Object> WeakPtr;
-
-       static unsigned long ActiveObjects;
 };
 
 template<class T>
@@ -37,6 +46,11 @@ public:
 
 typedef function<Object::Ptr ()> factory_function;
 
+/**
+ * factory<T>
+ *
+ * Returns a new object of type T.
+ */
 template<class T>
 Object::Ptr factory(void)
 {
index 4e5a2f00f6000a75b2419becba5102f02640d628..135eb52506cafe1bd7b8eb2d094b8b4f920ff255 100644 (file)
@@ -58,7 +58,8 @@ void Socket::Close(bool from_dtor)
                closesocket(m_FD);
                m_FD = INVALID_SOCKET;
 
-               /* nobody can possibly have a valid event subscription when the destructor has been called */
+               /* nobody can possibly have a valid event subscription when the
+                  destructor has been called */
                if (!from_dtor) {
                        EventArgs ea;
                        ea.Source = shared_from_this();
@@ -70,62 +71,33 @@ void Socket::Close(bool from_dtor)
                Stop();
 }
 
-string Socket::FormatErrorCode(int code)
-{
-       char *message;
-       string result = "Unknown socket error.";
-
-#ifdef _WIN32
-       if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, code, 0, (char *)&message, 0, NULL) != 0) {
-               result = string(message);
-               LocalFree(message);
-       }
-#else /* _WIN32 */
-       if (code != 0)
-               message = strerror(code);
-
-       result = string(message);
-#endif /* _WIN32 */
-
-       return result;
-}
-
-int Socket::ExceptionEventHandler(const EventArgs& ea)
+void Socket::HandleSocketError(void)
 {
        int opt;
        socklen_t optlen = sizeof(opt);
 
        int rc = getsockopt(GetFD(), SOL_SOCKET, SO_ERROR, (char *)&opt, &optlen);
 
-       if (rc < 0) {
-               Close();
-               return 0;
-       }
-
-       if (opt != 0) {
+       if (rc >= 0 && opt != 0) {
                SocketErrorEventArgs sea;
                sea.Code = opt;
-               sea.Message = FormatErrorCode(sea.Code);
+#ifdef _WIN32
+               sea.Message = Win32Exception::FormatErrorCode(sea.Code);
+#else /* _WIN32 */
+               sea.Message = PosixException::FormatErrorCode(sea.Code);
+#endif /* _WIN32 */
                OnError(sea);
-
-               Close();
        }
 
-       return 0;
+       Close();
+       return;
 }
 
-void Socket::CloseAllSockets(void)
+int Socket::ExceptionEventHandler(const EventArgs& ea)
 {
-       for (Socket::CollectionType::iterator i = Sockets.begin(); i != Sockets.end(); ) {
-               Socket::Ptr socket = i->lock();
-
-               i++;
-
-               if (socket == NULL)
-                       continue;
+       HandleSocketError();
 
-               socket->Close();
-       }
+       return 0;
 }
 
 bool Socket::WantsToRead(void) const
index 1e8d4ff2d54e15dc930234aec81de1854745b2c1..6ac7228f6b9c2123d2f5b5c496eb2227de8526c7 100644 (file)
@@ -19,12 +19,10 @@ private:
 
        int ExceptionEventHandler(const EventArgs& ea);
 
-protected:
-       string FormatErrorCode(int errorCode);
-
 protected:
        Socket(void);
 
+       void HandleSocketError(void);
        void Close(bool from_dtor);
 
 public:
@@ -40,8 +38,6 @@ public:
        void SetFD(SOCKET fd);
        SOCKET GetFD(void) const;
 
-       static void CloseAllSockets(void);
-
        Event<EventArgs> OnReadable;
        Event<EventArgs> OnWritable;
        Event<EventArgs> OnException;
index 0325220f48d27ed272fbdeec64a4a9c5207507a2..ff057a2f61134966206eb05e32772c94fcf31440 100644 (file)
@@ -41,17 +41,7 @@ void TCPClient::Connect(const string& hostname, unsigned short port)
 #else /* _WIN32 */
        if (rc < 0 && errno != EINPROGRESS) {
 #endif /* _WIN32 */
-               SocketErrorEventArgs sea;
-#ifdef _WIN32
-               sea.Code = WSAGetLastError();
-#else /* _WIN32 */
-               sea.Code = errno;
-#endif /* _WIN32 */
-               sea.Message = FormatErrorCode(sea.Code);
-
-               OnError(sea);
-
-               Close();
+               HandleSocketError();
        }
 
        m_PeerHost = hostname;
@@ -95,19 +85,7 @@ int TCPClient::ReadableEventHandler(const EventArgs& ea)
                return 0;
 
        if (rc <= 0) {
-               if (rc < 0) {
-                       SocketErrorEventArgs sea;
-#ifdef _WIN32
-                       sea.Code = WSAGetLastError();
-#else /* _WIN32 */
-                       sea.Code = errno;
-#endif /* _WIN32 */
-                       sea.Message = FormatErrorCode(sea.Code);
-
-                       OnError(sea);
-               }
-
-               Close();
+               HandleSocketError();
                return 0;
        }
 
@@ -127,19 +105,7 @@ int TCPClient::WritableEventHandler(const EventArgs& ea)
        rc = send(GetFD(), (const char *)m_SendQueue->GetReadBuffer(), m_SendQueue->GetSize(), 0);
 
        if (rc <= 0) {
-               if (rc < 0) {
-                       SocketErrorEventArgs sea;
-#ifdef _WIN32
-                       sea.Code = WSAGetLastError();
-#else /* _WIN32 */
-                       sea.Code = errno;
-#endif /* _WIN32 */
-                       sea.Message = FormatErrorCode(sea.Code);
-
-                       OnError(sea);
-               }
-
-               Close();
+               HandleSocketError();
                return 0;
        }
 
index b4b7f7b70014f0b3963b7748afa8927c6cfab72d..2f5f03f5297f9788466ada0ed6b515f89a23f2e7 100644 (file)
@@ -42,18 +42,9 @@ int TCPServer::ReadableEventHandler(const EventArgs& ea)
 
        fd = accept(GetFD(), (sockaddr *)&addr, &addrlen);
 
-       if (fd == INVALID_SOCKET) {
-               SocketErrorEventArgs sea;
-#ifdef _WIN32
-               sea.Code = WSAGetLastError();
-#else /* _WIN32 */
-               sea.Code = errno;
-#endif /* _WIN32 */
-               sea.Message = FormatErrorCode(sea.Code);
-
-               OnError(sea);
-
-               Close();
+       if (fd < 0) {
+               HandleSocketError();
+               return 0;
        }
 
        NewClientEventArgs nea;
index 1d8b6dcbac0e430be135b3b428e290b2e00c1ba7..f1120ce5b0545be096e9c72bea651da696ab560f 100644 (file)
@@ -9,14 +9,9 @@ void TCPSocket::MakeSocket(void)
        int fd = socket(AF_INET, SOCK_STREAM, 0);
 
        if (fd == INVALID_SOCKET) {
-               SocketErrorEventArgs sea;
-#ifdef _WIN32
-               sea.Code = WSAGetLastError();
-#else /* _WIN32 */
-               sea.Code = errno;
-#endif /* _WIN32 */
-               sea.Message = FormatErrorCode(sea.Code);
-               OnError(sea);
+               HandleSocketError();
+
+               return;
        }
 
        SetFD(fd);
@@ -40,19 +35,6 @@ void TCPSocket::Bind(const char *hostname, unsigned short port)
        sin.sin_addr.s_addr = hostname ? inet_addr(hostname) : htonl(INADDR_ANY);
        sin.sin_port = htons(port);
 
-       int rc = ::bind(GetFD(), (sockaddr *)&sin, sizeof(sin));
-
-       if (rc < 0) {
-               SocketErrorEventArgs sea;
-#ifdef _WIN32
-               sea.Code = WSAGetLastError();
-#else /* _WIN32 */
-               sea.Code = errno;
-#endif /* _WIN32 */
-               sea.Message = FormatErrorCode(sea.Code);
-
-               OnError(sea);
-               
-               Close();
-       }
+       if (::bind(GetFD(), (sockaddr *)&sin, sizeof(sin)) < 0)
+               HandleSocketError();
 }
index 342c9130088e51d4deea991728cfb0d03e9d17d4..dcc3d00faecd06adced14155f1823127ad0e2833 100644 (file)
@@ -8,6 +8,12 @@ typedef struct threadparam_s
        void *param;
 } threadparam_t;
 
+/**
+ * ThreadStartProc
+ *
+ * Helper function that deals with OS-specific differences in the thread
+ * proc's function signature.
+ */
 #ifdef _WIN32
 static DWORD WINAPI ThreadStartProc(LPVOID param)
 {
@@ -26,21 +32,35 @@ static void *ThreadStartProc(void *param)
 }
 #endif /* _WIN32 */
 
-
+/**
+ * Thread
+ *
+ * Constructor for the thread class. Creates a new thread that begins
+ * executing immediately.
+ */
 Thread::Thread(ThreadProc callback)
 {
        threadparam_t *tparam = new threadparam_t();
 
        if (tparam == NULL)
-               throw exception(/*"Out of memory"*/);
+               throw OutOfMemoryException("Out of memory");
 
 #ifdef _WIN32
        m_Thread = CreateThread(NULL, 0, ThreadStartProc, tparam, CREATE_SUSPENDED, NULL);
+
+       if (m_Thread == NULL)
+               throw Win32Exception("CreateThread failed.", GetLastError());
 #else /* _WIN32 */
        pthread_create(&m_Thread, NULL, ThreadStartProc, &tparam);
 #endif /* _WIN32 */
 }
 
+/**
+ * ~Thread
+ *
+ * Destructor for the Thread class. Cleans up the resources associated
+ * with the thread.
+ */
 Thread::~Thread(void)
 {
 #ifdef _WIN32
@@ -50,6 +70,11 @@ Thread::~Thread(void)
 #endif
 }
 
+/**
+ * Join
+ *
+ * Waits until the thread has finished executing.
+ */
 void Thread::Join(void)
 {
 #ifdef _WIN32
index 58c11aa056dc66e62ec7769ecf31e38e95a0dafd..8a6cba1068a3e5572d1b5b794f6b6d17cd206eec 100644 (file)
@@ -6,6 +6,11 @@ namespace icinga
 
 typedef void (*ThreadProc)(void *);
 
+/**
+ * Thread
+ *
+ * A wrapper around OS-specific thread functionality.
+ */
 class I2_BASE_API Thread
 {
 private:
@@ -16,10 +21,9 @@ private:
 #endif
 
 public:
-       Thread(void (*callback)(void *));
+       Thread(ThreadProc callback);
        ~Thread(void);
 
-       void Start(void);
        void Join(void);
 };
 
index b7b9f034178b66834deba5e907234c52f4344863..d9d1872857080d96711b00b6c64fda8f652e0ffe 100644 (file)
@@ -61,20 +61,6 @@ void Timer::CallExpiredTimers(void)
        }
 }
 
-void Timer::StopAllTimers(void)
-{
-       for (Timer::CollectionType::iterator i = Timers.begin(); i != Timers.end(); ) {
-               Timer::Ptr timer = i->lock();
-
-               i++;
-
-               if (timer == NULL)
-                       continue;
-
-               timer->Stop();
-       }
-}
-
 /* Note: the timer delegate must not call Disable() on any other timers than
  * the timer that originally invoked the delegate */
 void Timer::Call(void)
index 22ee8c53daf6dc87916b9e67b3e12e3dbc4a12e0..cd57dae80929ac46fc1f5d9ff041b1a71fd424f8 100644 (file)
@@ -44,7 +44,6 @@ public:
 
        static time_t GetNextCall(void);
        static void CallExpiredTimers(void);
-       static void StopAllTimers(void);
 
        void Start(void);
        void Stop(void);
diff --git a/base/utility.cpp b/base/utility.cpp
new file mode 100644 (file)
index 0000000..167f703
--- /dev/null
@@ -0,0 +1,48 @@
+#include "i2-base.h"
+
+using namespace icinga;
+
+/**
+ * Daemonize
+ *
+ * Detaches from the controlling terminal.
+ */
+void Utility::Daemonize(void) {
+#ifndef _WIN32
+       pid_t pid;
+       pid_t sid;
+       int fd;
+
+       pid = fork();
+       if (pid == -1) {
+               return false;
+       }
+
+       if (pid)
+               exit(0);
+
+       fd = open("/dev/null", O_RDWR);
+       if (fd) {
+               if (fd != 0) {
+                       dup2(fd, 0);
+               }
+
+               if (fd != 1) {
+                       dup2(fd, 1);
+               }
+
+               if (fd != 2) {
+                       dup2(fd, 2);
+               }
+
+               if (fd > 2) {
+                       close(fd);
+               }
+       }
+
+       sid = setsid();
+       if (sid == -1) {
+               return false;
+       }
+#endif
+}
diff --git a/base/utility.h b/base/utility.h
new file mode 100644 (file)
index 0000000..d4c7f84
--- /dev/null
@@ -0,0 +1,46 @@
+#ifndef UTILITY_H
+#define UTILITY_H
+
+namespace icinga
+{
+
+/**
+ * Utility
+ *
+ * Utility functions.
+ */
+class I2_BASE_API Utility
+{
+private:
+       Utility(void);
+
+public:
+       /**
+        * GetTypeName
+        *
+        * Returns the type name of an object (using RTTI).
+        */
+       template<class T>
+       static string GetTypeName(const T& value)
+       {
+               string klass = typeid(value).name();
+
+#ifdef HAVE_GCC_ABI_DEMANGLE
+               int status;
+               char *realname = abi::__cxa_demangle(klass.c_str(), 0, 0, &status);
+
+               if (realname != NULL) {
+                       klass = string(realname);
+                       free(realname);
+               }
+#endif /* HAVE_GCC_ABI_DEMANGLE */
+
+               return klass;
+       }
+
+       static void Daemonize(void);
+};
+
+}
+
+#endif /* UTILITY_H */
index 365568de8f1d93069af2a3858b0e2bcacc04b649..9a2f66f96e078f67b014fd73c31f1febfc381caf 100644 (file)
@@ -146,7 +146,7 @@ int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea)
        return 0;
 }
 
-int ConfigRpcComponent::LocalPropertyChangedHandler(const DictionaryPropertyChangedEventArgs& ea)
+int ConfigRpcComponent::LocalPropertyChangedHandler(const PropertyChangedEventArgs& ea)
 {
        ConfigObject::Ptr object = static_pointer_cast<ConfigObject>(ea.Source);
        
index f86af142db86ee6ad4525fac609300d6025e40d1..2f1085bcd8e325c51d1b17e627aab96c77d41c24 100644 (file)
@@ -16,7 +16,7 @@ private:
 
        int LocalObjectCreatedHandler(const EventArgs& ea);
        int LocalObjectRemovedHandler(const EventArgs& ea);
-       int LocalPropertyChangedHandler(const DictionaryPropertyChangedEventArgs& ea);
+       int LocalPropertyChangedHandler(const PropertyChangedEventArgs& ea);
 
        int FetchObjectsHandler(const NewRequestEventArgs& ea);
        int RemoteObjectUpdatedHandler(const NewRequestEventArgs& ea);