From d45bcf99b1306fa5980d191b26a9cb3e896d2f0c Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Tue, 12 Jun 2012 09:36:18 +0200 Subject: [PATCH] Integrate libdyn with the configfile component. --- base/Makefile.am | 8 +- base/application.cpp | 12 - base/application.h | 3 - base/base.vcxproj | 8 +- base/configcollection.cpp | 109 --------- base/configcollection.h | 62 ----- base/confighive.cpp | 93 -------- base/confighive.h | 56 ----- base/configobject.cpp | 215 +++++++++++------- base/configobject.h | 80 +++++-- base/i2-base.h | 4 +- {dyn => base}/objectmap.cpp | 2 +- {dyn => base}/objectmap.h | 15 +- {dyn => base}/objectset.cpp | 2 +- {dyn => base}/objectset.h | 13 +- components/configfile/Makefile.am | 7 +- components/configfile/configfile.vcxproj | 8 +- components/configfile/configfilecomponent.cpp | 64 +----- components/configfile/configfilecomponent.h | 2 +- components/configfile/i2-configfile.h | 2 +- components/configrpc/configrpc.vcxproj | 4 +- components/configrpc/configrpccomponent.cpp | 58 ++--- components/configrpc/configrpccomponent.h | 13 +- components/discovery/discovery.vcxproj | 4 +- components/discovery/discoverycomponent.cpp | 57 ++--- components/discovery/discoverycomponent.h | 2 - dyn/Makefile.am | 8 +- dyn/configitem.cpp | 27 +-- dyn/configitem.h | 2 +- dyn/dyn.vcxproj | 6 - dyn/dynamicobject.cpp | 118 ---------- dyn/dynamicobject.h | 60 ----- dyn/i2-dyn.h | 3 - dyntest/dyntest.cpp | 2 +- icinga/icingaapplication.cpp | 106 +++------ icinga/icingaapplication.h | 16 +- icinga/icingacomponent.cpp | 10 - icinga/icingacomponent.h | 1 - 38 files changed, 334 insertions(+), 928 deletions(-) delete mode 100644 base/configcollection.cpp delete mode 100644 base/configcollection.h delete mode 100644 base/confighive.cpp delete mode 100644 base/confighive.h rename {dyn => base}/objectmap.cpp (98%) rename {dyn => base}/objectmap.h (91%) rename {dyn => base}/objectset.cpp (98%) rename {dyn => base}/objectset.h (93%) delete mode 100644 dyn/dynamicobject.cpp delete mode 100644 dyn/dynamicobject.h diff --git a/base/Makefile.am b/base/Makefile.am index e6cefbe3f..a3455a0c8 100644 --- a/base/Makefile.am +++ b/base/Makefile.am @@ -9,10 +9,6 @@ libbase_la_SOURCES = \ application.h \ component.cpp \ component.h \ - configcollection.cpp \ - configcollection.h \ - confighive.cpp \ - confighive.h \ configobject.cpp \ configobject.h \ cxx11-compat.h \ @@ -27,6 +23,10 @@ libbase_la_SOURCES = \ i2-base.h \ object.cpp \ object.h \ + objectset.cpp \ + objectset.h \ + objectmap.cpp \ + objectmap.h \ socket.cpp \ socket.h \ tcpclient.cpp \ diff --git a/base/application.cpp b/base/application.cpp index 0e6a5753c..77f5dde84 100644 --- a/base/application.cpp +++ b/base/application.cpp @@ -49,8 +49,6 @@ Application::Application(void) if (IsDebuggerPresent()) m_Debugging = true; #endif /* _WIN32 */ - - m_ConfigHive = make_shared(); } /** @@ -197,16 +195,6 @@ void Application::Shutdown(void) m_ShuttingDown = true; } -/** - * Returns the application's configuration hive. - * - * @returns The config hive. - */ -ConfigHive::Ptr Application::GetConfigHive(void) const -{ - return m_ConfigHive; -} - /** * Loads a component from a shared library. * diff --git a/base/application.h b/base/application.h index befeeff06..958608d24 100644 --- a/base/application.h +++ b/base/application.h @@ -47,8 +47,6 @@ public: static void Log(string message); - ConfigHive::Ptr GetConfigHive(void) const; - shared_ptr LoadComponent(const string& path, const ConfigObject::Ptr& componentConfig); void RegisterComponent(shared_ptr component); @@ -67,7 +65,6 @@ private: static bool m_ShuttingDown; /**< Whether the application is in the process of shutting down. */ - ConfigHive::Ptr m_ConfigHive; /**< The application's configuration. */ map< string, shared_ptr > m_Components; /**< Components that were loaded by the application. */ vector m_Arguments; /**< Command-line arguments */ diff --git a/base/base.vcxproj b/base/base.vcxproj index 51871ee7b..f7e4cc647 100644 --- a/base/base.vcxproj +++ b/base/base.vcxproj @@ -13,13 +13,13 @@ - - + + @@ -34,12 +34,12 @@ - - + + diff --git a/base/configcollection.cpp b/base/configcollection.cpp deleted file mode 100644 index 483a50dbb..000000000 --- a/base/configcollection.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#include "i2-base.h" - -using namespace icinga; - -/** - * Sets the hive this collection belongs to. - * - * @param hive The hive. - */ -void ConfigCollection::SetHive(const ConfigHive::WeakPtr& hive) -{ - m_Hive = hive; -} - -/** - * Retrieves the hive this collection belongs to. - * - * @returns The hive. - */ -ConfigHive::WeakPtr ConfigCollection::GetHive(void) const -{ - return m_Hive; -} - -/** - * Adds a new object to this collection. - * - * @param object The new object. - */ -void ConfigCollection::AddObject(const ConfigObject::Ptr& object) -{ - RemoveObject(object); - - Objects[object->GetName()] = object; - object->Commit(); -} - -/** - * Removes an object from this collection - * - * @param object The object that is to be removed. - */ -void ConfigCollection::RemoveObject(const ConfigObject::Ptr& object) -{ - ObjectIterator oi = Objects.find(object->GetName()); - - if (oi != Objects.end()) { - Objects.erase(oi); - - EventArgs ea; - ea.Source = object; - OnObjectRemoved(ea); - - ConfigHive::Ptr hive = m_Hive.lock(); - if (hive) - hive->OnObjectRemoved(ea); - } -} - -/** - * Retrieves an object by name. - * - * @param name The name of the object. - * @returns The object or a null pointer if the specified object - * could not be found. - */ -ConfigObject::Ptr ConfigCollection::GetObject(const string& name) const -{ - ObjectConstIterator oi = Objects.find(name); - - if (oi == Objects.end()) - return ConfigObject::Ptr(); - - return oi->second; -} - -/** - * Invokes the specified callback for each object contained in this collection. - * - * @param callback The callback. - */ -void ConfigCollection::ForEachObject(function callback) -{ - EventArgs ea; - - for (ObjectIterator oi = Objects.begin(); oi != Objects.end(); oi++) { - ea.Source = oi->second; - callback(ea); - } -} diff --git a/base/configcollection.h b/base/configcollection.h deleted file mode 100644 index 4f448faa6..000000000 --- a/base/configcollection.h +++ /dev/null @@ -1,62 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#ifndef CONFIGCOLLECTION_H -#define CONFIGCOLLECTION_H - -namespace icinga -{ - -class ConfigHive; - -/** - * A collection of configuration objects that each have the same type. - * - * @ingroup base - */ -class I2_BASE_API ConfigCollection : public Object -{ -public: - typedef shared_ptr Ptr; - typedef weak_ptr WeakPtr; - - typedef map::iterator ObjectIterator; - typedef map::const_iterator ObjectConstIterator; - map Objects; - - void SetHive(const weak_ptr& hive); - weak_ptr GetHive(void) const; - - void AddObject(const ConfigObject::Ptr& object); - void RemoveObject(const ConfigObject::Ptr& object); - ConfigObject::Ptr GetObject(const string& name = string()) const; - - void ForEachObject(function callback); - - Observable OnObjectCommitted; - Observable OnObjectRemoved; - -private: - weak_ptr m_Hive; - -}; - -} - -#endif /* CONFIGCOLLECTION_H */ diff --git a/base/confighive.cpp b/base/confighive.cpp deleted file mode 100644 index 37a4e6330..000000000 --- a/base/confighive.cpp +++ /dev/null @@ -1,93 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#include "i2-base.h" - -using namespace icinga; - -/** - * Adds a new object to this hive. - * - * @param object The new object. - */ -void ConfigHive::AddObject(const ConfigObject::Ptr& object) -{ - object->SetHive(static_pointer_cast(shared_from_this())); - GetCollection(object->GetType())->AddObject(object); -} - -/** - * Removes an object from this hive. - * - * @param object The object that is to be removed. - */ -void ConfigHive::RemoveObject(const ConfigObject::Ptr& object) -{ - GetCollection(object->GetType())->RemoveObject(object); -} - -/** - * Retrieves an object by type and name. - * - * @param type The type of the object. - * @param name The name of the object. - * @returns The object or a null pointer if the specified object - * could not be found. - */ -ConfigObject::Ptr ConfigHive::GetObject(const string& type, const string& name) -{ - return GetCollection(type)->GetObject(name); -} - -/** - * Retrieves a collection by name. Creates an empty collection - * if the collection doesn't already exist. - * - * @param collection The name of the collection. - * @returns The collection or a null pointer if the specified collection - * could not be found. - */ -ConfigCollection::Ptr ConfigHive::GetCollection(const string& collection) -{ - CollectionConstIterator ci = Collections.find(collection); - - if (ci == Collections.end()) { - Collections[collection] = make_shared(); - ci = Collections.find(collection); - } - - return ci->second; -} - -/** - * Invokes the specified callback for each object contained in this hive. - * - * @param type The config object type. - * @param callback The callback. - */ -void ConfigHive::ForEachObject(const string& type, - function callback) -{ - CollectionIterator ci = Collections.find(type); - - if (ci == Collections.end()) - return; - - ci->second->ForEachObject(callback); -} diff --git a/base/confighive.h b/base/confighive.h deleted file mode 100644 index ac473205a..000000000 --- a/base/confighive.h +++ /dev/null @@ -1,56 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#ifndef CONFIGHIVE_H -#define CONFIGHIVE_H - -namespace icinga -{ - -/** - * A collection of all configuration objects that belong to an application. - * - * @ingroup base - */ -class I2_BASE_API ConfigHive : public Object -{ -public: - typedef shared_ptr Ptr; - typedef weak_ptr WeakPtr; - - typedef map::iterator CollectionIterator; - typedef map::const_iterator CollectionConstIterator; - map Collections; - - void AddObject(const ConfigObject::Ptr& object); - void RemoveObject(const ConfigObject::Ptr& object); - ConfigObject::Ptr GetObject(const string& type, - const string& name = string()); - ConfigCollection::Ptr GetCollection(const string& type); - - void ForEachObject(const string& type, - function callback); - - Observable OnObjectCommitted; - Observable OnObjectRemoved; -}; - -} - -#endif /* CONFIGHIVE_H */ diff --git a/base/configobject.cpp b/base/configobject.cpp index d0a1ba49e..6a6dd3242 100644 --- a/base/configobject.cpp +++ b/base/configobject.cpp @@ -21,114 +21,157 @@ using namespace icinga; -/** - * Constructor for the ConfigObject class. - * - * @param type The type of the object. - * @param name The name of the object. - */ -ConfigObject::ConfigObject(const string& type, const string& name) +ConfigObject::ConfigObject(Dictionary::Ptr properties) + : m_Properties(properties), m_Tags(make_shared()) +{ } + +ConfigObject::ConfigObject(string type, string name) + : m_Properties(make_shared()), m_Tags(make_shared()) { - m_Type = type; - m_Name = name; - m_Replicated = false; + SetProperty("__type", type); + SetProperty("__name", name); } -/** - * Sets the hive this object belongs to. - * - * @param hive The hive. - */ -void ConfigObject::SetHive(const ConfigHive::WeakPtr& hive) +void ConfigObject::SetProperties(Dictionary::Ptr properties) { - if (m_Hive.lock()) - throw logic_error("Config object already has a parent hive."); - - m_Hive = hive; + m_Properties = properties; } -/** - * Retrieves the hive this object belongs to. - * - * @returns The hive. - */ -ConfigHive::WeakPtr ConfigObject::GetHive(void) const +Dictionary::Ptr ConfigObject::GetProperties(void) const { - return m_Hive; + return m_Properties; } -/** - * Sets the name of this object. - * - * @param name The name. - */ -void ConfigObject::SetName(const string& name) +Dictionary::Ptr ConfigObject::GetTags(void) const { - m_Name = name; + return m_Tags; } -/** - * Retrieves the name of this object. - * - * @returns The name. - */ -string ConfigObject::GetName(void) const -{ - return m_Name; -} - -/** - * Sets the type of this object. - * - * @param type The type. - */ -void ConfigObject::SetType(const string& type) -{ - m_Type = type; -} - -/** - * Retrieves the type of this object. - * - * @returns The type. - */ string ConfigObject::GetType(void) const { - return m_Type; + string type; + GetProperties()->GetProperty("__type", &type); + return type; } -/** - * Sets whether this object was replicated. - * - * @param replicated Whether this object was replicated. - */ -void ConfigObject::SetReplicated(bool replicated) +string ConfigObject::GetName(void) const { - m_Replicated = replicated; + string name; + GetProperties()->GetProperty("__name", &name); + return name; } -/** - * Retrieves whether this object was replicated. - * - * @returns Whether this object was replicated. - */ -bool ConfigObject::IsReplicated(void) const +void ConfigObject::SetLocal(bool value) { - return m_Replicated; + GetProperties()->SetProperty("__local", value ? 1 : 0); +} + +bool ConfigObject::IsLocal(void) const +{ + bool value; + GetProperties()->GetProperty("__local", &value); + return (value != 0); +} + +void ConfigObject::SetAbstract(bool value) +{ + GetProperties()->SetProperty("__abstract", value ? 1 : 0); +} + +bool ConfigObject::IsAbstract(void) const +{ + long value; + GetProperties()->GetProperty("__abstract", &value); + return (value != 0); } -/** - * Handles changed properties by propagating them to the hive - * and collection this object is contained in. - * - */ void ConfigObject::Commit(void) { - ConfigHive::Ptr hive = m_Hive.lock(); - if (hive) { - EventArgs ea; - ea.Source = shared_from_this(); - hive->GetCollection(m_Type)->OnObjectCommitted(ea); - hive->OnObjectCommitted(ea); - } + ConfigObject::Ptr dobj = GetObject(GetType(), GetName()); + ConfigObject::Ptr self = static_pointer_cast(shared_from_this()); + assert(!dobj || dobj == self); + GetAllObjects()->CheckObject(self); } + +void ConfigObject::Unregister(void) +{ + ConfigObject::Ptr self = static_pointer_cast(shared_from_this()); + GetAllObjects()->RemoveObject(self); +} + +ObjectSet::Ptr ConfigObject::GetAllObjects(void) +{ + static ObjectSet::Ptr allObjects; + + if (!allObjects) { + allObjects = make_shared >(); + allObjects->Start(); + } + + return allObjects; +} + +ConfigObject::TNMap::Ptr ConfigObject::GetObjectsByTypeAndName(void) +{ + static ConfigObject::TNMap::Ptr tnmap; + + if (!tnmap) { + tnmap = make_shared(GetAllObjects(), &ConfigObject::TypeAndNameGetter); + tnmap->Start(); + } + + return tnmap; +} + +ConfigObject::Ptr ConfigObject::GetObject(string type, string name) +{ + ConfigObject::TNMap::Range range; + range = GetObjectsByTypeAndName()->GetRange(make_pair(type, name)); + + assert(distance(range.first, range.second) <= 1); + + if (range.first == range.second) + return ConfigObject::Ptr(); + else + return range.first->second; +} + +bool ConfigObject::TypeAndNameGetter(const ConfigObject::Ptr& object, pair *key) +{ + *key = make_pair(object->GetType(), object->GetName()); + + return true; +} + +function ConfigObject::MakeTypePredicate(string type) +{ + return bind(&ConfigObject::TypePredicate, _1, type); +} + +bool ConfigObject::TypePredicate(const ConfigObject::Ptr& object, string type) +{ + return (object->GetType() == type); +} + +ConfigObject::TMap::Ptr ConfigObject::GetObjectsByType(void) +{ + static ObjectMap::Ptr tmap; + + if (!tmap) { + tmap = make_shared(GetAllObjects(), &ConfigObject::TypeGetter); + tmap->Start(); + } + + return tmap; +} + +bool ConfigObject::TypeGetter(const ConfigObject::Ptr& object, string *key) +{ + *key = object->GetType(); + return true; +} + +ConfigObject::TMap::Range ConfigObject::GetObjects(string type) +{ + return GetObjectsByType()->GetRange(type); +} \ No newline at end of file diff --git a/base/configobject.h b/base/configobject.h index 8871d8c50..52f530c61 100644 --- a/base/configobject.h +++ b/base/configobject.h @@ -20,46 +20,82 @@ #ifndef CONFIGOBJECT_H #define CONFIGOBJECT_H -#include - namespace icinga { -class ConfigHive; - -/** - * A configuration object that has arbitrary properties. - * - * @ingroup base - */ -class I2_BASE_API ConfigObject : public Dictionary +class I2_BASE_API ConfigObject : public Object { public: typedef shared_ptr Ptr; typedef weak_ptr WeakPtr; - ConfigObject(const string& type, const string& name); + typedef ObjectMap, ConfigObject::Ptr> TNMap; + typedef ObjectMap TMap; + typedef ObjectSet Set; - void SetHive(const weak_ptr& hive); - weak_ptr GetHive(void) const; + ConfigObject(Dictionary::Ptr properties); + ConfigObject(string type, string name); - void SetName(const string& name); + void SetProperties(Dictionary::Ptr config); + Dictionary::Ptr GetProperties(void) const; + + template + void SetProperty(const string& key, const T& value) + { + GetProperties()->SetProperty(key, value); + } + + template + bool GetProperty(const string& key, T *value) const + { + return GetProperties()->GetProperty(key, value); + } + + Dictionary::Ptr GetTags(void) const; + + template + void SetTag(const string& key, const T& value) + { + GetTags()->SetProperty(key, value); + } + + template + bool GetTag(const string& key, T *value) const + { + return GetTags()->GetProperty(key, value); + } + + string GetType(void) const; string GetName(void) const; - void SetType(const string& type); - string GetType(void) const; + void SetLocal(bool value); + bool IsLocal(void) const; - void SetReplicated(bool replicated); - bool IsReplicated(void) const; + void SetAbstract(bool value); + bool IsAbstract(void) const; void Commit(void); + void Unregister(void); + + static ObjectSet::Ptr GetAllObjects(void); + + static TNMap::Ptr GetObjectsByTypeAndName(void); + static TMap::Ptr GetObjectsByType(void); + + static ConfigObject::Ptr GetObject(string type, string name); + + static TMap::Range GetObjects(string type); + + static function MakeTypePredicate(string type); private: - weak_ptr m_Hive; + Dictionary::Ptr m_Properties; + Dictionary::Ptr m_Tags; - string m_Name; - string m_Type; - bool m_Replicated; + static bool TypeAndNameGetter(const ConfigObject::Ptr& object, pair *key); + static bool TypePredicate(const ConfigObject::Ptr& object, string type); + + static bool TypeGetter(const ConfigObject::Ptr& object, string *key); }; } diff --git a/base/i2-base.h b/base/i2-base.h index 151488063..e57abdf30 100644 --- a/base/i2-base.h +++ b/base/i2-base.h @@ -154,9 +154,9 @@ using namespace std::tr1::placeholders; #include "tcpclient.h" #include "tcpserver.h" #include "tlsclient.h" +#include "objectset.h" +#include "objectmap.h" #include "configobject.h" -#include "configcollection.h" -#include "confighive.h" #include "application.h" #include "component.h" diff --git a/dyn/objectmap.cpp b/base/objectmap.cpp similarity index 98% rename from dyn/objectmap.cpp rename to base/objectmap.cpp index 87e29b3c1..c5ac6be39 100644 --- a/dyn/objectmap.cpp +++ b/base/objectmap.cpp @@ -17,7 +17,7 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#include "i2-dyn.h" +#include "i2-base.h" using namespace icinga; diff --git a/dyn/objectmap.h b/base/objectmap.h similarity index 91% rename from dyn/objectmap.h rename to base/objectmap.h index d4d96da71..6456ab4c4 100644 --- a/dyn/objectmap.h +++ b/base/objectmap.h @@ -24,7 +24,7 @@ namespace icinga { template -class I2_DYN_API ObjectMap : public Object +class ObjectMap : public Object { public: typedef shared_ptr > Ptr; @@ -56,6 +56,19 @@ public: return m_Objects.equal_range(key); } + void ForeachObject(TKey key, function&)> callback) + { + ObjectSetEventArgs ea; + ea.Source = shared_from_this(); + + Range range = GetRange(key); + + for (Iterator it = range.first; it != range.second; it++) { + ea.Target(*it); + callback(ea); + } + } + private: multimap m_Objects; typename ObjectSet::Ptr m_Parent; diff --git a/dyn/objectset.cpp b/base/objectset.cpp similarity index 98% rename from dyn/objectset.cpp rename to base/objectset.cpp index 87e29b3c1..c5ac6be39 100644 --- a/dyn/objectset.cpp +++ b/base/objectset.cpp @@ -17,7 +17,7 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#include "i2-dyn.h" +#include "i2-base.h" using namespace icinga; diff --git a/dyn/objectset.h b/base/objectset.h similarity index 93% rename from dyn/objectset.h rename to base/objectset.h index af0796a71..f7bcde34b 100644 --- a/dyn/objectset.h +++ b/base/objectset.h @@ -30,7 +30,7 @@ struct ObjectSetEventArgs : public EventArgs }; template -class I2_DYN_API ObjectSet : public Object +class ObjectSet : public Object { public: typedef shared_ptr > Ptr; @@ -121,6 +121,17 @@ public: return m_Objects.end(); } + void ForeachObject(function&)> callback) + { + ObjectSetEventArgs ea; + ea.Source = shared_from_this(); + + for (Iterator it = Begin(); it != End(); it++) { + ea.Target(*it); + callback(ea); + } + } + private: set m_Objects; diff --git a/components/configfile/Makefile.am b/components/configfile/Makefile.am index 931ae6790..57ba90013 100644 --- a/components/configfile/Makefile.am +++ b/components/configfile/Makefile.am @@ -11,9 +11,7 @@ configfile_la_SOURCES = \ configfile_la_CPPFLAGS = \ $(BOOST_CPPFLAGS) \ -I${top_srcdir}/base \ - -I${top_srcdir}/icinga \ - -I${top_srcdir}/jsonrpc \ - -I${top_srcdir}/third-party/cJSON + -I${top_srcdir}/dyn configfile_la_LDFLAGS = \ $(BOOST_LDFLAGS) \ @@ -24,5 +22,4 @@ configfile_la_LDFLAGS = \ configfile_la_LIBADD = \ $(top_builddir)/base/libbase.la \ - $(top_builddir)/icinga/libicinga.la \ - $(top_builddir)/third-party/cJSON/libcJSON.la + $(top_builddir)/dyn/libdyn.la diff --git a/components/configfile/configfile.vcxproj b/components/configfile/configfile.vcxproj index 74e474d15..01e6e01d4 100644 --- a/components/configfile/configfile.vcxproj +++ b/components/configfile/configfile.vcxproj @@ -45,11 +45,11 @@ - $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\third-party\cJSON;$(IncludePath) + $(SolutionDir)\base;$(SolutionDir)\icinga;$(SolutionDir)\dyn;$(IncludePath) $(OutDir);$(LibraryPath) - $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\third-party\cJSON;$(IncludePath) + $(SolutionDir)\base;$(SolutionDir)\icinga;$(SolutionDir)\dyn;$(IncludePath) $(OutDir);$(LibraryPath) @@ -63,7 +63,7 @@ Windows true - base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies) + base.lib;dyn.lib;%(AdditionalDependencies) @@ -86,7 +86,7 @@ true true true - base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies) + base.lib;dyn.lib;%(AdditionalDependencies) $(OutDir)\base.lib;$(OutDir)\jsonrpc.lib diff --git a/components/configfile/configfilecomponent.cpp b/components/configfile/configfilecomponent.cpp index d89b8c0be..c7f04a2c8 100644 --- a/components/configfile/configfilecomponent.cpp +++ b/components/configfile/configfilecomponent.cpp @@ -17,10 +17,7 @@ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * ******************************************************************************/ -#include -#include #include "i2-configfile.h" -#include "cJSON.h" using namespace icinga; @@ -38,66 +35,13 @@ void ConfigFileComponent::Start(void) if (!GetConfig()->GetProperty("configFilename", &filename)) throw logic_error("Missing 'configFilename' property"); - fp.open(filename.c_str(), ifstream::in); - if (fp.fail()) - throw runtime_error("Could not open config file"); - - GetIcingaApplication()->Log("Reading config file: " + filename); + Application::Log("Compiling config file: " + filename); - while (!fp.eof()) { - size_t bufferSize = 1024; - char *buffer = (char *)fifo->GetWriteBuffer(&bufferSize); - fp.read(buffer, bufferSize); - if (fp.bad()) - throw runtime_error("Could not read from config file"); - fifo->Write(NULL, fp.gcount()); - } + vector configItems = ConfigCompiler::CompileFile(filename); - fp.close(); + Application::Log("Executing config items..."); - fifo->Write("\0", 1); - - /* TODO: implement config parsing, for now we just use JSON */ - cJSON *jsonobj = cJSON_Parse((const char *)fifo->GetReadBuffer()); - fifo->Read(NULL, fifo->GetSize()); - - if (jsonobj == NULL) - throw ConfigParserException("Could not parse config file."); - - for (cJSON *typeobj = jsonobj->child; typeobj != NULL; typeobj = typeobj->next) { - string type = typeobj->string; - - for (cJSON *object = typeobj->child; object != NULL; object = object->next) { - string name = object->string; - - ConfigObject::Ptr cfgobj = make_shared(type, name); - - for (cJSON *property = object->child; property != NULL; property = property->next) { - string key = property->string; - - if (property->type == cJSON_String) { - string value = property->valuestring; - - cfgobj->SetProperty(key, value); - } else if (property->type == cJSON_Array) { - Dictionary::Ptr items = make_shared(); - - for (cJSON *item = property->child; item != NULL; item = item->next) { - if (item->type != cJSON_String) - continue; - - items->AddUnnamedProperty(item->valuestring); - } - - cfgobj->SetProperty(key, items); - } - } - - GetConfigHive()->AddObject(cfgobj); - } - } - - cJSON_Delete(jsonobj); + ConfigVM::ExecuteItems(configItems); } void ConfigFileComponent::Stop(void) diff --git a/components/configfile/configfilecomponent.h b/components/configfile/configfilecomponent.h index fd4d4df85..618450deb 100644 --- a/components/configfile/configfilecomponent.h +++ b/components/configfile/configfilecomponent.h @@ -33,7 +33,7 @@ DEFINE_EXCEPTION_CLASS(ConfigParserException); /** * @ingroup configfile */ -class ConfigFileComponent : public IcingaComponent +class ConfigFileComponent : public Component { public: typedef shared_ptr Ptr; diff --git a/components/configfile/i2-configfile.h b/components/configfile/i2-configfile.h index 25dbe9334..0c534de88 100644 --- a/components/configfile/i2-configfile.h +++ b/components/configfile/i2-configfile.h @@ -28,7 +28,7 @@ */ #include -#include +#include #include "configfilecomponent.h" diff --git a/components/configrpc/configrpc.vcxproj b/components/configrpc/configrpc.vcxproj index f42b96386..47805d16a 100644 --- a/components/configrpc/configrpc.vcxproj +++ b/components/configrpc/configrpc.vcxproj @@ -46,12 +46,12 @@ true - $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(IncludePath) + $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\dyn;$(IncludePath) $(OutDir);$(LibraryPath) false - $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(IncludePath) + $(SolutionDir)\base;$(SolutionDir)\jsonrpc;$(SolutionDir)\icinga;$(SolutionDir)\dyn;$(IncludePath) $(OutDir);$(LibraryPath) diff --git a/components/configrpc/configrpccomponent.cpp b/components/configrpc/configrpccomponent.cpp index 37c2c3697..01190acaf 100644 --- a/components/configrpc/configrpccomponent.cpp +++ b/components/configrpc/configrpccomponent.cpp @@ -29,7 +29,6 @@ string ConfigRpcComponent::GetName(void) const void ConfigRpcComponent::Start(void) { EndpointManager::Ptr endpointManager = GetEndpointManager(); - ConfigHive::Ptr configHive = GetConfigHive(); m_ConfigRpcEndpoint = make_shared(); @@ -38,8 +37,9 @@ void ConfigRpcComponent::Start(void) m_ConfigRpcEndpoint->RegisterTopicHandler("config::FetchObjects", bind_weak(&ConfigRpcComponent::FetchObjectsHandler, shared_from_this())); - configHive->OnObjectCommitted += bind_weak(&ConfigRpcComponent::LocalObjectCommittedHandler, shared_from_this()); - configHive->OnObjectRemoved += bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this()); + ConfigObject::GetAllObjects()->OnObjectAdded += bind_weak(&ConfigRpcComponent::LocalObjectCommittedHandler, shared_from_this()); + ConfigObject::GetAllObjects()->OnObjectCommitted += bind_weak(&ConfigRpcComponent::LocalObjectCommittedHandler, shared_from_this()); + ConfigObject::GetAllObjects()->OnObjectRemoved += bind_weak(&ConfigRpcComponent::LocalObjectRemovedHandler, shared_from_this()); m_ConfigRpcEndpoint->RegisterPublication("config::ObjectCommitted"); m_ConfigRpcEndpoint->RegisterPublication("config::ObjectRemoved"); @@ -110,27 +110,23 @@ bool ConfigRpcComponent::ShouldReplicateObject(const ConfigObject::Ptr& object) int ConfigRpcComponent::FetchObjectsHandler(const NewRequestEventArgs& ea) { Endpoint::Ptr client = ea.Sender; - ConfigHive::Ptr configHive = GetConfigHive(); + ConfigObject::Set::Ptr allObjects = ConfigObject::GetAllObjects(); - for (ConfigHive::CollectionIterator ci = configHive->Collections.begin(); ci != configHive->Collections.end(); ci++) { - ConfigCollection::Ptr collection = ci->second; + for (ConfigObject::Set::Iterator ci = allObjects->Begin(); ci != allObjects->End(); ci++) { + ConfigObject::Ptr object = *ci; - for (ConfigCollection::ObjectIterator oi = collection->Objects.begin(); oi != collection->Objects.end(); oi++) { - ConfigObject::Ptr object = oi->second; + if (!ShouldReplicateObject(object)) + continue; - if (!ShouldReplicateObject(object)) - continue; + RequestMessage request = MakeObjectMessage(object, "config::ObjectCreated", true); - RequestMessage request = MakeObjectMessage(object, "config::ObjectCreated", true); - - GetEndpointManager()->SendUnicastMessage(m_ConfigRpcEndpoint, client, request); - } + GetEndpointManager()->SendUnicastMessage(m_ConfigRpcEndpoint, client, request); } return 0; } -int ConfigRpcComponent::LocalObjectCommittedHandler(const EventArgs& ea) +int ConfigRpcComponent::LocalObjectCommittedHandler(const ObjectSetEventArgs& ea) { ConfigObject::Ptr object = static_pointer_cast(ea.Source); @@ -143,7 +139,7 @@ int ConfigRpcComponent::LocalObjectCommittedHandler(const EventArgs& ea) return 0; } -int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea) +int ConfigRpcComponent::LocalObjectRemovedHandler(const ObjectSetEventArgs& ea) { ConfigObject::Ptr object = static_pointer_cast(ea.Source); @@ -159,7 +155,6 @@ int ConfigRpcComponent::LocalObjectRemovedHandler(const EventArgs& ea) int ConfigRpcComponent::RemoteObjectCommittedHandler(const NewRequestEventArgs& ea) { RequestMessage message = ea.Request; - bool was_null = false; MessagePart params; if (!message.GetParams(¶ms)) @@ -173,26 +168,18 @@ int ConfigRpcComponent::RemoteObjectCommittedHandler(const NewRequestEventArgs& if (!params.GetProperty("type", &type)) return 0; - ConfigHive::Ptr configHive = GetConfigHive(); - ConfigObject::Ptr object = configHive->GetObject(type, name); - - if (!object) { - was_null = true; - object = make_shared(type, name); - } - MessagePart properties; if (!params.GetProperty("properties", &properties)) return 0; - for (DictionaryIterator i = properties.Begin(); i != properties.End(); i++) { - object->SetProperty(i->first, i->second); - } + ConfigObject::Ptr object = ConfigObject::GetObject(type, name); - if (was_null) { - object->SetReplicated(true); - configHive->AddObject(object); - } + if (!object) + object = make_shared(properties.GetDictionary()); + else + object->SetProperties(properties.GetDictionary()); + + object->Commit(); return 0; } @@ -213,14 +200,13 @@ int ConfigRpcComponent::RemoteObjectRemovedHandler(const NewRequestEventArgs& ea if (!params.GetProperty("type", &type)) return 0; - ConfigHive::Ptr configHive = GetConfigHive(); - ConfigObject::Ptr object = configHive->GetObject(type, name); + ConfigObject::Ptr object = ConfigObject::GetObject(type, name); if (!object) return 0; - if (object->IsReplicated()) - configHive->RemoveObject(object); + if (!object->IsLocal()) + object->Unregister(); return 0; } diff --git a/components/configrpc/configrpccomponent.h b/components/configrpc/configrpccomponent.h index a7a0eda4e..2ae882d6a 100644 --- a/components/configrpc/configrpccomponent.h +++ b/components/configrpc/configrpccomponent.h @@ -28,14 +28,19 @@ namespace icinga */ class ConfigRpcComponent : public IcingaComponent { +public: + virtual string GetName(void) const; + virtual void Start(void); + virtual void Stop(void); + private: VirtualEndpoint::Ptr m_ConfigRpcEndpoint; int NewEndpointHandler(const NewEndpointEventArgs& ea); int SessionEstablishedHandler(const EventArgs& ea); - int LocalObjectCommittedHandler(const EventArgs& ea); - int LocalObjectRemovedHandler(const EventArgs& ea); + int LocalObjectCommittedHandler(const ObjectSetEventArgs& ea); + int LocalObjectRemovedHandler(const ObjectSetEventArgs& ea); int FetchObjectsHandler(const NewRequestEventArgs& ea); int RemoteObjectCommittedHandler(const NewRequestEventArgs& ea); @@ -45,10 +50,6 @@ private: string method, bool includeProperties); static bool ShouldReplicateObject(const ConfigObject::Ptr& object); -public: - virtual string GetName(void) const; - virtual void Start(void); - virtual void Stop(void); }; } diff --git a/components/discovery/discovery.vcxproj b/components/discovery/discovery.vcxproj index ff09b4f25..4e6a6ed42 100644 --- a/components/discovery/discovery.vcxproj +++ b/components/discovery/discovery.vcxproj @@ -58,7 +58,7 @@ Windows true - base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies) + base.lib;jsonrpc.lib;icinga.lib;%(AdditionalDependencies) @@ -76,7 +76,7 @@ true true true - base.lib;jsonrpc.lib;icinga.lib;cJSON.lib;%(AdditionalDependencies) + base.lib;jsonrpc.lib;icinga.lib;%(AdditionalDependencies) diff --git a/components/discovery/discoverycomponent.cpp b/components/discovery/discoverycomponent.cpp index 9965353bf..b9b50396b 100644 --- a/components/discovery/discoverycomponent.cpp +++ b/components/discovery/discoverycomponent.cpp @@ -348,14 +348,10 @@ bool DiscoveryComponent::HasMessagePermission(Dictionary::Ptr roles, string mess if (!roles) return false; - ConfigHive::Ptr configHive = GetConfigHive(); - ConfigCollection::Ptr roleCollection = configHive->GetCollection("role"); + ConfigObject::TMap::Range range = ConfigObject::GetObjects("role"); - for (DictionaryIterator ip = roles->Begin(); ip != roles->End(); ip++) { - ConfigObject::Ptr role = roleCollection->GetObject(ip->second); - - if (!role) - continue; + for (ConfigObject::TMap::Iterator ip = range.first; ip != range.second; ip++) { + ConfigObject::Ptr role = ip->second; Object::Ptr object; if (!role->GetProperty(messageType, &object)) @@ -395,10 +391,7 @@ void DiscoveryComponent::ProcessDiscoveryMessage(string identity, DiscoveryMessa message.GetNode(&info->Node); message.GetService(&info->Service); - ConfigHive::Ptr configHive = GetConfigHive(); - ConfigCollection::Ptr endpointCollection = configHive->GetCollection("endpoint"); - - ConfigObject::Ptr endpointConfig = endpointCollection->GetObject(identity); + ConfigObject::Ptr endpointConfig = ConfigObject::GetObject("endpoint", identity); Dictionary::Ptr roles; if (endpointConfig) { Object::Ptr object; @@ -485,31 +478,6 @@ int DiscoveryComponent::RegisterComponentMessageHandler(const NewRequestEventArg return 0; } -/** - * Processes "endpoint" config objects. - * - * @param ea Event arguments for the new config object. - * @returns 0 - */ -int DiscoveryComponent::EndpointConfigHandler(const EventArgs& ea) -{ - ConfigObject::Ptr object = static_pointer_cast(ea.Source); - - EndpointManager::Ptr endpointManager = GetEndpointManager(); - - /* Check if we're already connected to this endpoint. */ - if (endpointManager->GetEndpointByIdentity(object->GetName())) - return 0; - - string node, service; - if (object->GetProperty("node", &node) && object->GetProperty("service", &service)) { - /* reconnect to this endpoint */ - endpointManager->AddConnection(node, service); - } - - return 0; -} - /** * Checks whether we have to reconnect to other components and removes stale * components from the registry. @@ -525,8 +493,21 @@ int DiscoveryComponent::DiscoveryTimerHandler(const TimerEventArgs& tea) time(&now); /* check whether we have to reconnect to one of our upstream endpoints */ - ConfigCollection::Ptr endpointCollection = GetConfigHive()->GetCollection("endpoint"); - endpointCollection->ForEachObject(bind(&DiscoveryComponent::EndpointConfigHandler, this, _1)); + ConfigObject::TMap::Range range = ConfigObject::GetObjects("endpoint"); + + for (ConfigObject::TMap::Iterator it = range.first; it != range.second; it++) { + ConfigObject::Ptr object = it->second; + + /* Check if we're already connected to this endpoint. */ + if (endpointManager->GetEndpointByIdentity(object->GetName())) + continue; + + string node, service; + if (object->GetProperty("node", &node) && object->GetProperty("service", &service)) { + /* reconnect to this endpoint */ + endpointManager->AddConnection(node, service); + } + } map::iterator curr, i; for (i = m_Components.begin(); i != m_Components.end(); ) { diff --git a/components/discovery/discoverycomponent.h b/components/discovery/discoverycomponent.h index 6a93cd1cd..75bc0ea5e 100644 --- a/components/discovery/discoverycomponent.h +++ b/components/discovery/discoverycomponent.h @@ -76,8 +76,6 @@ private: void FinishDiscoverySetup(Endpoint::Ptr endpoint); - int EndpointConfigHandler(const EventArgs& ea); - bool HasMessagePermission(Dictionary::Ptr roles, string messageType, string message); static const int RegistrationTTL = 300; diff --git a/dyn/Makefile.am b/dyn/Makefile.am index 5aeea9611..c66654070 100644 --- a/dyn/Makefile.am +++ b/dyn/Makefile.am @@ -21,13 +21,7 @@ libdyn_la_SOURCES = \ expression.cpp \ expression.h \ expressionlist.cpp \ - expressionlist.h \ - dynamicobject.cpp \ - dynamicobject.h \ - objectset.cpp \ - objectset.h \ - objectmap.cpp \ - objectmap.h + expressionlist.h libdyn_la_CPPFLAGS = \ -DI2_DYN_BUILD \ diff --git a/dyn/configitem.cpp b/dyn/configitem.cpp index fbc77c111..ea274cec3 100644 --- a/dyn/configitem.cpp +++ b/dyn/configitem.cpp @@ -107,26 +107,27 @@ ConfigItem::TNMap::Ptr ConfigItem::GetObjectsByTypeAndName(void) void ConfigItem::Commit(void) { - DynamicObject::Ptr dobj = m_DynamicObject.lock(); - - if (!dobj) { - dobj = DynamicObject::GetObject(GetType(), GetName()); - - if (!dobj) - dobj = make_shared(); - - m_DynamicObject = dobj; - } + ConfigObject::Ptr dobj = m_ConfigObject.lock(); Dictionary::Ptr properties = make_shared(); CalculateProperties(properties); - dobj->SetConfig(properties); + + if (!dobj) + dobj = ConfigObject::GetObject(GetType(), GetName()); + + if (!dobj) + dobj = make_shared(properties); + else + dobj->SetProperties(properties); + + m_ConfigObject = dobj; + dobj->Commit(); ConfigItem::Ptr ci = GetObject(GetType(), GetName()); ConfigItem::Ptr self = static_pointer_cast(shared_from_this()); if (ci && ci != self) { - ci->m_DynamicObject.reset(); + ci->m_ConfigObject.reset(); GetAllObjects()->RemoveObject(ci); } GetAllObjects()->CheckObject(self); @@ -134,7 +135,7 @@ void ConfigItem::Commit(void) void ConfigItem::Unregister(void) { - // TODO: unregister associated DynamicObject + // TODO: unregister associated ConfigObject ConfigItem::Ptr self = static_pointer_cast(shared_from_this()); GetAllObjects()->RemoveObject(self); diff --git a/dyn/configitem.h b/dyn/configitem.h index 4e85f9a29..4358ce93f 100644 --- a/dyn/configitem.h +++ b/dyn/configitem.h @@ -57,7 +57,7 @@ private: vector m_Parents; ExpressionList::Ptr m_ExpressionList; - DynamicObject::WeakPtr m_DynamicObject; + ConfigObject::WeakPtr m_ConfigObject; static bool GetTypeAndName(const ConfigItem::Ptr& object, pair *key); }; diff --git a/dyn/dyn.vcxproj b/dyn/dyn.vcxproj index 02b532612..4d4f244d3 100644 --- a/dyn/dyn.vcxproj +++ b/dyn/dyn.vcxproj @@ -16,12 +16,9 @@ - - - @@ -29,11 +26,8 @@ - - - diff --git a/dyn/dynamicobject.cpp b/dyn/dynamicobject.cpp deleted file mode 100644 index 542b23b92..000000000 --- a/dyn/dynamicobject.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#include "i2-dyn.h" - -using namespace icinga; - -DynamicObject::DynamicObject(void) - : m_Config(make_shared()), m_Tags(make_shared()) -{ -} - -void DynamicObject::SetConfig(Dictionary::Ptr config) -{ - m_Config = config; -} - -Dictionary::Ptr DynamicObject::GetConfig(void) const -{ - return m_Config; -} - -Dictionary::Ptr DynamicObject::GetTags(void) -{ - if (!m_Tags) - m_Tags = make_shared(); - - return m_Tags; -} - -string DynamicObject::GetType(void) const -{ - string type; - GetConfig()->GetProperty("__type", &type); - return type; -} - -string DynamicObject::GetName(void) const -{ - string name; - GetConfig()->GetProperty("__name", &name); - return name; -} - -void DynamicObject::Commit(void) -{ - DynamicObject::Ptr dobj = GetObject(GetType(), GetName()); - DynamicObject::Ptr self = static_pointer_cast(shared_from_this()); - assert(!dobj || dobj == self); - GetAllObjects()->CheckObject(self); -} - -void DynamicObject::Unregister(void) -{ - DynamicObject::Ptr self = static_pointer_cast(shared_from_this()); - GetAllObjects()->RemoveObject(self); -} - -ObjectSet::Ptr DynamicObject::GetAllObjects(void) -{ - static ObjectSet::Ptr allObjects; - - if (!allObjects) { - allObjects = make_shared >(); - allObjects->Start(); - } - - return allObjects; -} - -DynamicObject::TNMap::Ptr DynamicObject::GetObjectsByTypeAndName(void) -{ - static DynamicObject::TNMap::Ptr tnmap; - - if (!tnmap) { - tnmap = make_shared(GetAllObjects(), &DynamicObject::GetTypeAndName); - tnmap->Start(); - } - - return tnmap; -} - -DynamicObject::Ptr DynamicObject::GetObject(string type, string name) -{ - DynamicObject::TNMap::Range range; - range = GetObjectsByTypeAndName()->GetRange(make_pair(type, name)); - - assert(distance(range.first, range.second) <= 1); - - if (range.first == range.second) - return DynamicObject::Ptr(); - else - return range.first->second; -} - -bool DynamicObject::GetTypeAndName(const DynamicObject::Ptr& object, pair *key) -{ - *key = make_pair(object->GetType(), object->GetName()); - - return true; -} - diff --git a/dyn/dynamicobject.h b/dyn/dynamicobject.h deleted file mode 100644 index e8d118a35..000000000 --- a/dyn/dynamicobject.h +++ /dev/null @@ -1,60 +0,0 @@ -/****************************************************************************** - * Icinga 2 * - * Copyright (C) 2012 Icinga Development Team (http://www.icinga.org/) * - * * - * This program is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public License * - * as published by the Free Software Foundation; either version 2 * - * of the License, or (at your option) any later version. * - * * - * This program is distributed in the hope that it will be useful, * - * but WITHOUT ANY WARRANTY; without even the implied warranty of * - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * - * GNU General Public License for more details. * - * * - * You should have received a copy of the GNU General Public License * - * along with this program; if not, write to the Free Software Foundation * - * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. * - ******************************************************************************/ - -#ifndef DYNAMICOBJECT_H -#define DYNAMICOBJECT_H - -namespace icinga -{ - -class I2_DYN_API DynamicObject : public Object -{ -public: - typedef shared_ptr Ptr; - typedef weak_ptr WeakPtr; - - typedef ObjectMap, DynamicObject::Ptr> TNMap; - - DynamicObject(void); - - void SetConfig(Dictionary::Ptr config); - Dictionary::Ptr GetConfig(void) const; - - Dictionary::Ptr GetTags(void); - - string GetType(void) const; - string GetName(void) const; - - void Commit(void); - void Unregister(void); - - static ObjectSet::Ptr GetAllObjects(void); - static TNMap::Ptr GetObjectsByTypeAndName(void); - static DynamicObject::Ptr GetObject(string type, string name); - -private: - Dictionary::Ptr m_Config; - Dictionary::Ptr m_Tags; - - static bool GetTypeAndName(const DynamicObject::Ptr& object, pair *key); -}; - -} - -#endif /* DYNAMICOBJECT_H */ diff --git a/dyn/i2-dyn.h b/dyn/i2-dyn.h index da65f7d68..ce20b737c 100644 --- a/dyn/i2-dyn.h +++ b/dyn/i2-dyn.h @@ -41,9 +41,6 @@ #include "debuginfo.h" #include "expression.h" #include "expressionlist.h" -#include "objectset.h" -#include "objectmap.h" -#include "dynamicobject.h" #include "configitem.h" #include "configcompiler.h" #include "configvm.h" diff --git a/dyntest/dyntest.cpp b/dyntest/dyntest.cpp index 3ac8a15a7..e114ce79f 100644 --- a/dyntest/dyntest.cpp +++ b/dyntest/dyntest.cpp @@ -1,5 +1,5 @@ #include -#include +//#include using namespace icinga; diff --git a/icinga/icingaapplication.cpp b/icinga/icingaapplication.cpp index 5ce487dc6..f4c326848 100644 --- a/icinga/icingaapplication.cpp +++ b/icinga/icingaapplication.cpp @@ -52,25 +52,32 @@ int IcingaApplication::Main(const vector& args) string componentDirectory = GetExeDirectory() + "/../lib/icinga2"; AddComponentSearchDir(componentDirectory); - /* register handler for 'icinga' config objects */ - ConfigCollection::Ptr icingaCollection = GetConfigHive()->GetCollection("icinga"); - function NewIcingaConfigHandler = bind_weak(&IcingaApplication::NewIcingaConfigHandler, shared_from_this()); - icingaCollection->OnObjectCommitted += NewIcingaConfigHandler; - icingaCollection->ForEachObject(NewIcingaConfigHandler); - icingaCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedIcingaConfigHandler, shared_from_this()); + ConfigObject::Ptr icingaConfig = ConfigObject::GetObject("application", "icinga"); + + if (!icingaConfig) + throw runtime_error("Configuration must contain an 'application' object named 'icinga'."); + + if (!icingaConfig->IsLocal()) + throw runtime_error("'icinga' application object must be 'local'."); + + icingaConfig->GetProperty("privkey", &m_PrivateKeyFile); + icingaConfig->GetProperty("pubkey", &m_PublicKeyFile); + icingaConfig->GetProperty("cakey", &m_CAKeyFile); + icingaConfig->GetProperty("node", &m_Node); + icingaConfig->GetProperty("service", &m_Service); /* register handler for 'component' config objects */ - ConfigCollection::Ptr componentCollection = GetConfigHive()->GetCollection("component"); - function NewComponentHandler = bind_weak(&IcingaApplication::NewComponentHandler, shared_from_this()); - componentCollection->OnObjectCommitted += NewComponentHandler; - componentCollection->ForEachObject(NewComponentHandler); - componentCollection->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedComponentHandler, shared_from_this()); + static ConfigObject::Set::Ptr componentObjects = make_shared(ConfigObject::GetAllObjects(), ConfigObject::MakeTypePredicate("component")); + function&)> NewComponentHandler = bind_weak(&IcingaApplication::NewComponentHandler, shared_from_this()); + componentObjects->OnObjectAdded += NewComponentHandler; + componentObjects->OnObjectCommitted += NewComponentHandler; + componentObjects->OnObjectRemoved += bind_weak(&IcingaApplication::DeletedComponentHandler, shared_from_this()); + componentObjects->Start(); /* load config file */ ConfigObject::Ptr fileComponentConfig = make_shared("component", "configfile"); - fileComponentConfig->SetProperty("configFilename", args[1]); - fileComponentConfig->SetProperty("replicate", 0); - GetConfigHive()->AddObject(fileComponentConfig); + fileComponentConfig->GetProperties()->SetProperty("configFilename", args[1]); + fileComponentConfig->Commit(); if (!GetPrivateKeyFile().empty() && !GetPublicKeyFile().empty() && !GetCAKeyFile().empty()) { /* set up SSL context */ @@ -103,12 +110,12 @@ EndpointManager::Ptr IcingaApplication::GetEndpointManager(void) return m_EndpointManager; } -int IcingaApplication::NewComponentHandler(const EventArgs& ea) +int IcingaApplication::NewComponentHandler(const ObjectSetEventArgs& ea) { - ConfigObject::Ptr object = static_pointer_cast(ea.Source); + ConfigObject::Ptr object = ea.Target; /* don't allow replicated config objects */ - if (object->IsReplicated()) + if (!object->IsLocal()) return 0; string path; @@ -125,9 +132,9 @@ int IcingaApplication::NewComponentHandler(const EventArgs& ea) return 0; } -int IcingaApplication::DeletedComponentHandler(const EventArgs& ea) +int IcingaApplication::DeletedComponentHandler(const ObjectSetEventArgs& ea) { - ConfigObject::Ptr object = static_pointer_cast(ea.Source); + ConfigObject::Ptr object = ea.Target; Component::Ptr component = GetComponent(object->GetName()); UnregisterComponent(component); @@ -135,87 +142,26 @@ int IcingaApplication::DeletedComponentHandler(const EventArgs& ea) return 0; } -int IcingaApplication::NewIcingaConfigHandler(const EventArgs& ea) -{ - ConfigObject::Ptr object = static_pointer_cast(ea.Source); - - /* don't allow replicated config objects */ - if (object->IsReplicated()) - return 0; - - string privkey; - if (object->GetProperty("privkey", &privkey)) - SetPrivateKeyFile(privkey); - - string pubkey; - if (object->GetProperty("pubkey", &pubkey)) - SetPublicKeyFile(pubkey); - - string cakey; - if (object->GetProperty("cakey", &cakey)) - SetCAKeyFile(cakey); - - string node; - if (object->GetProperty("node", &node)) - SetNode(node); - - string service; - if (object->GetProperty("service", &service)) - SetService(service); - - return 0; -} - -int IcingaApplication::DeletedIcingaConfigHandler(const EventArgs&) -{ - throw runtime_error("Unsupported operation."); -} - -void IcingaApplication::SetPrivateKeyFile(string privkey) -{ - m_PrivateKeyFile = privkey; -} - string IcingaApplication::GetPrivateKeyFile(void) const { return m_PrivateKeyFile; } -void IcingaApplication::SetPublicKeyFile(string pubkey) -{ - m_PublicKeyFile = pubkey; -} - string IcingaApplication::GetPublicKeyFile(void) const { return m_PublicKeyFile; } -void IcingaApplication::SetCAKeyFile(string cakey) -{ - m_CAKeyFile = cakey; -} - string IcingaApplication::GetCAKeyFile(void) const { return m_CAKeyFile; } -void IcingaApplication::SetNode(string node) -{ - m_Node = node; -} - string IcingaApplication::GetNode(void) const { return m_Node; } -void IcingaApplication::SetService(string service) -{ - m_Service = service; -} - string IcingaApplication::GetService(void) const { return m_Service; diff --git a/icinga/icingaapplication.h b/icinga/icingaapplication.h index cf7172975..de6b82b0e 100644 --- a/icinga/icingaapplication.h +++ b/icinga/icingaapplication.h @@ -38,19 +38,10 @@ public: EndpointManager::Ptr GetEndpointManager(void); - void SetPrivateKeyFile(string privkey); string GetPrivateKeyFile(void) const; - - void SetPublicKeyFile(string pubkey); string GetPublicKeyFile(void) const; - - void SetCAKeyFile(string cakey); string GetCAKeyFile(void) const; - - void SetNode(string node); string GetNode(void) const; - - void SetService(string service); string GetService(void) const; private: @@ -62,11 +53,8 @@ private: string m_Node; string m_Service; - int NewComponentHandler(const EventArgs& ea); - int DeletedComponentHandler(const EventArgs& ea); - - int NewIcingaConfigHandler(const EventArgs& ea); - int DeletedIcingaConfigHandler(const EventArgs& ea); + int NewComponentHandler(const ObjectSetEventArgs& ea); + int DeletedComponentHandler(const ObjectSetEventArgs& ea); int NewRpcListenerHandler(const EventArgs& ea); int DeletedRpcListenerHandler(const EventArgs& ea); diff --git a/icinga/icingacomponent.cpp b/icinga/icingacomponent.cpp index 8e711336a..f33f4bb34 100644 --- a/icinga/icingacomponent.cpp +++ b/icinga/icingacomponent.cpp @@ -36,13 +36,3 @@ EndpointManager::Ptr IcingaComponent::GetEndpointManager(void) const return app->GetEndpointManager(); } - -ConfigHive::Ptr IcingaComponent::GetConfigHive(void) const -{ - IcingaApplication::Ptr app = GetIcingaApplication(); - - if (!app) - return ConfigHive::Ptr(); - - return app->GetConfigHive(); -} diff --git a/icinga/icingacomponent.h b/icinga/icingacomponent.h index 63836a20e..700409353 100644 --- a/icinga/icingacomponent.h +++ b/icinga/icingacomponent.h @@ -33,7 +33,6 @@ class I2_ICINGA_API IcingaComponent : public Component protected: IcingaApplication::Ptr GetIcingaApplication(void) const; EndpointManager::Ptr GetEndpointManager(void) const; - ConfigHive::Ptr GetConfigHive(void) const; }; }