]> granicus.if.org Git - icinga2/commitdiff
Added support for configuration and loadable components.
authorGunnar Beutner <gunnar.beutner@netways.de>
Sat, 31 Mar 2012 13:18:09 +0000 (15:18 +0200)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sat, 31 Mar 2012 13:18:30 +0000 (15:18 +0200)
13 files changed:
base/application.cpp
base/application.h
base/base.vcxproj
base/component.cpp [new file with mode: 0644]
base/component.h [new file with mode: 0644]
base/confighive.cpp [new file with mode: 0644]
base/confighive.h [new file with mode: 0644]
base/configobject.cpp [new file with mode: 0644]
base/configobject.h [new file with mode: 0644]
base/delegate.h
base/i2-base.h
base/unix.h
base/win32.h

index efd6badf423f330b251500ec64a52e9d29b7b94b..fa96db0012b903b725887d103f07948b41b55811 100644 (file)
@@ -12,6 +12,7 @@ Application::Application(void)
 #endif
 
        m_ShuttingDown = false;
+       m_ConfigHive = new_object<ConfigHive>();
 }
 
 Application::~Application(void)
@@ -22,6 +23,10 @@ Application::~Application(void)
 #ifdef _WIN32
        WSACleanup();
 #endif
+
+       for (map<string, Component::RefType>::iterator i = m_Components.begin(); i != m_Components.end(); i++) {
+               i->second->Stop();
+       }
 }
 
 void Application::RunEventLoop(void)
@@ -156,3 +161,62 @@ void Application::Shutdown(void)
 {
        m_ShuttingDown = true;
 }
+
+ConfigHive::RefType Application::GetConfigHive(void)
+{
+       return m_ConfigHive;
+}
+
+Component::RefType Application::LoadComponent(string name)
+{
+       Component::RefType component;
+       Component *(*pCreateComponent)();
+
+       ConfigObject::RefType componentConfig = m_ConfigHive->GetObject("component", name);
+
+       if (componentConfig.get() == NULL) {
+               componentConfig = new_object<ConfigObject>();
+               componentConfig->SetName(name);
+               componentConfig->SetType("component");
+               m_ConfigHive->AddObject(componentConfig);
+       }
+
+       string path = componentConfig->GetProperty("path", name);
+
+#ifdef _WIN32
+       HMODULE hModule = LoadLibrary(path.c_str());
+
+       if (hModule == INVALID_HANDLE_VALUE)
+               throw exception(/*"Could not load module"*/);
+
+       pCreateComponent = (Component *(*)())GetProcAddress(hModule, "CreateComponent");
+
+       if (pCreateComponent == NULL)
+               throw exception(/*"Module does not contain CreateComponent function"*/);
+
+#else /* _WIN32 */
+       // TODO: implement
+#endif /* _WIN32 */
+
+       component = Component::RefType(pCreateComponent());
+       component->SetApplication(static_pointer_cast<Application>(shared_from_this()));
+       m_Components[component->GetName()] = component;
+
+       component->Start(componentConfig);
+
+       return component;
+}
+
+void Application::UnloadComponent(string name)
+{
+       map<string, Component::RefType>::iterator ci = m_Components.find(name);
+
+       if (ci == m_Components.end())
+               return;
+
+       Component::RefType component = ci->second;
+       component->Stop();
+       m_Components.erase(ci);
+
+       // TODO: unload DLL
+}
index d665319de899f2582650308fb544db0b2f30888d..110f5dd081736509ee226399a1f4d546c1398273 100644 (file)
@@ -1,14 +1,21 @@
 #ifndef I2_APPLICATION_H
 #define I2_APPLICATION_H
 
