DABC (Data Acquisition Backbone Core)  2.9.9
Configuration.cxx
Go to the documentation of this file.
1 // $Id: Configuration.cxx 4723 2021-03-13 17:39:37Z 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/Configuration.h"
17 
18 #include <unistd.h>
19 #include <cstdlib>
20 #include <fnmatch.h>
21 
22 #include "dabc/Manager.h"
23 #include "dabc/Factory.h"
24 
25 std::string dabc::Configuration::fLocalHost = "";
26 
28  ConfigBase(fname),
29  fSelected(nullptr),
30  fMgrHost(),
31  fMgrPort(0),
32  fMgrName("Manager"),
33  fMgrNodeId(0),
34  fMgrNumNodes(0)
35 {
36 }
37 
39 {
40 }
41 
42 bool dabc::Configuration::SelectContext(unsigned nodeid, unsigned numnodes)
43 {
44  fSelected = FindContext(nodeid);
45 
46  if (!fSelected) return false;
47 
48  envDABCNODEID = dabc::format("%u", nodeid);
49  envDABCNUMNODES = dabc::format("%u", numnodes);
50 
51  envDABCSYS = GetEnv(xmlDABCSYS);
52 
53  envDABCUSERDIR = Find1(fSelected, "", xmlRunNode, xmlDABCUSERDIR);
54  if (envDABCUSERDIR.empty()) envDABCUSERDIR = GetEnv(xmlDABCUSERDIR);
55 
56  char sbuf[1000];
57  if (getcwd(sbuf, sizeof(sbuf)))
58  envDABCWORKDIR = sbuf;
59  else
60  envDABCWORKDIR = ".";
61 
62  fMgrHost = NodeName(nodeid);
63  fMgrPort = NodePort(nodeid);
64  envHost = fMgrHost;
65 
66  fMgrName = ContextName(nodeid);
67  envContext = fMgrName;
68 
69  DOUT2("Select context nodeid %u name %s", nodeid, fMgrName.c_str());
70 
71  dabc::SetDebugPrefix(fMgrName.c_str());
72 
73  fMgrNodeId = nodeid;
74  fMgrNumNodes = numnodes;
75 
76  if (numnodes>1) {
79  } else {
82  }
83 
84  std::string val = Find1(fSelected, "", xmlRunNode, xmlDebuglevel);
85  if (!val.empty()) dabc::SetDebugLevel(std::stoi(val));
86 
87  val = Find1(fSelected, "", xmlRunNode, xmlNoDebugPrefix);
88  if (!val.empty()) {
91  }
92 
93  val = Find1(fSelected, "", xmlRunNode, xmlLoglevel);
94  if (!val.empty()) dabc::SetFileLevel(std::stoi(val));
95 
96  std::string syslog = "";
97  val = Find1(fSelected, "", xmlRunNode, xmlSysloglevel);
98  if (!val.empty()) {
99  syslog = "DABC";
100  dabc::Logger::Instance()->SetSyslogLevel(std::stoi(val));
101  }
102 
103  std::string log = Find1(fSelected, "", xmlRunNode, xmlLogfile);
104  if (log.length()>0)
105  dabc::Logger::Instance()->LogFile(log.c_str());
106 
107  log = Find1(fSelected, "", xmlRunNode, xmlSyslog);
108  if (!log.empty()) syslog = log;
109  if (!syslog.empty())
110  dabc::Logger::Instance()->Syslog(syslog.c_str());
111 
112  log = Find1(fSelected, "", xmlRunNode, xmlLoglimit);
113  if (log.length()>0)
114  dabc::Logger::Instance()->SetLogLimit(std::stoi(log));
115 
116  fLocalHost = Find1(fSelected, "", xmlRunNode, xmlSocketHost);
117 
118  return true;
119 }
120 
122 {
123  if (!fSelected) return std::string("");
124 
125  return Find1(fSelected, "", xmlRunNode, xmlInitFunc);
126 }
127 
129 {
130  if (!fSelected) return std::string("");
131 
132  return Find1(fSelected, "", xmlRunNode, xmlRunFunc);
133 }
134 
136 {
137  if (!fSelected) return -1;
138  std::string res = Find1(fSelected, "", xmlRunNode, xmlCpuInfo);
139  if (res.empty()) return -1;
140  int kind(0);
141  return dabc::str_to_int(res.c_str(), &kind) ? kind : -1;
142 }
143 
145 {
146  if (!fSelected) return true;
147  std::string res = Find1(fSelected, "", xmlRunNode, "slow-time");
148  if (res == xmlFalseValue) return false;
149  if (res == xmlTrueValue) return true;
150  return true;
151 }
152 
154 {
155  if (!fSelected) return 0;
156  std::string res = Find1(fSelected, "", xmlRunNode, xmlUseControl);
157  if (res == xmlFalseValue) return -1;
158  if (res == xmlTrueValue) return 1;
159  return 0;
160 }
161 
163 {
164  if (!fSelected) return 0;
165  std::string res = Find1(fSelected, "", xmlRunNode, xmlPublisher);
166  if (res == xmlFalseValue) return -1;
167  if (res == xmlTrueValue) return 1;
168  return 0;
169 }
170 
171 
173 {
174  if (!fSelected) return std::string("");
175 
176  return Find1(fSelected, "", xmlRunNode, xmlMasterProcess);
177 }
178 
180 {
181  if (!fSelected) return 0.;
182  std::string res = Find1(fSelected, "", xmlRunNode, xmlRunTime);
183  if (res.empty()) return 0.;
184  double runtime(0.);
185  return dabc::str_to_double(res.c_str(), &runtime) ? runtime : 0.;
186 }
187 
189 {
190  if (!fSelected) return 0.;
191  std::string res = Find1(fSelected, "", xmlRunNode, xmlHaltTime);
192  if (res.empty()) return 0.;
193  double halttime(0.);
194  return dabc::str_to_double(res.c_str(), &halttime) ? halttime : 0.;
195 }
196 
198 {
199  if (!fSelected) return 0.;
200  std::string res = Find1(fSelected, "", xmlRunNode, xmlThrdStopTime);
201  if (res.empty()) return 0.;
202  double stoptime(0.);
203  return dabc::str_to_double(res.c_str(), &stoptime) ? stoptime : 0.;
204 }
205 
207 {
208  if (!fSelected) return true;
209  return Find1(fSelected, "", xmlRunNode, xmlNormalMainThrd) == xmlTrueValue;
210 }
211 
213 {
214  if (!fSelected) return "";
215 
216  return Find1(fSelected, "", xmlRunNode, xmlAffinity);
217 }
218 
220 {
221  if (!fSelected) return "";
222 
223  return Find1(fSelected, "", xmlRunNode, xmlThreadsLayout);
224 }
225 
226 
227 std::string dabc::Configuration::GetUserPar(const char* name, const char* dflt)
228 {
229  if (!fSelected) return std::string("");
230  return Find1(fSelected, dflt ? dflt : "", xmlUserNode, name);
231 }
232 
233 int dabc::Configuration::GetUserParInt(const char* name, int dflt)
234 {
235  std::string sres = GetUserPar(name);
236  if (sres.empty()) return dflt;
237  int res(dflt);
238  return dabc::str_to_int(sres.c_str(), &res) ? res : dflt;
239 }
240 
242 {
243  if (!fSelected) return std::string();
244 
245  XMLNodePointer_t node = Xml::GetChild(fSelected);
246 
247  while (node!=0) {
248  if (IsNodeName(node, xmlApplication)) break;
249  node = Xml::GetNext(node);
250  }
251 
252  if (node==0)
253  node = FindMatch(0, fSelected, xmlApplication);
254 
255  const char* res = Xml::GetAttr(node, xmlClassAttr);
256 
257  return std::string(res ? res : "");
258 }
259 
260 
262 {
263  if (!fSelected) return false;
264 
265  std::string libname;
266  XMLNodePointer_t last = 0;
267 
268  do {
269  libname = FindN(fSelected, last, xmlRunNode, xmlUserLib);
270  if (libname.empty()) break;
271  DOUT2("Find library %s in config", libname.c_str());
272  if (!dabc::Factory::LoadLibrary(ResolveEnv(libname))) return false;
273  } while (true);
274 
275  return true;
276 }
277 
278 bool dabc::Configuration::NextCreationNode(XMLNodePointer_t& prev, const char* nodename, bool check_name_for_multicast)
279 {
280  do {
281 
282  prev = FindMatch(prev, fSelected, nodename);
283  if (prev==0) break;
284 
285  // if creation node marked with auto="false" attribute, than creation is disabled
286  const char* autoattr = Xml::GetAttr(prev, xmlAutoAttr);
287  if ((autoattr!=0) && strcmp(autoattr,xmlFalseValue)==0) continue;
288 
289  if (!check_name_for_multicast) break;
290  const char* nameattr = Xml::GetAttr(prev, xmlNameAttr);
291  if ((nameattr==0) || (strpbrk(nameattr,"*?")==0)) break;
292 
293  } while (prev!=0);
294 
295  return prev!=0;
296 }
297 
Base class to read configuration from xml file.
Definition: ConfigBase.h:104
double GetHaltTime()
Returns time, required to halt DABC process.
double GetThrdStopTime()
Returns time, required for normal thread stopping.
static std::string fLocalHost
Definition: Configuration.h:46
std::string ThreadsLayout()
int GetUserParInt(const char *name, int dflt=0)
bool NextCreationNode(XMLNodePointer_t &prev, const char *nodename, bool check_name_for_multicast)
Method is used to find xml nodes like Module, MemoryPool, Connection in the xml file which should be ...
bool SelectContext(unsigned nodeid, unsigned numnodes)
std::string InitFuncName()
int UseControl()
Defines if control used in dabc node.
std::string RunFuncName()
std::string MasterName()
Defines master DABC process, to which application will be connected Master means all control and moni...
std::string Affinity()
Return default affinity of any newly created thread.
std::string GetUserPar(const char *name, const char *dflt=0)
int WithPublisher()
Returns true when publisher should be created.
Configuration(const char *fname=0)
double GetRunTime()
Returns configured time to run DABC process.
std::string ConetextAppClass()
static bool LoadLibrary(const std::string &fname)
Definition: Factory.cxx:26
virtual void Syslog(const char *prefix)
Definition: logging.cxx:226
void SetLogLimit(unsigned limit=100)
Definition: logging.h:85
virtual void LogFile(const char *fname)
Definition: logging.cxx:210
void SetSyslogLevel(int level=0)
Definition: logging.cxx:182
void SetDebugMask(unsigned mask)
Definition: logging.h:70
static Logger * Instance()
Definition: logging.h:104
void SetFileMask(unsigned mask)
Definition: logging.h:78
#define DOUT2(args ...)
Definition: logging.h:170
XMLNodePointer_t GetNext(XMLNodePointer_t xmlnode)
Definition: XmlEngine.cxx:925
const char * GetAttr(XMLNodePointer_t xmlnode, const char *name)
Definition: XmlEngine.cxx:411
XMLNodePointer_t GetChild(XMLNodePointer_t xmlnode)
Definition: XmlEngine.cxx:906
const char * xmlThrdStopTime
Definition: ConfigBase.cxx:75
const char * xmlNoDebugPrefix
Definition: ConfigBase.cxx:67
const char * xmlLoglimit
Definition: ConfigBase.cxx:69
const char * xmlInitFunc
Definition: ConfigBase.cxx:82
void * XMLNodePointer_t
Definition: XmlEngine.h:25
void SetDebugLevel(int level=0)
Definition: logging.cxx:468
const char * xmlCpuInfo
Definition: ConfigBase.cxx:84
const char * xmlMasterProcess
Definition: ConfigBase.cxx:87
bool str_to_double(const char *val, double *res)
Convert string to double value.
Definition: string.cxx:216
const char * xmlUserNode
Definition: ConfigBase.cxx:48
std::string format(const char *fmt,...)
Definition: string.cxx:49
const char * xmlThreadsLayout
Definition: ConfigBase.cxx:78
const char * xmlAutoAttr
Definition: ConfigBase.cxx:43
const char * xmlApplication
Definition: ConfigBase.cxx:30
const char * xmlDebuglevel
Definition: ConfigBase.cxx:66
const char * xmlLogfile
Definition: ConfigBase.cxx:68
const char * xmlRunTime
Definition: ConfigBase.cxx:73
const char * xmlRunNode
Definition: ConfigBase.cxx:47
const char * xmlDABCUSERDIR
Definition: ConfigBase.cxx:55
const char * xmlHaltTime
Definition: ConfigBase.cxx:74
const char * xmlLoglevel
Definition: ConfigBase.cxx:70
const char * xmlDABCSYS
Definition: ConfigBase.cxx:54
const char * xmlTrueValue
Definition: ConfigBase.cxx:90
const char * xmlSyslog
Definition: ConfigBase.cxx:72
const char * xmlSocketHost
Definition: ConfigBase.cxx:85
const char * xmlPublisher
Definition: ConfigBase.cxx:88
void SetDebugPrefix(const char *prefix=0)
Definition: logging.cxx:478
const char * xmlUseControl
Definition: ConfigBase.cxx:86
const char * xmlFalseValue
Definition: ConfigBase.cxx:91
const char * xmlClassAttr
Definition: ConfigBase.cxx:36
const char * xmlAffinity
Definition: ConfigBase.cxx:77
void SetFileLevel(int level=0)
Definition: logging.cxx:473
const char * xmlNameAttr
Definition: ConfigBase.cxx:33
const char * xmlRunFunc
Definition: ConfigBase.cxx:83
const char * xmlSysloglevel
Definition: ConfigBase.cxx:71
const char * xmlNormalMainThrd
Definition: ConfigBase.cxx:76
Logger * lgr()
Definition: logging.cxx:483
bool str_to_int(const char *val, int *res)
Convert string to integer value.
Definition: string.cxx:142
const char * xmlUserLib
Definition: ConfigBase.cxx:81