LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
G4ZMQServer Class Reference

#include "G4ZMQServer.hh"

Inheritance diagram for G4ZMQServer:

Public Member Functions

 G4ZMQServer ()
 
 ~G4ZMQServer ()
 
void SetEndpoint (const G4String &endpoint)
 
G4String GetEndpoint () const
 
void SetDebug (G4bool flag)
 
virtual G4UIsession * SessionStart ()
 
virtual void PauseSessionStart (const G4String &message)
 
virtual G4int ReceiveG4cout (const G4String &coutString)
 
virtual G4int ReceiveG4cerr (const G4String &cerrString)
 

Private Member Functions

G4String GetCommand (const G4String &input)
 
virtual void ExecuteCommand (const G4String &command)
 
virtual G4bool GetHelpChoice (G4int &)
 
virtual void ExitHelp () const
 

Private Attributes

G4bool qdebug_
 
G4String endpoint_
 
G4UItcsh * shell_
 

Detailed Description

Definition at line 36 of file G4ZMQServer.hh.

Constructor & Destructor Documentation

G4ZMQServer::G4ZMQServer ( )

Definition at line 74 of file G4ZMQServer.cc.

References endpoint_, qdebug_, and shell_.

75 {
76  endpoint_ = "tcp://127.0.0.1:5555";
77  qdebug_ = false;
78  shell_= new G4UItcsh();
79  shell_-> SetLsColor(BLUE, RED);
80 
81  ::ui_manager = G4UImanager::GetUIpointer();
82  ::ui_manager-> SetSession(this);
83  ::ui_manager-> SetCoutDestination(this);
84 
85  ::qexit = false;
86 }
G4String endpoint_
Definition: G4ZMQServer.hh:54
G4bool qdebug_
Definition: G4ZMQServer.hh:53
G4UItcsh * shell_
Definition: G4ZMQServer.hh:55
G4ZMQServer::~G4ZMQServer ( )

Definition at line 89 of file G4ZMQServer.cc.

References shell_.

90 {
91  delete shell_;
92 }
G4UItcsh * shell_
Definition: G4ZMQServer.hh:55

Member Function Documentation

void G4ZMQServer::ExecuteCommand ( const G4String command)
privatevirtual

Definition at line 263 of file G4ZMQServer.cc.

Referenced by SessionStart().

264 {
265  auto rc = ::ui_manager-> ApplyCommand(command);
266  auto pcode = rc % 100;
267  auto status = rc - pcode;
268 
269  G4UIcommand* cmd = nullptr;
270  if( status != fCommandSucceeded ) cmd = FindCommand(command);
271 
272  switch ( status ) {
273  case fCommandSucceeded:
274  break;
275  case fCommandNotFound:
276  G4cerr << "command <" << ::ui_manager-> SolveAlias(command)
277  << "> not found" << G4endl;
278  break;
279  case fIllegalApplicationState:
280  G4cerr << "illegal application state -- command refused" << G4endl;
281  break;
282  case fParameterOutOfRange:
283  G4cerr << "Parameter is out of range" << G4endl;
284  break;
285  case fParameterOutOfCandidates:
286  G4cerr << "Parameter is out of candidate list (index "
287  << pcode << ")" << G4endl;
288  G4cerr << "Candidates : "
289  << cmd-> GetParameter(pcode)-> GetParameterCandidates()
290  << G4endl;
291  break;
292  case fParameterUnreadable:
293  G4cerr << "Parameter is wrong type and/or is not omittable (index "
294  << pcode << ")" << G4endl;
295  break;
296  case fAliasNotFound:
297  break;
298  default:
299  G4cerr << "command refused (" << status << ")" << G4endl;
300  break;
301  }
302 }
void G4ZMQServer::ExitHelp ( ) const
privatevirtual

Definition at line 311 of file G4ZMQServer.cc.

312 {
313 }
G4String G4ZMQServer::GetCommand ( const G4String input)
private

Definition at line 203 of file G4ZMQServer.cc.

References shell_.

Referenced by SessionStart().

