Geant4  v4-10.4-release
 모두 클래스 네임스페이스들 파일들 함수 변수 타입정의 열거형 타입 열거형 멤버 Friends 매크로 그룹들 페이지들
G4ZMQServer.cc
이 파일의 문서화 페이지로 가기
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id: G4RunManager.hh 95232 2016-02-01 14:31:22Z gcosmo $
28 //
29 //
30 #include <cstring>
31 #include <sstream>
32 #include <zmq.hpp>
33 #include "G4UItcsh.hh"
34 #include "G4UImanager.hh"
35 #include "G4UIcommandTree.hh"
36 #include "G4ZMQServer.hh"
37 
38 // --------------------------------------------------------------------------
39 namespace {
40 
41 G4UImanager* ui_manager = nullptr;
42 G4bool qexit = false;
43 std::stringstream cout_stream;
44 std::string black_str = "\033[30m";
45 std::string command_list = "";
46 
47 void ThrowException(const std::string& message)
48 {
49  std::stringstream ss;
50  ss << "[ERROR] " << message << std::endl;
51  throw std::runtime_error(ss.str());
52 }
53 
54 // --------------------------------------------------------------------------
55 void GetCommandTree(G4UIcommandTree* ctree)
56 {
57  command_list += (ctree-> GetPathName() + " ");
58 
59  auto n_cmd = ctree-> GetCommandEntry();
60  for ( auto icmd = 1; icmd <= n_cmd; icmd++ ) {
61  auto cmd_path = ctree-> GetCommand(icmd)-> GetCommandPath();
62  command_list += (cmd_path + " ");
63  }
64 
65  auto n_tree = ctree-> GetTreeEntry();
66  for ( auto itr = 1; itr <= n_tree ; itr++ ) {
67  G4UIcommandTree* atree = ctree-> GetTree(itr);
68  ::GetCommandTree(atree);
69  }
70 }
71 
72 } // end of namespace
73 
74 // --------------------------------------------------------------------------
76 {
77  endpoint_ = "tcp://127.0.0.1:5555";
78  qdebug_ = false;
79  shell_= new G4UItcsh();
80  shell_-> SetLsColor(BLUE, RED);
81 
82  ::ui_manager = G4UImanager::GetUIpointer();
83  ::ui_manager-> SetSession(this);
84  ::ui_manager-> SetCoutDestination(this);
85 
86  ::qexit = false;
87 }
88 
89 // --------------------------------------------------------------------------
91 {
92  delete shell_;
93 }
94 
95 // --------------------------------------------------------------------------
97 {
98  zmq::context_t context(1);
99  zmq::socket_t socket( context, ZMQ_REP );
100  socket.bind(endpoint_);
101 
102  enum { kBufferSize = 4096 };
103  char buffer[kBufferSize];
104 
105  while ( ! ::qexit ) {
106  if ( qdebug_ ) {
107  std::cout << "@@ Waiting..." << std::endl;
108  }
109 
110  // waiting command
111  zmq::message_t request;
112  G4bool qok = socket.recv(&request);
113  if ( qok == false ) ::ThrowException("G4ZMQSever: socket recv error");
114  auto end_pos = request.size();
115  if ( end_pos >= kBufferSize ) end_pos = kBufferSize - 1;
116  std::memcpy(buffer, request.data(), end_pos);
117  buffer[end_pos] = '\0';
118  std::string cmd_str = buffer;
119 
120  if ( qdebug_ ) {
121  std::cout << "@@ Recv=" << cmd_str << "<<" << std::endl;
122  }
123 
124  // store output & send back response
125  ::cout_stream.str("");
126 
127  if ( cmd_str == "@@ping" ) {
128  G4cout << "pong" << G4endl;
129 
130  } else if ( cmd_str == "@@debug") {
131  qdebug_ = true;
132  G4cout << "G4ZMQ debug activated" << G4endl;
133 
134  } else if ( cmd_str == "@@nodebug") {
135  qdebug_ = false;
136  G4cout << "G4ZMQ debug deactivated" << G4endl;
137 
138  } else if ( cmd_str == "@@get_command_tree" ) {
139  auto cwd_name = GetCurrentWorkingDirectory();
140  auto cwd_tree = FindDirectory(cwd_name.c_str());
141  ::command_list = "";
142  ::GetCommandTree(cwd_tree);
143  G4cout << ::command_list << std::flush;
144 
145  } else if ( cmd_str == "@@get_fullcommand_tree" ) {
146  auto root = ::ui_manager-> GetTree();
147  ::command_list = "";
148  ::GetCommandTree(root);
149  G4cout << ::command_list << std::flush;
150 
151  } else if ( cmd_str == "help" ) {
152  G4cout << "help <command>" << G4endl;
153 
154  } else {
155  G4String new_command = GetCommand(cmd_str);
156  if ( qdebug_ ) {
157  std::cout << ::black_str << "@@ Cmd="
158  << new_command << "<<" << std::endl;
159  }
160  ExecuteCommand(new_command);
161  }
162 
163  std::string reply = ::cout_stream.str();
164  size_t cout_size = reply.size();
165  zmq::message_t message(cout_size);
166  std::strncpy((char*)message.data(), reply.c_str(), cout_size);
167  qok = socket.send(message);
168  if ( qok == false ) ::ThrowException("G4ZMQServer: socket send error");
169  }
170 
171  return nullptr;
172 }
173 
174 // --------------------------------------------------------------------------
176 {
177 }
178 
179 // --------------------------------------------------------------------------
181 {
182  if ( qdebug_ ) {
183  std::cout << coutString << std::flush;
184  }
185 
186  ::cout_stream << coutString << std::flush;
187 
188  return 0;
189 }
190 
191 // --------------------------------------------------------------------------
193 {
194  if ( qdebug_ ) {
195  std::cerr << cerrString << std::flush;
196  }
197 
198  ::cout_stream << cerrString << std::flush;
199 
200  return 0;
201 }
202 
203 // --------------------------------------------------------------------------
205 {
206  const std::string nullstr = "";
207  G4String cmdstr = input;
208 
209  G4String cstr = cmdstr.strip(G4String::leading);
210  if ( cstr.length() == 0 ) {
211  cmdstr = nullstr;
212 
213  // define built-in shell commands...
214  } else if ( cstr(0) == '#' ) {
215  G4cout << cstr << G4endl;
216  cmdstr = nullstr;
217 
218  } else if ( cstr == "ls" || cstr.substr(0,3) == "ls " ) {
219  ListDirectory(cstr);
220  cmdstr = nullstr;
221 
222  } else if ( cstr == "lc" || cstr.substr(0,3) == "lc " ) {
223  shell_-> ListCommand(cstr.remove(0,2));
224  cmdstr = nullstr;
225 
226  } else if (cstr == "pwd" ) {
227  G4cout << "Current Command Directory : "
229  cmdstr = nullstr;
230 
231  } else if ( cstr == "cwd" ) {
232  shell_-> ShowCurrentDirectory();
233  cmdstr = nullstr;
234 
235  } else if (cstr == "cd" || cstr.substr(0,3) == "cd " ) {
237  shell_-> SetCurrentDirectory(GetCurrentWorkingDirectory());
238  cmdstr = nullstr;
239 
240  } else if ( cstr == "help" || cstr.substr(0,5) == "help " ) {
241  TerminalHelp(cstr);
242  cmdstr = nullstr;
243 
244  } else if ( cstr(0) == '?' ) {
245  ShowCurrent(cstr);
246  cmdstr = nullstr;
247 
248  } else if ( cstr == "history" ) {
249  auto nh= ::ui_manager-> GetNumberOfHistory();
250  for (auto i = 0; i < nh; i++) {
251  G4cout << i << ": " << ::ui_manager->GetPreviousCommand(i) << G4endl;
252  }
253  cmdstr = nullstr;
254 
255  } else if ( cstr == "exit" ) {
256  ::qexit = true;
257  cmdstr = nullstr;
258  }
259 
260  return ModifyToFullPathCommand(cmdstr);
261 }
262 
263 // --------------------------------------------------------------------------
265 {
266  auto rc = ::ui_manager-> ApplyCommand(command);
267  auto pcode = rc % 100;
268  auto status = rc - pcode;
269 
270  G4UIcommand* cmd = nullptr;
271  if( status != fCommandSucceeded ) cmd = FindCommand(command);
272 
273  switch ( status ) {
274  case fCommandSucceeded:
275  break;
276  case fCommandNotFound:
277  G4cerr << "command <" << ::ui_manager-> SolveAlias(command)
278  << "> not found" << G4endl;
279  break;
281  G4cerr << "illegal application state -- command refused" << G4endl;
282  break;
284  G4cerr << "Parameter is out of range" << G4endl;
285  break;
287  G4cerr << "Parameter is out of candidate list (index "
288  << pcode << ")" << G4endl;
289  G4cerr << "Candidates : "
290  << cmd-> GetParameter(pcode)-> GetParameterCandidates()
291  << G4endl;
292  break;
294  G4cerr << "Parameter is wrong type and/or is not omittable (index "
295  << pcode << ")" << G4endl;
296  break;
297  case fAliasNotFound:
298  break;
299  default:
300  G4cerr << "command refused (" << status << ")" << G4endl;
301  break;
302  }
303 }
304 
305 // --------------------------------------------------------------------------
307 {
308  return true;
309 }
310 
311 // --------------------------------------------------------------------------
313 {
314 }
virtual void ExecuteCommand(const G4String &command)
Definition: G4ZMQServer.cc:264
G4String & remove(str_size)
G4String endpoint_
Definition: G4ZMQServer.hh:55
G4String GetCommand(const G4String &input)
Definition: G4ZMQServer.cc:204
#define G4endl
Definition: G4ios.hh:61
void message(RunManager *runmanager)
Definition: ts_scorers.cc:72
virtual G4bool GetHelpChoice(G4int &)
Definition: G4ZMQServer.cc:306
virtual void ExitHelp() const
Definition: G4ZMQServer.cc:312
#define buffer
Definition: xmlparse.cc:628
virtual void PauseSessionStart(const G4String &message)
Definition: G4ZMQServer.cc:175
void TerminalHelp(const G4String &)
static G4UImanager * GetUIpointer()
Definition: G4UImanager.cc:73
virtual G4UIsession * SessionStart()
Definition: G4ZMQServer.cc:96
G4bool qdebug_
Definition: G4ZMQServer.hh:54
bool G4bool
Definition: G4Types.hh:79
G4String strip(G4int strip_Type=trailing, char c=' ')
G4GLOB_DLL std::ostream G4cerr
G4UIcommand * FindCommand(const char *commandName) const
G4String GetCurrentWorkingDirectory() const
const XML_Char * context
Definition: expat.h:434
int G4int
Definition: G4Types.hh:78
void ListDirectory(const G4String &) const
virtual G4int ReceiveG4cerr(const G4String &cerrString)
Definition: G4ZMQServer.cc:192
void ChangeDirectoryCommand(const G4String &)
G4GLOB_DLL std::ostream G4cout
G4UItcsh * shell_
Definition: G4ZMQServer.hh:56
void ShowCurrent(const G4String &) const
G4String ModifyToFullPathCommand(const char *aCommandLine) const
G4UIcommandTree * FindDirectory(const char *dirName) const
virtual G4int ReceiveG4cout(const G4String &coutString)
Definition: G4ZMQServer.cc:180