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 | // 12/06/2002 G4UIGainServer H. MInamimoto and H. Yoshida created |
---|
28 | // $Id: G4UIGainServer.cc,v 1.11 2007/11/16 14:59:35 kmura Exp $ |
---|
29 | // $Name: $ |
---|
30 | // |
---|
31 | #ifndef WIN32 |
---|
32 | |
---|
33 | #include "G4UIGainServer.hh" |
---|
34 | #include <netdb.h> |
---|
35 | |
---|
36 | #include <sstream> |
---|
37 | #include "G4StateManager.hh" |
---|
38 | #include "G4UIcommandTree.hh" |
---|
39 | #include "G4UIcommand.hh" |
---|
40 | #include "G4UIcommandStatus.hh" |
---|
41 | |
---|
42 | |
---|
43 | ////////////////////////////////////////////// |
---|
44 | G4UIGainServer::G4UIGainServer() |
---|
45 | ////////////////////////////////////////////// |
---|
46 | { |
---|
47 | TVersion ="T1.0a"; JVersion="J1.0a"; |
---|
48 | prefix = "/"; |
---|
49 | |
---|
50 | port = DEFAULT_PORT; |
---|
51 | while(SetUPServer() == false){ |
---|
52 | G4cout<<"can't get the port no. "<<port<<" Now, try to get the next port "<<port+1<<G4endl; |
---|
53 | port++; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | UI= G4UImanager::GetUIpointer(); |
---|
58 | UI-> SetSession(this); |
---|
59 | UI-> SetCoutDestination(this); |
---|
60 | |
---|
61 | G4StateManager* statM = G4StateManager::GetStateManager(); |
---|
62 | promptCharacter = statM->GetStateString(statM->GetCurrentState()); |
---|
63 | uiMode = terminal_mode; |
---|
64 | |
---|
65 | iExit= FALSE; |
---|
66 | iCont= FALSE; |
---|
67 | |
---|
68 | G4UIcommandTree* tree = UI->GetTree(); |
---|
69 | GetNewTreeStructure(tree,0); |
---|
70 | GetNewTreeValues(tree,0); |
---|
71 | previousTreeCommands = newTreeCommands; |
---|
72 | previousTreeParams = newTreeParams; |
---|
73 | previousTreePCP = newTreePCP; |
---|
74 | |
---|
75 | } |
---|
76 | |
---|
77 | ///////////////////////////// |
---|
78 | G4UIGainServer::~G4UIGainServer() |
---|
79 | ///////////////////////////// |
---|
80 | { |
---|
81 | |
---|
82 | if(G4UImanager::GetUIpointer()) { |
---|
83 | UI-> SetSession(NULL); |
---|
84 | UI-> SetCoutDestination(NULL); |
---|
85 | } |
---|
86 | |
---|
87 | if(G4UImanager::GetUIpointer()!=0){ |
---|
88 | UI->SetSession(NULL); |
---|
89 | UI->SetCoutDestination(NULL); |
---|
90 | } |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | ///////////////////////////////////////// |
---|
95 | G4UIsession* G4UIGainServer::SessionStart() |
---|
96 | ///////////////////////////////////////// |
---|
97 | { |
---|
98 | G4String newCommand; |
---|
99 | |
---|
100 | G4StateManager* statM = G4StateManager::GetStateManager(); |
---|
101 | promptCharacter = statM->GetStateString(statM->GetCurrentState()); |
---|
102 | |
---|
103 | iExit= TRUE; |
---|
104 | |
---|
105 | WaitingConnection(); |
---|
106 | while(iExit){ |
---|
107 | newCommand= GetCommand(); |
---|
108 | ExecuteCommand(newCommand); |
---|
109 | } |
---|
110 | return NULL; |
---|
111 | } |
---|
112 | |
---|
113 | ////////////////////////////////////////////////// |
---|
114 | void G4UIGainServer::PauseSessionStart(G4String msg) |
---|
115 | ////////////////////////////////////////////////// |
---|
116 | { |
---|
117 | promptCharacter = msg; |
---|
118 | G4cout<<"@@PROMPT \""<<promptCharacter<<"\""<<G4endl; |
---|
119 | |
---|
120 | iCont= TRUE; |
---|
121 | |
---|
122 | G4String newCommand= GetCommand(); |
---|
123 | while(iCont){ |
---|
124 | ExecuteCommand(newCommand); |
---|
125 | newCommand= GetCommand(); |
---|
126 | strcpy(buf,"nowIdle"); |
---|
127 | write(socketD[1],buf,strlen(buf)); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | //////////////////////////////////////////////////// |
---|
132 | void G4UIGainServer::ExecuteCommand(G4String aCommand) |
---|
133 | //////////////////////////////////////////////////// |
---|
134 | { |
---|
135 | if(aCommand.length()<2) return; |
---|
136 | |
---|
137 | G4UIcommandTree* tree = UI->GetTree(); |
---|
138 | if(aCommand.length()<2) return; |
---|
139 | G4int returnVal = UI->ApplyCommand(aCommand); |
---|
140 | G4int paramIndex = returnVal % 100; |
---|
141 | // 0 - 98 : paramIndex-th parameter is invalid |
---|
142 | // 99 : convination of parameters is invalid |
---|
143 | G4int commandStatus = returnVal - paramIndex; |
---|
144 | |
---|
145 | UpdateState(); |
---|
146 | |
---|
147 | if(uiMode != terminal_mode){ |
---|
148 | switch(commandStatus) { |
---|
149 | case fCommandSucceeded: |
---|
150 | GetNewTreeStructure(tree,0); |
---|
151 | GetNewTreeValues(tree,0); |
---|
152 | if(CommandUpdated()){ |
---|
153 | NotifyCommandUpdate(); |
---|
154 | } else{ |
---|
155 | UpdateParamVal(); |
---|
156 | } |
---|
157 | previousTreeCommands = newTreeCommands; |
---|
158 | previousTreeParams = newTreeParams; |
---|
159 | previousTreePCP = newTreePCP; |
---|
160 | break; |
---|
161 | case fCommandNotFound: |
---|
162 | G4cerr << "@@ErrResult \" <" << UI->SolveAlias(aCommand) << "> not found.\"" << G4endl; |
---|
163 | break; |
---|
164 | case fIllegalApplicationState: |
---|
165 | G4cerr << "@@ErrResult \"illegal application state -- command refused.\"" << G4endl; |
---|
166 | break; |
---|
167 | case fParameterOutOfRange: |
---|
168 | G4cout << "@@ErrResult \"Parameter Out of Range.\"" << G4endl; |
---|
169 | break; |
---|
170 | case fParameterUnreadable: |
---|
171 | G4cout << "@@ErrResult \"Parameter is wrong type and/or is not omittable.\""<<G4endl; |
---|
172 | break; |
---|
173 | case fParameterOutOfCandidates: |
---|
174 | G4cerr << "@@ErrResult \"Parameter is out of candidate.\"" << G4endl; |
---|
175 | break; |
---|
176 | case fAliasNotFound: |
---|
177 | default: |
---|
178 | G4cerr << "command refused (" << commandStatus << ")" << G4endl; |
---|
179 | } |
---|
180 | } |
---|
181 | } |
---|
182 | |
---|
183 | /////////////////////////////////// |
---|
184 | G4String G4UIGainServer::GetCommand() |
---|
185 | /////////////////////////////////// |
---|
186 | { |
---|
187 | G4String newCommand; |
---|
188 | G4String nullString; |
---|
189 | |
---|
190 | while( 1 ) |
---|
191 | { |
---|
192 | G4UIcommandTree* tree = UI->GetTree(); |
---|
193 | if ( uiMode != terminal_mode ){ |
---|
194 | G4cout << "@@PROMPT \"" << promptCharacter << "\"" << G4endl; |
---|
195 | } |
---|
196 | if ( uiMode != java_mode ){ |
---|
197 | G4cout << promptCharacter << "> " << G4endl; |
---|
198 | }else{ |
---|
199 | G4cout << "@@Ready" << G4endl; |
---|
200 | } |
---|
201 | |
---|
202 | |
---|
203 | ///////////////////////////// |
---|
204 | ///////////////////////////// |
---|
205 | read(socketD[1],buf,1024); |
---|
206 | newCommand=buf; |
---|
207 | //DEBUG cout<<"->"<<newCommand<<"<-"<<newCommand.length()<<G4endl; |
---|
208 | //newCommand.readLine( G4cin, FALSE ); |
---|
209 | ///////////////////////////// |
---|
210 | ///////////////////////////// |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | if (!G4cin.good()) { G4cin.clear(); newCommand = nullString; iExit=false;break;} |
---|
215 | |
---|
216 | newCommand = newCommand.strip(G4String::leading); |
---|
217 | if( newCommand.length() < 1) { break; } |
---|
218 | |
---|
219 | while( newCommand(newCommand.length()-1) == '_' ) |
---|
220 | { |
---|
221 | G4String newLine; |
---|
222 | newCommand.remove(newCommand.length()-1); |
---|
223 | newLine.readLine( G4cin ); |
---|
224 | if (!G4cin.good()) { G4cin.clear(); newCommand = nullString; iExit=false;break;} |
---|
225 | newCommand.append(newLine); |
---|
226 | } |
---|
227 | |
---|
228 | G4String nC = newCommand.strip(G4String::leading); |
---|
229 | if( nC.length() < 1) { break; } |
---|
230 | |
---|
231 | // -------------------- nC.toUpper(); |
---|
232 | if( nC == "@@GainmodeJAVA" ) { |
---|
233 | uiMode = java_mode; |
---|
234 | G4cout << G4endl << "@@Version " << JVersion << G4endl; |
---|
235 | SendCommandProperties(tree); |
---|
236 | NotifyStateChange(); |
---|
237 | } |
---|
238 | else if( nC == "@@GainmodeTcl" ) { |
---|
239 | uiMode = tcl_mode; |
---|
240 | G4cout << G4endl << "@@Version " << TVersion << G4endl; |
---|
241 | SendCommandProperties(tree); |
---|
242 | NotifyStateChange(); |
---|
243 | } |
---|
244 | else if( nC(0) == '#' ) |
---|
245 | { G4cout << nC << G4endl; } |
---|
246 | |
---|
247 | else if( nC == "ls" || nC(0,3) == "ls " ) |
---|
248 | { ListDirectory( nC ); } |
---|
249 | else if( nC == "pwd" ) |
---|
250 | { G4cout << "Current Working Directory : " << prefix << G4endl; } |
---|
251 | else if( nC(0,2) == "cd" || nC(0,3) == "cd " ) |
---|
252 | { ChangeDirectory( nC ); } |
---|
253 | else if( nC == "help" || nC(0,5) == "help ") |
---|
254 | { TerminalHelp( nC ); } |
---|
255 | else if( nC(0) == '?' ) |
---|
256 | { ShowCurrent( nC ); } |
---|
257 | else if( nC(0,4) == "hist" || nC == "history") |
---|
258 | { |
---|
259 | G4int nh = UI->GetNumberOfHistory(); |
---|
260 | for(int i=0;i<nh;i++) |
---|
261 | { G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl; } |
---|
262 | } |
---|
263 | else if( nC(0) == '!' ) |
---|
264 | { |
---|
265 | G4String ss = nC(1,nC.length()-1); |
---|
266 | G4int vl; |
---|
267 | const char* tt = ss; |
---|
268 | std::istringstream is((char*)tt); |
---|
269 | is >> vl; |
---|
270 | G4int nh = UI->GetNumberOfHistory(); |
---|
271 | if(vl>=0 && vl<nh) |
---|
272 | { |
---|
273 | newCommand = UI->GetPreviousCommand(vl); |
---|
274 | G4cout << newCommand << G4endl; |
---|
275 | break; |
---|
276 | } |
---|
277 | else |
---|
278 | { G4cerr << "history " << vl << " is not found." << G4endl; } |
---|
279 | } |
---|
280 | else if( nC(0,4) == "exit" ) |
---|
281 | { |
---|
282 | if( iCont ) |
---|
283 | { |
---|
284 | if ( uiMode == terminal_mode){ |
---|
285 | G4cerr << "You are now processing RUN." << G4endl; |
---|
286 | G4cerr << "Please abrot it using \"/run/abort\" command first" << G4endl; |
---|
287 | G4cerr << " and use \"continue\" command until the application" << G4endl; |
---|
288 | G4cerr << " becomes to Idle." << G4endl; |
---|
289 | }else{ |
---|
290 | G4cout << "@@ErrResult \"You are now processing RUN.\"" << G4endl; |
---|
291 | } |
---|
292 | } |
---|
293 | else |
---|
294 | { |
---|
295 | close(socketD[1]); |
---|
296 | close(socketD[2]); |
---|
297 | iExit = false; |
---|
298 | newCommand = nullString; |
---|
299 | break; |
---|
300 | } |
---|
301 | } |
---|
302 | else if( nC == "cont" || nC == "continue" ) |
---|
303 | { |
---|
304 | iCont = false; |
---|
305 | newCommand = nullString; |
---|
306 | break; |
---|
307 | } |
---|
308 | else |
---|
309 | { break; } |
---|
310 | } |
---|
311 | return GetFullPath(newCommand); |
---|
312 | } |
---|
313 | |
---|
314 | ////////////////////////////////////////////////////// |
---|
315 | G4int G4UIGainServer::ReceiveG4cout(G4String coutString) |
---|
316 | ////////////////////////////////////////////////////// |
---|
317 | { |
---|
318 | if(socketD[1]>0){ |
---|
319 | write(socketD[1],coutString,coutString.length()); |
---|
320 | } |
---|
321 | return 0; |
---|
322 | |
---|
323 | |
---|
324 | |
---|
325 | |
---|
326 | |
---|
327 | |
---|
328 | |
---|
329 | //std::cout << coutString << std::flush; |
---|
330 | //return 0; |
---|
331 | } |
---|
332 | |
---|
333 | ////////////////////////////////////////////////////// |
---|
334 | G4int G4UIGainServer::ReceiveG4cerr(G4String cerrString) |
---|
335 | ////////////////////////////////////////////////////// |
---|
336 | { |
---|
337 | if(socketD[2]>0){ |
---|
338 | write(socketD[2],cerrString,cerrString.length()); |
---|
339 | } |
---|
340 | return 0; |
---|
341 | |
---|
342 | |
---|
343 | |
---|
344 | //std::cerr << cerrString << std::flush; |
---|
345 | //return 0; |
---|
346 | } |
---|
347 | |
---|
348 | /////////////////////////////////////////////// |
---|
349 | G4bool G4UIGainServer::GetHelpChoice(G4int& aInt) |
---|
350 | /////////////////////////////////////////////// |
---|
351 | { |
---|
352 | G4cin >> aInt; |
---|
353 | if(!G4cin.good()){ |
---|
354 | G4cin.clear(); |
---|
355 | G4cin.ignore(30,'\n'); |
---|
356 | return FALSE; |
---|
357 | } |
---|
358 | return TRUE; |
---|
359 | } |
---|
360 | |
---|
361 | ///////////////////////////// |
---|
362 | void G4UIGainServer::ExitHelp() |
---|
363 | ///////////////////////////// |
---|
364 | { |
---|
365 | char temp[100]; |
---|
366 | G4cin.getline(temp, 100); |
---|
367 | } |
---|
368 | |
---|
369 | ///////////////////////////// |
---|
370 | bool G4UIGainServer::SetUPServer(){ |
---|
371 | ///////////////////////////// |
---|
372 | |
---|
373 | socketD[0] = socket(AF_INET,SOCK_STREAM,0); |
---|
374 | |
---|
375 | if(socketD[0]<0){ |
---|
376 | perror("server:socket"); |
---|
377 | return (false); |
---|
378 | //exit(1); |
---|
379 | } |
---|
380 | |
---|
381 | memset( (char *)&saddr,'\0',sizeof(saddr)) ; |
---|
382 | |
---|
383 | saddr.sin_family = AF_INET; |
---|
384 | saddr.sin_addr.s_addr = INADDR_ANY; |
---|
385 | saddr.sin_port = htons(port); |
---|
386 | unlink(SOCK_NAME); |
---|
387 | |
---|
388 | if(bind(socketD[0] , (struct sockaddr *)&saddr , sizeof(saddr))<0){ |
---|
389 | perror("bind"); |
---|
390 | return (false); |
---|
391 | //exit(1); |
---|
392 | } |
---|
393 | else{ G4cout<<"G4GainServer waiting at "<<port<<G4endl; } |
---|
394 | |
---|
395 | if(listen(socketD[0],1)<0){ |
---|
396 | perror("listen"); |
---|
397 | return (false); |
---|
398 | //exit(1); |
---|
399 | } |
---|
400 | |
---|
401 | return (true); |
---|
402 | } |
---|
403 | |
---|
404 | //////////////////////////////////////// |
---|
405 | void G4UIGainServer::WaitingConnection(){ |
---|
406 | //////////////////////////////////////// |
---|
407 | len = sizeof(caddr); |
---|
408 | |
---|
409 | for(int i=1;i<=2;i++){ |
---|
410 | #if defined __APPLE__ && (__GNUC__<4) |
---|
411 | if((socketD[i] = accept(socketD[0], (struct sockaddr *)&caddr,(int *)&len))<0){ |
---|
412 | #else |
---|
413 | if((socketD[i] = accept(socketD[0], (struct sockaddr *)&caddr,(socklen_t *)&len))<0){ |
---|
414 | #endif |
---|
415 | G4cerr<<"accept:"<<i<<G4endl; |
---|
416 | exit(1); |
---|
417 | } |
---|
418 | } |
---|
419 | close(socketD[0]); |
---|
420 | } |
---|
421 | |
---|
422 | /////////////////////////////////////////////////// |
---|
423 | G4String G4UIGainServer::GetFullPath(G4String aNewCommand){ |
---|
424 | /////////////////////////////////////////////////// |
---|
425 | G4String newCommand = aNewCommand.strip(G4String::both); |
---|
426 | G4String tmpString; |
---|
427 | if( newCommand(0) == '/' ) |
---|
428 | { tmpString = newCommand; } |
---|
429 | else if( newCommand(0,3) == "../" ) |
---|
430 | { |
---|
431 | G4String tmpPrefix = prefix; |
---|
432 | /*G4int*/ unsigned i_direc = 0; |
---|
433 | while( i_direc < newCommand.length() ) |
---|
434 | { |
---|
435 | if( newCommand(i_direc,3) == "../" ) |
---|
436 | { |
---|
437 | i_direc += 3; |
---|
438 | prefix = ModifyPrefix( G4String("../") ); |
---|
439 | } |
---|
440 | else |
---|
441 | { break; } |
---|
442 | } |
---|
443 | tmpString = prefix; |
---|
444 | tmpString.append( newCommand( i_direc, newCommand.length()-i_direc ) ); |
---|
445 | prefix = tmpPrefix; |
---|
446 | } |
---|
447 | else |
---|
448 | { |
---|
449 | tmpString = prefix; |
---|
450 | tmpString.append( newCommand ); |
---|
451 | } |
---|
452 | return tmpString; |
---|
453 | } |
---|
454 | |
---|
455 | //////////////////////////////// |
---|
456 | void G4UIGainServer::SessionTerminate(){ |
---|
457 | //////////////////////////////// |
---|
458 | G4cout<<"***** Terminal session end *****"<<G4endl; |
---|
459 | } |
---|
460 | |
---|
461 | |
---|
462 | ////////////////////////////////////////////// |
---|
463 | void G4UIGainServer::ShowCurrent(G4String newCommand){ |
---|
464 | ////////////////////////////////////////////// |
---|
465 | G4String theCommand = GetFullPath(newCommand(1,newCommand.length()-1)); |
---|
466 | G4String curV = UI->GetCurrentValues(theCommand); |
---|
467 | if( ! (curV.isNull()||curV(0)=='\0' ) ) { |
---|
468 | if (uiMode == terminal_mode){ |
---|
469 | G4cout << "Current value(s) of the parameter(s) : " << curV << G4endl; |
---|
470 | }else{ |
---|
471 | G4cout << "@@CurrentValue " << curV << G4endl; |
---|
472 | } |
---|
473 | } else if (uiMode == terminal_mode){ |
---|
474 | G4cout << "Current value is not available." << G4endl; |
---|
475 | } else { |
---|
476 | G4cout << "@@ErrResult \"Current value is not available.\"" << G4endl; |
---|
477 | } |
---|
478 | } |
---|
479 | |
---|
480 | ////////////////////////////////////////////////// |
---|
481 | void G4UIGainServer::ChangeDirectory(G4String newCommand){ |
---|
482 | ////////////////////////////////////////////////// |
---|
483 | G4String savedPrefix = prefix; |
---|
484 | if( newCommand.length() <= 3 ) |
---|
485 | { prefix = "/"; } |
---|
486 | else |
---|
487 | { |
---|
488 | G4String aNewPrefix = newCommand(3,newCommand.length()-3); |
---|
489 | G4String newPrefix = aNewPrefix.strip(G4String::both); |
---|
490 | if( newPrefix(0) == '/' ) |
---|
491 | { prefix = newPrefix; } |
---|
492 | else if( newPrefix(0) != '.' ) |
---|
493 | { |
---|
494 | prefix += newPrefix; |
---|
495 | } |
---|
496 | else |
---|
497 | { prefix = ModifyPrefix( newPrefix ); } |
---|
498 | } |
---|
499 | if( prefix( prefix.length() - 1 ) != '/' ) |
---|
500 | { prefix += "/"; } |
---|
501 | if( FindDirPath( prefix ) == NULL ) |
---|
502 | { |
---|
503 | G4cout << "Directory <" << prefix << "> is not found." << G4endl; |
---|
504 | prefix = savedPrefix; |
---|
505 | } |
---|
506 | } |
---|
507 | //////////////////////////////////////////////// |
---|
508 | void G4UIGainServer::ListDirectory(G4String newCommand){ |
---|
509 | //////////////////////////////////////////////// |
---|
510 | G4String targetDir('\0'); |
---|
511 | if( newCommand.length() <= 3 ) |
---|
512 | { targetDir = prefix; } |
---|
513 | else |
---|
514 | { |
---|
515 | G4String newPrefix = newCommand(3,newCommand.length()-3); |
---|
516 | newPrefix.strip(G4String::both); |
---|
517 | if( newPrefix(0) == '/' ) |
---|
518 | { targetDir = newPrefix; } |
---|
519 | else if( newPrefix(0) != '.' ) |
---|
520 | { |
---|
521 | targetDir = prefix; |
---|
522 | targetDir += newPrefix; |
---|
523 | } |
---|
524 | else |
---|
525 | { targetDir = ModifyPrefix( newPrefix ); } |
---|
526 | } |
---|
527 | if( targetDir( targetDir.length() - 1 ) != '/' ) |
---|
528 | { targetDir += "/"; } |
---|
529 | G4UIcommandTree * commandTree = FindDirPath( targetDir ); |
---|
530 | if( commandTree == NULL ) |
---|
531 | { G4cout << "Directory <" << targetDir << "> is not found." << G4endl; } |
---|
532 | else |
---|
533 | { commandTree->ListCurrent(); } |
---|
534 | } |
---|
535 | |
---|
536 | /////////////////////////////////////////////// |
---|
537 | void G4UIGainServer::TerminalHelp(G4String newCommand){ |
---|
538 | /////////////////////////////////////////////// |
---|
539 | G4UIcommandTree* treeTop = UI->GetTree(); |
---|
540 | str_size i = newCommand.index(" "); |
---|
541 | |
---|
542 | if(i!=std::string::npos){ |
---|
543 | G4String newValue = newCommand(i+1,newCommand.length()-(i+1)); |
---|
544 | newValue.strip(G4String::both); |
---|
545 | if(newValue(0)!='/'){ |
---|
546 | newValue.prepend(prefix); |
---|
547 | } |
---|
548 | G4UIcommand* theCommand = treeTop->FindPath(newValue); |
---|
549 | if(theCommand !=NULL){ |
---|
550 | theCommand->List(); |
---|
551 | return; |
---|
552 | } |
---|
553 | else{ |
---|
554 | G4cout<<"Command<" << newValue << "is not found."<<G4endl; |
---|
555 | return; |
---|
556 | } |
---|
557 | } |
---|
558 | |
---|
559 | G4UIcommandTree* floor[10]; |
---|
560 | floor[0] = treeTop; |
---|
561 | int iFloor = 0; |
---|
562 | unsigned prefixIndex = 1; |
---|
563 | while(prefixIndex<prefix.length()-1){ |
---|
564 | int ii = prefix.index("/",prefixIndex); |
---|
565 | floor[iFloor+1]= |
---|
566 | floor[iFloor]->GetTree(G4String(prefix(0,ii+1))); |
---|
567 | prefixIndex = ii+1; |
---|
568 | iFloor++; |
---|
569 | } |
---|
570 | floor[iFloor]->ListCurrentWithNum(); |
---|
571 | while(1){ |
---|
572 | int i; |
---|
573 | G4cout<<G4endl <<"Type the number (0:end, -n:n level back) :"<<std::flush; |
---|
574 | G4cin >> i; |
---|
575 | if(!G4cin.good()){ |
---|
576 | G4cin.clear(); |
---|
577 | G4cin.ignore(30,'\n'); |
---|
578 | G4cout<<G4endl <<"Not a number,once more"<<G4endl; continue; |
---|
579 | } |
---|
580 | else if(i<0){ |
---|
581 | iFloor += i; |
---|
582 | if(iFloor <0) iFloor =0; |
---|
583 | floor[iFloor]->ListCurrentWithNum(); continue; |
---|
584 | } |
---|
585 | else if(i==0){break;} |
---|
586 | else if(i>0){ |
---|
587 | int n_tree = floor[iFloor]->GetTreeEntry(); |
---|
588 | if(i>n_tree){ |
---|
589 | if(i<=n_tree+floor[iFloor]->GetCommandEntry()){ |
---|
590 | floor[iFloor]->GetCommand(i-n_tree)->List(); |
---|
591 | } |
---|
592 | } |
---|
593 | else{ |
---|
594 | floor[iFloor+1] = floor[iFloor]->GetTree(i); |
---|
595 | iFloor++; |
---|
596 | floor[iFloor]->ListCurrentWithNum(); |
---|
597 | } |
---|
598 | } |
---|
599 | } |
---|
600 | G4cout<<"Exit from Help."<<G4endl <<G4endl; |
---|
601 | G4cout<<G4endl; |
---|
602 | char temp[100]; |
---|
603 | G4cin.getline(temp,100); |
---|
604 | } |
---|
605 | |
---|
606 | |
---|
607 | /////////////////////////////////////////////////// |
---|
608 | G4String G4UIGainServer::ModifyPrefix(G4String newCommand){ |
---|
609 | /////////////////////////////////////////////////// |
---|
610 | G4String newPrefix = prefix; |
---|
611 | while(1){ |
---|
612 | if(newCommand(0,2) ==".."){ |
---|
613 | if(newPrefix !="/"){ |
---|
614 | G4String tmpString = newPrefix(0,newPrefix.length()-1); |
---|
615 | newPrefix = newPrefix(0,tmpString.last('/')+1); |
---|
616 | } |
---|
617 | } |
---|
618 | else{ |
---|
619 | newPrefix += newCommand; |
---|
620 | break; |
---|
621 | } |
---|
622 | if(newCommand == ".." || newCommand == "../"){ |
---|
623 | break; |
---|
624 | } |
---|
625 | newCommand=newCommand(3,newCommand.length()-3); |
---|
626 | } |
---|
627 | return newPrefix; |
---|
628 | } |
---|
629 | |
---|
630 | ////////////////////////////////////////////////////////// |
---|
631 | G4UIcommandTree* G4UIGainServer::FindDirPath(G4String newCommand){ |
---|
632 | ////////////////////////////////////////////////////////// |
---|
633 | G4UIcommandTree * comTree = UI->GetTree(); |
---|
634 | /*int*/ unsigned idx = 1; |
---|
635 | while( idx < newCommand.length()-1 ) |
---|
636 | { |
---|
637 | int i = newCommand.index("/",idx); |
---|
638 | comTree = comTree->GetTree(G4String(newCommand(0,i+1))); |
---|
639 | if( comTree == NULL ) |
---|
640 | { return NULL; } |
---|
641 | idx = i+1; |
---|
642 | } |
---|
643 | return comTree; |
---|
644 | } |
---|
645 | |
---|
646 | //// ----- for JAVA Gain |
---|
647 | |
---|
648 | ////////////////////////////////////////////////////////// |
---|
649 | void G4UIGainServer::SendCommandProperties(G4UIcommandTree* tree){ |
---|
650 | ////////////////////////////////////////////////////////// |
---|
651 | if( tree == NULL ) { |
---|
652 | G4cerr << "GetTree() returnes null." << G4endl; |
---|
653 | return; |
---|
654 | } |
---|
655 | if (uiMode == java_mode){ |
---|
656 | G4cout << "@@JTreeBegin" << G4endl; |
---|
657 | CodeGenJavaTree(tree, 0); |
---|
658 | G4cout << "@@JTreeEnd" << G4endl; |
---|
659 | CodeGenJavaParams(tree, 0); |
---|
660 | }else{} |
---|
661 | } |
---|
662 | |
---|
663 | //////////////////////////////////////////////////////////// |
---|
664 | void G4UIGainServer::SendParameterProperties(G4UIcommandTree* tree){ |
---|
665 | //////////////////////////////////////////////////////////// |
---|
666 | if( tree == NULL ) { |
---|
667 | G4cerr << "GetTree() returnes null." << G4endl; |
---|
668 | return; |
---|
669 | } |
---|
670 | if (uiMode == java_mode){ |
---|
671 | CodeGenJavaParams(tree, 0); |
---|
672 | }else{ } |
---|
673 | } |
---|
674 | |
---|
675 | ////////////////////////////////////////////////////////////// |
---|
676 | void G4UIGainServer::CodeGenJavaTree(G4UIcommandTree* tree,int level){ |
---|
677 | ////////////////////////////////////////////////////////////// |
---|
678 | int treeEntry, commandEntry; |
---|
679 | treeEntry = tree->GetTreeEntry(); |
---|
680 | commandEntry = tree->GetCommandEntry(); |
---|
681 | |
---|
682 | if(level!=0) { |
---|
683 | for(int i=0; i<commandEntry; i++){ |
---|
684 | G4cout << tree->GetCommand(i+1)->GetCommandPath() << G4endl; |
---|
685 | } |
---|
686 | } |
---|
687 | if(treeEntry == 0) return; //end recursion |
---|
688 | |
---|
689 | for(int j=0; j<treeEntry; j++){ |
---|
690 | CodeGenJavaTree(tree->GetTree(j+1), level+1); |
---|
691 | } |
---|
692 | } |
---|
693 | |
---|
694 | //////////////////////////////////////////////////////////////// |
---|
695 | void G4UIGainServer::CodeGenJavaParams(G4UIcommandTree* tree,int level){ |
---|
696 | //////////////////////////////////////////////////////////////// |
---|
697 | int treeEntry,commandEntry,i; |
---|
698 | G4UIcommandTree* treeLink; |
---|
699 | |
---|
700 | treeEntry = tree->GetTreeEntry(); |
---|
701 | commandEntry = tree->GetCommandEntry(); |
---|
702 | |
---|
703 | for(i=0;i<commandEntry; i++){ |
---|
704 | SendAParamProperty(tree->GetCommand(i+1)); |
---|
705 | } |
---|
706 | if(treeEntry ==0) return; |
---|
707 | |
---|
708 | for(i=0;i<treeEntry; i++){ |
---|
709 | treeLink = tree->GetTree(i+1); |
---|
710 | G4cout<<"@@JDirGuieBegin"<<G4endl; |
---|
711 | G4cout<<treeLink->GetPathName()<<G4endl <<treeLink->GetTitle()<<G4endl; |
---|
712 | G4cout<<"@@JDirGuideEnd"<<G4endl; |
---|
713 | CodeGenJavaParams(treeLink,level+1); |
---|
714 | } |
---|
715 | } |
---|
716 | |
---|
717 | /////////////////////////////////////////////////// |
---|
718 | void G4UIGainServer::SendAParamProperty(G4UIcommand* Comp){ |
---|
719 | /////////////////////////////////////////////////// |
---|
720 | int guidanceEntry, parameterEntry; |
---|
721 | G4String title, title2; |
---|
722 | G4UIparameter * prp; |
---|
723 | char c[2]; |
---|
724 | guidanceEntry = Comp->GetGuidanceEntries(); |
---|
725 | parameterEntry = Comp->GetParameterEntries(); |
---|
726 | G4cout << "@@JParamBegin" << G4endl; |
---|
727 | G4cout << Comp->GetCommandPath() << G4endl; |
---|
728 | G4cout << guidanceEntry << G4endl; |
---|
729 | for (int j=0; j<guidanceEntry; j++){ |
---|
730 | title = Comp->GetGuidanceLine(j); |
---|
731 | title2 = ""; |
---|
732 | if (title != ""){ |
---|
733 | for(int i=0; i< (int)title.length(); i++){ |
---|
734 | c[0]=title(i); |
---|
735 | c[1]= '\0'; |
---|
736 | if ( c[0] == '\n' || c[0] == '\r') { |
---|
737 | c[0]= ' '; |
---|
738 | } |
---|
739 | title2.append(c); |
---|
740 | } |
---|
741 | } |
---|
742 | G4cout << title2 << G4endl; |
---|
743 | } |
---|
744 | G4cout << Comp->GetRange() << G4endl; |
---|
745 | G4cout << parameterEntry << G4endl; |
---|
746 | for( int par=0; par<parameterEntry; par++) { |
---|
747 | prp = (G4UIparameter *)Comp->GetParameter(par); |
---|
748 | G4cout << prp->GetParameterName() << G4endl; |
---|
749 | G4cout << prp->GetParameterGuidance() << G4endl; |
---|
750 | G4cout << prp->GetParameterType() << G4endl; |
---|
751 | G4cout << prp->IsOmittable() << G4endl; |
---|
752 | G4cout << prp->GetDefaultValue() << G4endl; |
---|
753 | G4cout << prp->GetParameterRange() << G4endl; |
---|
754 | G4cout << prp->GetParameterCandidates() << G4endl; |
---|
755 | } |
---|
756 | G4cout << "@@JParamEnd" << G4endl; |
---|
757 | } |
---|
758 | |
---|
759 | ////////////////////////////////////////////////////////////// |
---|
760 | void G4UIGainServer::SendDisableList(G4UIcommandTree* tree,int level){ |
---|
761 | ////////////////////////////////////////////////////////////// |
---|
762 | int treeEntry, commandEntry; |
---|
763 | G4UIcommand * Comp; |
---|
764 | treeEntry = tree->GetTreeEntry(); |
---|
765 | commandEntry = tree->GetCommandEntry(); |
---|
766 | |
---|
767 | for(int com=0; com<commandEntry; com++) { |
---|
768 | Comp = tree->GetCommand(com+1); |
---|
769 | if( Comp->IsAvailable()==false ) { |
---|
770 | G4cout << Comp->GetCommandPath()<<G4endl; |
---|
771 | } |
---|
772 | } |
---|
773 | if( treeEntry == 0 ) return; // end recursion |
---|
774 | |
---|
775 | for( int i=0; i<treeEntry; i++) { |
---|
776 | SendDisableList(tree->GetTree(i+1), level+1); |
---|
777 | // be sure the function name is the same |
---|
778 | } |
---|
779 | } |
---|
780 | |
---|
781 | |
---|
782 | |
---|
783 | //####### update check routines #################################### |
---|
784 | |
---|
785 | /////////////////////////////// |
---|
786 | void G4UIGainServer::UpdateState(void){ |
---|
787 | /////////////////////////////// |
---|
788 | static G4ApplicationState previousState= G4State_PreInit; |
---|
789 | G4ApplicationState newState; |
---|
790 | G4StateManager *statM = G4StateManager::GetStateManager(); |
---|
791 | newState = statM->GetCurrentState(); |
---|
792 | if( newState != previousState ) |
---|
793 | { |
---|
794 | NotifyStateChange(); |
---|
795 | previousState = newState; |
---|
796 | } |
---|
797 | } |
---|
798 | |
---|
799 | ///////////////////////////////////// |
---|
800 | void G4UIGainServer::NotifyStateChange(void) |
---|
801 | ///////////////////////////////////// |
---|
802 | { |
---|
803 | G4String stateString; |
---|
804 | G4StateManager * statM = G4StateManager::GetStateManager(); |
---|
805 | G4UIcommandTree * tree = UI->GetTree(); |
---|
806 | stateString = statM->GetStateString(statM->GetCurrentState()); |
---|
807 | if ( uiMode != terminal_mode ){ |
---|
808 | G4cout << "@@State \"" << stateString << "\"" << G4endl; |
---|
809 | G4cout << "@@DisableListBegin"<<G4endl; |
---|
810 | SendDisableList(tree, 0); |
---|
811 | G4cout << "@@DisableListEnd" <<G4endl; |
---|
812 | } |
---|
813 | } |
---|
814 | |
---|
815 | /////////////////////////////////////// |
---|
816 | void G4UIGainServer::NotifyCommandUpdate(void) |
---|
817 | /////////////////////////////////////// |
---|
818 | { |
---|
819 | G4UIcommandTree * tree = UI->GetTree(); |
---|
820 | SendCommandProperties(tree); |
---|
821 | } |
---|
822 | |
---|
823 | ///////////////////////////////////////////////////// |
---|
824 | void G4UIGainServer::NotifyParameterUpdate(G4UIcommand* com) |
---|
825 | ///////////////////////////////////////////////////// |
---|
826 | { |
---|
827 | SendAParamProperty(com); |
---|
828 | } |
---|
829 | |
---|
830 | ////////////////////////////////// |
---|
831 | int G4UIGainServer::CommandUpdated(void){ |
---|
832 | ////////////////////////////////// |
---|
833 | int added=0, deleted=0; |
---|
834 | int pEntry= previousTreeCommands.size(); |
---|
835 | int nEntry= newTreeCommands.size(); |
---|
836 | int i,j; |
---|
837 | for( i=0; i<pEntry; i++) { // check deleted command(s) |
---|
838 | for( j=0; j<nEntry; j++) { |
---|
839 | if( previousTreeCommands[i] == newTreeCommands[j]) break; |
---|
840 | } |
---|
841 | if( j==nEntry ) { |
---|
842 | deleted = 1; |
---|
843 | //G4cout <<"deleted: "<< previousTreeCommands(i) << G4endl; |
---|
844 | } |
---|
845 | } |
---|
846 | for( i=0; i<nEntry; i++) { // check added command(s) |
---|
847 | for( j=0; j<pEntry; j++) { |
---|
848 | if( newTreeCommands[i] == previousTreeCommands[j]) break; |
---|
849 | } |
---|
850 | if( j==pEntry ) { |
---|
851 | added = 1; |
---|
852 | // G4cout <<"added: "<< newTreeCommands(i) << G4endl; |
---|
853 | } |
---|
854 | } |
---|
855 | if( added && deleted==0 ) {G4cout<<"c added"<<G4endl;return added;} |
---|
856 | if( added==0 && deleted ) {G4cout<<"c deleted"<<G4endl;return deleted;} |
---|
857 | if( added && deleted ) {G4cout<<"c add/deleted"<<G4endl;return addedAndDeleted;} |
---|
858 | return notChanged; |
---|
859 | } |
---|
860 | |
---|
861 | ////////////////////////////////////////////////////////////////////// |
---|
862 | void G4UIGainServer::GetNewTreeStructure(G4UIcommandTree * tree, int level) { |
---|
863 | ////////////////////////////////////////////////////////////////////// |
---|
864 | G4String commandPath; |
---|
865 | G4String title; |
---|
866 | G4String pathName; //tree name |
---|
867 | G4UIcommandTree * t; |
---|
868 | int treeEntry = tree->GetTreeEntry(); |
---|
869 | int commandEntry = tree->GetCommandEntry(); |
---|
870 | |
---|
871 | if( level==0 ) { newTreeCommands.clear();} |
---|
872 | for(int com=0; com<commandEntry; com++){ |
---|
873 | commandPath = tree->GetCommand(com+1)->GetCommandPath(); |
---|
874 | title = tree->GetCommand(com+1)->GetTitle(); |
---|
875 | newTreeCommands.push_back( commandPath + " " + title ); |
---|
876 | } |
---|
877 | |
---|
878 | if(treeEntry == 0) return; //end recursion |
---|
879 | |
---|
880 | for(int i=0; i< treeEntry; i++){ |
---|
881 | t = tree->GetTree(i+1); |
---|
882 | pathName = t->GetPathName(); |
---|
883 | title = t->GetTitle(); |
---|
884 | newTreeCommands.push_back( pathName + " " + title ); |
---|
885 | GetNewTreeStructure(t, level+1); |
---|
886 | } |
---|
887 | } |
---|
888 | |
---|
889 | //////////////////////////////////// |
---|
890 | void G4UIGainServer::UpdateParamVal(void) { |
---|
891 | //////////////////////////////////// |
---|
892 | // call NotifyParameterUpdate() if the value of each |
---|
893 | // command/parameter is updated. |
---|
894 | // assuming the command structure is not changed. |
---|
895 | int pEntry= previousTreeParams.size(); |
---|
896 | int nEntry= newTreeParams.size(); |
---|
897 | int i; |
---|
898 | G4UIcommand* Comp; |
---|
899 | if (pEntry != nEntry) return; |
---|
900 | for( i=0; i<nEntry; i++) { |
---|
901 | if( previousTreeParams[i] != newTreeParams[i]){ |
---|
902 | Comp = newTreePCP[i]; |
---|
903 | G4cout << Comp->GetCommandPath() |
---|
904 | << " command is updated." <<G4endl; |
---|
905 | NotifyParameterUpdate(Comp); |
---|
906 | } |
---|
907 | } |
---|
908 | } |
---|
909 | |
---|
910 | ////////////////////////////////////////////////////////////////// |
---|
911 | void G4UIGainServer::GetNewTreeValues( G4UIcommandTree * tree, int level){ // recursive |
---|
912 | ////////////////////////////////////////////////////////////////// |
---|
913 | G4String commandPath; |
---|
914 | G4String pathName; //tree name |
---|
915 | G4UIcommandTree * t; |
---|
916 | int parameterEntry; |
---|
917 | int treeEntry = tree->GetTreeEntry(); |
---|
918 | int commandEntry = tree->GetCommandEntry(); |
---|
919 | G4UIcommand * Comp; |
---|
920 | G4UIparameter * prp; |
---|
921 | G4String param, str(" "); |
---|
922 | |
---|
923 | if( level==0 ) { newTreeParams.clear(); } |
---|
924 | for(int com=0; com<commandEntry; com++) { |
---|
925 | Comp = tree->GetCommand(com+1); |
---|
926 | commandPath = Comp->GetCommandPath(); |
---|
927 | parameterEntry = Comp->GetParameterEntries(); |
---|
928 | param = commandPath +" "; |
---|
929 | for( int par=0; par< parameterEntry; par++) { |
---|
930 | prp = (G4UIparameter *)Comp->GetParameter(par); |
---|
931 | param += prp->GetParameterName() +" "; |
---|
932 | str(0) = prp->GetParameterType(); |
---|
933 | param += str + " "; |
---|
934 | param += prp->GetDefaultValue() +" "; |
---|
935 | param += prp->GetParameterRange() +" "; |
---|
936 | param += prp->GetParameterCandidates(); |
---|
937 | } |
---|
938 | newTreeParams.push_back( param + "\n"); |
---|
939 | newTreePCP.push_back( Comp ); |
---|
940 | } |
---|
941 | if( treeEntry == 0 ) return; // end recursion |
---|
942 | for( int i=0; i< treeEntry; i++) { |
---|
943 | t = tree->GetTree(i+1); |
---|
944 | GetNewTreeValues(t, level+1); |
---|
945 | } |
---|
946 | } |
---|
947 | |
---|
948 | |
---|
949 | #endif |
---|
950 | |
---|
951 | |
---|
952 | |
---|
953 | |
---|
954 | |
---|
955 | |
---|
956 | |
---|
957 | |
---|
958 | |
---|
959 | |
---|
960 | |
---|
961 | |
---|