DABC (Data Acquisition Backbone Core)  2.9.9
Player.cxx
Go to the documentation of this file.
1 // $Id: Player.cxx 4464 2020-04-15 12:14:20Z 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 "gosip/Player.h"
17 
18 #include "dabc/Publisher.h"
19 
20 #include <cstdio>
21 
22 gosip::Player::Player(const std::string &name, dabc::Command cmd) :
23  dabc::ModuleAsync(name, cmd)
24 {
25  fWorkerHierarchy.Create("FESA", true);
26 
27  dabc::CommandDefinition cmddef = CreateCmdDef("CmdGosip");
28  //cmddef.SetField(dabc::prop_auth, true); // require authentication
29  cmddef.AddArg("cmd", "string", true, "-");
30 
32  ui.SetField(dabc::prop_kind, "DABC.HTML");
33  ui.SetField("_UserFilePath", "${DABCSYS}/plugins/gosip/htm/");
34  ui.SetField("_UserFileMain", "main.htm");
35 
36  CreateTimer("update", 1.);
37 
38  PublishPars("GOSIP/Test");
39 }
40 
42 {
43 }
44 
46 {
47  //dabc::Hierarchy ui = fWorkerHierarchy.FindChild("UserInterface");
48  //DOUT0("Process timer '%s'", ui.GetField("FilePath").AsStr().c_str());
49 }
50 
51 
53 {
54  if (cmd.IsName("CmdGosip")) {
55 
56  int sfp = cmd.GetInt("sfp", 0);
57  int dev = cmd.GetInt("dev", 0);
58 
59  bool log_output = cmd.GetInt("log") > 0;
60 
61  std::string addr;
62  if ((sfp<0) || (dev<0))
63  addr = "-- -1 -1";
64  else
65  addr = dabc::format("%d %d", sfp, dev);
66 
67  std::vector<std::string> gosipcmd = cmd.GetField("cmd").AsStrVect();
68 
69  std::vector<std::string> gosipres;
70  std::vector<std::string> gosiplog;
71 
72  DOUT2("*** CmdGosip len %u ****", gosipcmd.size());
73  for (unsigned n=0;n<gosipcmd.size();n++) {
74 
75  std::string currcmd = gosipcmd[n];
76 
77  bool isreading = (currcmd.find("-r")==0);
78  bool iswriting = (currcmd.find("-w")==0);
79 
80  if (!isreading && !iswriting && (currcmd[0]!='-')) {
81  isreading = true;
82  currcmd = std::string("-r adr ") + currcmd;
83  }
84 
85  size_t ppp = currcmd.find("adr");
86  if (ppp!=std::string::npos) currcmd.replace(ppp,3,addr);
87 
88  currcmd = "gosipcmd " + currcmd;
89 
90  if (log_output) gosiplog.push_back(currcmd);
91 
92  // DOUT0("CMD %s", currcmd.c_str());
93 
94  FILE* pipe = popen(currcmd.c_str(), "r");
95 
96  if (!pipe) {
97  gosipres.push_back("<err>");
98  break;
99  }
100 
101  char buf[50000];
102  memset(buf, 0, sizeof(buf));
103  int totalsize = 0;
104 
105  while(!feof(pipe) && (totalsize<200000)) {
106  int size = (int)fread(buf,1, sizeof(buf)-1, pipe); //cout<<buffer<<" size="<<size<<endl;
107  if (size<=0) break;
108  while ((size>0) && ((buf[size-1]==' ') || (buf[size-1]=='\n'))) size--;
109  buf[size] = 0;
110  totalsize+=size;
111  if (log_output && (size>0)) gosiplog.push_back(buf);
112  // DOUT0("Get %s", buf);
113  }
114 
115  pclose(pipe);
116 
117  if (iswriting) {
118 
119  // DOUT0("Writing res:%s len %d", buf, strlen(buf));
120 
121  if (strlen(buf) > 2) {
122  gosipres.push_back("<err>");
123  break;
124  }
125 
126  gosipres.push_back("<ok>");
127  continue;
128  }
129 
130  if (isreading) {
131  long value = 0;
132  if (!dabc::str_to_lint(buf,&value)) {
133  gosipres.push_back("<err>");
134  break;
135  }
136  // DOUT0("Reading ok %ld", value);
137  gosipres.push_back(dabc::format("%ld", value));
138  continue;
139  }
140 
141  // no idea that should be returned by other commands
142  gosipres.push_back("<undef>");
143  }
144 
145  while (gosipres.size() < gosipcmd.size()) gosipres.push_back("<skip>");
146 
147  cmd.SetField("res", gosipres);
148  if (log_output)
149  cmd.SetField("log", gosiplog);
150 
151  DOUT2("*** CmdGosip finished ****");
152 
153 
154  return dabc::cmd_true;
155  }
156 
158 }
Command definition class.
Definition: Parameter.h:333
CommandDefinition & AddArg(const std::string &name, const std::string &kind="string", bool required=true, const RecordField &dflt=RecordField())
Definition: Parameter.cxx:593
Represents command with its arguments.
Definition: Command.h:99
int GetInt(const std::string &name, int dflt=0) const
Definition: Command.h:139
Represents objects hierarchy of remote (or local) DABC process.
Definition: Hierarchy.h:285
Hierarchy CreateHChild(const std::string &name, bool allowslahes=false, bool sortorder=false)
Create child item in hierarchy with specified name If allowslahes enabled, instead of subfolders item...
Definition: Hierarchy.h:392
void Create(const std::string &name, bool withmutex=false)
Create top-level object with specified name.
Definition: Hierarchy.cxx:934
virtual int ExecuteCommand(Command cmd)
Main method where commands are executed.
Definition: Module.h:232
unsigned CreateTimer(const std::string &name, double period_sec=-1., bool synchron=false)
Definition: Module.cxx:109
std::vector< std::string > AsStrVect() const
Definition: Record.cxx:923
RecordField GetField(const std::string &name) const
Definition: Record.h:510
bool SetField(const std::string &name, const RecordField &v)
Definition: Record.h:516
bool IsName(const char *name) const
Returns true if object name is the same as specified one.
Definition: Reference.cxx:177
CommandDefinition CreateCmdDef(const std::string &name)
Definition: Worker.cxx:603
Hierarchy fWorkerHierarchy
place for publishing of worker parameters
Definition: Worker.h:168
virtual bool PublishPars(const std::string &path)
Definition: Worker.cxx:1081
Player(const std::string &name, dabc::Command cmd=nullptr)
Definition: Player.cxx:22
virtual void ProcessTimerEvent(unsigned timer)
Method called by framework when timer event is produced.
Definition: Player.cxx:45
virtual int ExecuteCommand(dabc::Command cmd)
Main method where commands are executed.
Definition: Player.cxx:52
virtual ~Player()
Definition: Player.cxx:41
#define DOUT2(args ...)
Definition: logging.h:170
Event manipulation API.
Definition: api.h:23
std::string format(const char *fmt,...)
Definition: string.cxx:49
const char * prop_kind
Definition: Hierarchy.cxx:29
bool str_to_lint(const char *val, long *res)
Convert string to long integer value.
Definition: string.cxx:162
@ cmd_true
Definition: Command.h:38