+#include <map>
+
 namespace icinga {
 
 using std::vector;
+using std::map;
 using std::string;
 
+class Component;
+
 class Application : public Object {
 private:
        bool m_ShuttingDown;
+       ConfigHive::RefType m_ConfigHive;
+       map< string, shared_ptr<Component> > m_Components;
 
 public:
        typedef shared_ptr<Application> RefType;
@@ -24,6 +31,12 @@ public:
        void RunEventLoop(void);
        bool Daemonize(void);
        void Shutdown(void);
+
+       ConfigHive::RefType GetConfigHive(void);
+
+       shared_ptr<Component> LoadComponent(string name);
+       void UnloadComponent(string name);
+       shared_ptr<Component> GetComponent(string name);
 };
 
 template<class T>
index a22f34a7499e0d27adf85e646951574dd1ff25dd..e78a762b6a728da86658bbae3554262fa9a7b026 100644 (file)
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="application.cpp" />
+    <ClCompile Include="component.cpp" />
+    <ClCompile Include="condvar.cpp" />
+    <ClCompile Include="confighive.cpp" />
+    <ClCompile Include="configobject.cpp" />
     <ClCompile Include="fifo.cpp" />
     <ClCompile Include="memory.cpp" />
+    <ClCompile Include="mutex.cpp" />
     <ClCompile Include="object.cpp" />
     <ClCompile Include="socket.cpp" />
     <ClCompile Include="tcpclient.cpp" />
     <ClCompile Include="tcpserver.cpp" />
     <ClCompile Include="tcpsocket.cpp" />
+    <ClCompile Include="thread.cpp" />
     <ClCompile Include="timer.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="application.h" />
+    <ClInclude Include="component.h" />
+    <ClInclude Include="condvar.h" />
+    <ClInclude Include="confighive.h" />
+    <ClInclude Include="configobject.h" />
     <ClInclude Include="delegate.h" />
     <ClInclude Include="event.h" />
     <ClInclude Include="fifo.h" />
     <ClInclude Include="tcpclient.h" />
     <ClInclude Include="tcpserver.h" />
     <ClInclude Include="tcpsocket.h" />
+    <ClInclude Include="thread.h" />
     <ClInclude Include="timer.h" />
     <ClInclude Include="unix.h" />
     <ClInclude Include="win32.h" />
   </ItemGroup>
+  <ItemGroup>
+    <None Include="mutex.h" />
+  </ItemGroup>
   <PropertyGroup Label="Globals">
     <ProjectGuid>{9C92DA90-FD53-43A9-A244-90F2E8AF9677}</ProjectGuid>
     <Keyword>Win32Proj</Keyword>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
     <ConfigurationType>StaticLibrary</ConfigurationType>
     <UseDebugLibraries>true</UseDebugLibraries>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
   </PropertyGroup>
   <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
     <UseDebugLibraries>false</UseDebugLibraries>
     <WholeProgramOptimization>true</WholeProgramOptimization>
-    <CharacterSet>Unicode</CharacterSet>
+    <CharacterSet>MultiByte</CharacterSet>
     <ConfigurationType>StaticLibrary</ConfigurationType>
   </PropertyGroup>
   <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
diff --git a/base/component.cpp b/base/component.cpp
new file mode 100644 (file)
index 0000000..d18d9a2
--- /dev/null
@@ -0,0 +1,13 @@
+#include "i2-base.h"
+
+using namespace icinga;
+
+void Component::SetApplication(Application::WeakRefType application)
+{
+       m_Application = application;
+}
+
+Application::RefType Component::GetApplication(void)
+{
+       return m_Application.lock();
+}
diff --git a/base/component.h b/base/component.h
new file mode 100644 (file)
index 0000000..9b979e9
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef I2_COMPONENT_H
+#define I2_COMPONENT_H
+
+namespace icinga
+{
+
+class Component : public Object
+{
+private:
+       Application::WeakRefType m_Application;
+
+public:
+       typedef shared_ptr<Component> RefType;
+       typedef weak_ptr<Component> WeakRefType;
+
+       void SetApplication(Application::WeakRefType application);
+       Application::RefType GetApplication(void);
+
+       virtual string GetName(void) = 0;
+       virtual void Start(ConfigObject::RefType componentConfig) = 0;
+       virtual void Stop(void) = 0;
+};
+
+#define EXPORT_COMPONENT(klass) \
+       extern "C" I2_EXPORT icinga::Component *CreateComponent(void)   \
+       {                                                                                                               \
+               return new klass();                                                                     \
+       }
+
+}
+
+#endif /* I2_COMPONENT_H */
diff --git a/base/confighive.cpp b/base/confighive.cpp
new file mode 100644 (file)
index 0000000..7c454a1
--- /dev/null
@@ -0,0 +1,55 @@
+#include "i2-base.h"
+
+using namespace icinga;
+
+void ConfigHive::AddObject(ConfigObject::RefType object)
+{
+       string type = object->GetType();
+       TypeIterator ti = Objects.find(type);
+
+       if (ti == Objects.end()) {
+               Objects[type] = map<string, ConfigObject::RefType>();
+               ti = Objects.find(type);
+       }
+
+       object->SetHive(static_pointer_cast<ConfigHive>(shared_from_this()));
+
+       string name = object->GetName();
+       ti->second[name] = object;
+
+       ConfigHiveEventArgs::RefType ea = new_object<ConfigHiveEventArgs>();
+       ea->Source = shared_from_this();
+       ea->ConfigObject = object;
+       OnObjectCreated(ea);
+}
+
+void ConfigHive::RemoveObject(ConfigObject::RefType object)
+{
+       string type = object->GetType();
+       TypeIterator ti = Objects.find(type);
+
+       if (ti == Objects.end())
+               return;
+
+       ti->second.erase(object->GetName());
+
+       ConfigHiveEventArgs::RefType ea = new_object<ConfigHiveEventArgs>();
+       ea->Source = shared_from_this();
+       ea->ConfigObject = object;
+       OnObjectRemoved(ea);
+}
+
+ConfigObject::RefType ConfigHive::GetObject(const string& type, const string& name)
+{
+       ConfigHive::TypeIterator ti = Objects.find(type);
+
+       if (ti == Objects.end())
+               return ConfigObject::RefType();
+
+       ConfigHive::ObjectIterator oi = ti->second.find(name);
+
+       if (oi == ti->second.end())
+               return ConfigObject::RefType();
+
+       return oi->second;
+}
diff --git a/base/confighive.h b/base/confighive.h
new file mode 100644 (file)
index 0000000..5286077
--- /dev/null
@@ -0,0 +1,42 @@
+#ifndef I2_CONFIGHIVE_H
+#define I2_CONFIGHIVE_H
+
+#include <map>
+
+namespace icinga
+{
+
+using std::map;
+
+struct ConfigHiveEventArgs : public EventArgs
+{
+       typedef shared_ptr<ConfigHiveEventArgs> RefType;
+       typedef weak_ptr<ConfigHiveEventArgs> WeakRefType;
+
+       ConfigObject::RefType ConfigObject;
+       string Property;
+       string OldValue;
+};
+
+class ConfigHive : public Object
+{
+public:
+       typedef shared_ptr<ConfigHive> RefType;
+       typedef weak_ptr<ConfigHive> WeakRefType;
+
+       typedef map< string, map<string, ConfigObject::RefType> >::iterator TypeIterator;
+       typedef map<string, ConfigObject::RefType>::iterator ObjectIterator;
+       map< string, map<string, ConfigObject::RefType> > Objects;
+
+       void AddObject(ConfigObject::RefType object);
+       void RemoveObject(ConfigObject::RefType object);
+       ConfigObject::RefType GetObject(const string& type, const string& name = string());
+
+       event<ConfigHiveEventArgs::RefType> OnObjectCreated;
+       event<ConfigHiveEventArgs::RefType> OnObjectRemoved;
+       event<ConfigHiveEventArgs::RefType> OnPropertyChanged;
+};
+
+}
+
+#endif /* I2_CONFIGHIVE_H */
diff --git a/base/configobject.cpp b/base/configobject.cpp
new file mode 100644 (file)
index 0000000..94834f0
--- /dev/null
@@ -0,0 +1,74 @@
+#include "i2-base.h"
+
+using namespace icinga;
+
+void ConfigObject::SetHive(const ConfigHive::WeakRefType& hive)
+{
+       m_Hive = hive;
+}
+
+ConfigHive::WeakRefType ConfigObject::GetHive(void) const
+{
+       return m_Hive;
+}
+
+void ConfigObject::SetName(const string& name)
+{
+       m_Name = name;
+}
+
+string ConfigObject::GetName(void) const
+{
+       return m_Name;
+}
+
+void ConfigObject::SetType(const string& type)
+{
+       m_Type = type;
+}
+
+string ConfigObject::GetType(void) const
+{
+       return m_Type;
+}
+
+void ConfigObject::SetProperty(const string& name, const string& value)
+{
+       string oldValue = GetProperty(name);
+
+       Properties[name] = value;
+
+       ConfigHive::RefType hive = m_Hive.lock();
+       if (hive.get() != NULL) {
+               ConfigHiveEventArgs::RefType ea = new_object<ConfigHiveEventArgs>();
+               ea->Source = hive;
+               ea->ConfigObject = static_pointer_cast<ConfigObject>(shared_from_this());
+               ea->Property = name;
+               ea->OldValue = oldValue;
+               hive->OnPropertyChanged(ea);
+       }
+}
+
+string ConfigObject::GetProperty(const string& name, const string& default) const
+{
+       map<string, string>::const_iterator vi = Properties.find(name);
+       if (vi == Properties.end())
+               return default;
+       return vi->second;
+}
+
+int ConfigObject::GetPropertyInteger(const string& name, int default) const
+{
+       string value = GetProperty(name);
+       if (value == string())
+               return default;
+       return strtol(value.c_str(), NULL, 10);
+}
+
+double ConfigObject::GetPropertyDouble(const string& name, double default) const
+{
+       string value = GetProperty(name);
+       if (value == string())
+               return default;
+       return strtod(value.c_str(), NULL);
+}
diff --git a/base/configobject.h b/base/configobject.h
new file mode 100644 (file)
index 0000000..939af74
--- /dev/null
@@ -0,0 +1,49 @@
+#ifndef I2_CONFIGOBJECT_H
+#define I2_CONFIGOBJECT_H
+
+#include <map>
+
+namespace icinga
+{
+
+using std::map;
+using std::string;
+
+class ConfigHive;
+
+class ConfigObject : public Object
+{
+private:
+       weak_ptr<ConfigHive> m_Hive;
+
+       string m_Name;
+       string m_Type;
+
+public:
+       typedef shared_ptr<ConfigObject> RefType;
+       typedef weak_ptr<ConfigObject> WeakRefType;
+
+       typedef map<string, string>::iterator ParameterIterator;
+       map<string, string> Properties;
+
+       void SetHive(const weak_ptr<ConfigHive>& name);
+       weak_ptr<ConfigHive> GetHive(void) const;
+
+       void SetName(const string& name);
+       string GetName(void) const;
+
+       void SetType(const string& type);
+       string GetType(void) const;
+
+       void SetProperty(const string& name, const string& value);
+       void SetPropertyInteger(const string& name, int value);
+       void SetPropertyDouble(const string& name, double value);
+
+       string GetProperty(const string& name, const string& default = string()) const;
+       int GetPropertyInteger(const string& name, int default = 0) const;
+       double GetPropertyDouble(const string& name, double default = 0.0f) const;
+};
+
+}
+
+#endif /* I2_CONFIGOBJECT_H */
index 3d86c89fc4f613dccbb7e0847e610ba4be4af268..232291abba02887c7dcd92aa4f218f53f49fafc5 100644 (file)
@@ -14,7 +14,7 @@ int delegate_fwd(int (TObject::*function)(TArgs), weak_ptr<TObject> wref, const
 {
        shared_ptr<TObject> ref = wref.lock();
 
-       if (ref == NULL)
+       if (ref.get() == NULL)
                return -1;
 
        return (ref.get()->*function)(args);
index c0c302a6f2ddfea57634220cf17cc9fbf29b005d..d2667160426c84c08c0a13ef858666dbc37f07ba 100644 (file)
@@ -46,6 +46,9 @@
 #include "tcpsocket.h"
 #include "tcpclient.h"
 #include "tcpserver.h"
+#include "configobject.h"
+#include "confighive.h"
 #include "application.h"
+#include "component.h"
 
 #endif /* I2_BASE_H */
\ No newline at end of file
index 507e18a995c7035b85c61debe22ac2fa599d4e4b..f9d9e88b08ef8e1dc7126867c0512f8099527daa 100644 (file)
@@ -25,4 +25,8 @@ inline void closesocket(int fd)
 
 #define ioctlsocket ioctl
 
+/* default visibility takes care of exported symbols */
+#define I2_EXPORT
+#define I2_IMPORT
+
 #endif /* I2_UNIX_H */
\ No newline at end of file
index 4554b1c681811e20c5da182365499dd59eed212a..a75c521a61e4869eb57bdb588fd4943221b31de6 100644 (file)
@@ -1,9 +1,13 @@
 #ifndef I2_WIN32_H
 #define I2_WIN32_H
 
+#define NOGDI
 #include <windows.h>
 #include <imagehlp.h>
 
 typedef int socklen_t;
 
+#define I2_EXPORT __declspec(dllexport)
+#define I2_IMPORT __declspec(dllimport)
+
 #endif /* I2_WIN32_H */
\ No newline at end of file