2019-02-25 14:48:22 +01:00
|
|
|
/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
|
2013-09-17 13:18:26 +02:00
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
#ifndef APIFUNCTION_H
|
|
|
|
#define APIFUNCTION_H
|
2013-09-17 13:18:26 +02:00
|
|
|
|
2014-05-25 16:23:35 +02:00
|
|
|
#include "remote/i2-remote.hpp"
|
|
|
|
#include "remote/messageorigin.hpp"
|
|
|
|
#include "base/registry.hpp"
|
|
|
|
#include "base/value.hpp"
|
|
|
|
#include "base/dictionary.hpp"
|
2014-05-03 20:02:22 +02:00
|
|
|
#include <vector>
|
2013-09-17 13:18:26 +02:00
|
|
|
|
|
|
|
namespace icinga
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
2020-11-12 14:23:41 +01:00
|
|
|
* A function available over the internal cluster API.
|
2013-09-17 13:18:26 +02:00
|
|
|
*
|
2014-05-03 20:02:22 +02:00
|
|
|
* @ingroup base
|
2013-09-17 13:18:26 +02:00
|
|
|
*/
|
2018-01-04 06:11:04 +01:00
|
|
|
class ApiFunction final : public Object
|
2013-09-17 13:18:26 +02:00
|
|
|
{
|
|
|
|
public:
|
2014-11-07 12:32:25 +01:00
|
|
|
DECLARE_PTR_TYPEDEFS(ApiFunction);
|
2013-09-17 13:18:26 +02:00
|
|
|
|
2017-11-21 11:52:55 +01:00
|
|
|
typedef std::function<Value(const MessageOrigin::Ptr& origin, const Dictionary::Ptr&)> Callback;
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2018-01-04 08:54:18 +01:00
|
|
|
ApiFunction(Callback function);
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2015-08-04 14:47:44 +02:00
|
|
|
Value Invoke(const MessageOrigin::Ptr& origin, const Dictionary::Ptr& arguments);
|
2014-05-03 20:02:22 +02:00
|
|
|
|
|
|
|
static ApiFunction::Ptr GetByName(const String& name);
|
|
|
|
static void Register(const String& name, const ApiFunction::Ptr& function);
|
|
|
|
static void Unregister(const String& name);
|
|
|
|
|
|
|
|
private:
|
|
|
|
Callback m_Callback;
|
2013-09-17 13:18:26 +02:00
|
|
|
};
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
/**
|
|
|
|
* A registry for API functions.
|
|
|
|
*
|
|
|
|
* @ingroup base
|
|
|
|
*/
|
2017-12-31 07:22:16 +01:00
|
|
|
class ApiFunctionRegistry : public Registry<ApiFunctionRegistry, ApiFunction::Ptr>
|
2014-05-03 20:02:22 +02:00
|
|
|
{
|
|
|
|
public:
|
2018-01-04 04:25:35 +01:00
|
|
|
static ApiFunctionRegistry *GetInstance();
|
2014-05-03 20:02:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#define REGISTER_APIFUNCTION(name, ns, callback) \
|
2016-08-27 09:35:08 +02:00
|
|
|
INITIALIZE_ONCE([]() { \
|
|
|
|
ApiFunction::Ptr func = new ApiFunction(callback); \
|
|
|
|
ApiFunctionRegistry::GetInstance()->Register(#ns "::" #name, func); \
|
|
|
|
})
|
2014-05-03 20:02:22 +02:00
|
|
|
|
2013-09-17 13:18:26 +02:00
|
|
|
}
|
|
|
|
|
2014-05-03 20:02:22 +02:00
|
|
|
#endif /* APIFUNCTION_H */
|