EOS 源代码解读 plugin插件

Posted thefist11

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了EOS 源代码解读 plugin插件相关的知识,希望对你有一定的参考价值。

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

2. plugin 类

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 initialized
         }

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

3. XML类图

以上是关于EOS 源代码解读 plugin插件的主要内容,如果未能解决你的问题,请参考以下文章

EOS 源代码解读 插件-流程

EOS 源代码解读 插件-set_program_options

EOS代码分析5 接收网络信息

ranger源代码解读插件原理(HDFS)

EOS生产区块:解析插件producer_plugin

刘文彬 EOS行为核心:解析插件chain_plugin