DABC (Data Acquisition Backbone Core)  2.9.9
ConnectionRequest.cxx
Go to the documentation of this file.
1 // $Id: ConnectionRequest.cxx 3862 2018-05-11 10:06:18Z 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/ConnectionRequest.h"
17 
18 
19 dabc::ConnectionObject::ConnectionObject(Reference port, const std::string &localurl) :
20  ParameterContainer(port, ObjectName(), "connection", true),
21  fConnState(sInit), // specify non-init state to be able change it
22  fLocalUrl(localurl),
23  fProgress(0),
24  fServerId(),
25  fClientId(),
26  fCustomData(),
27  fConnId(),
28  fRemoteCmd(),
29  fInlineDataSize(0),
30  fAccDelay(0.),
31  fSetDelay(0.),
32  fAllowedField()
33 {
34  SetSynchron(true, 0.);
35 }
36 
38 {
39 }
40 
41 bool dabc::ConnectionObject::_CanChangeField(const std::string &name)
42 {
43  // special case when only field can be changed once
44  if (!fAllowedField.empty() && (fAllowedField==name)) {
45  fAllowedField.clear();
46  return true;
47  }
48 
49  if ((name == "state") && (fAllowedField=="%%state%%")) {
50  fAllowedField.clear();
51  return true;
52  }
53 
54  // changes of parameter fields (used by the configuration) only in init state are allowed
55  if (fConnState == sInit) return true;
56 
57  return false;
58 }
59 
61 {
62  return "state";
63 }
64 
66 {
67  switch (state) {
68  case sInit: return "Init";
69  case sPending: return "Pending";
70  case sConnecting: return "Connecting";
71  case sConnected: return "Connected";
72  case sDisconnected: return "Disconnected";
73  case sBroken: return "Broken";
74  case sFailed: return "Failed";
75  }
76  return "---";
77 }
78 
80 {
81  LockGuard lock(ObjectMutex());
82  return fConnState;
83 }
84 
86 {
87  bool signal(false);
88 
89  {
90  LockGuard lock(ObjectMutex());
91  if (fConnState==state) return;
92 
93  // if state transition is not forced by caller, first check if previous state is corresponds
94  if (!force) {
95  if ((state==sPending) && (fConnState!=sInit)) return;
96  }
97 
98  fConnState = state;
99 
100  signal = (state!=sInit);
101 
102  if (signal) fAllowedField = "%%state%%";
103  }
104 
105  DOUT3("Change connection state to %s", GetStateName(state));
106 
107  if (signal)
108  SetField("", GetStateName(state));
109 }
110 
111 // ------------------------------------------------------------------
112 
114 {
115  if (GetObject()) GetObject()->ChangeState(state, force);
116 }
117 
118 
119 void dabc::ConnectionRequest::SetAllowedField(const std::string &name)
120 {
121  LockGuard lock(ObjectMutex());
122 
123  if (GetObject()) GetObject()->fAllowedField = name;
124 }
125 
126 
128 {
129  if ((node==0) || null()) return;
130 
131  const char* thrdname = Xml::GetAttr(node, xmlThreadAttr);
132  if (thrdname!=0) {
133  SetAllowedField(xmlThreadAttr);
134  SetConnThread(thrdname);
135  }
136 
137  const char* useackn = Xml::GetAttr(node, xmlUseacknAttr);
138  if (useackn!=0) {
139  SetAllowedField(xmlUseacknAttr);
140  SetUseAckn(strcmp(useackn, xmlTrueValue)==0);
141  }
142 
143  const char* isserver = Xml::GetAttr(node, "server");
144  if (isserver!=0) {
145  SetAllowedField("server");
146  SetServerSide(strcmp(isserver, xmlTrueValue)==0);
147  }
148 
149  const char* optional = Xml::GetAttr(node, xmlOptionalAttr);
150  if (optional!=0) {
151  SetAllowedField(xmlOptionalAttr);
152  SetOptional(strcmp(optional, xmlTrueValue)==0);
153  }
154 
155  const char* poolname = Xml::GetAttr(node, xmlPoolAttr);
156  if (poolname!=0) SetPoolName(poolname);
157 
158  const char* devname = Xml::GetAttr(node, xmlDeviceAttr);
159  if (devname!=0) {
160  SetAllowedField(xmlDeviceAttr);
161  SetConnDevice(devname);
162  }
163 
164  const char* tmout = Xml::GetAttr(node, xmlTimeoutAttr);
165  double tmout_val(10.);
166  if ((tmout!=0) && str_to_double(tmout, &tmout_val)) {
167  SetAllowedField(xmlTimeoutAttr);
168  SetConnTimeout(tmout_val);
169  }
170 }
void ChangeState(EState state, bool force)
Change state of connection object.
ConnectionObject::EState GetState()
virtual std::string DefaultFiledName() const
ConnectionObject(Reference port, const std::string &localurl)
virtual bool _CanChangeField(const std::string &)
Reimplement virtual method of ParameterContainer to block field change if object in non-init state.
static const char * GetStateName(EState state)
void SetConfigFromXml(XMLNodePointer_t node)
void ChangeState(ConnectionObject::EState state, bool force)
void SetAllowedField(const std::string &name="")
Lock guard for posix mutex.
Definition: threads.h:127
Container for parameter object.
Definition: Parameter.h:52
void SetSynchron(bool on, double interval=1., bool everyevnt=false)
Specifies that parameter produce 'modified' events synchronous with changes of parameter.
Definition: Parameter.cxx:283
Reference on the arbitrary object
Definition: Reference.h:73
#define DOUT3(args ...)
Definition: logging.h:176
const char * GetAttr(XMLNodePointer_t xmlnode, const char *name)
Definition: XmlEngine.cxx:411
void * XMLNodePointer_t
Definition: XmlEngine.h:25
const char * xmlTimeoutAttr
Definition: ConfigBase.cxx:42
const char * xmlPoolAttr
Definition: ConfigBase.cxx:41
const char * xmlUseacknAttr
Definition: ConfigBase.cxx:39
bool str_to_double(const char *val, double *res)
Convert string to double value.
Definition: string.cxx:216
const char * xmlDeviceAttr
Definition: ConfigBase.cxx:37
const char * xmlOptionalAttr
Definition: ConfigBase.cxx:40
const char * xmlThreadAttr
Definition: ConfigBase.cxx:38
const char * xmlTrueValue
Definition: ConfigBase.cxx:90