DABC (Data Acquisition Backbone Core)  2.9.9
Application.h
Go to the documentation of this file.
1 // $Id: Application.h 4781 2021-07-14 12:28:15Z linev $
2 
3 /************************************************************
4  * The Data Acquisition Backbone Core (DABC) *
5  ************************************************************
6  * Copyright (C) 2009 - *
7  * GSI Helmholtzzentrum fuer Schwerionenforschung GmbH *
8  * Planckstr. 1, 64291 Darmstadt, Germany *
9  * Contact: http://dabc.gsi.de *
10  ************************************************************
11  * This software can be used under the GPL license *
12  * agreements as stated in LICENSE.txt file *
13  * which is part of the distribution. *
14  ************************************************************/
15 
16 #ifndef DABC_Application
17 #define DABC_Application
18 
19 #ifndef DABC_Worker
20 #include "dabc/Worker.h"
21 #endif
22 
23 namespace dabc {
24 
25  class CmdStateTransition : public Command {
26  DABC_COMMAND(CmdStateTransition, "CmdStateTransition");
27 
28  CmdStateTransition(const std::string &state) :
29  dabc::Command(CmdName())
30  {
31  SetStr("State", state);
32  }
33  };
34 
35 
36  class Manager;
37  class ApplicationRef;
38 
73  class Application : public Worker {
74 
75  friend class Manager;
76  friend class ApplicationRef;
77 
78  public:
79 
80  typedef void* ExternalFunction();
81 
82  // these are states in default state machine
83  static const char* stHalted() { return "Halted"; }
84  static const char* stReady() { return "Ready"; }
85  static const char* stRunning() { return "Running"; }
86  static const char* stFailure() { return "Failure"; }
87  static const char* stTransition() { return "Transition"; }
88 
89  // these are commands provided by state machine
90  static const char* stcmdDoConfigure() { return "DoConfigure"; }
91  static const char* stcmdDoStart() { return "DoStart"; }
92  static const char* stcmdDoStop() { return "DoStop"; }
93  static const char* stcmdDoHalt() { return "DoHalt"; }
94 
95  private:
96 
97  int CallInitFunc(Command statecmd, const std::string &tgtstate);
98 
99  protected:
100 
101  std::string fAppClass;
102 
104 
106 
108 
109  double fConnTimeout;
110 
111  std::vector<std::string> fAppDevices;
112  std::vector<std::string> fAppPools;
113  std::vector<std::string> fAppModules;
114 
115  virtual int ExecuteCommand(Command cmd);
116  virtual bool ReplyCommand(Command cmd);
117 
119  virtual void OnThreadAssigned();
120 
122  virtual void ObjectCleanup();
123 
125  void SetAppState(const std::string &name);
126 
128  void SetInitFunc(ExternalFunction* initfunc);
129 
131  virtual bool IsModulesRunning();
132 
134  virtual bool CreateAppModules() { return false; }
135 
137  virtual bool StartModules();
138 
140  virtual bool StopModules();
141 
143  virtual bool CleanupApplication();
144 
146  virtual int DoTransition(const std::string &state, Command cmd);
147 
149  virtual int SMCommandTimeout() const { return 10; }
150 
151  virtual void BuildFieldsMap(RecordFieldsMap* cont);
152 
154  virtual double ProcessTimeout(double);
155 
156  public:
157 
158  Application(const char* classname = 0);
159  virtual ~Application();
160 
161  static const char* StateParName() { return "State"; }
162 
163  std::string GetState() const { return Par(StateParName()).Value().AsStr(); }
164 
165  virtual bool Find(ConfigIO &cfg);
166 
167  virtual const char* ClassName() const { return fAppClass.c_str(); }
168  };
169 
170  // ________________________________________________________________________________
171 
172 
178  class ApplicationRef : public WorkerRef {
179 
181 
182  bool ChangeState(const std::string &state)
183  { return Execute(CmdStateTransition(state)); }
184 
186  { return Execute("StartAllModules"); }
187 
189  { return Execute("StopAllModules"); }
190 
196  bool AddObject(const std::string &kind, const std::string &name);
197 
198  };
199 
200 
201 
202 }
203 
204 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Reference on dabc::Application class
Definition: Application.h:178
bool AddObject(const std::string &kind, const std::string &name)
Adds object into application list List used when objects must be destroyed or application start/stop ...
bool ChangeState(const std::string &state)
Definition: Application.h:182
Base class for user-specific applications.
Definition: Application.h:73
std::string fAppClass
Definition: Application.h:101
virtual int DoTransition(const std::string &state, Command cmd)
Do action, required to make transition into specified state.
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Definition: Application.cxx:89
virtual bool IsModulesRunning()
Return true if all application-relevant modules are running.
static const char * stcmdDoHalt()
Definition: Application.h:93
void * ExternalFunction()
Definition: Application.h:80
virtual bool Find(ConfigIO &cfg)
Method to locate object in xml file.
virtual void OnThreadAssigned()
Method called at thread assignment - application may switch into running state.
Definition: Application.cxx:54
bool fAnyModuleWasRunning
indicate when any module was running, than once can automatically stop application
Definition: Application.h:105
static const char * StateParName()
Definition: Application.h:161
static const char * stcmdDoStop()
Definition: Application.h:92
virtual int SMCommandTimeout() const
Default state machine command timeout.
Definition: Application.h:149
static const char * stFailure()
Definition: Application.h:86
void SetInitFunc(ExternalFunction *initfunc)
Set external function, which creates all necessary components of the application.
virtual void BuildFieldsMap(RecordFieldsMap *cont)
Fill fields map, which is relevant for the object Objects hierarchy produced from dabc::Manager.
void SetAppState(const std::string &name)
Directly changes value of the state parameter.
std::string GetState() const
Definition: Application.h:163
Application(const char *classname=0)
Definition: Application.cxx:24
static const char * stHalted()
Definition: Application.h:83
bool fSelfControl
when true, application itself decide when stop main loop
Definition: Application.h:107
static const char * stReady()
Definition: Application.h:84
virtual bool StartModules()
Start all application modules.
virtual double ProcessTimeout(double)
Timeout used by application to control stop state of modules and brake application.
static const char * stTransition()
Definition: Application.h:87
static const char * stRunning()
Definition: Application.h:85
virtual bool ReplyCommand(Command cmd)
Reimplement this method to react on command reply Return true if command can be destroyed by framewor...
Definition: Application.cxx:66
virtual bool CreateAppModules()
Create application modules.
Definition: Application.h:134
virtual bool CleanupApplication()
Delete all components created in application, excluding state parameter.
std::vector< std::string > fAppDevices
list of devices, created by application
Definition: Application.h:111
std::vector< std::string > fAppPools
list of pools, created by application
Definition: Application.h:112
virtual const char * ClassName() const
Returns class name of the object instance.
Definition: Application.h:167
ExternalFunction * fInitFunc
Definition: Application.h:103
double fConnTimeout
time for connecting all modules, default 5s
Definition: Application.h:109
virtual ~Application()
Definition: Application.cxx:50
static const char * stcmdDoConfigure()
Definition: Application.h:90
int CallInitFunc(Command statecmd, const std::string &tgtstate)
virtual bool StopModules()
Stop all application modules.
virtual void ObjectCleanup()
Cleanup application.
Definition: Application.cxx:60
std::vector< std::string > fAppModules
list of modules, created by application
Definition: Application.h:113
static const char * stcmdDoStart()
Definition: Application.h:91
CmdStateTransition(const std::string &state)
Definition: Application.h:28
DABC_COMMAND(CmdStateTransition, "CmdStateTransition")
Represents command with its arguments.
Definition: Command.h:99
bool SetStr(const std::string &name, const char *value)
Definition: Command.h:134
Interface class between xml configuration and dabc objects.
Definition: ConfigIO.h:38
Manager of everything in DABC
Definition: Manager.h:291
RecordField Value() const
Returns parameter value.
Definition: Parameter.h:202
std::string AsStr(const std::string &dflt="") const
Definition: Record.cxx:749
Reference on dabc::Worker
Definition: Worker.h:466
bool Execute(Command cmd, double tmout=-1.)
Definition: Worker.cxx:1147
Active object, which is working inside dabc::Thread.
Definition: Worker.h:116
Parameter Par(const std::string &name) const
Returns reference on worker parameter object.
Definition: Worker.cxx:516
Event manipulation API.
Definition: api.h:23