FairMQ  1.3.9
C++ Message Passing Framework
FairMQStateMachine.h
1 /********************************************************************************
2  * Copyright (C) 2014 GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
3  * *
4  * This software is distributed under the terms of the *
5  * GNU Lesser General Public Licence (LGPL) version 3, *
6  * copied verbatim in the file "LICENSE" *
7  ********************************************************************************/
15 #ifndef FAIRMQSTATEMACHINE_H_
16 #define FAIRMQSTATEMACHINE_H_
17 
18 #define FAIRMQ_INTERFACE_VERSION 3
19 
20 #include "FairMQLogger.h"
21 
22 #include <string>
23 #include <memory>
24 #include <functional>
25 #include <mutex>
26 
28 {
29  public:
30  enum Event
31  {
32  INIT_DEVICE,
33  internal_DEVICE_READY,
34  INIT_TASK,
35  internal_READY,
36  RUN,
37  PAUSE,
38  STOP,
39  RESET_TASK,
40  RESET_DEVICE,
41  internal_IDLE,
42  END,
43  ERROR_FOUND
44  };
45 
46  enum State
47  {
48  OK,
49  Error,
50  IDLE,
51  INITIALIZING_DEVICE,
52  DEVICE_READY,
53  INITIALIZING_TASK,
54  READY,
55  RUNNING,
56  PAUSED,
57  RESETTING_TASK,
58  RESETTING_DEVICE,
59  EXITING
60  };
61 
63  virtual ~FairMQStateMachine();
64 
65  int GetInterfaceVersion() const;
66 
67  bool ChangeState(int event);
68  bool ChangeState(const std::string& event);
69 
70  void WaitForEndOfState(int event);
71  void WaitForEndOfState(const std::string& event);
72 
73  bool WaitForEndOfStateForMs(int event, int durationInMs);
74  bool WaitForEndOfStateForMs(const std::string& event, int durationInMs);
75 
76  void SubscribeToStateChange(const std::string& key, std::function<void(const State)> callback);
77  void UnsubscribeFromStateChange(const std::string& key);
78 
79  void CallStateChangeCallbacks(const State state) const;
80 
81  std::string GetCurrentStateName() const;
82  static std::string GetStateName(const State);
83  int GetCurrentState() const;
84  bool CheckCurrentState(int state) const;
85  bool CheckCurrentState(const std::string& state) const;
86 
87  // actions to be overwritten by derived classes
88  virtual void InitWrapper() {}
89  virtual void InitTaskWrapper() {}
90  virtual void RunWrapper() {}
91  virtual void PauseWrapper() {}
92  virtual void ResetWrapper() {}
93  virtual void ResetTaskWrapper() {}
94  virtual void Exit() {}
95  virtual void Unblock() {}
96 
97  void ProcessWork();
98 
99  private:
100  static int GetEventNumber(const std::string& event);
101 
102  std::mutex fChangeStateMutex;
103 
104  std::shared_ptr<void> fFsm;
105 };
106 
107 #endif /* FAIRMQSTATEMACHINE_H_ */
Definition: FairMQStateMachine.h:27

privacy