DABC (Data Acquisition Backbone Core)  2.9.9
dabc_exe.cxx
Go to the documentation of this file.
1 // $Id: dabc_exe.cxx 4722 2021-03-13 13:56:03Z 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 <cstdlib>
17 #include <string>
18 #include <clocale>
19 
20 #include "dabc/Manager.h"
21 #include "dabc/Configuration.h"
22 #include "dabc/Factory.h"
23 #include "dabc/api.h"
24 
26 {
27  if (dabc::mgr.null()) return false;
28 
29  int ctrl = cfg.UseControl();
30  std::string master = cfg.MasterName();
31 
32  if ((ctrl==0) && (cfg.NumNodes()>1)) ctrl = 1;
33 
34  if ((ctrl > 0) || !master.empty()) {
35  DOUT2("Connecting control");
36  if (!dabc::mgr.CreateControl(ctrl>0)) {
37  EOUT("Cannot establish connection to command system");
38  return false;
39  }
40 
41  if (!master.empty()) {
42  dabc::Command cmd("ConfigureMaster");
43  cmd.SetStr("Master", master);
44  cmd.SetStr("NameSufix", "DABC");
45  if (dabc::mgr.GetCommandChannel().Execute(cmd) != dabc::cmd_true) {
46  DOUT0("FAIL to activate connection to master %s", master.c_str());
47  return false;
48  }
49  }
50  }
51 
52  return true;
53 }
54 
55 
56 int command_shell(const char* node)
57 {
58  dabc::CreateManager("cmd", 0);
59 
60  dabc::mgr.RunCmdLoop(1000, node);
61 
63 
64  return 0;
65 }
66 
67 
68 int main(int numc, char* args[])
69 {
70  ::setlocale(LC_ALL, "C");
71 
73 
74 // dabc::SetDebugLevel(0);
75 
76  DOUT2("Start cnt = %u", dabc::Object::NumInstances());
77 
78  if ((numc>1) and (strcmp(args[1],"cmd")==0))
79  return command_shell(numc>2 ? args[2] : "");
80 
81  const char *cfgfile = nullptr;
82 
83  if(numc > 1) cfgfile = args[1];
84 
85  unsigned nodeid = 1000000;
86  unsigned numnodes = 0;
87  //bool dorun = false;
88 
89  dabc::Configuration cfg(cfgfile);
90  if (!cfg.IsOk()) {
91  DOUT3("Cannot read configuration from file %s - Exit", (cfgfile ? cfgfile : "---") );
92  return 7;
93  }
94 
95  if (cfg.GetVersion() < 2) {
96  EOUT("Only dabc version 2 for xml files is supported - Exit");
97  return 7;
98  }
99 
100  int cnt = 2;
101  while (cnt<numc) {
102 
103  const char* arg = args[cnt++];
104 
105  if (strcmp(arg,"-slow-time")==0) {
107  } else
108  if (strcmp(arg,"-nodeid")==0) {
109  if (cnt < numc)
110  nodeid = std::stoul(args[cnt++]);
111  } else
112  if (strcmp(arg,"-numnodes")==0) {
113  if (cnt < numc)
114  numnodes = std::stoul(args[cnt++]);
115  } else
116  if (strcmp(arg,"-run")==0) {
117  //dorun = true;
118  } else
119  if (strcmp(arg,"-norun")==0) {
120  //dorun = false;
121  } else {
122  const char* separ = strchr(arg,'=');
123  if ((separ!=0) && (separ!=arg)) {
124  std::string argname, argvalue;
125  argname.append(arg, separ - arg);
126  argvalue = separ+1;
127 
128  // remove leading/trailing quotes if found
129  if ((argvalue.length() > 1) && (argvalue[0] == argvalue[argvalue.length()-1]))
130  if ((argvalue[0] == '\'') || (argvalue[0] == '\"')) {
131  argvalue.erase(0,1);
132  argvalue.resize(argvalue.length()-1);
133  }
134 
135  cfg.AddCmdVariable(argname.c_str(), argvalue.c_str());
136  }
137  }
138  }
139 
140  if (numnodes==0) numnodes = cfg.NumNodes();
141  if (nodeid>=numnodes) nodeid = 0;
142 
143  DOUT2("Using config file: %s id: %u", cfgfile, nodeid);
144 
145  if (!cfg.SelectContext(nodeid, numnodes)) {
146  EOUT("Did not found context");
147  return 1;
148  }
149 
150  if (cfg.UseSlowTime())
152 
153  // reserve special thread
154  std::string affinity = cfg.Affinity();
155  if (!affinity.empty())
156  dabc::PosixThread::SetDfltAffinity(affinity.c_str());
157  else
159 
160  char sbuf[200];
161  if (dabc::PosixThread::GetDfltAffinity(sbuf, sizeof(sbuf)))
162  DOUT2("Process affinity %s", sbuf);
163 
164  DOUT2("Create manager");
165 
166  new dabc::Manager(cfg.MgrName(), &cfg);
167 
168  // ensure that all submitted events are processed
170 
171  int res = 0;
172 
173  if (!cfg.LoadLibs()) res = -2;
174  if (res==0)
175  if (!CreateManagerControl(cfg)) res = -1;
176 
177  if (res==0)
178  dabc::mgr.Execute("InitFactories");
179 
180  if ((res==0) && (cfg.WithPublisher() > 0))
182 
186 
187  int cpuinfo = cfg.ShowCpuInfo();
188  if (cpuinfo>=0) {
189  dabc::mgr.CreateModule("dabc::CpuInfoModule", "/CpuInfo", dabc::Manager::MgrThrdName());
190  dabc::mgr.StartModule("/CpuInfo");
191  }
192 
193  if (runfunc!=0) {
194  if (res==0) runfunc();
195  } else {
196  if (res==0)
197  if (!dabc::mgr.CreateApplication(cfg.ConetextAppClass())) res = -3;
198 
199  if (res==0)
201  }
202 
203  dabc::mgr()->HaltManager();
204 
205  dabc::mgr.Destroy();
206 
208  DOUT2("Exit cnt = %u", dabc::Object::NumInstances());
209 
210  return res;
211 }
void * ExternalFunction()
Definition: Application.h:80
Represents command with its arguments.
Definition: Command.h:99
bool SetStr(const std::string &name, const char *value)
Definition: Command.h:134
int GetVersion() const
Definition: ConfigBase.h:179
void AddCmdVariable(const char *name, const char *value)
Add variable from command line.
Definition: ConfigBase.cxx:151
bool IsOk() const
Definition: ConfigBase.h:177
unsigned NumNodes()
returns number of nodes in xml file
Definition: ConfigBase.cxx:334
Full-functional class to reading configuration from xml files.
Definition: Configuration.h:34
bool SelectContext(unsigned nodeid, unsigned numnodes)
const char * MgrName() const
Definition: Configuration.h:56
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.
int WithPublisher()
Returns true when publisher should be created.
double GetRunTime()
Returns configured time to run DABC process.
std::string ConetextAppClass()
static void * FindSymbol(const std::string &symbol)
Definition: Factory.cxx:49
void RunMainLoop(double runtime=0.)
Run manager main loop.
Definition: Manager.h:683
ModuleRef CreateModule(const std::string &classname, const std::string &modulename, const std::string &thrdname="")
Definition: Manager.cxx:1981
void RunCmdLoop(double runtime=0., const std::string &remnode="")
Run manager command loop.
Definition: Manager.h:687
void StartModule(const std::string &modulename)
Definition: Manager.cxx:2023
bool CreatePublisher()
Create publisher, which manage all published hierarchies.
Definition: Manager.cxx:2315
Manager of everything in DABC
Definition: Manager.h:291
static const char * MgrThrdName()
Definition: Manager.h:476
static void InspectGarbageCollector()
\ brief Methods to inspect how many objects pointers are remained
Definition: Object.cxx:1099
static unsigned NumInstances()
Static variable counts total number of objects instances.
Definition: Object.h:435
static bool SetDfltAffinity(const char *aff=nullptr)
Sets default affinity for next threads to be created and for main process.
Definition: threads.cxx:183
static bool GetDfltAffinity(char *buf, unsigned maxbuf)
Returns default affinity mask in form "xxxooosss".
Definition: threads.cxx:424
void Destroy()
Release reference and starts destroyment of referenced object.
Definition: Reference.cxx:148
bool SyncWorker(double tmout=-1.)
Synchronize worker with caller thread.
Definition: Worker.cxx:1184
bool Execute(Command cmd, double tmout=-1.)
Definition: Worker.cxx:1147
int command_shell(const char *node)
Definition: dabc_exe.cxx:56
bool CreateManagerControl(dabc::Configuration &cfg)
Definition: dabc_exe.cxx:25
int main(int numc, char *args[])
Definition: dabc_exe.cxx:68
#define DOUT2(args ...)
Definition: logging.h:170
#define DOUT0(args ...)
Definition: logging.h:156
#define DOUT3(args ...)
Definition: logging.h:176
#define EOUT(args ...)
Definition: logging.h:150
bool InstallSignalHandlers()
Method is used to install DABC-specific Ctrl-C handler It allows to correctly stop program execution ...
Definition: api.cxx:83
bool CreateManager(const std::string &name, int cmd_port=-1)
Function should be used to create manager instance.
Definition: api.cxx:26
ManagerRef mgr
Definition: Manager.cxx:42
bool DestroyManager()
Function can be used to destroy manager.
Definition: api.cxx:112
@ cmd_true
Definition: Command.h:38
static void SetUseSlow()
Definition: timing.h:178