Implement REGISTER_SCRIPTFUNCTION() and clean up how check types are registered.

This commit is contained in:
Gunnar Beutner 2013-01-22 09:21:50 +01:00
parent 7dc761236d
commit 02de634c12
7 changed files with 26 additions and 23 deletions

View 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));

View 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)

View 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 */

View 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);
}

View 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);
};

View 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);
}

View 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);