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