#endif
m_ShuttingDown = false;
+ m_ConfigHive = new_object<ConfigHive>();
}
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)
{
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
+}
#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;
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>
</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" />
--- /dev/null
+#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();
+}
--- /dev/null
+#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 */
--- /dev/null
+#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;
+}
--- /dev/null
+#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 */
--- /dev/null
+#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);
+}
--- /dev/null
+#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 */
{
shared_ptr<TObject> ref = wref.lock();
- if (ref == NULL)
+ if (ref.get() == NULL)
return -1;
return (ref.get()->*function)(args);
#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
#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
#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