Skip to content

Commit

Permalink
Added templates for plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
pbosetti committed Jun 21, 2024
1 parent cb9d637 commit 3588d54
Show file tree
Hide file tree
Showing 3 changed files with 350 additions and 0 deletions.
132 changes: 132 additions & 0 deletions src/plugin/template_filter.cpp
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(&params);

// 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;
}

105 changes: 105 additions & 0 deletions src/plugin/template_sink.cpp
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(&params);

// Process data
plugin.load_data(input);

return 0;
}
113 changes: 113 additions & 0 deletions src/plugin/template_source.cpp
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(&params);

// Process data
plugin.get_output(output);

// Produce output
cout << "Clock: " << output << endl;

return 0;
}

0 comments on commit 3588d54

Please sign in to comment.