Skip to content

Commit

Permalink
feat: add plugin2plugin local interface
Browse files Browse the repository at this point in the history
Enable plugins running under the same sysagentd
to interact via memory instead of using GDT
protocol
  • Loading branch information
dfranusic committed Jan 23, 2022
1 parent 17e6372 commit e7fdf2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/include/mink_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace mink_utils {
extern std::string const PLG_INIT_FN;
extern std::string const PLG_TERM_FN;
extern std::string const PLG_CMD_HNDLR;
extern std::string const PLG_CMD_HNDLR_LOCAL;
extern std::string const PLG_CMD_LST;

// fwd declaration
Expand Down Expand Up @@ -90,11 +91,12 @@ namespace mink_utils {
* Run plugin hook
*
* @param[in] cmd_id Command id
* @param[in] data Custom data
* @param[in] data Custom data (input/output)
* @param[in] local Local request flag
*
* @return 0 for success or error code
*/
int run(int cmd_id, void *data);
int run(int cmd_id, void *data, bool is_local = false);

private:
/** Pointer to MINK daemon descriptor */
Expand All @@ -117,6 +119,8 @@ namespace mink_utils {
int type;
/** Plugin cmd handler method */
PluginManager::plg_cmd_hndlr_t cmdh;
/** Plugin cmd handler method (local) */
PluginManager::plg_cmd_hndlr_t cmdh_l;
/** Plugin terminate method */
PluginManager::plg_term_t termh;
/** Custom data filled by plugin */
Expand Down
23 changes: 20 additions & 3 deletions src/utils/mink_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
std::string const mink_utils::PLG_INIT_FN("init");
std::string const mink_utils::PLG_TERM_FN("terminate");
std::string const mink_utils::PLG_CMD_HNDLR("run");
std::string const mink_utils::PLG_CMD_HNDLR_LOCAL("run_local");
std::string const mink_utils::PLG_CMD_LST("COMMANDS");

mink_utils::PluginManager::~PluginManager(){
Expand Down Expand Up @@ -48,8 +49,10 @@ mink_utils::PluginDescriptor *mink_utils::PluginManager::load(const std::string
reinterpret_cast<plg_term_t>(dlsym(h, PLG_TERM_FN.c_str()));
plg_cmd_hndlr_t cmdh =
reinterpret_cast<plg_cmd_hndlr_t>(dlsym(h, PLG_CMD_HNDLR.c_str()));
plg_cmd_hndlr_t cmdh_l =
reinterpret_cast<plg_cmd_hndlr_t>(dlsym(h, PLG_CMD_HNDLR_LOCAL.c_str()));

// all 4 must exist
// first 4 must exist
if (!(reg_hooks && init && term && cmdh)) {
dlclose(h);
return nullptr;
Expand All @@ -69,6 +72,7 @@ mink_utils::PluginDescriptor *mink_utils::PluginManager::load(const std::string
pd->name = std::string(fpath);
pd->type = 0;
pd->cmdh = cmdh;
pd->cmdh_l = cmdh_l;
pd->termh = term;
pd->data = nullptr;

Expand All @@ -92,11 +96,24 @@ int mink_utils::PluginManager::unload(PluginDescriptor *pd){
return 0;
}

int mink_utils::PluginManager::run(int cmd_id, void *data){
int mink_utils::PluginManager::run(int cmd_id, void *data, bool is_local) {
// plugin for cmd
auto pd = hooks.find(cmd_id);
if(pd == hooks.end()) return 1;
// run handler
// local
if (is_local) {
// handler implemented
if (pd->second->cmdh_l) {
return pd->second->cmdh_l(this, pd->second, cmd_id, data);

// local handler not found
} else {
return -1;
}
}

// remote
return pd->second->cmdh(this, pd->second, cmd_id, data);

}

0 comments on commit e7fdf2d

Please sign in to comment.