32 #include "G4UItcsh.hh" 33 #include "G4UImanager.hh" 34 #include "G4UIcommandTree.hh" 40 G4UImanager* ui_manager =
nullptr;
42 std::stringstream cout_stream;
43 std::string black_str =
"\033[30m";
44 std::string command_list =
"";
46 void ThrowException(
const std::string&
message)
49 ss <<
"[ERROR] " << message << std::endl;
50 throw std::runtime_error(ss.str());
54 void GetCommandTree(G4UIcommandTree* ctree)
56 command_list += (ctree-> GetPathName() +
" ");
58 auto n_cmd = ctree-> GetCommandEntry();
59 for (
auto icmd = 1; icmd <= n_cmd; icmd++ ) {
60 auto cmd_path = ctree-> GetCommand(icmd)-> GetCommandPath();
61 command_list += (cmd_path +
" ");
64 auto n_tree = ctree-> GetTreeEntry();
65 for (
auto itr = 1; itr <= n_tree ; itr++ ) {
66 G4UIcommandTree* atree = ctree-> GetTree(itr);
67 ::GetCommandTree(atree);
79 shell_-> SetLsColor(BLUE, RED);
81 ::ui_manager = G4UImanager::GetUIpointer();
82 ::ui_manager-> SetSession(
this);
83 ::ui_manager-> SetCoutDestination(
this);
97 zmq::context_t context(1);
98 zmq::socket_t socket( context, ZMQ_REP );
101 enum { kBufferSize = 4096 };
102 char buffer[kBufferSize];
104 while ( ! ::qexit ) {
106 std::cout <<
"@@ Waiting..." << std::endl;
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;
120 std::cout <<
"@@ Recv=" << cmd_str <<
"<<" << std::endl;
124 ::cout_stream.str(
"");
126 if ( cmd_str ==
"@@ping" ) {
127 G4cout <<
"pong" << G4endl;
129 }
else if ( cmd_str ==
"@@debug") {
131 G4cout <<
"G4ZMQ debug activated" << G4endl;
133 }
else if ( cmd_str ==
"@@nodebug") {
135 G4cout <<
"G4ZMQ debug deactivated" << G4endl;
137 }
else if ( cmd_str ==
"@@get_command_tree" ) {
138 auto cwd_name = GetCurrentWorkingDirectory();
139 auto cwd_tree = FindDirectory(cwd_name.c_str());
141 ::GetCommandTree(cwd_tree);
142 G4cout << ::command_list << std::flush;
144 }
else if ( cmd_str ==
"@@get_fullcommand_tree" ) {
145 auto root = ::ui_manager-> GetTree();
147 ::GetCommandTree(root);
148 G4cout << ::command_list << std::flush;
150 }
else if ( cmd_str ==
"help" ) {
151 G4cout <<
"help <command>" << G4endl;
156 std::cout << ::black_str <<
"@@ Cmd=" 157 << new_command <<
"<<" << std::endl;
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");
182 std::cout << coutString << std::flush;
185 ::cout_stream << coutString << std::flush;
194 std::cerr << cerrString << std::flush;
197 ::cout_stream << cerrString << std::flush;
205 const std::string nullstr =
"";
208 G4String cstr = cmdstr.strip(G4String::leading);
209 if ( cstr.length() == 0 ) {
213 }
else if ( cstr(0) ==
'#' ) {
214 G4cout << cstr << G4endl;
217 }
else if ( cstr ==
"ls" || cstr.substr(0,3) ==
"ls " ) {
221 }
else if ( cstr ==
"lc" || cstr.substr(0,3) ==
"lc " ) {
222 shell_-> ListCommand(cstr.remove(0,2));
225 }
else if (cstr ==
"pwd" ) {
226 G4cout <<
"Current Command Directory : " 227 << GetCurrentWorkingDirectory() << G4endl;
230 }
else if ( cstr ==
"cwd" ) {
231 shell_-> ShowCurrentDirectory();
234 }
else if (cstr ==
"cd" || cstr.substr(0,3) ==
"cd " ) {
235 ChangeDirectoryCommand(cstr);
236 shell_-> SetCurrentDirectory(GetCurrentWorkingDirectory());
239 }
else if ( cstr ==
"help" || cstr.substr(0,5) ==
"help " ) {
243 }
else if ( cstr(0) ==
'?' ) {
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;
254 }
else if ( cstr ==
"exit" ) {
259 return ModifyToFullPathCommand(cmdstr);
265 auto rc = ::ui_manager-> ApplyCommand(command);
266 auto pcode = rc % 100;
267 auto status = rc - pcode;
269 G4UIcommand* cmd =
nullptr;
270 if( status != fCommandSucceeded ) cmd = FindCommand(command);
273 case fCommandSucceeded:
275 case fCommandNotFound:
276 G4cerr <<
"command <" << ::ui_manager-> SolveAlias(command)
277 <<
"> not found" << G4endl;
279 case fIllegalApplicationState:
280 G4cerr <<
"illegal application state -- command refused" << G4endl;
282 case fParameterOutOfRange:
283 G4cerr <<
"Parameter is out of range" << G4endl;
285 case fParameterOutOfCandidates:
286 G4cerr <<
"Parameter is out of candidate list (index " 287 << pcode <<
")" << G4endl;
288 G4cerr <<
"Candidates : " 289 << cmd-> GetParameter(pcode)-> GetParameterCandidates()
292 case fParameterUnreadable:
293 G4cerr <<
"Parameter is wrong type and/or is not omittable (index " 294 << pcode <<
")" << G4endl;
299 G4cerr <<
"command refused (" << status <<
")" << G4endl;
virtual void ExecuteCommand(const G4String &command)
G4String GetCommand(const G4String &input)
void message(RunManager *runmanager)
virtual G4bool GetHelpChoice(G4int &)
virtual void ExitHelp() const
virtual void PauseSessionStart(const G4String &message)
virtual G4UIsession * SessionStart()
virtual G4int ReceiveG4cerr(const G4String &cerrString)
virtual G4int ReceiveG4cout(const G4String &coutString)