EOS 源代码解读 插件-流程
Posted thefist11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EOS 源代码解读 插件-流程相关的知识,希望对你有一定的参考价值。
application启动插件的流程:
step1. 调用各个插件的initialize()
step2. 调用各个插件的initialize_logging();
step3. 调用各个插件的startup()
step4. exec()
1. initialize
programs\\EOS\\main.cpp
int main(int argc, char** argv)
try
if(!app().initialize<chain_plugin, chain_api_plugin, http_plugin, history_plugin, history_api_plugin, net_plugin, maker_plugin, maker_api_plugin>(argc, argv))
class application
bool initialize(int argc, char** argv)
return initialize_impl(argc, argv, find_plugin<Plugin>()...);
bool application::initialize_impl(int argc, char** argv, vector<abstract_plugin*> autostart_plugins)
set_program_options();
try
for (auto plugin : autostart_plugins)
if (plugin != nullptr && plugin->get_state() == abstract_plugin::registered)
plugin->initialize(options);
1.1. abstract_plugin 类
包含四个状态: 已注册、 已初始化、 已启动、 已停止。
class abstract_plugin
public:
enum state
registered, ///< the plugin is constructed but doesn't do anything
initialized, ///< the plugin has initialized any state required but is idle
started, ///< the plugin is actively running
stopped ///< the plugin is no longer running
;
virtual ~abstract_plugin()
virtual state get_state()const = 0;
virtual const std::string& name()const = 0;
virtual void set_program_options( options_description& cli,
options_description& cfg ) = 0;
virtual void initialize(const variables_map& options) = 0;
virtual void handle_sighup() = 0;
virtual void startup() = 0;
virtual void shutdown() = 0;
;
template<typename Impl> class plugin : public abstract_plugin
public:
plugin():_name(boost::core::demangle(typeid(Impl).name()))
virtual ~plugin()
virtual state get_state()const override return _state;
virtual const std::string& name()const override return _name;
virtual void register_dependencies()
static_cast<Impl*>(this)->plugin_requires([&](auto& plug));
virtual void initialize(const variables_map& options) override
if(\\_state == registered)
\\_state = initialized;
//分步初始化,第二步
static_cast<Impl*>(this)->plugin_requires([&](auto& plug) plug.initialize(options); );//初始化此插件依赖的插件,并递归调用依赖插件
static_cast<Impl*>(this)->plugin_initialize(options); //初始化插件
//ilog( "initializing plugin $name", ("name",name()) );
app().plugin_initialized(*this);//保存启动的插件
assert(\\_state == initialized); /// if initial state was not registered, final state cannot be initiaized
virtual void handle_sighup() override
virtual void startup() override
if(_state == initialized)
_state = started;
static_cast<Impl*>(this)->plugin_requires([&](auto& plug) plug.startup(); );
static_cast<Impl*>(this)->plugin_startup();
app().plugin_started(*this);
assert(_state == started); // if initial state was not initialized, final state cannot be started
virtual void shutdown() override
if(_state == started)
_state = stopped;
//ilog( "shutting down plugin $name", ("name",name()) );
static_cast<Impl*>(this)->plugin_shutdown();
protected:
plugin(const string& name) : _name(name)
private:
state _state = abstract_plugin::registered;
std::string _name;
;
2. startup
void application::startup()
//during startup, run a second thread to catch SIGINT/SIGTERM/SIGPIPE/SIGHUP
boost::asio::io_service startup_thread_ios;
setup_signal_handling_on_ios(startup_thread_ios, true);
std::thread startup_thread([&startup_thread_ios]()
startup_thread_ios.run();
);
auto clean_up_signal_thread = [&startup_thread_ios, &startup_thread]()
startup_thread_ios.stop();
startup_thread.join();
;
try
for( auto plugin : initialized_plugins )
if( is_quiting() ) break;
plugin->startup();
catch( ... )
clean_up_signal_thread();
shutdown();
throw;
3. exec
void application::exec()
boost::asio::io_service::work work(*io_serv);
(void)work;
bool more = true;
while( more || io_serv->run_one() )
while( io_serv->poll_one() )
// execute the highest priority item
more = pri_queue.execute_highest();
shutdown(); /// perform synchronous shutdown
io_serv.reset();
4. shutdown
void application::shutdown()
for(auto ritr = running_plugins.rbegin();
ritr != running_plugins.rend(); ++ritr)
(*ritr)->shutdown();
for(auto ritr = running_plugins.rbegin();
ritr != running_plugins.rend(); ++ritr)
plugins.erase((*ritr)->name());
running_plugins.clear();
initialized_plugins.clear();
plugins.clear();
quit();
5. quit
void application::quit()
my->_is_quiting = true;
io_serv->stop();
以上是关于EOS 源代码解读 插件-流程的主要内容,如果未能解决你的问题,请参考以下文章