source: trunk/source/interfaces/GAG/src/G4UIGainServer.cc @ 989

Last change on this file since 989 was 989, checked in by garnier, 15 years ago

fichiers manquants

File size: 28.2 KB
Line 
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.12 2008/07/18 06:39:43 kmura Exp $
29// $Name: geant4-09-02-ref-02 $
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//////////////////////////////////////////////
44G4UIGainServer::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/////////////////////////////
78G4UIGainServer::~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/////////////////////////////////////////
95G4UIsession* 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//////////////////////////////////////////////////
114void 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////////////////////////////////////////////////////
132void 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///////////////////////////////////
184G4String 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//////////////////////////////////////////////////////
315G4int 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//////////////////////////////////////////////////////
334G4int 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///////////////////////////////////////////////
349G4bool 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/////////////////////////////
362void G4UIGainServer::ExitHelp()
363/////////////////////////////
364{
365    char temp[100];
366    G4cin.getline(temp, 100);
367}
368
369/////////////////////////////
370bool 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////////////////////////////////////////
405void 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            G4Exception("G4UIGainServer::WaitingConnection", "Invalid Socket",
418                        FatalException, "Cannot establish connection");
419
420
421        }
422    }
423    close(socketD[0]);
424}
425
426///////////////////////////////////////////////////
427G4String G4UIGainServer::GetFullPath(G4String aNewCommand){
428///////////////////////////////////////////////////
429  G4String newCommand = aNewCommand.strip(G4String::both);
430  G4String tmpString;
431  if( newCommand(0) == '/' ) 
432  { tmpString = newCommand; }
433  else if( newCommand(0,3) == "../" )
434  {
435    G4String tmpPrefix = prefix;
436    /*G4int*/ unsigned i_direc = 0;
437    while( i_direc < newCommand.length() )
438    { 
439      if( newCommand(i_direc,3) == "../" )
440      {
441        i_direc += 3;
442        prefix = ModifyPrefix( G4String("../") );
443      }
444      else
445      { break; }
446    }
447    tmpString = prefix;
448    tmpString.append( newCommand( i_direc, newCommand.length()-i_direc ) );
449    prefix = tmpPrefix;
450  }
451  else
452  {
453    tmpString = prefix;
454    tmpString.append( newCommand );
455  }
456  return tmpString;
457}
458
459////////////////////////////////
460void G4UIGainServer::SessionTerminate(){
461////////////////////////////////
462    G4cout<<"***** Terminal session end *****"<<G4endl;
463}
464
465
466//////////////////////////////////////////////
467void G4UIGainServer::ShowCurrent(G4String newCommand){
468//////////////////////////////////////////////
469  G4String theCommand = GetFullPath(newCommand(1,newCommand.length()-1));
470  G4String curV = UI->GetCurrentValues(theCommand);
471  if( ! (curV.isNull()||curV(0)=='\0' ) ) {
472    if (uiMode == terminal_mode){
473      G4cout << "Current value(s) of the parameter(s) : " << curV << G4endl;
474    }else{
475      G4cout << "@@CurrentValue " << curV << G4endl;
476    }
477  } else if (uiMode == terminal_mode){
478      G4cout << "Current value is not available." << G4endl;
479    } else {
480      G4cout << "@@ErrResult \"Current value is not available.\"" << G4endl;
481    }
482}
483
484//////////////////////////////////////////////////
485void G4UIGainServer::ChangeDirectory(G4String newCommand){
486//////////////////////////////////////////////////
487  G4String savedPrefix = prefix;
488  if( newCommand.length() <= 3 )
489  { prefix = "/"; }
490  else
491  { 
492    G4String aNewPrefix = newCommand(3,newCommand.length()-3);
493    G4String newPrefix = aNewPrefix.strip(G4String::both);
494    if( newPrefix(0) == '/' )
495    { prefix = newPrefix; }
496    else if( newPrefix(0) != '.' )
497    { 
498      prefix += newPrefix;
499    }
500    else
501    { prefix = ModifyPrefix( newPrefix ); }
502  }
503  if( prefix( prefix.length() - 1 ) != '/' )
504  { prefix += "/"; }
505  if( FindDirPath( prefix ) == NULL )
506  {
507    G4cout << "Directory <" << prefix << "> is not found." << G4endl;
508    prefix = savedPrefix;
509  }
510}
511////////////////////////////////////////////////
512void G4UIGainServer::ListDirectory(G4String newCommand){
513////////////////////////////////////////////////
514  G4String targetDir('\0');
515  if( newCommand.length() <= 3 )
516  { targetDir = prefix; }
517  else
518  {
519    G4String newPrefix = newCommand(3,newCommand.length()-3);
520    newPrefix.strip(G4String::both);
521    if( newPrefix(0) == '/' )
522    { targetDir = newPrefix; }
523    else if( newPrefix(0) != '.' )
524    {
525      targetDir = prefix;
526      targetDir += newPrefix;
527    }
528    else
529    { targetDir = ModifyPrefix( newPrefix ); }
530  }
531  if( targetDir( targetDir.length() - 1 ) != '/' )
532  { targetDir += "/"; }
533  G4UIcommandTree * commandTree = FindDirPath( targetDir );
534  if( commandTree == NULL )
535  { G4cout << "Directory <" << targetDir << "> is not found." << G4endl; }
536  else
537  { commandTree->ListCurrent(); }
538}
539
540///////////////////////////////////////////////
541void G4UIGainServer::TerminalHelp(G4String newCommand){
542///////////////////////////////////////////////
543    G4UIcommandTree* treeTop = UI->GetTree();
544    str_size i = newCommand.index(" ");
545   
546    if(i!=std::string::npos){
547        G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
548        newValue.strip(G4String::both);
549        if(newValue(0)!='/'){
550            newValue.prepend(prefix);
551        }
552        G4UIcommand* theCommand = treeTop->FindPath(newValue);
553        if(theCommand !=NULL){
554            theCommand->List();
555            return;
556        }
557        else{
558            G4cout<<"Command<" << newValue << "is not found."<<G4endl;
559            return;
560        }
561    }
562
563    G4UIcommandTree* floor[10];
564    floor[0] = treeTop;
565    int iFloor = 0;
566    unsigned prefixIndex = 1;
567    while(prefixIndex<prefix.length()-1){
568        int ii = prefix.index("/",prefixIndex);
569        floor[iFloor+1]=
570          floor[iFloor]->GetTree(G4String(prefix(0,ii+1)));
571        prefixIndex = ii+1;
572        iFloor++;
573    }
574    floor[iFloor]->ListCurrentWithNum();
575    while(1){
576        int i;
577        G4cout<<G4endl <<"Type the number (0:end, -n:n level back) :"<<std::flush;
578        G4cin >> i;
579        if(!G4cin.good()){
580            G4cin.clear();
581            G4cin.ignore(30,'\n');
582            G4cout<<G4endl <<"Not a number,once more"<<G4endl; continue;
583        }
584        else if(i<0){
585            iFloor += i;
586            if(iFloor <0) iFloor =0;
587            floor[iFloor]->ListCurrentWithNum(); continue;
588        }
589        else if(i==0){break;}
590        else if(i>0){
591            int n_tree = floor[iFloor]->GetTreeEntry();
592            if(i>n_tree){
593                if(i<=n_tree+floor[iFloor]->GetCommandEntry()){
594                    floor[iFloor]->GetCommand(i-n_tree)->List();
595                }
596            }
597            else{
598                floor[iFloor+1] = floor[iFloor]->GetTree(i);
599                iFloor++;
600                floor[iFloor]->ListCurrentWithNum();
601            }
602        }
603    }
604    G4cout<<"Exit from Help."<<G4endl <<G4endl;
605    G4cout<<G4endl;
606    char temp[100];
607    G4cin.getline(temp,100);
608}
609
610
611///////////////////////////////////////////////////
612G4String G4UIGainServer::ModifyPrefix(G4String newCommand){
613///////////////////////////////////////////////////
614    G4String newPrefix = prefix;
615    while(1){
616        if(newCommand(0,2) ==".."){
617            if(newPrefix !="/"){
618                G4String tmpString = newPrefix(0,newPrefix.length()-1);
619                newPrefix = newPrefix(0,tmpString.last('/')+1);
620            }
621        }
622        else{
623            newPrefix += newCommand;
624            break;
625        }
626        if(newCommand == ".." || newCommand == "../"){
627            break;
628        }
629        newCommand=newCommand(3,newCommand.length()-3);
630    }
631    return newPrefix;
632}
633
634//////////////////////////////////////////////////////////
635G4UIcommandTree* G4UIGainServer::FindDirPath(G4String newCommand){
636//////////////////////////////////////////////////////////
637  G4UIcommandTree * comTree = UI->GetTree();
638  /*int*/ unsigned idx = 1; 
639  while( idx < newCommand.length()-1 )
640  { 
641    int i = newCommand.index("/",idx);
642    comTree = comTree->GetTree(G4String(newCommand(0,i+1)));
643    if( comTree == NULL )
644    { return NULL; }
645    idx = i+1;
646  }
647  return comTree;
648}
649
650//// ----- for JAVA Gain
651
652//////////////////////////////////////////////////////////
653void G4UIGainServer::SendCommandProperties(G4UIcommandTree* tree){
654//////////////////////////////////////////////////////////
655  if( tree == NULL ) {
656    G4cerr << "GetTree() returnes null." << G4endl;
657    return;
658  }
659  if (uiMode == java_mode){
660    G4cout << "@@JTreeBegin" << G4endl;
661    CodeGenJavaTree(tree, 0);
662    G4cout << "@@JTreeEnd" << G4endl;
663    CodeGenJavaParams(tree, 0);
664  }else{}
665}
666
667////////////////////////////////////////////////////////////
668void G4UIGainServer::SendParameterProperties(G4UIcommandTree* tree){
669////////////////////////////////////////////////////////////
670  if( tree == NULL ) {
671    G4cerr << "GetTree() returnes null." << G4endl;
672    return;
673  }
674  if (uiMode == java_mode){
675    CodeGenJavaParams(tree, 0);
676  }else{ }
677}
678
679//////////////////////////////////////////////////////////////
680void G4UIGainServer::CodeGenJavaTree(G4UIcommandTree* tree,int level){
681//////////////////////////////////////////////////////////////
682  int treeEntry, commandEntry;
683  treeEntry = tree->GetTreeEntry();
684  commandEntry = tree->GetCommandEntry();
685
686  if(level!=0) {
687    for(int i=0; i<commandEntry; i++){
688      G4cout << tree->GetCommand(i+1)->GetCommandPath() << G4endl;
689    }
690  }
691  if(treeEntry == 0) return; //end recursion
692
693  for(int j=0; j<treeEntry; j++){
694    CodeGenJavaTree(tree->GetTree(j+1), level+1);
695  }
696}
697
698////////////////////////////////////////////////////////////////
699void G4UIGainServer::CodeGenJavaParams(G4UIcommandTree* tree,int level){
700////////////////////////////////////////////////////////////////
701    int treeEntry,commandEntry,i;
702    G4UIcommandTree* treeLink;
703
704    treeEntry = tree->GetTreeEntry();
705    commandEntry = tree->GetCommandEntry();
706
707    for(i=0;i<commandEntry; i++){
708        SendAParamProperty(tree->GetCommand(i+1));
709    }
710    if(treeEntry ==0) return;
711
712    for(i=0;i<treeEntry; i++){
713        treeLink = tree->GetTree(i+1);
714        G4cout<<"@@JDirGuieBegin"<<G4endl;
715        G4cout<<treeLink->GetPathName()<<G4endl <<treeLink->GetTitle()<<G4endl;
716        G4cout<<"@@JDirGuideEnd"<<G4endl;
717        CodeGenJavaParams(treeLink,level+1);
718    }
719}
720
721///////////////////////////////////////////////////
722void G4UIGainServer::SendAParamProperty(G4UIcommand* Comp){
723///////////////////////////////////////////////////
724  int guidanceEntry, parameterEntry;
725  G4String title, title2;
726  G4UIparameter * prp;
727  char c[2];
728  guidanceEntry = Comp->GetGuidanceEntries();
729  parameterEntry = Comp->GetParameterEntries();
730  G4cout << "@@JParamBegin" << G4endl;
731  G4cout << Comp->GetCommandPath() << G4endl;
732  G4cout << guidanceEntry << G4endl;
733  for (int j=0; j<guidanceEntry; j++){
734    title = Comp->GetGuidanceLine(j);
735    title2 = "";
736    if (title != ""){
737      for(int i=0; i< (int)title.length(); i++){
738        c[0]=title(i);
739        c[1]= '\0';
740        if ( c[0] == '\n' || c[0] == '\r') {
741          c[0]= ' ';
742        }
743        title2.append(c);
744      }
745    }
746    G4cout << title2 << G4endl;
747  }
748  G4cout << Comp->GetRange() << G4endl;
749  G4cout << parameterEntry << G4endl;
750  for( int par=0; par<parameterEntry; par++) {
751    prp = (G4UIparameter *)Comp->GetParameter(par);
752    G4cout << prp->GetParameterName() << G4endl;
753    G4cout << prp->GetParameterGuidance() << G4endl;
754    G4cout << prp->GetParameterType() << G4endl;
755    G4cout << prp->IsOmittable() << G4endl;
756    G4cout << prp->GetDefaultValue() << G4endl;
757    G4cout << prp->GetParameterRange() << G4endl;
758    G4cout << prp->GetParameterCandidates() << G4endl;
759  }
760  G4cout << "@@JParamEnd" << G4endl;
761}
762
763//////////////////////////////////////////////////////////////
764void G4UIGainServer::SendDisableList(G4UIcommandTree* tree,int level){
765//////////////////////////////////////////////////////////////
766  int treeEntry, commandEntry;
767  G4UIcommand * Comp;
768  treeEntry = tree->GetTreeEntry();
769  commandEntry = tree->GetCommandEntry();
770
771  for(int com=0; com<commandEntry; com++) {
772    Comp = tree->GetCommand(com+1);
773    if( Comp->IsAvailable()==false ) {
774       G4cout << Comp->GetCommandPath()<<G4endl;
775    }
776  }
777  if( treeEntry == 0 ) return;     // end recursion
778
779  for( int i=0; i<treeEntry; i++) {
780    SendDisableList(tree->GetTree(i+1), level+1);
781    // be sure the function name is the same
782  }
783}
784
785
786
787//####### update check routines ####################################
788
789///////////////////////////////
790void G4UIGainServer::UpdateState(void){
791///////////////////////////////
792   static G4ApplicationState previousState= G4State_PreInit;
793   G4ApplicationState  newState;
794   G4StateManager *statM = G4StateManager::GetStateManager();
795   newState = statM->GetCurrentState();
796   if( newState != previousState ) 
797   {
798      NotifyStateChange();
799      previousState = newState; 
800   }
801}
802
803/////////////////////////////////////
804void G4UIGainServer::NotifyStateChange(void)
805/////////////////////////////////////
806{
807   G4String stateString;
808   G4StateManager * statM = G4StateManager::GetStateManager();
809   G4UIcommandTree * tree = UI->GetTree();
810   stateString = statM->GetStateString(statM->GetCurrentState());
811   if ( uiMode != terminal_mode ){
812     G4cout << "@@State \"" << stateString << "\"" << G4endl;
813     G4cout << "@@DisableListBegin"<<G4endl;
814     SendDisableList(tree, 0);
815     G4cout << "@@DisableListEnd" <<G4endl;
816   }
817}
818
819///////////////////////////////////////
820void G4UIGainServer::NotifyCommandUpdate(void)
821///////////////////////////////////////
822{
823  G4UIcommandTree * tree = UI->GetTree();
824  SendCommandProperties(tree);
825}
826
827/////////////////////////////////////////////////////
828void G4UIGainServer::NotifyParameterUpdate(G4UIcommand* com)
829/////////////////////////////////////////////////////
830{
831    SendAParamProperty(com);
832}
833
834//////////////////////////////////
835int G4UIGainServer::CommandUpdated(void){
836//////////////////////////////////
837  int added=0, deleted=0;
838  int pEntry= previousTreeCommands.size();
839  int nEntry= newTreeCommands.size();
840  int i,j;
841  for( i=0; i<pEntry; i++) {      // check deleted command(s)
842      for( j=0; j<nEntry; j++) {
843         if( previousTreeCommands[i] == newTreeCommands[j]) break;
844      }
845      if( j==nEntry ) { 
846         deleted = 1;
847         //G4cout <<"deleted: "<< previousTreeCommands(i) << G4endl;
848      }
849  }
850  for( i=0; i<nEntry; i++) {      // check added command(s)
851      for( j=0; j<pEntry; j++) {
852         if( newTreeCommands[i] == previousTreeCommands[j]) break;
853      }
854      if( j==pEntry ) { 
855         added = 1;
856      //   G4cout <<"added: "<< newTreeCommands(i) << G4endl;
857      }
858  }
859  if( added    && deleted==0 ) {G4cout<<"c added"<<G4endl;return added;}
860  if( added==0 && deleted ) {G4cout<<"c deleted"<<G4endl;return deleted;}
861  if( added    && deleted ) {G4cout<<"c add/deleted"<<G4endl;return addedAndDeleted;}
862  return notChanged;
863}
864
865//////////////////////////////////////////////////////////////////////
866void G4UIGainServer::GetNewTreeStructure(G4UIcommandTree * tree, int level) { 
867//////////////////////////////////////////////////////////////////////
868  G4String commandPath;
869  G4String title; 
870  G4String pathName; //tree name
871  G4UIcommandTree * t;
872  int treeEntry    = tree->GetTreeEntry();
873  int commandEntry = tree->GetCommandEntry();
874
875  if( level==0 ) { newTreeCommands.clear();}
876  for(int com=0; com<commandEntry; com++){
877      commandPath = tree->GetCommand(com+1)->GetCommandPath();
878      title = tree->GetCommand(com+1)->GetTitle();
879      newTreeCommands.push_back( commandPath + " " + title );
880  }
881
882  if(treeEntry == 0) return; //end recursion
883
884  for(int i=0; i< treeEntry; i++){
885    t = tree->GetTree(i+1);
886    pathName =  t->GetPathName();   
887    title = t->GetTitle();
888    newTreeCommands.push_back( pathName + " " + title );
889    GetNewTreeStructure(t, level+1);
890  }
891}
892
893////////////////////////////////////
894void G4UIGainServer::UpdateParamVal(void) {
895////////////////////////////////////
896  // call NotifyParameterUpdate() if the value of each
897  //  command/parameter is updated.
898  //  assuming the command structure is not changed.
899  int pEntry= previousTreeParams.size();
900  int nEntry= newTreeParams.size();
901  int i;
902  G4UIcommand* Comp;
903  if (pEntry != nEntry) return; 
904  for( i=0; i<nEntry; i++) {
905    if( previousTreeParams[i] != newTreeParams[i]){
906       Comp = newTreePCP[i];
907       G4cout << Comp->GetCommandPath()
908            << " command is updated." <<G4endl; 
909       NotifyParameterUpdate(Comp);
910    }
911  }
912}
913
914//////////////////////////////////////////////////////////////////
915void G4UIGainServer::GetNewTreeValues( G4UIcommandTree * tree, int level){ // recursive
916//////////////////////////////////////////////////////////////////
917   G4String commandPath;
918   G4String pathName; //tree name
919   G4UIcommandTree * t;
920   int parameterEntry;
921   int treeEntry    = tree->GetTreeEntry();
922   int commandEntry = tree->GetCommandEntry();
923   G4UIcommand * Comp;
924   G4UIparameter * prp; 
925   G4String param, str(" ");
926
927   if( level==0 ) { newTreeParams.clear(); }
928   for(int com=0; com<commandEntry; com++) {
929      Comp = tree->GetCommand(com+1);
930      commandPath    = Comp->GetCommandPath();
931      parameterEntry = Comp->GetParameterEntries();
932      param = commandPath +" ";
933      for( int par=0; par< parameterEntry; par++) {
934         prp = (G4UIparameter *)Comp->GetParameter(par);
935         param += prp->GetParameterName() +" ";
936         str(0) = prp->GetParameterType();
937         param += str + " ";
938         param += prp->GetDefaultValue()  +" ";
939         param += prp->GetParameterRange() +" ";
940         param += prp->GetParameterCandidates();
941      }
942     newTreeParams.push_back( param + "\n"); 
943     newTreePCP.push_back( Comp ); 
944   }
945   if( treeEntry == 0 )  return;     // end recursion
946   for( int i=0; i< treeEntry; i++) {
947      t = tree->GetTree(i+1);
948      GetNewTreeValues(t, level+1);
949   }
950}
951
952
953#endif
954
955       
956
957
958
959
960
961
962
963
964
965       
Note: See TracBrowser for help on using the repository browser.