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