DABC (Data Acquisition Backbone Core)  2.9.9
Command.h
Go to the documentation of this file.
1 // $Id: Command.h 4467 2020-04-15 12:35:01Z 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_Command
17 #define DABC_Command
18 
19 #ifndef DABC_Record
20 #include "dabc/Record.h"
21 #endif
22 
23 #ifndef DABC_timing
24 #include "dabc/timing.h"
25 #endif
26 
27 #include <list>
28 
29 
30 namespace dabc {
31 
32  class CommandsQueue;
33  class Worker;
34  class Thread;
35 
36  enum CommandRes {
37  cmd_false = 0,
38  cmd_true = 1,
39 
40  cmd_timedout = -1, // command execution timedout
41  cmd_ignore = -2, // returned when command was ignored or rejected
42  cmd_postponed = -3, // command will be executed later
44  cmd_canceled = -5
45  };
46 
47 
57 
58  friend class Command;
59 
60  protected:
61 
62  struct CallerRec {
63  Worker *worker{nullptr};
64  bool *exe_ready{nullptr};
65  CallerRec() = default;
66  CallerRec(Worker* w, bool* e) : worker(w), exe_ready(e) {}
67  CallerRec(const CallerRec& src) : worker(src.worker), exe_ready(src.exe_ready) {}
68 
70  {
71  worker = src.worker;
72  exe_ready = src.exe_ready;
73  return *this;
74  }
75  };
76 
77  std::list<CallerRec> fCallers;
79  bool fCanceled;
80 
81  // make destructor protected that nobody can delete command directly
82  CommandContainer(const std::string &name = "Command");
83 
84  virtual ~CommandContainer();
85 
86  virtual const char* ClassName() const { return "Command"; }
87  };
88 
99  class Command : public Record {
100 
102 
103  friend class CommandContainer;
104  friend class CommandsQueue;
105  friend class Worker;
106  friend class Thread;
107 
108  private:
109 
110  void AddCaller(Worker *worker, bool *exe_ready = nullptr);
111  void RemoveCaller(Worker *worker, bool *exe_ready = nullptr);
112 
113  bool IsLastCallerSync();
114 
115  protected:
116 
117  virtual void CreateRecord(const std::string &name) { SetObject(new CommandContainer(name)); }
118 
119  public:
121  // Command() {}
122 
123  Command(const std::string &name) throw();
124 
126  void ChangeName(const std::string &name);
127 
129  friend int operator==(const Command& cmd1, const Command& cmd2)
130  { return cmd1() == cmd2(); }
131 
132  // set of methods to keep old interface, it is preferable to use field methods
133 
134  bool SetStr(const std::string &name, const char* value) { return value==0 ? RemoveField(name) : SetField(name, value); }
135  bool SetStr(const std::string &name, const std::string &value) { return SetField(name, value); }
136  std::string GetStr(const std::string &name, const std::string &dflt = "") const { return GetField(name).AsStr(dflt); }
137 
138  bool SetInt(const std::string &name, int v) { return SetField(name, v); }
139  int GetInt(const std::string &name, int dflt = 0) const { return GetField(name).AsInt(dflt); }
140 
141  bool SetBool(const std::string &name, bool v) { return SetField(name, v); }
142  bool GetBool(const std::string &name, bool dflt = false) const { return GetField(name).AsBool(dflt); }
143 
144  bool SetDouble(const std::string &name, double v) { return SetField(name, v); }
145  double GetDouble(const std::string &name, double dflt = 0.) const { return GetField(name).AsDouble(dflt); }
146 
147  bool SetUInt(const std::string &name, unsigned v) { return SetField(name, v); }
148  unsigned GetUInt(const std::string &name, unsigned dflt = 0) const { return GetField(name).AsUInt(dflt); }
149 
151  void SetPtr(const std::string &name, void* p);
153  void* GetPtr(const std::string &name, void* deflt = 0) const;
154 
156  bool SetRef(const std::string &name, Reference ref);
157 
159  Reference GetRef(const std::string &name);
160 
162  bool SetRawData(Buffer rawdata);
163 
165  bool SetStrRawData(const std::string &str);
166 
169  Buffer GetRawData();
170 
171  void AddValuesFrom(const Command& cmd, bool canoverwrite = true);
172 
173  void SetResult(int res) { SetInt(ResultParName(), res); }
174  int GetResult() const { return GetInt(ResultParName(), cmd_false); }
176  bool HasResult() const { return HasField(ResultParName()); }
177 
183  Command& SetTimeout(double tm);
184 
186  bool IsTimeoutSet() const;
187 
192  double TimeTillTimeout(double extra = 0.) const;
193 
195  bool IsTimedout() const { return TimeTillTimeout() == 0.; }
196 
198  void ResetTimeout() { SetTimeout(-1); }
199 
209  bool ReadFromCmdString(const std::string &str);
210 
213  void SetPriority(int prio) { SetInt(PriorityParName(), prio); }
214 
216  int GetPriority() const;
217 
219  void Print(int lvl=0, const char* from = nullptr) const;
220 
229  void Reply(int res = cmd_noresult);
230 
233 
235  void ReplyTrue() { Reply(cmd_true); }
236 
238  void ReplyBool(bool res) { Reply(res ? cmd_true : cmd_false); }
239 
242 
245  void Release();
246 
250  void Cancel();
251 
253  bool IsCanceled();
254 
264  Command& SetReceiver(const std::string &itemname)
265  { SetStr(ReceiverParName(), itemname); return *this; }
266 
267  std::string GetReceiver() const { return GetStr(ReceiverParName()); }
269 
272  static const char* ReceiverParName() { return "_Receiver_"; }
273 
275  static const char* ResultParName() { return "_Result_"; }
276 
278  static const char* PriorityParName() { return "_Priority_"; }
279  };
280 
281 
282 #define DABC_COMMAND(cmd_class, cmd_name) \
283  public: \
284  static const char* CmdName() { return cmd_name; } \
285  cmd_class() : dabc::Command(CmdName()) {} \
286  cmd_class(const dabc::Command& src) : dabc::Command(src) \
287  { if (!src.IsName(CmdName())) throw dabc::Exception(dabc::ex_Command, "Wrong command name in assignment constructor", src.GetName()); } \
288  cmd_class& operator=(const cmd_class& cmd) { dabc::Command::operator=(cmd); return *this; } \
289  cmd_class& operator=(const dabc::Command& cmd) { \
290  if (!cmd.IsName(CmdName())) throw dabc::Exception(dabc::ex_Command, "Wrong command name in assignment operator", cmd.GetName()); \
291  dabc::Command::operator=(cmd); return *this; \
292  }
293 
294 }
295 
296 #endif
#define DABC_REFERENCE(RefClass, ParentClass, T)
Definition: Reference.h:222
Reference on memory from memory pool.
Definition: Buffer.h:135
Container object for command which should not been seen to normal user.
Definition: Command.h:56
virtual const char * ClassName() const
Returns class name of the object instance.
Definition: Command.h:86
bool fCanceled
indicate if command was canceled ant not need to be executed further
Definition: Command.h:79
std::list< CallerRec > fCallers
list of callers
Definition: Command.h:77
virtual ~CommandContainer()
Definition: Command.cxx:40
TimeStamp fTimeout
absolute time when timeout will be expired
Definition: Command.h:78
CommandContainer(const std::string &name="Command")
Definition: Command.cxx:22
Represents command with its arguments.
Definition: Command.h:99
double TimeTillTimeout(double extra=0.) const
Returns time which remains until command should be timed out.
Definition: Command.cxx:132
virtual void CreateRecord(const std::string &name)
Definition: Command.h:117
void ReplyBool(bool res)
Reply on the command with true or false value.
Definition: Command.h:238
static const char * ReceiverParName()
Name of the parameter, used to specified command receiver.
Definition: Command.h:272
void ResetTimeout()
Disable timeout for the command.
Definition: Command.h:198
bool ReadFromCmdString(const std::string &str)
Read command from string, which is typed in std output.
Definition: Command.cxx:292
bool IsLastCallerSync()
Definition: Command.cxx:96
void SetPtr(const std::string &name, void *p)
Set pointer argument for the command.
Definition: Command.cxx:151
static const char * PriorityParName()
Name of the parameter, used to keep command priority.
Definition: Command.h:278
unsigned GetUInt(const std::string &name, unsigned dflt=0) const
Definition: Command.h:148
void ClearResult()
Definition: Command.h:175
void AddCaller(Worker *worker, bool *exe_ready=nullptr)
Definition: Command.cxx:68
bool SetStr(const std::string &name, const char *value)
Definition: Command.h:134
bool SetStrRawData(const std::string &str)
Set raw data with string content.
Definition: Command.cxx:340
void Cancel()
Call this method to cancel command execution.
Definition: Command.cxx:203
void ReplyTimedout()
Reply on the command with timeout value.
Definition: Command.h:241
bool SetBool(const std::string &name, bool v)
Definition: Command.h:141
friend int operator==(const Command &cmd1, const Command &cmd2)
Compare operator, returns true when command contains pointer on same object.
Definition: Command.h:129
double GetDouble(const std::string &name, double dflt=0.) const
Definition: Command.h:145
bool SetInt(const std::string &name, int v)
Definition: Command.h:138
bool SetRawData(Buffer rawdata)
Set raw data to the command, which can be transported also between nodes.
Definition: Command.cxx:334
std::string GetStr(const std::string &name, const std::string &dflt="") const
Definition: Command.h:136
Command & SetTimeout(double tm)
Set maximum time which can be used for command execution.
Definition: Command.cxx:108
friend class CommandContainer
Definition: Command.h:103
void ReplyFalse()
Reply on the command with false (cmd_false==0) value.
Definition: Command.h:232
bool IsTimeoutSet() const
Return true if timeout was specified.
Definition: Command.cxx:124
void SetResult(int res)
Definition: Command.h:173
bool SetUInt(const std::string &name, unsigned v)
Definition: Command.h:147
void Release()
Method used to clean command - all internal data will be cleaned, command container will be released.
Definition: Command.cxx:198
void AddValuesFrom(const Command &cmd, bool canoverwrite=true)
Definition: Command.cxx:187
Command & SetReceiver(const std::string &itemname)
These methods prepare command so, that one can submit command to the manager like: dabc::mgr....
Definition: Command.h:264
void ReplyTrue()
Reply on the command with true (cmd_true==1) value.
Definition: Command.h:235
int GetResult() const
Definition: Command.h:174
bool GetBool(const std::string &name, bool dflt=false) const
Definition: Command.h:142
Reference GetRef(const std::string &name)
Returns reference from the command, can be called only once.
Definition: Command.cxx:175
static const char * ResultParName()
Name of the parameter, used to keep command result.
Definition: Command.h:275
bool SetDouble(const std::string &name, double v)
Definition: Command.h:144
void Print(int lvl=0, const char *from=nullptr) const
Show on debug output content of command.
Definition: Command.cxx:193
bool IsCanceled()
Return true if command was canceled.
Definition: Command.cxx:214
bool IsTimedout() const
Returns true if command timeout is expired.
Definition: Command.h:195
int GetInt(const std::string &name, int dflt=0) const
Definition: Command.h:139
Buffer GetRawData()
Returns reference on raw data Can be called only once - raw data reference will be cleaned.
Definition: Command.cxx:347
void Reply(int res=cmd_noresult)
Replied on the command.
Definition: Command.cxx:225
void ChangeName(const std::string &name)
Change command name, should not be used for remote commands.
Definition: Command.cxx:62
void RemoveReceiver()
Definition: Command.h:268
bool HasResult() const
Definition: Command.h:176
void SetPriority(int prio)
Set command priority, defines how fast command should be treated In special cases priority allows to ...
Definition: Command.h:213
bool SetStr(const std::string &name, const std::string &value)
Definition: Command.h:135
std::string GetReceiver() const
Definition: Command.h:267
bool SetRef(const std::string &name, Reference ref)
Set reference to the command.
Definition: Command.cxx:168
void RemoveCaller(Worker *worker, bool *exe_ready=nullptr)
Definition: Command.cxx:78
int GetPriority() const
Returns command priority.
Definition: Command.cxx:146
void * GetPtr(const std::string &name, void *deflt=0) const
Get pointer argument from the command.
Definition: Command.cxx:158
Queue of commands
Definition: CommandsQueue.h:39
Container for records fields.
Definition: Record.h:440
uint64_t AsUInt(uint64_t dflt=0) const
Definition: Record.cxx:525
bool AsBool(bool dflt=false) const
Definition: Record.cxx:477
std::string AsStr(const std::string &dflt="") const
Definition: Record.cxx:749
int64_t AsInt(int64_t dflt=0) const
Definition: Record.cxx:501
double AsDouble(double dflt=0.) const
Definition: Record.cxx:549
RecordField GetField(const std::string &name) const
Definition: Record.h:510
bool HasField(const std::string &name) const
Definition: Record.h:498
bool SetField(const std::string &name, const RecordField &v)
Definition: Record.h:516
bool RemoveField(const std::string &name)
Definition: Record.h:501
Reference on the arbitrary object
Definition: Reference.h:73
void SetObject(Object *obj, bool withmutex=true)
Direct set of object to reference.
Definition: Reference.cxx:37
friend class Command
Definition: Reference.h:76
Represent thread functionality.
Definition: Thread.h:109
Active object, which is working inside dabc::Thread.
Definition: Worker.h:116
Event manipulation API.
Definition: api.h:23
CommandRes
Definition: Command.h:36
@ cmd_noresult
Definition: Command.h:43
@ cmd_postponed
Definition: Command.h:42
@ cmd_false
Definition: Command.h:37
@ cmd_timedout
Definition: Command.h:40
@ cmd_ignore
Definition: Command.h:41
@ cmd_true
Definition: Command.h:38
@ cmd_canceled
Definition: Command.h:44
CallerRec()=default
pointer on variable, which is used to indicate that execution is done
CallerRec & operator=(CallerRec &src)
Definition: Command.h:69
CallerRec(const CallerRec &src)
Definition: Command.h:67
bool * exe_ready
pointer on worker, do not use reference while worker will care about correct cleanup
Definition: Command.h:64
CallerRec(Worker *w, bool *e)
Definition: Command.h:66
Class for acquiring and holding timestamps.
Definition: timing.h:40