204 {
205  const std::string nullstr = "";
206  G4String cmdstr = input;
207 
208  G4String cstr = cmdstr.strip(G4String::leading);
209  if ( cstr.length() == 0 ) {
210  cmdstr = nullstr;
211 
212  // define built-in shell commands...
213  } else if ( cstr(0) == '#' ) {
214  G4cout << cstr << G4endl;
215  cmdstr = nullstr;
216 
217  } else if ( cstr == "ls" || cstr.substr(0,3) == "ls " ) {
218  ListDirectory(cstr);
219  cmdstr = nullstr;
220 
221  } else if ( cstr == "lc" || cstr.substr(0,3) == "lc " ) {
222  shell_-> ListCommand(cstr.remove(0,2));
223  cmdstr = nullstr;
224 
225  } else if (cstr == "pwd" ) {
226  G4cout << "Current Command Directory : "
227  << GetCurrentWorkingDirectory() << G4endl;
228  cmdstr = nullstr;
229 
230  } else if ( cstr == "cwd" ) {
231  shell_-> ShowCurrentDirectory();
232  cmdstr = nullstr;
233 
234  } else if (cstr == "cd" || cstr.substr(0,3) == "cd " ) {
235  ChangeDirectoryCommand(cstr);
236  shell_-> SetCurrentDirectory(GetCurrentWorkingDirectory());
237  cmdstr = nullstr;
238 
239  } else if ( cstr == "help" || cstr.substr(0,5) == "help " ) {
240  TerminalHelp(cstr);
241  cmdstr = nullstr;
242 
243  } else if ( cstr(0) == '?' ) {
244  ShowCurrent(cstr);
245  cmdstr = nullstr;
246 
247  } else if ( cstr == "history" ) {
248  auto nh= ::ui_manager-> GetNumberOfHistory();
249  for (auto i = 0; i < nh; i++) {
250  G4cout << i << ": " << ::ui_manager->GetPreviousCommand(i) << G4endl;
251  }
252  cmdstr = nullstr;
253 
254  } else if ( cstr == "exit" ) {
255  ::qexit = true;
256  cmdstr = nullstr;
257  }
258 
259  return ModifyToFullPathCommand(cmdstr);
260 }
G4UItcsh * shell_
Definition: G4ZMQServer.hh:55
G4String G4ZMQServer::GetEndpoint ( ) const
inline

Definition at line 71 of file G4ZMQServer.hh.

References endpoint_.

72 {
73  return endpoint_;
74 }
G4String endpoint_
Definition: G4ZMQServer.hh:54
G4bool G4ZMQServer::GetHelpChoice ( G4int &  )
privatevirtual

Definition at line 305 of file G4ZMQServer.cc.

306 {
307  return true;
308 }
void G4ZMQServer::PauseSessionStart ( const G4String message)
virtual

Definition at line 174 of file G4ZMQServer.cc.

175 {
176 }
G4int G4ZMQServer::ReceiveG4cerr ( const G4String cerrString)
virtual

Definition at line 191 of file G4ZMQServer.cc.

References qdebug_.

192 {
193  if ( qdebug_ ) {
194  std::cerr << cerrString << std::flush;
195  }
196 
197  ::cout_stream << cerrString << std::flush;
198 
199  return 0;
200 }
G4bool qdebug_
Definition: G4ZMQServer.hh:53
G4int G4ZMQServer::ReceiveG4cout ( const G4String coutString)
virtual

Definition at line 179 of file G4ZMQServer.cc.

References qdebug_.

180 {
181  if ( qdebug_ ) {
182  std::cout << coutString << std::flush;
183  }
184 
185  ::cout_stream << coutString << std::flush;
186 
187  return 0;
188 }
G4bool qdebug_
Definition: G4ZMQServer.hh:53
G4UIsession * G4ZMQServer::SessionStart ( )
virtual

Definition at line 95 of file G4ZMQServer.cc.

References endpoint_, ExecuteCommand(), GetCommand(), message(), and qdebug_.

