G4UIGainServer.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // 12/06/2002 G4UIGainServer H. MInamimoto and H. Yoshida created
00028 // $Id$
00029 //
00030 #ifndef WIN32
00031 
00032 #include "G4UIGainServer.hh"
00033 #include <netdb.h>
00034 
00035 #include <sstream>
00036 #include "G4StateManager.hh"
00037 #include "G4UIcommandTree.hh"
00038 #include "G4UIcommand.hh"
00039 #include "G4UIcommandStatus.hh"
00040 
00041 
00043 G4UIGainServer::G4UIGainServer()
00045 {
00046     TVersion ="T1.0a"; JVersion="J1.0a";
00047     prefix = "/";
00048 
00049     port = DEFAULT_PORT;
00050     while(SetUPServer() == false){
00051         G4cout<<"can't get the port no. "<<port<<" Now, try to get the next port "<<port+1<<G4endl;
00052         port++;
00053     }
00054 
00055 
00056     UI= G4UImanager::GetUIpointer();
00057     UI-> SetSession(this);
00058     UI-> SetCoutDestination(this);
00059 
00060     G4StateManager* statM = G4StateManager::GetStateManager();
00061     promptCharacter = statM->GetStateString(statM->GetCurrentState());
00062     uiMode = terminal_mode;
00063 
00064     iExit= FALSE;
00065     iCont= FALSE;
00066 
00067     G4UIcommandTree* tree = UI->GetTree();
00068     GetNewTreeStructure(tree,0);
00069     GetNewTreeValues(tree,0);
00070     previousTreeCommands = newTreeCommands;
00071     previousTreeParams = newTreeParams;
00072     previousTreePCP = newTreePCP;
00073 
00074 }
00075 
00077 G4UIGainServer::~G4UIGainServer() 
00078 
00079 { 
00080 
00081     if(G4UImanager::GetUIpointer()) {
00082       UI-> SetSession(NULL);
00083       UI-> SetCoutDestination(NULL);
00084     }
00085 
00086     if(G4UImanager::GetUIpointer()!=0){
00087         UI->SetSession(NULL);
00088         UI->SetCoutDestination(NULL);
00089     }
00090 }
00091 
00092 
00094 G4UIsession* G4UIGainServer::SessionStart()
00096 {
00097     G4String newCommand;
00098 
00099     G4StateManager* statM = G4StateManager::GetStateManager();
00100     promptCharacter = statM->GetStateString(statM->GetCurrentState());
00101     
00102     iExit= TRUE;
00103 
00104     WaitingConnection();
00105     while(iExit){
00106         newCommand= GetCommand();
00107         ExecuteCommand(newCommand);
00108     }
00109     return NULL;
00110 }
00111 
00113 void G4UIGainServer::PauseSessionStart(const G4String& msg)
00115 {
00116     promptCharacter = msg;
00117     G4cout<<"@@PROMPT \""<<promptCharacter<<"\""<<G4endl;
00118 
00119     iCont= TRUE;
00120 
00121     G4String newCommand= GetCommand();
00122     while(iCont){
00123       ExecuteCommand(newCommand);
00124       newCommand= GetCommand();
00125       strcpy(buf,"nowIdle");
00126       write(socketD[1],buf,strlen(buf));
00127     }
00128 }
00129 
00131 void G4UIGainServer::ExecuteCommand(const G4String& aCommand)
00133 {
00134     if(aCommand.length()<2) return;
00135 
00136     G4UIcommandTree* tree = UI->GetTree();
00137     if(aCommand.length()<2) return;
00138     G4int returnVal = UI->ApplyCommand(aCommand);
00139     G4int paramIndex = returnVal % 100;
00140     // 0 - 98 : paramIndex-th parameter is invalid
00141     // 99     : convination of parameters is invalid
00142     G4int commandStatus = returnVal - paramIndex;
00143 
00144     UpdateState();
00145 
00146     if(uiMode != terminal_mode){
00147         switch(commandStatus) {
00148         case fCommandSucceeded:
00149             GetNewTreeStructure(tree,0);
00150             GetNewTreeValues(tree,0);
00151             if(CommandUpdated()){
00152                 NotifyCommandUpdate();
00153             } else{
00154                 UpdateParamVal();
00155             }
00156             previousTreeCommands = newTreeCommands;
00157             previousTreeParams = newTreeParams;
00158             previousTreePCP = newTreePCP;
00159             break;
00160         case fCommandNotFound:
00161             G4cerr << "@@ErrResult \" <" << UI->SolveAlias(aCommand) << "> not found.\"" << G4endl;
00162             break;
00163         case fIllegalApplicationState:
00164             G4cerr << "@@ErrResult \"illegal application state -- command refused.\"" << G4endl;
00165             break;
00166         case fParameterOutOfRange:
00167             G4cout << "@@ErrResult \"Parameter Out of Range.\"" << G4endl;
00168             break;
00169         case fParameterUnreadable:
00170             G4cout << "@@ErrResult \"Parameter is wrong type and/or is not omittable.\""<<G4endl;
00171             break;
00172         case fParameterOutOfCandidates:
00173             G4cerr << "@@ErrResult \"Parameter is out of candidate.\"" << G4endl;
00174             break;
00175         case fAliasNotFound:
00176         default:
00177             G4cerr << "command refused (" << commandStatus << ")" << G4endl;
00178         }
00179     }
00180 }
00181 
00183 G4String G4UIGainServer::GetCommand()
00185 {
00186     G4String newCommand;
00187     G4String nullString;
00188 
00189   while( 1 )
00190   {
00191     G4UIcommandTree* tree = UI->GetTree();
00192     if ( uiMode != terminal_mode ){
00193       G4cout << "@@PROMPT \"" << promptCharacter << "\"" << G4endl;
00194     }
00195     if ( uiMode != java_mode ){
00196       G4cout << promptCharacter << "> " << G4endl;
00197     }else{
00198       G4cout << "@@Ready" << G4endl;
00199     }
00200 
00201 
00204     read(socketD[1],buf,1024);
00205     newCommand=buf;
00206     //DEBUG cout<<"->"<<newCommand<<"<-"<<newCommand.length()<<G4endl;
00207     //newCommand.readLine( G4cin, FALSE );
00210 
00211 
00212 
00213     if (!G4cin.good()) { G4cin.clear(); newCommand = nullString; iExit=false;break;}
00214 
00215     newCommand = newCommand.strip(G4String::leading);
00216     if( newCommand.length() < 1) { break; }
00217 
00218     while( newCommand(newCommand.length()-1) == '_' )
00219     {
00220       G4String newLine;
00221       newCommand.remove(newCommand.length()-1);
00222       newLine.readLine( G4cin );
00223       if (!G4cin.good()) { G4cin.clear(); newCommand = nullString; iExit=false;break;}
00224       newCommand.append(newLine);
00225     }
00226 
00227     G4String nC = newCommand.strip(G4String::leading);
00228     if( nC.length() < 1) { break; }
00229 
00230     // -------------------- nC.toUpper();
00231     if( nC == "@@GainmodeJAVA" ) {
00232       uiMode = java_mode;
00233       G4cout << G4endl << "@@Version " << JVersion << G4endl;
00234       SendCommandProperties(tree);
00235       NotifyStateChange();
00236     }
00237     else if( nC == "@@GainmodeTcl" ) {
00238       uiMode = tcl_mode;
00239       G4cout << G4endl << "@@Version " << TVersion << G4endl;
00240       SendCommandProperties(tree);
00241       NotifyStateChange();
00242     }
00243     else if( nC(0) == '#' )
00244       { G4cout << nC << G4endl; }
00245 
00246     else if( nC == "ls"  || nC(0,3) == "ls " )
00247     { ListDirectory( nC ); }
00248     else if( nC == "pwd" )
00249     { G4cout << "Current Working Directory : " << prefix << G4endl; }
00250     else if( nC(0,2) == "cd"  || nC(0,3) == "cd " )
00251     { ChangeDirectory( nC ); }
00252     else if(  nC == "help" || nC(0,5) == "help ")
00253     { TerminalHelp( nC ); }
00254     else if( nC(0) == '?' )
00255     { ShowCurrent( nC ); }
00256     else if( nC(0,4) == "hist"   || nC == "history")
00257     {
00258       G4int nh = UI->GetNumberOfHistory();
00259       for(int i=0;i<nh;i++)
00260       { G4cout << i << ": " << UI->GetPreviousCommand(i) << G4endl; }
00261     }
00262     else if( nC(0) == '!' )
00263     {
00264       G4String ss = nC(1,nC.length()-1);
00265       G4int vl;
00266       const char* tt = ss;
00267       std::istringstream is((char*)tt);
00268       is >> vl;
00269       G4int nh = UI->GetNumberOfHistory();
00270       if(vl>=0 && vl<nh)
00271       {
00272         newCommand = UI->GetPreviousCommand(vl);
00273         G4cout << newCommand << G4endl;
00274         break;
00275       }
00276       else
00277       { G4cerr << "history " << vl << " is not found." << G4endl; }
00278     }
00279     else if( nC(0,4) == "exit" )
00280     {
00281       if( iCont )
00282       {
00283         if ( uiMode == terminal_mode){
00284           G4cerr << "You are now processing RUN." << G4endl;
00285           G4cerr << "Please abrot it using \"/run/abort\" command first" << G4endl;
00286           G4cerr << " and use \"continue\" command until the application" << G4endl;
00287           G4cerr << " becomes to Idle." << G4endl;
00288         }else{
00289           G4cout << "@@ErrResult \"You are now processing RUN.\"" << G4endl;
00290         }
00291       }
00292       else
00293       {
00294         close(socketD[1]);
00295         close(socketD[2]);
00296         iExit = false;
00297         newCommand = nullString;
00298         break;
00299       }
00300     }
00301     else if(  nC == "cont" || nC == "continue" )
00302     {
00303       iCont = false;
00304       newCommand = nullString;
00305       break;
00306     }
00307     else
00308     { break; }
00309   }
00310   return GetFullPath(newCommand);
00311 }
00312 
00314 G4int G4UIGainServer::ReceiveG4cout(const G4String& coutString)
00316 {
00317     if(socketD[1]>0){
00318         write(socketD[1],coutString,coutString.length());
00319     }
00320     return 0;
00321 
00322   //std::cout << coutString << std::flush;
00323   //return 0;
00324 }
00325 
00327 G4int G4UIGainServer::ReceiveG4cerr(const G4String& cerrString)
00329 {
00330     if(socketD[2]>0){
00331         write(socketD[2],cerrString,cerrString.length());
00332     }
00333     return 0;
00334 
00335   //std::cerr << cerrString << std::flush;
00336   //return 0;
00337 }
00338 
00340 G4bool G4UIGainServer::GetHelpChoice(G4int& aInt)
00342 {
00343     G4cin >> aInt;
00344     if(!G4cin.good()){
00345         G4cin.clear();
00346         G4cin.ignore(30,'\n');
00347         return FALSE;
00348     }
00349     return TRUE;
00350 }
00351 
00353 void G4UIGainServer::ExitHelp() const
00355 {
00356     char temp[100];
00357     G4cin.getline(temp, 100);
00358 }
00359 
00361 bool G4UIGainServer::SetUPServer(){
00363 
00364     socketD[0] = socket(AF_INET,SOCK_STREAM,0);
00365 
00366     if(socketD[0]<0){
00367         perror("server:socket");
00368         return (false);
00369         //exit(1);
00370     }
00371 
00372     memset( (char *)&saddr,'\0',sizeof(saddr)) ;
00373 
00374     saddr.sin_family = AF_INET;
00375     saddr.sin_addr.s_addr = INADDR_ANY;
00376     saddr.sin_port = htons(port);
00377     unlink(SOCK_NAME);    
00378 
00379     if(bind(socketD[0] , (struct sockaddr *)&saddr , sizeof(saddr))<0){
00380         perror("bind");
00381         return (false);
00382         //exit(1);
00383     }
00384     else{ G4cout<<"G4GainServer waiting at "<<port<<G4endl; }
00385 
00386     if(listen(socketD[0],1)<0){
00387         perror("listen");
00388         return (false);
00389         //exit(1);
00390     }
00391 
00392     return (true);
00393 }
00394 
00396 void G4UIGainServer::WaitingConnection(){
00398     len = sizeof(caddr);
00399 
00400     for(int i=1;i<=2;i++){
00401 #if defined __APPLE__ && (__GNUC__<4)
00402         if((socketD[i] = accept(socketD[0], (struct sockaddr *)&caddr,(int *)&len))<0){
00403 #else
00404         if((socketD[i] = accept(socketD[0], (struct sockaddr *)&caddr,(socklen_t *)&len))<0){
00405 #endif
00406             G4cerr<<"accept:"<<i<<G4endl;
00407             //exit(1);
00408             G4Exception("G4UIGainServer::WaitingConnection()",
00409                         "UI0004",
00410                         FatalException,
00411                         "Invalid Socket. Cannot establish connection");
00412         }
00413     }
00414     close(socketD[0]);
00415 }
00416 
00418 G4String G4UIGainServer::GetFullPath(G4String aNewCommand){
00420   G4String newCommand = aNewCommand.strip(G4String::both);
00421   G4String tmpString;
00422   if( newCommand(0) == '/' ) 
00423   { tmpString = newCommand; }
00424   else if( newCommand(0,3) == "../" )
00425   {
00426     G4String tmpPrefix = prefix;
00427     /*G4int*/ unsigned i_direc = 0;
00428     while( i_direc < newCommand.length() )
00429     { 
00430       if( newCommand(i_direc,3) == "../" )
00431       {
00432         i_direc += 3;
00433         prefix = ModifyPrefix( G4String("../") );
00434       }
00435       else
00436       { break; }
00437     }
00438     tmpString = prefix;
00439     tmpString.append( newCommand( i_direc, newCommand.length()-i_direc ) );
00440     prefix = tmpPrefix;
00441   }
00442   else
00443   {
00444     tmpString = prefix;
00445     tmpString.append( newCommand );
00446   }
00447   return tmpString;
00448 }
00449 
00451 void G4UIGainServer::SessionTerminate(){
00453     G4cout<<"***** Terminal session end *****"<<G4endl;
00454 }
00455 
00456 
00458 void G4UIGainServer::ShowCurrent(G4String newCommand){
00460   G4String theCommand = GetFullPath(newCommand(1,newCommand.length()-1));
00461   G4String curV = UI->GetCurrentValues(theCommand);
00462   if( ! (curV.isNull()||curV(0)=='\0' ) ) {
00463     if (uiMode == terminal_mode){
00464       G4cout << "Current value(s) of the parameter(s) : " << curV << G4endl;
00465     }else{
00466       G4cout << "@@CurrentValue " << curV << G4endl;
00467     }
00468   } else if (uiMode == terminal_mode){
00469       G4cout << "Current value is not available." << G4endl;
00470     } else {
00471       G4cout << "@@ErrResult \"Current value is not available.\"" << G4endl;
00472     }
00473 }
00474 
00476 void G4UIGainServer::ChangeDirectory(G4String newCommand){
00478   G4String savedPrefix = prefix;
00479   if( newCommand.length() <= 3 )
00480   { prefix = "/"; }
00481   else
00482   { 
00483     G4String aNewPrefix = newCommand(3,newCommand.length()-3);
00484     G4String newPrefix = aNewPrefix.strip(G4String::both);
00485     if( newPrefix(0) == '/' )
00486     { prefix = newPrefix; }
00487     else if( newPrefix(0) != '.' )
00488     { 
00489       prefix += newPrefix;
00490     }
00491     else
00492     { prefix = ModifyPrefix( newPrefix ); }
00493   }
00494   if( prefix( prefix.length() - 1 ) != '/' )
00495   { prefix += "/"; }
00496   if( FindDirPath( prefix ) == NULL )
00497   {
00498     G4cout << "Directory <" << prefix << "> is not found." << G4endl;
00499     prefix = savedPrefix;
00500   }
00501 }
00503 void G4UIGainServer::ListDirectory(G4String newCommand){
00505   G4String targetDir('\0');
00506   if( newCommand.length() <= 3 )
00507   { targetDir = prefix; }
00508   else
00509   {
00510     G4String newPrefix = newCommand(3,newCommand.length()-3);
00511     newPrefix.strip(G4String::both);
00512     if( newPrefix(0) == '/' )
00513     { targetDir = newPrefix; }
00514     else if( newPrefix(0) != '.' )
00515     {
00516       targetDir = prefix;
00517       targetDir += newPrefix;
00518     }
00519     else
00520     { targetDir = ModifyPrefix( newPrefix ); }
00521   }
00522   if( targetDir( targetDir.length() - 1 ) != '/' )
00523   { targetDir += "/"; }
00524   G4UIcommandTree * commandTree = FindDirPath( targetDir );
00525   if( commandTree == NULL )
00526   { G4cout << "Directory <" << targetDir << "> is not found." << G4endl; }
00527   else
00528   { commandTree->ListCurrent(); }
00529 }
00530 
00532 void G4UIGainServer::TerminalHelp(G4String newCommand){
00534     G4UIcommandTree* treeTop = UI->GetTree();
00535     str_size i = newCommand.index(" ");
00536     
00537     if(i!=std::string::npos){
00538         G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
00539         newValue.strip(G4String::both);
00540         if(newValue(0)!='/'){
00541             newValue.prepend(prefix);
00542         }
00543         G4UIcommand* theCommand = treeTop->FindPath(newValue);
00544         if(theCommand !=NULL){
00545             theCommand->List();
00546             return;
00547         }
00548         else{
00549             G4cout<<"Command<" << newValue << "is not found."<<G4endl;
00550             return;
00551         }
00552     }
00553 
00554     G4UIcommandTree* floor[10];
00555     floor[0] = treeTop;
00556     int iFloor = 0;
00557     unsigned prefixIndex = 1;
00558     while(prefixIndex<prefix.length()-1){
00559         int ii = prefix.index("/",prefixIndex);
00560         floor[iFloor+1]=
00561           floor[iFloor]->GetTree(G4String(prefix(0,ii+1)));
00562         prefixIndex = ii+1;
00563         iFloor++;
00564     }
00565     floor[iFloor]->ListCurrentWithNum();
00566     while(1){
00567         int j;
00568         G4cout<<G4endl <<"Type the number (0:end, -n:n level back) :"<<std::flush;
00569         G4cin >> j;
00570         if(!G4cin.good()){
00571             G4cin.clear();
00572             G4cin.ignore(30,'\n');
00573             G4cout<<G4endl <<"Not a number,once more"<<G4endl; continue;
00574         }
00575         else if(j<0){
00576             iFloor += j;
00577             if(iFloor <0) iFloor =0;
00578             floor[iFloor]->ListCurrentWithNum(); continue;
00579         }
00580         else if(j==0){break;}
00581         else if(j>0){
00582             int n_tree = floor[iFloor]->GetTreeEntry();
00583             if(j>n_tree){
00584                 if(j<=n_tree+floor[iFloor]->GetCommandEntry()){
00585                     floor[iFloor]->GetCommand(j-n_tree)->List();
00586                 }
00587             }
00588             else{
00589                 floor[iFloor+1] = floor[iFloor]->GetTree(j);
00590                 iFloor++;
00591                 floor[iFloor]->ListCurrentWithNum();
00592             }
00593         }
00594     }
00595     G4cout<<"Exit from Help."<<G4endl <<G4endl;
00596     G4cout<<G4endl;
00597     char temp[100];
00598     G4cin.getline(temp,100);
00599 }
00600 
00601 
00603 G4String G4UIGainServer::ModifyPrefix(G4String newCommand){
00605     G4String newPrefix = prefix;
00606     while(1){
00607         if(newCommand(0,2) ==".."){
00608             if(newPrefix !="/"){
00609                 G4String tmpString = newPrefix(0,newPrefix.length()-1);
00610                 newPrefix = newPrefix(0,tmpString.last('/')+1);
00611             }
00612         }
00613         else{
00614             newPrefix += newCommand;
00615             break;
00616         }
00617         if(newCommand == ".." || newCommand == "../"){
00618             break;
00619         }
00620         newCommand=newCommand(3,newCommand.length()-3);
00621     }
00622     return newPrefix;
00623 }
00624 
00626 G4UIcommandTree* G4UIGainServer::FindDirPath(G4String newCommand){
00628   G4UIcommandTree * comTree = UI->GetTree();
00629   /*int*/ unsigned idx = 1; 
00630   while( idx < newCommand.length()-1 )
00631   { 
00632     int i = newCommand.index("/",idx);
00633     comTree = comTree->GetTree(G4String(newCommand(0,i+1)));
00634     if( comTree == NULL )
00635     { return NULL; }
00636     idx = i+1;
00637   }
00638   return comTree;
00639 }
00640 
00642 
00644 void G4UIGainServer::SendCommandProperties(G4UIcommandTree* tree){
00646   if( tree == NULL ) {
00647     G4cerr << "GetTree() returnes null." << G4endl;
00648     return;
00649   }
00650   if (uiMode == java_mode){
00651     G4cout << "@@JTreeBegin" << G4endl;
00652     CodeGenJavaTree(tree, 0);
00653     G4cout << "@@JTreeEnd" << G4endl;
00654     CodeGenJavaParams(tree, 0);
00655   }else{}
00656 }
00657 
00659 void G4UIGainServer::SendParameterProperties(G4UIcommandTree* tree){
00661   if( tree == NULL ) {
00662     G4cerr << "GetTree() returnes null." << G4endl;
00663     return;
00664   }
00665   if (uiMode == java_mode){
00666     CodeGenJavaParams(tree, 0);
00667   }else{ }
00668 }
00669 
00671 void G4UIGainServer::CodeGenJavaTree(G4UIcommandTree* tree,int level){
00673   int treeEntry, commandEntry;
00674   treeEntry = tree->GetTreeEntry();
00675   commandEntry = tree->GetCommandEntry();
00676 
00677   if(level!=0) {
00678     for(int i=0; i<commandEntry; i++){
00679       G4cout << tree->GetCommand(i+1)->GetCommandPath() << G4endl;
00680     }
00681   }
00682   if(treeEntry == 0) return; //end recursion
00683 
00684   for(int j=0; j<treeEntry; j++){
00685     CodeGenJavaTree(tree->GetTree(j+1), level+1);
00686   }
00687 }
00688 
00690 void G4UIGainServer::CodeGenJavaParams(G4UIcommandTree* tree,int level){
00692     int treeEntry,commandEntry,i;
00693     G4UIcommandTree* treeLink;
00694 
00695     treeEntry = tree->GetTreeEntry();
00696     commandEntry = tree->GetCommandEntry();
00697 
00698     for(i=0;i<commandEntry; i++){
00699         SendAParamProperty(tree->GetCommand(i+1));
00700     }
00701     if(treeEntry ==0) return;
00702 
00703     for(i=0;i<treeEntry; i++){
00704         treeLink = tree->GetTree(i+1);
00705         G4cout<<"@@JDirGuieBegin"<<G4endl;
00706         G4cout<<treeLink->GetPathName()<<G4endl <<treeLink->GetTitle()<<G4endl;
00707         G4cout<<"@@JDirGuideEnd"<<G4endl;
00708         CodeGenJavaParams(treeLink,level+1);
00709     }
00710 }
00711 
00713 void G4UIGainServer::SendAParamProperty(G4UIcommand* Comp){
00715   int guidanceEntry, parameterEntry;
00716   G4String title, title2;
00717   G4UIparameter * prp;
00718   char c[2];
00719   guidanceEntry = Comp->GetGuidanceEntries();
00720   parameterEntry = Comp->GetParameterEntries();
00721   G4cout << "@@JParamBegin" << G4endl;
00722   G4cout << Comp->GetCommandPath() << G4endl;
00723   G4cout << guidanceEntry << G4endl;
00724   for (int j=0; j<guidanceEntry; j++){
00725     title = Comp->GetGuidanceLine(j);
00726     title2 = "";
00727     if (title != ""){
00728       for(int i=0; i< (int)title.length(); i++){
00729         c[0]=title(i);
00730         c[1]= '\0';
00731         if ( c[0] == '\n' || c[0] == '\r') {
00732           c[0]= ' ';
00733         }
00734         title2.append(c);
00735       }
00736     }
00737     G4cout << title2 << G4endl;
00738   }
00739   G4cout << Comp->GetRange() << G4endl;
00740   G4cout << parameterEntry << G4endl;
00741   for( int par=0; par<parameterEntry; par++) {
00742     prp = (G4UIparameter *)Comp->GetParameter(par);
00743     G4cout << prp->GetParameterName() << G4endl;
00744     G4cout << prp->GetParameterGuidance() << G4endl;
00745     G4cout << prp->GetParameterType() << G4endl;
00746     G4cout << prp->IsOmittable() << G4endl;
00747     G4cout << prp->GetDefaultValue() << G4endl;
00748     G4cout << prp->GetParameterRange() << G4endl;
00749     G4cout << prp->GetParameterCandidates() << G4endl;
00750   }
00751   G4cout << "@@JParamEnd" << G4endl;
00752 }
00753 
00755 void G4UIGainServer::SendDisableList(G4UIcommandTree* tree,int level){
00757   int treeEntry, commandEntry;
00758   G4UIcommand * Comp;
00759   treeEntry = tree->GetTreeEntry();
00760   commandEntry = tree->GetCommandEntry();
00761 
00762   for(int com=0; com<commandEntry; com++) {
00763     Comp = tree->GetCommand(com+1);
00764     if( Comp->IsAvailable()==false ) {
00765        G4cout << Comp->GetCommandPath()<<G4endl;
00766     }
00767   }
00768   if( treeEntry == 0 ) return;     // end recursion
00769 
00770   for( int i=0; i<treeEntry; i++) {
00771     SendDisableList(tree->GetTree(i+1), level+1);
00772     // be sure the function name is the same
00773   }
00774 }
00775 
00776 
00777 
00778 //####### update check routines ####################################
00779 
00781 void G4UIGainServer::UpdateState(void)
00783 {
00784    static G4ApplicationState previousState= G4State_PreInit;
00785    G4ApplicationState  newState;
00786    G4StateManager *statM = G4StateManager::GetStateManager();
00787    newState = statM->GetCurrentState();
00788    if( newState != previousState ) 
00789    {
00790       NotifyStateChange();
00791       previousState = newState; 
00792    }
00793 }
00794 
00796 void G4UIGainServer::NotifyStateChange(void)
00798 {
00799    G4String stateString;
00800    G4StateManager * statM = G4StateManager::GetStateManager();
00801    G4UIcommandTree * tree = UI->GetTree();
00802    stateString = statM->GetStateString(statM->GetCurrentState());
00803    if ( uiMode != terminal_mode ){
00804      G4cout << "@@State \"" << stateString << "\"" << G4endl;
00805      G4cout << "@@DisableListBegin"<<G4endl;
00806      SendDisableList(tree, 0);
00807      G4cout << "@@DisableListEnd" <<G4endl;
00808    }
00809 }
00810 
00812 void G4UIGainServer::NotifyCommandUpdate(void)
00814 {
00815   G4UIcommandTree * tree = UI->GetTree();
00816   SendCommandProperties(tree);
00817 }
00818 
00820 void G4UIGainServer::NotifyParameterUpdate(G4UIcommand* com)
00822 {
00823     SendAParamProperty(com);
00824 }
00825 
00827 int G4UIGainServer::CommandUpdated(void){
00829   int added=0, deleted=0;
00830   int pEntry= previousTreeCommands.size();
00831   int nEntry= newTreeCommands.size();
00832   int i,j;
00833   for( i=0; i<pEntry; i++) {      // check deleted command(s)
00834       for( j=0; j<nEntry; j++) {
00835          if( previousTreeCommands[i] == newTreeCommands[j]) break;
00836       }
00837       if( j==nEntry ) { 
00838          deleted = 1;
00839          //G4cout <<"deleted: "<< previousTreeCommands(i) << G4endl;
00840       }
00841   }
00842   for( i=0; i<nEntry; i++) {      // check added command(s)
00843       for( j=0; j<pEntry; j++) {
00844          if( newTreeCommands[i] == previousTreeCommands[j]) break;
00845       }
00846       if( j==pEntry ) { 
00847          added = 1;
00848       //   G4cout <<"added: "<< newTreeCommands(i) << G4endl;
00849       }
00850   }
00851   if( added    && deleted==0 ) {G4cout<<"c added"<<G4endl;return added;}
00852   if( added==0 && deleted ) {G4cout<<"c deleted"<<G4endl;return deleted;}
00853   if( added    && deleted ) {G4cout<<"c add/deleted"<<G4endl;return addedAndDeleted;}
00854   return notChanged;
00855 }
00856 
00858 void G4UIGainServer::GetNewTreeStructure(G4UIcommandTree * tree, int level) { 
00860   G4String commandPath;
00861   G4String title; 
00862   G4String pathName; //tree name
00863   G4UIcommandTree * t;
00864   int treeEntry    = tree->GetTreeEntry();
00865   int commandEntry = tree->GetCommandEntry();
00866 
00867   if( level==0 ) { newTreeCommands.clear();}
00868   for(int com=0; com<commandEntry; com++){
00869       commandPath = tree->GetCommand(com+1)->GetCommandPath();
00870       title = tree->GetCommand(com+1)->GetTitle();
00871       newTreeCommands.push_back( commandPath + " " + title );
00872   }
00873 
00874   if(treeEntry == 0) return; //end recursion
00875 
00876   for(int i=0; i< treeEntry; i++){
00877     t = tree->GetTree(i+1);
00878     pathName =  t->GetPathName();   
00879     title = t->GetTitle();
00880     newTreeCommands.push_back( pathName + " " + title );
00881     GetNewTreeStructure(t, level+1);
00882   }
00883 }
00884 
00886 void G4UIGainServer::UpdateParamVal(void) {
00888   // call NotifyParameterUpdate() if the value of each
00889   //  command/parameter is updated.
00890   //  assuming the command structure is not changed.
00891   int pEntry= previousTreeParams.size();
00892   int nEntry= newTreeParams.size();
00893   int i;
00894   G4UIcommand* Comp;
00895   if (pEntry != nEntry) return; 
00896   for( i=0; i<nEntry; i++) {
00897     if( previousTreeParams[i] != newTreeParams[i]){
00898        Comp = newTreePCP[i];
00899        G4cout << Comp->GetCommandPath()
00900             << " command is updated." <<G4endl; 
00901        NotifyParameterUpdate(Comp);
00902     }
00903   }
00904 }
00905 
00907 void G4UIGainServer::GetNewTreeValues( G4UIcommandTree * tree, int level){ // recursive
00909    G4String commandPath;
00910    G4String pathName; //tree name
00911    G4UIcommandTree * t;
00912    int parameterEntry;
00913    int treeEntry    = tree->GetTreeEntry();
00914    int commandEntry = tree->GetCommandEntry();
00915    G4UIcommand * Comp;
00916    G4UIparameter * prp; 
00917    G4String param, str(" ");
00918 
00919    if( level==0 ) { newTreeParams.clear(); }
00920    for(int com=0; com<commandEntry; com++) {
00921       Comp = tree->GetCommand(com+1);
00922       commandPath    = Comp->GetCommandPath();
00923       parameterEntry = Comp->GetParameterEntries();
00924       param = commandPath +" ";
00925       for( int par=0; par< parameterEntry; par++) {
00926          prp = (G4UIparameter *)Comp->GetParameter(par);
00927          param += prp->GetParameterName() +" ";
00928          str(0) = prp->GetParameterType();
00929          param += str + " ";
00930          param += prp->GetDefaultValue()  +" ";
00931          param += prp->GetParameterRange() +" ";
00932          param += prp->GetParameterCandidates();
00933       }
00934      newTreeParams.push_back( param + "\n"); 
00935      newTreePCP.push_back( Comp ); 
00936    }
00937    if( treeEntry == 0 )  return;     // end recursion
00938    for( int i=0; i< treeEntry; i++) {
00939       t = tree->GetTree(i+1);
00940       GetNewTreeValues(t, level+1);
00941    }
00942 }
00943 
00944 
00945 #endif
00946 
00947         
00948 
00949 
00950 
00951 
00952 
00953 
00954 
00955 
00956 
00957         

Generated on Mon May 27 17:50:06 2013 for Geant4 by  doxygen 1.4.7