generated from miroscic/plugin_cpp
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
350 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
/* | ||
_____ _ _ _ _ _ | ||
| ___(_) | |_ ___ _ __ _ __ | |_ _ __ _(_)_ __ | ||
| |_ | | | __/ _ \ '__| | '_ \| | | | |/ _` | | '_ \ | ||
| _| | | | || __/ | | |_) | | |_| | (_| | | | | | | ||
|_| |_|_|\__\___|_| | .__/|_|\__,_|\__, |_|_| |_| | ||
|_| |___/ | ||
A Template for a Filter Plugin | ||
*/ | ||
// Mandatory included headers | ||
#include "../filter.hpp" | ||
#include <nlohmann/json.hpp> | ||
#include <pugg/Kernel.h> | ||
// other includes as needed here | ||
|
||
// Define the name of the plugin | ||
#ifndef PLUGIN_NAME | ||
#define PLUGIN_NAME "CHANGE ME!" | ||
#endif | ||
|
||
// Load the namespaces | ||
using namespace std; | ||
using json = nlohmann::json; | ||
|
||
|
||
// Plugin class. This shall be the only part that needs to be modified, | ||
// implementing the actual functionality | ||
class PluginClassName : public Filter<json, json> { | ||
|
||
public: | ||
|
||
// Typically, no need to change this | ||
string kind() override { return PLUGIN_NAME; } | ||
|
||
// Implement the actual functionality here | ||
return_type load_data(json const &input) override { | ||
// Do something with the input data | ||
return return_type::success; | ||
} | ||
|
||
// We calculate the average of the last N values for each key and store it | ||
// into the output json object | ||
return_type process(json &out) override { | ||
out.clear(); | ||
|
||
// load the data as necessary and set the fields of the json out variable | ||
|
||
// This sets the agent_id field in the output json object, only when it is | ||
// not empty | ||
if (!_agent_id.empty()) out["agent_id"] = _agent_id; | ||
return return_type::success; | ||
} | ||
|
||
void set_params(void const *params) override { | ||
// Call the parent class method to set the common parameters | ||
// (e.g. agent_id, etc.) | ||
Filter::set_params(params); | ||
|
||
// provide sensible defaults for the parameters by setting e.g. | ||
_params["some_field"] = "default_value"; | ||
// more here... | ||
|
||
// then merge the defaults with the actually provided parameters | ||
// params needs to be cast to json | ||
_params.merge_patch(*(json *)params); | ||
} | ||
|
||
// Implement this method if you want to provide additional information | ||
map<string, string> info() override { | ||
// return a map of stringswith additional information about the plugin | ||
// it is used to print the information about the plugin when it is loaded | ||
// by the agent | ||
return {}; | ||
}; | ||
|
||
private: | ||
// Define the fields that are used to store internal resources | ||
}; | ||
|
||
|
||
/* | ||
____ _ _ _ _ | ||
| _ \| |_ _ __ _(_)_ __ __| |_ __(_)_ _____ _ __ | ||
| |_) | | | | |/ _` | | '_ \ / _` | '__| \ \ / / _ \ '__| | ||
| __/| | |_| | (_| | | | | | | (_| | | | |\ V / __/ | | ||
|_| |_|\__,_|\__, |_|_| |_| \__,_|_| |_| \_/ \___|_| | ||
|___/ | ||
Enable the class as plugin | ||
*/ | ||
INSTALL_FILTER_DRIVER(PluginClassName, json, json); | ||
|
||
|
||
/* | ||
_ | ||
_ __ ___ __ _(_)_ __ | ||
| '_ ` _ \ / _` | | '_ \ | ||
| | | | | | (_| | | | | | | ||
|_| |_| |_|\__,_|_|_| |_| | ||
*/ | ||
|
||
int main(int argc, char const *argv[]) | ||
{ | ||
PluginClassName plugin; | ||
json params; | ||
json input, output; | ||
|
||
// Set example values to params | ||
params["test"] = "value"; | ||
|
||
// Set the parameters | ||
plugin.set_params(¶ms); | ||
|
||
// Set input data | ||
input["data"] = { | ||
{"AX", 1}, | ||
{"AY", 2}, | ||
{"AZ", 3} | ||
}; | ||
|
||
// Set input data | ||
plugin.load_data(input); | ||
cout << "Input: " << input.dump(2) << endl; | ||
|
||
// Process data | ||
plugin.process(output); | ||
cout << "Output: " << output.dump(2) << endl; | ||
|
||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
____ _ _ _ _ | ||
/ ___|(_)_ __ | | __ _ __ | |_ _ __ _(_)_ __ | ||
\___ \| | '_ \| |/ / | '_ \| | | | |/ _` | | '_ \ | ||
___) | | | | | < | |_) | | |_| | (_| | | | | | | ||
|____/|_|_| |_|_|\_\ | .__/|_|\__,_|\__, |_|_| |_| | ||
|_| |___/ | ||
A Template for a Sink Plugin | ||
*/ | ||
|
||
// Mandatory included headers | ||
#include "../sink.hpp" | ||
#include <nlohmann/json.hpp> | ||
#include <pugg/Kernel.h> | ||
// other includes as needed here | ||
|
||
// Define the name of the plugin | ||
#ifndef PLUGIN_NAME | ||
#define PLUGIN_NAME "CHANGE ME!" | ||
#endif | ||
|
||
// Load the namespaces | ||
using namespace std; | ||
using json = nlohmann::json; | ||
|
||
|
||
// Plugin class. This shall be the only part that needs to be modified, | ||
// implementing the actual functionality | ||
class PluginClassName : public Sink<json> { | ||
|
||
public: | ||
|
||
// Typically, no need to change this | ||
string kind() override { return PLUGIN_NAME; } | ||
|
||
// Implement the actual functionality here | ||
return_type load_data(json const &input) override { | ||
// Do something with the input data | ||
return return_type::success; | ||
} | ||
|
||
void set_params(void const *params) override { | ||
// Call the parent class method to set the common parameters | ||
// (e.g. agent_id, etc.) | ||
Sink::set_params(params); | ||
|
||
// provide sensible defaults for the parameters by setting e.g. | ||
_params["some_field"] = "default_value"; | ||
// more here... | ||
|
||
// then merge the defaults with the actually provided parameters | ||
// params needs to be cast to json | ||
_params.merge_patch(*(json *)params); | ||
} | ||
|
||
// Implement this method if you want to provide additional information | ||
map<string, string> info() override { | ||
// return a map of stringswith additional information about the plugin | ||
// it is used to print the information about the plugin when it is loaded | ||
// by the agent | ||
return {}; | ||
}; | ||
|
||
private: | ||
// Define the fields that are used to store internal resources | ||
|
||
}; | ||
|
||
|
||
|
||
/* | ||
____ _ _ _ _ | ||
| _ \| |_ _ __ _(_)_ __ __| |_ __(_)_ _____ _ __ | ||
| |_) | | | | |/ _` | | '_ \ / _` | '__| \ \ / / _ \ '__| | ||
| __/| | |_| | (_| | | | | | | (_| | | | |\ V / __/ | | ||
|_| |_|\__,_|\__, |_|_| |_| \__,_|_| |_| \_/ \___|_| | ||
|___/ | ||
Enable the class as plugin | ||
*/ | ||
INSTALL_SINK_DRIVER(PluginClassName, json) | ||
|
||
|
||
/* | ||
_ | ||
_ __ ___ __ _(_)_ __ | ||
| '_ ` _ \ / _` | | '_ \ | ||
| | | | | | (_| | | | | | | ||
|_| |_| |_|\__,_|_|_| |_| | ||
For testing purposes, when directly executing the plugin | ||
*/ | ||
int main(int argc, char const *argv[]) { | ||
PluginClassName plugin; | ||
json input, params; | ||
// Set example values to params | ||
params["test"] = "value"; | ||
|
||
// Set the parameters | ||
plugin.set_params(¶ms); | ||
|
||
// Process data | ||
plugin.load_data(input); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
____ _ _ | ||
/ ___| ___ _ _ _ __ ___ ___ _ __ | |_ _ __ _(_)_ __ | ||
\___ \ / _ \| | | | '__/ __/ _ \ | '_ \| | | | |/ _` | | '_ \ | ||
___) | (_) | |_| | | | (_| __/ | |_) | | |_| | (_| | | | | | | ||
|____/ \___/ \__,_|_| \___\___| | .__/|_|\__,_|\__, |_|_| |_| | ||
|_| |___/ | ||
A Template for a Source Plugin | ||
*/ | ||
// Mandatory included headers | ||
#include "../source.hpp" | ||
#include <nlohmann/json.hpp> | ||
#include <pugg/Kernel.h> | ||
// other includes as needed here | ||
|
||
// Define the name of the plugin | ||
#ifndef PLUGIN_NAME | ||
#define PLUGIN_NAME "CHANGE ME!" | ||
#endif | ||
|
||
// Load the namespaces | ||
using namespace std; | ||
using json = nlohmann::json; | ||
|
||
|
||
// Plugin class. This shall be the only part that needs to be modified, | ||
// implementing the actual functionality | ||
class PluginClassName : public Source<json> { | ||
|
||
public: | ||
|
||
// Typically, no need to change this | ||
string kind() override { return PLUGIN_NAME; } | ||
|
||
// Implement the actual functionality here | ||
return_type get_output(json &out, | ||
std::vector<unsigned char> *blob = nullptr) override { | ||
out.clear(); | ||
|
||
// load the data as necessary and set the fields of the json out variable | ||
|
||
// This sets the agent_id field in the output json object, only when it is | ||
// not empty | ||
if (!_agent_id.empty()) out["agent_id"] = _agent_id; | ||
return return_type::success; | ||
} | ||
|
||
void set_params(void const *params) override { | ||
// Call the parent class method to set the common parameters | ||
// (e.g. agent_id, etc.) | ||
Source::set_params(params); | ||
|
||
// provide sensible defaults for the parameters by setting e.g. | ||
_params["some_field"] = "default_value"; | ||
// more here... | ||
|
||
// then merge the defaults with the actually provided parameters | ||
// params needs to be cast to json | ||
_params.merge_patch(*(json *)params); | ||
} | ||
|
||
// Implement this method if you want to provide additional information | ||
map<string, string> info() override { | ||
// return a map of stringswith additional information about the plugin | ||
// it is used to print the information about the plugin when it is loaded | ||
// by the agent | ||
return {}; | ||
}; | ||
|
||
private: | ||
// Define the fields that are used to store internal resources | ||
|
||
}; | ||
|
||
|
||
/* | ||
____ _ _ _ _ | ||
| _ \| |_ _ __ _(_)_ __ __| |_ __(_)_ _____ _ __ | ||
| |_) | | | | |/ _` | | '_ \ / _` | '__| \ \ / / _ \ '__| | ||
| __/| | |_| | (_| | | | | | | (_| | | | |\ V / __/ | | ||
|_| |_|\__,_|\__, |_|_| |_| \__,_|_| |_| \_/ \___|_| | ||
|___/ | ||
Enable the class as plugin | ||
*/ | ||
INSTALL_SOURCE_DRIVER(PluginClassName, json) | ||
|
||
|
||
/* | ||
_ | ||
_ __ ___ __ _(_)_ __ | ||
| '_ ` _ \ / _` | | '_ \ | ||
| | | | | | (_| | | | | | | ||
|_| |_| |_|\__,_|_|_| |_| | ||
For testing purposes, when directly executing the plugin | ||
*/ | ||
int main(int argc, char const *argv[]) { | ||
PluginClassName plugin; | ||
json output, params; | ||
// Set example values to params | ||
params["test"] = "value"; | ||
|
||
// Set the parameters | ||
plugin.set_params(¶ms); | ||
|
||
// Process data | ||
plugin.get_output(output); | ||
|
||
// Produce output | ||
cout << "Clock: " << output << endl; | ||
|
||
return 0; | ||
} |