]> granicus.if.org Git - icinga2/commitdiff
Implement REGISTER_SCRIPTFUNCTION() and clean up how check types are registered.
authorGunnar Beutner <gunnar@beutner.name>
Tue, 22 Jan 2013 08:21:50 +0000 (09:21 +0100)
committerGunnar Beutner <gunnar@beutner.name>
Tue, 22 Jan 2013 08:21:50 +0000 (09:21 +0100)
components/checker/checkercomponent.cpp
lib/base/dynamictype.h
lib/base/scriptfunction.h
lib/icinga/nullchecktask.cpp
lib/icinga/nullchecktask.h
lib/icinga/pluginchecktask.cpp
lib/icinga/pluginchecktask.h

index 928289a47e05183c8f007edc97b0100e6d742187..744349b94ddfda43af01238be6a72e3d982337ff 100644 (file)
@@ -37,10 +37,6 @@ void CheckerComponent::Start(void)
        m_CheckTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::CheckTimerHandler, this));
        m_CheckTimer->Start();
 
-       /* TODO: figure out a way to register check types */
-       PluginCheckTask::Register();
-       NullCheckTask::Register();
-
        m_ResultTimer = boost::make_shared<Timer>();
        m_ResultTimer->SetInterval(5);
        m_ResultTimer->OnTimerExpired.connect(boost::bind(&CheckerComponent::ResultTimerHandler, this));
index 7b47c75dc6bc96d383e1f3d32a57f2a65532ce20..818223b7b6a42d7cf83095e4f0ff21613ca4d442 100644 (file)
@@ -100,7 +100,7 @@ shared_ptr<T> DynamicObjectFactory(const Dictionary::Ptr& serializedUpdate)
 }
 
 #define REGISTER_TYPE_ALIAS(type, alias, attributeDesc) \
-       static RegisterTypeHelper g_Register ## type(alias, DynamicObjectFactory<type>, attributeDesc, (attributeDesc == NULL) ? 0 : sizeof(attributeDesc) / sizeof((static_cast<AttributeDescription *>(attributeDesc))[0]))
+       static RegisterTypeHelper g_RegisterDT_ ## type(alias, DynamicObjectFactory<type>, attributeDesc, (attributeDesc == NULL) ? 0 : sizeof(attributeDesc) / sizeof((static_cast<AttributeDescription *>(attributeDesc))[0]))
 
 #define REGISTER_TYPE(type, attributeDesc) \
        REGISTER_TYPE_ALIAS(type, #type, attributeDesc)
index 86a4594154d5e81df8f8b0135f1389a58e827659..107521143ab82f57c85adbe835d82b651b063874 100644 (file)
@@ -52,7 +52,26 @@ private:
        static map<String, ScriptFunction::Ptr> m_Functions;
 };
 
+/**
+ * Helper class for registering ScriptFunction implementation classes.
+ *
+ * @ingroup base
+ */
+class RegisterFunctionHelper
+{
+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);
+               }
+       }
+};
+
+#define REGISTER_SCRIPTFUNCTION(name, callback) \
+       static RegisterFunctionHelper g_RegisterSF_ ## type(name, callback)
+
 }
 
 #endif /* SCRIPTFUNCTION_H */
-
index 3df02340e277c71cb949d186e0ef38e1a770c7fc..9d6e92c2ab36dbabf25e472445f45bb4a344b4ef 100644 (file)
@@ -21,6 +21,8 @@
 
 using namespace icinga;
 
+REGISTER_SCRIPTFUNCTION("native::NullCheck",  &NullCheckTask::ScriptFunc);
+
 void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>& arguments)
 {
        if (arguments.size() < 1)
@@ -37,9 +39,3 @@ void NullCheckTask::ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>&
 
        task->FinishResult(cr);
 }
-
-void NullCheckTask::Register(void)
-{
-       ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(&NullCheckTask::ScriptFunc);
-       ScriptFunction::Register("native::NullCheck", func);
-}
index 13b268180e94f32b7e31b4cb93639bedf719373c..a38abaf0f3132cee8e9614f105bea321ddb10e57 100644 (file)
@@ -31,9 +31,6 @@ namespace icinga
 class I2_ICINGA_API NullCheckTask
 {
 public:
-       static void Register(void);
-
-private:
        static void ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>& arguments);
 };
 
index b699c4c1de17981df3b9a21c5a5a4d4677bd6b6a..e2ca0d1bb92d5e2c5401a6cc4ddd203cfb227291 100644 (file)
@@ -21,6 +21,8 @@
 
 using namespace icinga;
 
+REGISTER_SCRIPTFUNCTION("native::PluginCheck",  &PluginCheckTask::ScriptFunc);
+
 PluginCheckTask::PluginCheckTask(const ScriptTask::Ptr& task, const Process::Ptr& process)
        : m_Task(task), m_Process(process)
 { }
@@ -124,9 +126,3 @@ void PluginCheckTask::ProcessCheckOutput(const Dictionary::Ptr& result, String&
        result->Set("output", text);
        result->Set("performance_data_raw", perfdata);
 }
-
-void PluginCheckTask::Register(void)
-{
-       ScriptFunction::Ptr func = boost::make_shared<ScriptFunction>(&PluginCheckTask::ScriptFunc);
-       ScriptFunction::Register("native::PluginCheck", func);
-}
index d8dd8b3bfd17b15f085f95a905f1024bca55fd6b..b5b46e028e151acfbd27980bbf24f94d21287c4e 100644 (file)
@@ -31,10 +31,9 @@ namespace icinga
 class I2_ICINGA_API PluginCheckTask
 {
 public:
-       static void Register(void);
+       static void ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>& arguments);
 
 private:
-       static void ScriptFunc(const ScriptTask::Ptr& task, const vector<Value>& arguments);
 
        static void ProcessFinishedHandler(PluginCheckTask ct);
        static void ProcessCheckOutput(const Dictionary::Ptr& result, String& output);