DABC (Data Acquisition Backbone Core)  2.9.9
Device.cxx
Go to the documentation of this file.
1 // $Id: Device.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/Device.h"
17 
18 #include "dabc/Manager.h"
19 #include "dabc/Transport.h"
20 
21 
22 dabc::Device::Device(const std::string &name) :
23  Worker(MakePair(name)),
24  fDeviceDestroyed(false)
25 {
26  DOUT3("Device %p %s %s constructor prior:%d", this, GetName(), ItemName().c_str(), WorkerPriority());
27 }
28 
30 {
31  DOUT3("Device %s destr prior:%d", GetName(), WorkerPriority());
32 }
33 
34 
36 {
37  fDeviceDestroyed = true;
38 
40 }
41 
42 
43 int dabc::Device::ExecuteCommand(Command cmd)
44 {
45  if (cmd.IsName(CmdCreateTransport::CmdName())) {
46  CmdCreateTransport crcmd = cmd;
47 
48  PortRef port = dabc::mgr.FindPort(crcmd.PortName());
49 
50  if (port.null()) return cmd_false;
51 
52  TransportRef tr = CreateTransport(cmd, port);
53 
54  DOUT1("Device %s creates transport %p", GetName(), tr());
55 
56  if (tr.null()) return cmd_false;
57 
58  std::string thrdname = port.Cfg(xmlThreadAttr, cmd).AsStr();
59  if (thrdname.empty()) thrdname = ThreadName();
60 
61  DOUT1("Device %s Create thread %s for transport", GetName(), thrdname.c_str());
62 
63  // set reference on device
64  tr()->fTransportDevice = this;
65 
66  if (!tr.MakeThreadForWorker(thrdname)) {
67  EOUT("Fail to create thread for transport");
68  tr.Destroy();
69  return cmd_false;
70  }
71 
72  tr.ConnectPoolHandles();
73  if (port.IsInput())
74  dabc::LocalTransport::ConnectPorts(tr.OutputPort(), port, cmd);
75  if (port.IsOutput())
76  dabc::LocalTransport::ConnectPorts(port, tr.InputPort(), cmd);
77 
78  DOUT1("Device %s transport is created for port %p %s", GetName(), port(), port.ItemName().c_str());
79 
80  return cmd_true;
81  }
82 
83  return dabc::Worker::ExecuteCommand(cmd);
84 }
85 
86 
87 bool dabc::Device::Find(ConfigIO &cfg)
88 {
89  DOUT4("Device::Find %p name = %s parent %p", this, GetName(), GetParent());
90 
91  if (GetParent()==0) return false;
92 
93  // module will always have tag "Device", class could be specified with attribute
94  while (cfg.FindItem(xmlDeviceNode)) {
95  if (cfg.CheckAttr(xmlNameAttr, GetName())) return true;
96  }
97 
98  return false;
99 }
100 
virtual void ObjectCleanup()
Central cleanup method for worker.
Definition: Device.cxx:35
virtual bool Find(ConfigIO &cfg)
Method to locate object in xml file.
Definition: Device.cxx:87
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Definition: Device.cxx:43
virtual ~Device()
Definition: Device.cxx:29
Device(const std::string &name)
Definition: Device.cxx:22
static int ConnectPorts(Reference port1ref, Reference port2ref, Command cmd=nullptr)
Reference FindPort(const std::string &port)
Definition: Manager.cxx:2059
const char * GetName() const
Returns name of the object, thread safe
Definition: Object.h:295
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Definition: Worker.cxx:851
int WorkerPriority() const
Definition: Worker.h:260
virtual void ObjectCleanup()
Central cleanup method for worker.
Definition: Worker.cxx:238
#define DOUT3(args ...)
Definition: logging.h:176
#define EOUT(args ...)
Definition: logging.h:150
#define DOUT1(args ...)
Definition: logging.h:162
#define DOUT4(args ...)
Definition: logging.h:182
XMLNodePointer_t GetParent(XMLNodePointer_t xmlnode)
Definition: XmlEngine.cxx:917
ManagerRef mgr
Definition: Manager.cxx:42
const char * xmlThreadAttr
Definition: ConfigBase.cxx:38
const char * xmlNameAttr
Definition: ConfigBase.cxx:33
const char * xmlDeviceNode
Definition: Object.cxx:30
@ cmd_false
Definition: Command.h:37
@ cmd_true
Definition: Command.h:38