96 {
97  zmq::context_t context(1);
98  zmq::socket_t socket( context, ZMQ_REP );
99  socket.bind(endpoint_);
100 
101  enum { kBufferSize = 4096 };
102  char buffer[kBufferSize];
103 
104  while ( ! ::qexit ) {
105  if ( qdebug_ ) {
106  std::cout << "@@ Waiting..." << std::endl;
107  }
108 
109  // waiting command
110  zmq::message_t request;
111  G4bool qok = socket.recv(&request);
112  if ( qok == false ) ::ThrowException("G4ZMQSever: socket recv error");
113  auto end_pos = request.size();
114  if ( end_pos >= kBufferSize ) end_pos = kBufferSize - 1;
115  std::memcpy(buffer, request.data(), end_pos);
116  buffer[end_pos] = '\0';
117  std::string cmd_str = buffer;
118 
119  if ( qdebug_ ) {
120  std::cout << "@@ Recv=" << cmd_str << "<<" << std::endl;
121  }
122 
123  // store output & send back response
124  ::cout_stream.str("");
125 
126  if ( cmd_str == "@@ping" ) {
127  G4cout << "pong" << G4endl;
128 
129  } else if ( cmd_str == "@@debug") {
130  qdebug_ = true;
131  G4cout << "G4ZMQ debug activated" << G4endl;
132 
133  } else if ( cmd_str == "@@nodebug") {
134  qdebug_ = false;
135  G4cout << "G4ZMQ debug deactivated" << G4endl;
136 
137  } else if ( cmd_str == "@@get_command_tree" ) {
138  auto cwd_name = GetCurrentWorkingDirectory();
139  auto cwd_tree = FindDirectory(cwd_name.c_str());
140  ::command_list = "";
141  ::GetCommandTree(cwd_tree);
142  G4cout << ::command_list << std::flush;
143 
144  } else if ( cmd_str == "@@get_fullcommand_tree" ) {
145  auto root = ::ui_manager-> GetTree();
146  ::command_list = "";
147  ::GetCommandTree(root);
148  G4cout << ::command_list << std::flush;
149 
150  } else if ( cmd_str == "help" ) {
151  G4cout << "help <command>" << G4endl;
152 
153  } else {
154  G4String new_command = GetCommand(cmd_str);
155  if ( qdebug_ ) {
156  std::cout << ::black_str << "@@ Cmd="
157  << new_command << "<<" << std::endl;
158  }
159  ExecuteCommand(new_command);
160  }
161 
162  std::string reply = ::cout_stream.str();
163  size_t cout_size = reply.size();
164  zmq::message_t message(cout_size);
165  std::strncpy((char*)message.data(), reply.c_str(), cout_size);
166  qok = socket.send(message);
167  if ( qok == false ) ::ThrowException("G4ZMQServer: socket send error");
168  }
169 
170  return nullptr;
171 }
virtual void ExecuteCommand(const G4String &command)
Definition: G4ZMQServer.cc:263
G4String endpoint_
Definition: G4ZMQServer.hh:54
G4String GetCommand(const G4String &input)
Definition: G4ZMQServer.cc:203
void message(RunManager *runmanager)
Definition: ts_scorers.cc:74
G4bool qdebug_
Definition: G4ZMQServer.hh:53
void G4ZMQServer::SetDebug ( G4bool  flag)
inline

Definition at line 76 of file G4ZMQServer.hh.

References qdebug_.

77 {
78  qdebug_ = flag;
79 }
G4bool qdebug_
Definition: G4ZMQServer.hh:53
void G4ZMQServer::SetEndpoint ( const G4String endpoint)
inline

Definition at line 66 of file G4ZMQServer.hh.

References endpoint_.

67 {
68  endpoint_ = endpoint;
69 }
G4String endpoint_
Definition: G4ZMQServer.hh:54

Member Data Documentation

G4String G4ZMQServer::endpoint_
private

Definition at line 54 of file G4ZMQServer.hh.

Referenced by G4ZMQServer(), GetEndpoint(), SessionStart(), and SetEndpoint().

G4bool G4ZMQServer::qdebug_
private

Definition at line 53 of file G4ZMQServer.hh.

Referenced by G4ZMQServer(), ReceiveG4cerr(), ReceiveG4cout(), SessionStart(), and SetDebug().

G4UItcsh* G4ZMQServer::shell_
private

Definition at line 55 of file G4ZMQServer.hh.

Referenced by G4ZMQServer(), GetCommand(), and ~G4ZMQServer().


The documentation for this class was generated from the following files: