continue;
RequestMessage request = MakeObjectMessage(object, "config::ObjectUpdate", 0, true);
-
- EndpointManager::Ptr em = EndpointManager::GetInstance();
-
- {
- ObjectLock elock(em);
- em->SendUnicastMessage(m_Endpoint, endpoint, request);
- }
+ EndpointManager::GetInstance()->SendUnicastMessage(m_Endpoint, endpoint, request);
}
}
return;
RequestMessage request = MakeObjectMessage(object, "config::ObjectUpdate", tx, true);
-
- EndpointManager::Ptr em = EndpointManager::GetInstance();
- {
- ObjectLock olock(em);
- em->SendMulticastMessage(m_Endpoint, request);
- }
+ EndpointManager::GetInstance()->SendMulticastMessage(m_Endpoint, request);
}
void ReplicationComponent::RemoteObjectUpdateHandler(const RequestMessage& request)
-dlopen ${top_builddir}/components/compat/libcompat.la \
-dlopen ${top_builddir}/components/delegation/libdelegation.la \
-dlopen ${top_builddir}/components/demo/libdemo.la \
+ -dlopen ${top_builddir}/components/livestatus/liblivestatus.la \
-dlopen ${top_builddir}/components/notification/libnotification.la
if PYTHON_USE
process.h \
qstring.cpp \
qstring.h \
+ registry.h \
ringbuffer.cpp \
ringbuffer.h \
script.cpp \
scriptlanguage.h \
scripttask.cpp \
scripttask.h \
+ singleton.h \
socket.cpp \
socket.h \
stacktrace.cpp \
if (funcName.IsEmpty())
return ScriptTask::Ptr();
- ScriptFunction::Ptr func = ScriptFunction::GetByName(funcName);
+ ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(funcName);
if (!func)
BOOST_THROW_EXCEPTION(invalid_argument("Function '" + funcName + "' does not exist."));
static boost::mutex& GetStaticMutex(void);
};
+/**
+ * A registry for DynamicType objects.
+ *
+ * @ingroup base
+ */
+class DynamicTypeRegistry : public Registry<DynamicType::Ptr>
+{ };
+
/**
* Helper class for registering DynamicObject implementation classes.
*
#include "tlsstream.h"
#include "asynctask.h"
#include "process.h"
+#include "singleton.h"
+#include "registry.h"
#include "scriptfunction.h"
#include "scripttask.h"
#include "attribute.h"
--- /dev/null
+/******************************************************************************
+ * 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 REGISTRY_H
+#define REGISTRY_H
+
+namespace icinga
+{
+
+/**
+ * A registry.
+ *
+ * @ingroup base
+ */
+template<typename T>
+class I2_BASE_API Registry
+{
+public:
+ typedef map<String, T, string_iless> ItemMap;
+
+ static Registry<T> *GetInstance(void)
+ {
+ return Singleton<Registry<T> >::GetInstance();
+ }
+
+ void Register(const String& name, const T& item)
+ {
+ bool old_item = false;
+
+ {
+ boost::mutex::scoped_lock lock(m_Mutex);
+
+ if (m_Items.erase(name) > 0)
+ old_item = true;
+
+ m_Items[name] = item;
+ }
+
+ if (old_item)
+ OnUnregistered(name);
+
+ OnRegistered(name, item);
+ }
+
+ void Unregister(const String& name)
+ {
+ int erased;
+
+ {
+ boost::mutex::scoped_lock lock(m_Mutex);
+ erased = m_Items.erase(name);
+ }
+
+ if (erased > 0)
+ OnUnregistered(name);
+ }
+
+ T GetItem(const String& name) const
+ {
+ boost::mutex::scoped_lock lock(m_Mutex);
+
+ typename ItemMap::const_iterator it;
+ it = m_Items.find(name);
+
+ if (it == m_Items.end())
+ return T();
+
+ return it->second;
+ }
+
+ ItemMap GetItems(void) const
+ {
+ boost::mutex::scoped_lock lock(m_Mutex);
+
+ return m_Items; /* Makes a copy of the map. */
+ }
+
+ static signals2::signal<void (const String&, const T&)> OnRegistered;
+ static signals2::signal<void (const String&)> OnUnregistered;
+
+private:
+ mutable boost::mutex m_Mutex;
+ typename Registry<T>::ItemMap m_Items;
+};
+
+template<typename T>
+signals2::signal<void (const String&, const T&)> Registry<T>::OnRegistered;
+
+template<typename T>
+signals2::signal<void (const String&)> Registry<T>::OnUnregistered;
+
+}
+
+#endif /* REGISTRY_H */
using namespace icinga;
-signals2::signal<void (const String&, const ScriptFunction::Ptr&)> ScriptFunction::OnRegistered;
-signals2::signal<void (const String&)> ScriptFunction::OnUnregistered;
-
ScriptFunction::ScriptFunction(const Callback& function)
: m_Callback(function)
{ }
-/**
- * @threadsafety Always.
- */
-void ScriptFunction::Register(const String& name, const ScriptFunction::Ptr& function)
-{
- boost::mutex::scoped_lock lock(GetMutex());
-
- InternalGetFunctions()[name] = function;
- OnRegistered(name, function);
-}
-
-/**
- * @threadsafety Always.
- */
-void ScriptFunction::Unregister(const String& name)
-{
- boost::mutex::scoped_lock lock(GetMutex());
-
- InternalGetFunctions().erase(name);
- OnUnregistered(name);
-}
-
-/**
- * @threadsafety Always.
- */
-ScriptFunction::Ptr ScriptFunction::GetByName(const String& name)
-{
- boost::mutex::scoped_lock lock(GetMutex());
-
- map<String, ScriptFunction::Ptr>::iterator it;
-
- it = InternalGetFunctions().find(name);
-
- if (it == InternalGetFunctions().end())
- return ScriptFunction::Ptr();
-
- return it->second;
-}
-
/**
* @threadsafety Always.
*/
{
m_Callback(task, arguments);
}
-
-/**
- * @threadsafety Always.
- */
-map<String, ScriptFunction::Ptr> ScriptFunction::GetFunctions(void)
-{
- boost::mutex::scoped_lock lock(GetMutex());
-
- return InternalGetFunctions(); /* makes a copy of the map */
-}
-
-/**
- * @threadsafety Caller must hold the mutex returned by GetMutex().
- */
-map<String, ScriptFunction::Ptr>& ScriptFunction::InternalGetFunctions(void)
-{
- static map<String, ScriptFunction::Ptr> functions;
- return functions;
-}
-
-/**
- * @threadsafety Always.
- */
-boost::mutex& ScriptFunction::GetMutex(void)
-{
- static boost::mutex mtx;
- return mtx;
-}
explicit ScriptFunction(const Callback& function);
- static void Register(const String& name, const ScriptFunction::Ptr& function);
- static void Unregister(const String& name);
- static ScriptFunction::Ptr GetByName(const String& name);
-
- static map<String, ScriptFunction::Ptr> GetFunctions(void);
-
- static signals2::signal<void (const String&, const ScriptFunction::Ptr&)> OnRegistered;
- static signals2::signal<void (const String&)> OnUnregistered;
-
private:
Callback m_Callback;
- static map<String, ScriptFunction::Ptr>& InternalGetFunctions(void);
- static boost::mutex& GetMutex(void);
-
void Invoke(const shared_ptr<ScriptTask>& task, const vector<Value>& arguments);
friend class ScriptTask;
};
+/**
+ * A registry for script functions.
+ *
+ * @ingroup base
+ */
+class I2_BASE_API ScriptFunctionRegistry : public Registry<ScriptFunction::Ptr>
+{ };
+
/**
* Helper class for registering ScriptFunction implementation classes.
*
public:
RegisterFunctionHelper(const String& name, const ScriptFunction::Callback& function)
{
- if (!ScriptFunction::GetByName(name)) {
- ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(function);
- ScriptFunction::Register(name, func);
- }
+ ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(function);
+ ScriptFunctionRegistry::GetInstance()->Register(name, func);
}
};
ScriptInterpreter::~ScriptInterpreter(void)
{
BOOST_FOREACH(const String& function, m_SubscribedFunctions) {
- ScriptFunction::Unregister(function);
+ ScriptFunctionRegistry::GetInstance()->Unregister(function);
}
}
m_SubscribedFunctions.insert(name);
ScriptFunction::Ptr sf = boost::make_shared<ScriptFunction>(boost::bind(&ScriptInterpreter::ProcessCall, this, _1, name, _2));
- ScriptFunction::Register(name, sf);
+ ScriptFunctionRegistry::GetInstance()->Register(name, sf);
}
void ScriptInterpreter::UnsubscribeFunction(const String& name)
ObjectLock olock(this);
m_SubscribedFunctions.erase(name);
- ScriptFunction::Unregister(name);
+ ScriptFunctionRegistry::GetInstance()->Unregister(name);
}
--- /dev/null
+/******************************************************************************
+ * 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 SINGLETON_H
+#define SINGLETON_H
+
+namespace icinga
+{
+
+/**
+ * A singleton.
+ *
+ * @ingroup base
+ */
+template<typename T>
+class I2_BASE_API Singleton
+{
+public:
+ static T *GetInstance(void)
+ {
+ /* FIXME: This relies on static initializers being atomic. */
+ static boost::mutex mutex;
+ boost::mutex::scoped_lock lock(mutex);
+
+ if (!m_Instance)
+ m_Instance = new T();
+
+ return m_Instance;
+ }
+private:
+ friend T *T::GetInstance(void);
+
+ static T *m_Instance;
+};
+
+template<typename T>
+T *Singleton<T>::m_Instance = NULL;
+
+}
+
+#endif /* SINGLETON_H */
m_IncludeSearchDirs.push_back(dir);
}
-
-void ConfigCompiler::RegisterConfigFragment(const String& name, const String& fragment)
-{
- GetConfigFragments()[name] = fragment;
-}
-
-map<String, String>& ConfigCompiler::GetConfigFragments(void)
-{
- static map<String, String> fragments;
- return fragments;
-}
size_t ReadInput(char *buffer, size_t max_bytes);
void *GetScanner(void) const;
- static void RegisterConfigFragment(const String& name, const String& fragment);
- static map<String, String>& GetConfigFragments(void);
-
private:
String m_Path;
istream *m_Input;
void DestroyScanner(void);
};
+class ConfigFragmentRegistry : public Registry<String>
+{ };
+
/**
* Helper class for registering config fragments.
*
public:
RegisterConfigFragmentHelper(const String& name, const String& fragment)
{
- ConfigCompiler::RegisterConfigFragment(name, fragment);
+ ConfigFragmentRegistry::GetInstance()->Register(name, fragment);
}
};
String validator = ruleList->GetValidator();
if (!validator.IsEmpty()) {
- ScriptFunction::Ptr func = ScriptFunction::GetByName(validator);
+ ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(validator);
if (!func)
BOOST_THROW_EXCEPTION(invalid_argument("Validator function '" + validator + "' does not exist."));
String validator = ruleList->GetValidator();
if (!validator.IsEmpty()) {
- ScriptFunction::Ptr func = ScriptFunction::GetByName(validator);
+ ScriptFunction::Ptr func = ScriptFunctionRegistry::GetInstance()->GetItem(validator);
if (!func)
BOOST_THROW_EXCEPTION(invalid_argument("Validator function '" + validator + "' does not exist."));
map<String, PendingRequest>::iterator it;
for (it = m_Requests.begin(); it != m_Requests.end(); ++it) {
if (it->second.HasTimedOut()) {
- it->second.Callback(GetSelf(), Endpoint::Ptr(),
- it->second.Request, ResponseMessage(), true);
+ it->second.Callback(Endpoint::Ptr(), it->second.Request,
+ ResponseMessage(), true);
m_Requests.erase(it);
if (it == m_Requests.end())
return;
- it->second.Callback(GetSelf(), sender, it->second.Request, message, false);
+ it->second.Callback(sender, it->second.Request, message, false);
m_Requests.erase(it);
}
-EndpointManager::Ptr EndpointManager::GetInstance(void)
+EndpointManager *EndpointManager::GetInstance(void)
{
- static EndpointManager::Ptr instance;
-
- if (!instance)
- instance = boost::make_shared<EndpointManager>();
-
- return instance;
+ return Singleton<EndpointManager>::GetInstance();
}
EndpointManager(void);
- static EndpointManager::Ptr GetInstance(void);
+ static EndpointManager *GetInstance(void);
void SetIdentity(const String& identity);
String GetIdentity(void) const;
void SendMulticastMessage(const RequestMessage& message);
void SendMulticastMessage(const Endpoint::Ptr& sender, const RequestMessage& message);
- typedef function<void(const EndpointManager::Ptr&, const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> APICallback;
+ typedef function<void(const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> APICallback;
void SendAPIMessage(const Endpoint::Ptr& sender, const Endpoint::Ptr& recipient, RequestMessage& message,
const APICallback& callback, double timeout = 30);
void ProcessResponseMessage(const Endpoint::Ptr& sender, const ResponseMessage& message);
- signals2::signal<void (const EndpointManager::Ptr&, const Endpoint::Ptr&)> OnNewEndpoint;
+ signals2::signal<void (const Endpoint::Ptr&)> OnNewEndpoint;
private:
String m_Identity;
{
double Timeout;
RequestMessage Request;
- function<void(const EndpointManager::Ptr&, const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> Callback;
+ function<void(const Endpoint::Ptr, const RequestMessage&, const ResponseMessage&, bool TimedOut)> Callback;
bool HasTimedOut(void) const
{