DABC (Data Acquisition Backbone Core)  2.9.9
api.cxx
Go to the documentation of this file.
1 // $Id: api.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/api.h"
17 
18 #include <cstdio>
19 #include <cstdlib>
20 #include <signal.h>
21 
22 #include "dabc/Publisher.h"
23 #include "dabc/Configuration.h"
24 #include "dabc/Manager.h"
25 
26 bool dabc::CreateManager(const std::string &name, int cmd_port)
27 {
28  bool dofactories = false;
29 
30  if (dabc::mgr.null()) {
31  static dabc::Configuration cfg;
32 
33  new dabc::Manager(name, &cfg);
34 
35  // ensure that all submitted events are processed
37 
38  dofactories = true;
39  }
40 
41  if (cmd_port>=0)
42  dabc::mgr.CreateControl(cmd_port > 0, cmd_port);
43 
44  if (dofactories) {
45  dabc::mgr.Execute("InitFactories");
46  }
47 
48  return true;
49 }
50 
52 int DABC_SigCnt = 0;
53 
54 void DABC_GLOBAL_CtrlCHandler(int number)
55 {
56  if (DABC_SigThrd != dabc::PosixThread::Self()) return;
57 
58  DABC_SigCnt++;
59 
60  if ((DABC_SigCnt>2) || (dabc::mgr()==0)) {
61  printf("Force application exit\n");
62  if (dabc::lgr()!=0) dabc::lgr()->CloseFile();
63  exit(0);
64  }
65 
66  dabc::mgr()->ProcessCtrlCSignal();
67 }
68 
69 void DABC_GLOBAL_SigPipeHandler(int number)
70 {
71  // JAM2018:This was introduced to catch socket signals raised by external plugin libraries like LTSM/TSM client
72  // note that DABC sockets have SIGPIPE disabled by default
73  // todo: maybe one global signal handlers for all signals. For the moment, keep things rather separate...
74  if (DABC_SigThrd != dabc::PosixThread::Self()) return;
75  if (dabc::mgr()==0) {
76  printf("DABC_GLOBAL_SigPipeHandler: no manager, Force application exit\n");
77  if (dabc::lgr()!=0) dabc::lgr()->CloseFile();
78  exit(0);
79  }
80  dabc::mgr()->ProcessPipeSignal();
81 }
82 
84 {
85  if (DABC_SigThrd!=0) {
86  printf("Signal handler was already installed !!!\n");
87  return false;
88  }
89 
91 
92  if (signal(SIGINT, DABC_GLOBAL_CtrlCHandler)==SIG_ERR) {
93  printf("Cannot change handler for SIGINT\n");
94  return false;
95  }
96 
97  if (signal(SIGPIPE, DABC_GLOBAL_SigPipeHandler)==SIG_ERR) {
98  printf("Cannot change handler for SIGPIPE\n");
99  return false;
100  }
101 
102  return true;
103 }
104 
105 
106 bool dabc::CtrlCPressed()
107 {
108  return DABC_SigCnt > 0;
109 }
110 
111 
113 {
114  if (dabc::mgr.null()) return true;
115 
116  dabc::mgr()->HaltManager();
117 
118  dabc::mgr.Destroy();
119 
120  return true;
121 }
122 
123 
124 std::string dabc::MakeNodeName(const std::string &arg)
125 {
126  size_t pos = arg.find("dabc://");
127  if (pos==std::string::npos)
128  return std::string("dabc://") + arg;
129  if (pos == 0) return arg;
130 
131  return std::string();
132 }
133 
134 
135 bool dabc::ConnectDabcNode(const std::string &nodeaddr)
136 {
137  if (dabc::mgr.null()) {
138  EOUT("Manager was not created");
139  return false;
140  }
141 
142  std::string fullname = MakeNodeName(nodeaddr);
143  if (fullname.empty()) {
144  EOUT("Wrong address format %d", nodeaddr.c_str());
145  return false;
146  }
147 
148  dabc::mgr.CreateControl(false);
149 
150  dabc::Command cmd("Ping");
151  cmd.SetReceiver(fullname);
152  cmd.SetTimeout(10);
153 
154  if (dabc::mgr.GetCommandChannel().Execute(cmd) != dabc::cmd_true) {
155  EOUT("FAIL connection to %s", nodeaddr.c_str());
156  return false;
157  }
158 
159  return true;
160 }
161 
162 dabc::Hierarchy dabc::GetNodeHierarchy(const std::string &nodeaddr)
163 {
164  dabc::Hierarchy res;
165 
166  std::string fullname = MakeNodeName(nodeaddr);
167  if (fullname.empty()) {
168  EOUT("Wrong address format %d", nodeaddr.c_str());
169  return res;
170  }
171 
173  cmd.SetReceiver(fullname + dabc::Publisher::DfltName());
174  cmd.SetTimeout(10);
175 
176  if (dabc::mgr.GetCommandChannel().Execute(cmd)!=dabc::cmd_true) {
177  EOUT("Fail to get hierarchy from node %s", nodeaddr.c_str());
178  return res;
179  }
180 
182 
183  return res;
184 }
Command to request names list.
Definition: Publisher.h:58
static Hierarchy GetResNamesList(dabc::Command &cmd)
Definition: Publisher.cxx:63
Represents command with its arguments.
Definition: Command.h:99
Command & SetTimeout(double tm)
Set maximum time which can be used for command execution.
Definition: Command.cxx:108
Command & SetReceiver(const std::string &itemname)
These methods prepare command so, that one can submit command to the manager like: dabc::mgr....
Definition: Command.h:264
Full-functional class to reading configuration from xml files.
Definition: Configuration.h:34
Represents objects hierarchy of remote (or local) DABC process.
Definition: Hierarchy.h:285
void CloseFile()
Close any file open by logger.
Definition: logging.cxx:161
bool CreateControl(bool withserver, int serv_port=0, bool allow_clients=true)
Create command channel Parameter withserver defines if server socket will be created,...
Definition: Manager.cxx:2277
Manager of everything in DABC
Definition: Manager.h:291
static Thread_t Self()
Definition: threads.h:401
static const char * DfltName()
Definition: Publisher.h:214
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
#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 CtrlCPressed()
Returns true when CtrlC was pressed in handler.
Definition: api.cxx:106
Hierarchy GetNodeHierarchy(const std::string &nodeaddr)
Function request hierarchy of objects on remote node.
Definition: api.cxx:162
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
pthread_t Thread_t
Definition: threads.h:333
bool DestroyManager()
Function can be used to destroy manager.
Definition: api.cxx:112
std::string MakeNodeName(const std::string &arg)
Function creates node name, which can be supplied as receiver of dabc commands.
Definition: api.cxx:124
@ cmd_true
Definition: Command.h:38
bool ConnectDabcNode(const std::string &nodeaddr)
Function establish connection with specified dabc node.
Definition: api.cxx:135
Logger * lgr()
Definition: logging.cxx:483
int DABC_SigCnt
Definition: api.cxx:52
void DABC_GLOBAL_SigPipeHandler(int number)
Definition: api.cxx:69
void DABC_GLOBAL_CtrlCHandler(int number)
Definition: api.cxx:54
dabc::Thread_t DABC_SigThrd
Definition: api.cxx:51