Kagome
Polkadot Runtime Engine in C++17
app_state_manager.hpp
Go to the documentation of this file.
1 
6 #ifndef KAGOME_APPLICATION_DISPATCHER
7 #define KAGOME_APPLICATION_DISPATCHER
8 
9 #include "log/logger.hpp"
10 
11 namespace kagome::application {
12 
13  class AppStateManager : public std::enable_shared_from_this<AppStateManager> {
14  public:
15  using OnPrepare = std::function<bool()>;
16  using OnLaunch = std::function<bool()>;
17  using OnShutdown = std::function<void()>;
18 
19  enum class State {
20  Init,
21  Prepare,
23  Starting,
24  Works,
27  };
28 
29  virtual ~AppStateManager() = default;
30 
35  virtual void atPrepare(OnPrepare &&cb) = 0;
36 
41  virtual void atLaunch(OnLaunch &&cb) = 0;
42 
47  virtual void atShutdown(OnShutdown &&cb) = 0;
48 
55  void registerHandlers(OnPrepare &&prepare_cb,
56  OnLaunch &&launch_cb,
57  OnShutdown &&shutdown_cb) {
58  atPrepare(std::move(prepare_cb));
59  atLaunch(std::move(launch_cb));
60  atShutdown(std::move(shutdown_cb));
61  }
62 
68  template <typename Controlled>
69  void takeControl(Controlled &entity) {
70  registerHandlers([&entity]() -> bool { return entity.prepare(); },
71  [&entity]() -> bool { return entity.start(); },
72  [&entity]() -> void { return entity.stop(); });
73  }
74 
76  virtual void run() = 0;
77 
79  virtual void shutdown() = 0;
80 
82  virtual State state() const = 0;
83 
84  protected:
85  virtual void doPrepare() = 0;
86  virtual void doLaunch() = 0;
87  virtual void doShutdown() = 0;
88  };
89 
90  struct AppStateException : public std::runtime_error {
91  explicit AppStateException(std::string message)
92  : std::runtime_error("Wrong workflow at " + std::move(message)) {}
93  };
94 } // namespace kagome::application
95 
96 #endif // KAGOME_APPLICATION_DISPATCHER
virtual void atShutdown(OnShutdown &&cb)=0
Execute.
virtual void run()=0
Start application life cycle.
STL namespace.
virtual void atPrepare(OnPrepare &&cb)=0
Execute.
virtual void atLaunch(OnLaunch &&cb)=0
Execute.
void registerHandlers(OnPrepare &&prepare_cb, OnLaunch &&launch_cb, OnShutdown &&shutdown_cb)
Registration of all stages&#39; handlers at the same time.
virtual void shutdown()=0
Initiate shutting down (at any time)
void takeControl(Controlled &entity)
Registration special methods of object as handlers for stages of application life-cycle.
virtual State state() const =0
Get current stage.