Introduce MBFT::Module and MBFT::ModuleAPI
This commit is contained in:
@@ -0,0 +1,104 @@
|
||||
#include <iostream>
|
||||
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include "modules/Module.h"
|
||||
|
||||
MBFT::Module::Module(const std::string& module_path)
|
||||
: module_path(module_path)
|
||||
{
|
||||
load();
|
||||
}
|
||||
|
||||
MBFT::Module::~Module(void)
|
||||
{
|
||||
unload();
|
||||
}
|
||||
|
||||
void MBFT::Module::load(void) {
|
||||
if (module_shared_object != nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
module_shared_object = dlopen(module_path.c_str(), RTLD_NOW);
|
||||
|
||||
if (module_shared_object == NULL)
|
||||
{
|
||||
std::cout << __FILE__ << ": Failed to open library " << module_path << "\n";
|
||||
std::cout << __FILE__ << ": " << dlerror() << "\n";
|
||||
module_shared_object = nullptr;
|
||||
return;
|
||||
}
|
||||
|
||||
loaded_init = reinterpret_cast<void(*)(void)>(dlsym(module_shared_object, "init"));
|
||||
if (loaded_init == NULL)
|
||||
{
|
||||
std::cout << __FILE__ << ": No function init() in " << module_path << "\n";
|
||||
loaded_init = nullptr;
|
||||
}
|
||||
|
||||
loaded_process = reinterpret_cast<void(*)(MBFT::ModuleAPI&, const MBFT::InboundMessage&)>(dlsym(module_shared_object, "process"));
|
||||
if (loaded_process == NULL)
|
||||
{
|
||||
std::cout << __FILE__ << ": No function process() in " << module_path << "\n";
|
||||
loaded_process = nullptr;
|
||||
}
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
void MBFT::Module::unload(void)
|
||||
{
|
||||
if (module_shared_object == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
dlclose(module_shared_object);
|
||||
module_shared_object = nullptr;
|
||||
|
||||
loaded_init = nullptr;
|
||||
loaded_process = nullptr;
|
||||
}
|
||||
|
||||
void MBFT::Module::reload(void)
|
||||
{
|
||||
unload();
|
||||
load();
|
||||
}
|
||||
|
||||
void MBFT::Module::disable(void)
|
||||
{
|
||||
enabled = false;
|
||||
}
|
||||
|
||||
void MBFT::Module::enable(void)
|
||||
{
|
||||
enabled = true;
|
||||
}
|
||||
|
||||
void MBFT::Module::init(void)
|
||||
{
|
||||
if (loaded_init == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
loaded_init();
|
||||
}
|
||||
|
||||
void MBFT::Module::process(MBFT::ModuleAPI& api, const MBFT::InboundMessage& message)
|
||||
{
|
||||
if (!enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (loaded_process == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
loaded_process(api, message);
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/inc/Types.h"
|
||||
#include "modules/ModuleAPI.h"
|
||||
|
||||
namespace MBFT
|
||||
{
|
||||
class Module
|
||||
{
|
||||
private:
|
||||
void *module_shared_object{nullptr};
|
||||
std::string module_path;
|
||||
|
||||
void (*loaded_init)(void){nullptr};
|
||||
void (*loaded_process)(MBFT::ModuleAPI& api, const MBFT::InboundMessage& message){nullptr};
|
||||
|
||||
bool enabled{true};
|
||||
public:
|
||||
Module(const std::string& module_path);
|
||||
~Module(void);
|
||||
void load(void);
|
||||
void unload(void);
|
||||
void reload(void);
|
||||
|
||||
void disable(void);
|
||||
void enable(void);
|
||||
|
||||
void init(void);
|
||||
void process(MBFT::ModuleAPI& api, const MBFT::InboundMessage& message);
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
#include "modules/ModuleAPI.h"
|
||||
|
||||
MBFT::ModuleAPI::ModuleAPI(MBFT::IConnector& c)
|
||||
: connector(c)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void MBFT::ModuleAPI::set_context(const MBFT::InboundMessage * const ctx)
|
||||
{
|
||||
this->ctx = const_cast<MBFT::InboundMessage *>(ctx);
|
||||
}
|
||||
|
||||
void MBFT::ModuleAPI::v1_respond(const std::string& text)
|
||||
{
|
||||
if (ctx == nullptr)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
std::shared_ptr<MBFT::OutboundMessage> new_message = std::make_shared<MBFT::OutboundMessage>();
|
||||
MBFT::OutboundMessage& m = *new_message;
|
||||
|
||||
m.target_chat_id = ctx->chat->id;
|
||||
m.text = text;
|
||||
|
||||
connector.send(new_message);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "core/inc/Types.h"
|
||||
#include "connectors/IConnector.h"
|
||||
|
||||
namespace MBFT
|
||||
{
|
||||
class ModuleAPI
|
||||
{
|
||||
public:
|
||||
MBFT::IConnector& connector;
|
||||
MBFT::InboundMessage *ctx{nullptr};
|
||||
public:
|
||||
ModuleAPI(MBFT::IConnector& c);
|
||||
void set_context(const MBFT::InboundMessage * const ctx);
|
||||
|
||||
void v1_respond(const std::string& text);
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user