瑞波核心库Stoppable类讲解

本文详细介绍了瑞波系统中Stoppable类作为基类的应用,阐述了其在构造、启动和停止过程中的关键步骤,包括子组件构建、prepare、stopAsync、stop、onStop和onChildrenStopped等方法的作用,以及如何管理和协调各个依赖对象的停止顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

瑞波核心库Stoppable类主要提供启动和停止接口。


构建服务器或P2P代码的常见方法是将概念上的功能独立到单独的类来实现,进而聚合成更大的“应用程序”或“核心”对象,这些组件通常使用不可避免地相互依赖复杂的方式。他们还经常使用线程并执行异步I / O和 涉及套接字或其他操作系统对象的操作。这个过程开始和停止这样的系统可能是复杂的。这个接口确保了它们启动和停止。

下面是瑞波中使用Stoppable类做为基类的应用类:


                                                     Application
                                                            |
                   +--------------------+--------------------+
                   |                                         |                    |
              LoadManager          SHAMapStore       NodeStoreScheduler
                                                                                 |
                                                                           JobQueue
                                                                                 |
        +-----------+-----------+-----------+-----------+----+--------+
        |           |                    |                      |                        |             |
        |       NetworkOPs      |                  InboundLedgers    |        OrderbookDB
        |                                |                                               |
     Overlay                  InboundTransactions             LedgerMaster
        |                                                                                |

    PeerFinder                                                            LedgerCleaner

在这些类初始化过程中,Stoppable将会执行如下步奏:

    1.  Construct sub-components.
        These are all typically derived from Stoppable. There can be a deep
        hierarchy: Stoppable objects may themselves have Stoppable child

        objects. This captures the relationship of dependencies.

    2.  prepare()

        Because some components may depend on others, preparatory steps require
        that all objects be first constructed. The prepare step calls all
        Stoppable objects in the tree starting from the leaves and working up
        to the root. In this stage we are guaranteed that all objects have been
        constructed and are in a well-defined state.
    3.  onPrepare()
        This override is called for all Stoppable objects in the hierarchy
        during the prepare stage. It is guaranteed that all child Stoppable
        objects have already been prepared when this is called.
        Objects are called children first.
    4.  start()
        At this point all sub-components have been constructed and prepared,
        so it should be safe for them to be started. While some Stoppable
        objects may do nothing in their start function, others will start
        threads or call asynchronous i/o initiating functions like timers or
        sockets.
    5.  onStart()
        This override is called for all Stoppable objects in the hierarchy
        during the start stage. It is guaranteed that no child Stoppable
        objects have been started when this is called.

        Objects are called parent first.

在停止过程中将会顺序触发如下事件:

   6.  stopAsync() [optional]
        This notifies the root Stoppable and all its children that a stop is
        requested.
    7.  stop()
        This first calls stopAsync(), and then blocks on each child Stoppable in
        the in the tree from the bottom up, until the Stoppable indicates it has
        stopped. This will usually be called from the main thread of execution
        when some external signal indicates that the process should stop. For
        example, an RPC 'stop' command, or a SIGINT POSIX signal.
    8.  onStop()
        This override is called for the root Stoppable and all its children when
        stopAsync() is called. Derived classes should cancel pending I/O and
        timers, signal that threads should exit, queue cleanup jobs, and perform
        any other necessary final actions in preparation for exit.
        Objects are called parent first.
    9.  onChildrenStopped()
        This override is called when all the children have stopped. This informs
        the Stoppable that there should not be any more dependents making calls
        into its member functions. A Stoppable that has no children will still
        have this function called.
        Objects are called children first.
    10. stopped()
        The derived class calls this function to inform the Stoppable API that
        it has completed the stop. This unblocks the caller of stop().
        For stoppables which are only considered stopped when all of their
        children have stopped, and their own internal logic indicates a stop, it
        will be necessary to perform special actions in onChildrenStopped(). The
        funtion areChildrenStopped() can be used after children have stopped,
        but before the Stoppable logic itself has stopped, to determine if the
        stoppable's logic is a true stop.
        Pseudo code for this process is as follows:
        @code
        // Returns `true` if derived logic has stopped.
        //
        // When the logic stops, logicProcessingStop() is no longer called.
        // If children are still active we need to wait until we get a
        // notification that the children have stopped.
        //
        bool logicHasStopped ();
        // Called when children have stopped
        void onChildrenStopped ()
        {
            // We have stopped when the derived logic stops and children stop.
            if (logicHasStopped)
                stopped();
        }
        // derived-specific logic that executes periodically
        void logicProcessingStep ()
        {
            // process
            // ...
            // now see if we've stopped
            if (logicHasStopped() && areChildrenStopped())
                stopped();
        }
        @endcode
        Derived class that manage one or more threads should typically notify
        those threads in onStop that they should exit. In the thread function,
        when the last thread is about to exit it would call stopped().
    @note A Stoppable may not be restarted.

如上图可知道,Application为所有继承于Stoppable起源类,在Stoppable.h 和Stoppable.cpp中专门构建了

class RootStoppable : public Stoppable 用于管理所有其他Stoppable的 prepare,start,stop,以及isStopping,started查询。


博主QQ: 122209017

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值