WIP; Loading of multiple modules works
This commit is contained in:
parent
caba69b5d0
commit
32232ce6c2
@ -1,10 +1,11 @@
|
||||
#pragma once
|
||||
#include "server/rccpp.h"
|
||||
#include "core/types.h"
|
||||
|
||||
namespace interface
|
||||
{
|
||||
struct Server
|
||||
{
|
||||
virtual void load_module(const ss_ &module_name, const ss_ &path) = 0;
|
||||
virtual ss_ get_modules_path() = 0;
|
||||
};
|
||||
}
|
||||
|
@ -44,7 +44,6 @@ private:
|
||||
std::unordered_map<std::string, RCCPP_Info> component_info_;
|
||||
std::unordered_map<std::string, std::vector<void*>> constructed_objects;
|
||||
|
||||
std::vector<std::string> changed_classes_;
|
||||
bool compile(const std::string &in_path, const std::string &out_path);
|
||||
};
|
||||
|
||||
@ -111,16 +110,16 @@ bool CCompiler::build(const std::string &module_name,
|
||||
std::cout<<"Failed to load compiled library: "<<dlerror()<<std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
RCCPP_Constructor constructor = (RCCPP_Constructor)library_get_address(new_module, "createModule");
|
||||
|
||||
ss_ constructor_name = ss_()+"createModule_"+module_name;
|
||||
RCCPP_Constructor constructor = (RCCPP_Constructor)library_get_address(
|
||||
new_module, constructor_name.c_str());
|
||||
if(constructor == nullptr) {
|
||||
std::cout << "createModule() is missing from the library" << std::endl;
|
||||
std::cout<<constructor_name<<" is missing from the library"<<std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string classname = module_name;
|
||||
|
||||
auto it = component_info_.find(classname);
|
||||
auto it = component_info_.find(module_name);
|
||||
if(it != component_info_.end()) {
|
||||
RCCPP_Info &funcs = it->second;
|
||||
funcs.constructor = constructor;
|
||||
@ -129,10 +128,8 @@ bool CCompiler::build(const std::string &module_name,
|
||||
RCCPP_Info funcs;
|
||||
funcs.constructor = constructor;
|
||||
funcs.module = new_module;
|
||||
component_info_.emplace(classname, std::move(funcs));
|
||||
component_info_.emplace(module_name, std::move(funcs));
|
||||
}
|
||||
|
||||
changed_classes_.push_back(classname);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -13,6 +13,7 @@ namespace server {
|
||||
struct CState: public State, public interface::Server
|
||||
{
|
||||
up_<rccpp::Compiler> m_compiler;
|
||||
ss_ m_modules_path;
|
||||
|
||||
CState():
|
||||
m_compiler(rccpp::createCompiler())
|
||||
@ -23,12 +24,11 @@ struct CState: public State, public interface::Server
|
||||
g_server_config.interface_path+"/..");
|
||||
}
|
||||
|
||||
// State
|
||||
|
||||
void load_module(const ss_ &module_name, const ss_ &path)
|
||||
{
|
||||
std::cerr<<"Loading module "<<module_name<<" from "<<path<<std::endl;
|
||||
ss_ build_dst = g_server_config.rccpp_build_path + "/module.so";
|
||||
ss_ build_dst = g_server_config.rccpp_build_path +
|
||||
"/" + module_name + ".so";
|
||||
m_compiler->build(module_name, path+"/server/init.cpp", build_dst);
|
||||
|
||||
interface::Module *m = static_cast<interface::Module*>(
|
||||
@ -40,15 +40,17 @@ struct CState: public State, public interface::Server
|
||||
|
||||
void load_modules(const ss_ &path)
|
||||
{
|
||||
m_modules_path = path;
|
||||
ss_ first_module_path = path+"/__loader";
|
||||
load_module("__loader", first_module_path);
|
||||
/*ss_ first_module_path2 = path+"/test1";
|
||||
load_module("test1", first_module_path2);*/
|
||||
}
|
||||
|
||||
// interface::Server
|
||||
|
||||
|
||||
ss_ get_modules_path()
|
||||
{
|
||||
return m_modules_path;
|
||||
}
|
||||
};
|
||||
|
||||
State* createState()
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "interface/module.h"
|
||||
#include "interface/server.h"
|
||||
#include <iostream>
|
||||
|
||||
struct Module: public interface::Module
|
||||
{
|
||||
@ -8,15 +9,17 @@ struct Module: public interface::Module
|
||||
Module(interface::Server *server):
|
||||
m_server(server)
|
||||
{
|
||||
std::cout<<"__loader construct"<<std::endl;
|
||||
}
|
||||
|
||||
~Module()
|
||||
{
|
||||
std::cout<<"__loader destruct"<<std::endl;
|
||||
}
|
||||
|
||||
void start()
|
||||
{
|
||||
m_server->load_module("foo", "bar");
|
||||
m_server->load_module("test1", m_server->get_modules_path()+"/test1");
|
||||
}
|
||||
|
||||
int test_add(int a, int b)
|
||||
@ -26,7 +29,7 @@ struct Module: public interface::Module
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
EXPORT void* createModule(interface::Server *server)
|
||||
EXPORT void* createModule___loader(interface::Server *server)
|
||||
{
|
||||
return (void*)(new Module(server));
|
||||
}
|
||||
|
@ -1,14 +1,17 @@
|
||||
#include "interface/module.h"
|
||||
#include "interface/server.h"
|
||||
#include <iostream>
|
||||
|
||||
struct Module: public interface::Module
|
||||
{
|
||||
Module()
|
||||
{
|
||||
std::cout<<"test1 construct"<<std::endl;
|
||||
}
|
||||
|
||||
~Module()
|
||||
{
|
||||
std::cout<<"test1 destruct"<<std::endl;
|
||||
}
|
||||
|
||||
void start()
|
||||
@ -22,7 +25,7 @@ struct Module: public interface::Module
|
||||
};
|
||||
|
||||
extern "C" {
|
||||
EXPORT void* createModule(interface::Server *server)
|
||||
EXPORT void* createModule_test1(interface::Server *server)
|
||||
{
|
||||
return (void*)(new Module());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user