DABC (Data Acquisition Backbone Core)  2.9.9
ModuleItem.cxx
Go to the documentation of this file.
1 // $Id: ModuleItem.cxx 4476 2020-04-15 14:12:38Z 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 #include "dabc/ModuleItem.h"
17 
18 #include "dabc/Manager.h"
19 
20 dabc::ModuleItem::ModuleItem(int typ, Reference parent, const std::string &name) :
21  Worker(parent, name.c_str()),
22  fItemType(typ),
23  fItemId(0),
24  fSubId(0)
25 {
26  // we should reinitialize variable, while it will be cleared by constructor
27  parent = GetParentRef();
28 
29  Module* m = dynamic_cast<Module*> (GetParent());
30 
31  if (m==0)
32  throw dabc::Exception(ex_Generic, "Item created without module or in wrong place of hierarchy", ItemName());
33 
34  // use module priority for the all port items
36 }
37 
39 {
40  DOUT2("ModuleItem:%p start destructor", this);
41 
42  Module* m = dynamic_cast<Module*> (GetParent());
43 
44  if (m) m->RemoveModuleItem(this);
45 
46  DOUT2("ModuleItem:%p did destructor", this);
47 }
48 
49 
51 {
52  Module* m = dynamic_cast<Module*> (GetParent());
53  if (m) m->DoStop();
54 }
55 
56 
58 {
59  Module* m = dynamic_cast<Module*> (GetParent());
60  if (m) m->DoStart();
61 }
62 
63 
64 // ========================================================
65 
67 {
68  Reference parent = GetParent();
69 
70  while (!parent.null()) {
71  ModuleRef module = parent;
72  if (!module.null()) return module;
73  parent = parent.GetParent();
74  }
75 
76  return dabc::WorkerRef();
77 }
78 
80 {
81  ModuleRef module = GetModule();
82  return module.InfoParName();
83 }
84 
85 // ==============================================================================
86 
87 
88 dabc::Timer::Timer(Reference parent, bool systimer, const std::string &name, double period, bool synchron) :
89  ModuleItem(mitTimer, parent, name),
90  fSysTimer(systimer),
91  fPeriod(period),
92  fCounter(0),
93  fActive(false),
94  fSynhron(synchron),
95  fInaccuracy(0.)
96 {
97 }
98 
99 
101 {
102 }
103 
105 {
106  fActive = true;
107 
108  // for sys timer timeout will be activated by module itself
109  if ((fPeriod>0.) && !IsSysTimer())
110  ActivateTimeout(fPeriod);
111 }
112 
114 {
115  fActive = false;
116 
117  // for sys timer timeout will be deactivated by module itself
118  if (!IsSysTimer()) ActivateTimeout(-1);
119 }
120 
121 
122 double dabc::Timer::ProcessTimeout(double last_diff)
123 {
124  if (!fActive) return -1.;
125 
126  fCounter++;
127 
128  Module* m = (Module*) GetParent();
129 
130  if (m && m->IsRunning())
131  m->ProcessItemEvent(this, evntTimeout);
132 
133  // if we have not synchron timer, just fire next event
134  if (!fSynhron) return fPeriod;
135 
136  fInaccuracy += (last_diff - fPeriod);
137 
138  double res = fPeriod - fInaccuracy;
139  if (res < 0) res = 0;
140 
141  //DOUT0("Timer %s lastdif = %5.4f next %5.4f", GetName(), last_diff, res);
142 
143  return res;
144 }
145 
146 
147 // ==============================================================================
148 
149 dabc::ConnTimer::ConnTimer(Reference parent, const std::string &name, const std::string &portname) :
150  ModuleItem(mitConnTimer, parent, name),
151  fPortName(portname),
152  fErrorFlag(false)
153 {
154 }
155 
157 {
158  Module *m = dynamic_cast<Module *> (GetParent());
159 
160  // DOUT0("ConnTimer::ProcessTimeout m = %s", DNAME(m));
161 
162  if (!m) return -1;
163 
164  PortRef port = m->FindPort(fPortName);
165  if (port.null()) return -1.;
166 
167  std::string itemname = port.ItemName();
168 
169  if (!port()->IsDoingReconnect()) return -1;
170 
171  // DOUT0("Trying to reconnect port %s", itemname.c_str());
172 
173  if (port.IsConnected() || dabc::mgr.CreateTransport(itemname)) {
174  port()->SetDoingReconnect(false);
175  // DOUT0("Port %s connected again", itemname.c_str());
176  return -1.;
177  }
178 
179  if (!port()->TryNextReconnect(fErrorFlag)) return -1;
180 
181  return port()->GetReconnectPeriod() > 0 ? port()->GetReconnectPeriod() : 1.;
182 }
ConnTimer(Reference parent, const std::string &name, const std::string &portname)
Definition: ModuleItem.cxx:149
virtual double ProcessTimeout(double last_diff)
Definition: ModuleItem.cxx:156
DABC exception.
Definition: Exception.h:57
bool CreateTransport(const std::string &portname, const std::string &transportkind="", const std::string &thrdname="")
Definition: Manager.cxx:2210
WorkerRef GetModule() const
Definition: ModuleItem.cxx:66
std::string InfoParName() const
Definition: ModuleItem.cxx:79
Base class for module items like ports, timers, pool handles.
Definition: ModuleItem.h:68
void StartModule()
Starts module, should be called from module thread.
Definition: ModuleItem.cxx:57
virtual ~ModuleItem()
Definition: ModuleItem.cxx:38
void SetItemPriority(int pri=-1)
Definition: ModuleItem.h:86
ModuleItem(int typ, Reference parent, const std::string &name)
Definition: ModuleItem.cxx:20
void StopModule()
Stops module, should be called from module thread.
Definition: ModuleItem.cxx:50
Reference on dabc::Module class
Definition: Module.h:275
std::string InfoParName() const
Returns info parameter name, used to provide different kind of log/debug information.
Definition: Module.h:323
Base for dabc::ModuleSync and dabc::ModuleAsync classes.
Definition: Module.h:42
void RemoveModuleItem(ModuleItem *item)
Definition: Module.cxx:651
bool IsRunning() const
Returns true if module if running.
Definition: Module.h:109
virtual bool DoStop()
Definition: Module.cxx:586
virtual bool DoStart()
Definition: Module.cxx:563
PortRef FindPort(const std::string &name) const
Definition: Module.cxx:699
virtual void ProcessItemEvent(ModuleItem *item, uint16_t evid)
Definition: Module.h:252
std::string ItemName(bool compact=true) const
Produce string, which can be used as name argument in dabc::mgr.FindItem(name) call.
Definition: Object.cxx:1076
Object * GetParent() const
Returns pointer on parent object, thread safe
Definition: Object.h:286
Reference GetParentRef() const
Definition: Object.h:289
Reference on the dabc::Port class
Definition: Port.h:195
bool IsConnected()
Returns true if port is connected.
Definition: Port.cxx:250
Reference on the arbitrary object
Definition: Reference.h:73
Object * GetParent() const
Returns pointer on parent object.
Definition: Reference.cxx:162
bool null() const
Returns true if reference contains nullptr.
Definition: Reference.h:151
std::string ItemName(bool compact=true) const
Produce string, which can be used as name argument in dabc::mgr.FindItem(name) call.
Definition: Reference.cxx:241
virtual void DoStop()
Definition: ModuleItem.cxx:113
Timer(Reference parent, bool systimer, const std::string &name, double timeout=-1., bool synchron=false)
Definition: ModuleItem.cxx:88
virtual double ProcessTimeout(double last_diff)
Definition: ModuleItem.cxx:122
virtual ~Timer()
Definition: ModuleItem.cxx:100
virtual void DoStart()
Definition: ModuleItem.cxx:104
Reference on dabc::Worker
Definition: Worker.h:466
Active object, which is working inside dabc::Thread.
Definition: Worker.h:116
int WorkerPriority() const
Definition: Worker.h:260
#define DOUT2(args ...)
Definition: logging.h:170
XMLNodePointer_t GetParent(XMLNodePointer_t xmlnode)
Definition: XmlEngine.cxx:917
@ mitTimer
Definition: ModuleItem.h:34
@ mitConnTimer
Definition: ModuleItem.h:35
ManagerRef mgr
Definition: Manager.cxx:42
@ evntTimeout
Definition: ModuleItem.h:45
@ ex_Generic
Definition: Exception.h:41