46     TVersion =
"T1.0a"; JVersion=
"J1.0a";
 
   50     while(SetUPServer() == 
false){
 
   51         G4cout<<
"can't get the port no. "<<port<<
" Now, try to get the next port "<<port+1<<
G4endl;
 
   57     UI-> SetSession(
this);
 
   58     UI-> SetCoutDestination(
this);
 
   68     GetNewTreeStructure(tree,0);
 
   69     GetNewTreeValues(tree,0);
 
   70     previousTreeCommands = newTreeCommands;
 
   71     previousTreeParams = newTreeParams;
 
   72     previousTreePCP = newTreePCP;
 
   82       UI-> SetSession(NULL);
 
   83       UI-> SetCoutDestination(NULL);
 
  107         ExecuteCommand(newCommand);
 
  116     promptCharacter = msg;
 
  123       ExecuteCommand(newCommand);
 
  125       strcpy(buf,
"nowIdle");
 
  126       write(socketD[1],buf,strlen(buf));
 
  131 void G4UIGainServer::ExecuteCommand(
const G4String& aCommand)
 
  134     if(aCommand.length()<2) 
return;
 
  137     if(aCommand.length()<2) 
return;
 
  139     G4int paramIndex = returnVal % 100;
 
  142     G4int commandStatus = returnVal - paramIndex;
 
  147         switch(commandStatus) {
 
  149             GetNewTreeStructure(tree,0);
 
  150             GetNewTreeValues(tree,0);
 
  151             if(CommandUpdated()){
 
  152                 NotifyCommandUpdate();
 
  156             previousTreeCommands = newTreeCommands;
 
  157             previousTreeParams = newTreeParams;
 
  158             previousTreePCP = newTreePCP;
 
  164             G4cerr << 
"@@ErrResult \"illegal application state -- command refused.\"" << 
G4endl;
 
  167             G4cout << 
"@@ErrResult \"Parameter Out of Range.\"" << 
G4endl;
 
  170             G4cout << 
"@@ErrResult \"Parameter is wrong type and/or is not omittable.\""<<
G4endl;
 
  173             G4cerr << 
"@@ErrResult \"Parameter is out of candidate.\"" << 
G4endl;
 
  177             G4cerr << 
"command refused (" << commandStatus << 
")" << 
G4endl;
 
  193       G4cout << 
"@@PROMPT \"" << promptCharacter << 
"\"" << 
G4endl;
 
  204     read(socketD[1],buf,1024);
 
  213     if (!
G4cin.good()) { 
G4cin.clear(); newCommand = nullString; iExit=
false;
break;}
 
  216     if( newCommand.length() < 1) { 
break; }
 
  218     while( newCommand(newCommand.length()-1) == 
'_' )
 
  221       newCommand.
remove(newCommand.length()-1);
 
  223       if (!
G4cin.good()) { 
G4cin.clear(); newCommand = nullString; iExit=
false;
break;}
 
  224       newCommand.
append(newLine);
 
  228     if( nC.length() < 1) { 
break; }
 
  231     if( nC == 
"@@GainmodeJAVA" ) {
 
  234       SendCommandProperties(tree);
 
  237     else if( nC == 
"@@GainmodeTcl" ) {
 
  240       SendCommandProperties(tree);
 
  243     else if( nC(0) == 
'#' )
 
  246     else if( nC == 
"ls"  || nC(0,3) == 
"ls " )
 
  247     { ListDirectory( nC ); }
 
  248     else if( nC == 
"pwd" )
 
  250     else if( nC(0,2) == 
"cd"  || nC(0,3) == 
"cd " )
 
  251     { ChangeDirectory( nC ); }
 
  252     else if(  nC == 
"help" || nC(0,5) == 
"help ")
 
  253     { TerminalHelp( nC ); }
 
  254     else if( nC(0) == 
'?' )
 
  255     { ShowCurrent( nC ); }
 
  256     else if( nC(0,4) == 
"hist"   || nC == 
"history")
 
  259       for(
int i=0;i<nh;i++)
 
  262     else if( nC(0) == 
'!' )
 
  267       std::istringstream is((
char*)tt);
 
  277       { 
G4cerr << 
"history " << vl << 
" is not found." << 
G4endl; }
 
  279     else if( nC(0,4) == 
"exit" )
 
  285           G4cerr << 
"Please abrot it using \"/run/abort\" command first" << 
G4endl;
 
  286           G4cerr << 
" and use \"continue\" command until the application" << 
G4endl;
 
  289           G4cout << 
"@@ErrResult \"You are now processing RUN.\"" << 
G4endl;
 
  297         newCommand = nullString;
 
  301     else if(  nC == 
"cont" || nC == 
"continue" )
 
  304       newCommand = nullString;
 
  310   return GetFullPath(newCommand);
 
  318         write(socketD[1],coutString,coutString.length());
 
  331         write(socketD[2],cerrString,cerrString.length());
 
  346         G4cin.ignore(30,
'\n');
 
  353 void G4UIGainServer::ExitHelp() const
 
  357     G4cin.getline(temp, 100);
 
  361 bool G4UIGainServer::SetUPServer(){
 
  364     socketD[0] = socket(AF_INET,SOCK_STREAM,0);
 
  367         perror(
"server:socket");
 
  372     memset( (
char *)&saddr,
'\0',
sizeof(saddr)) ;
 
  374     saddr.sin_family = AF_INET;
 
  375     saddr.sin_addr.s_addr = INADDR_ANY;
 
  376     saddr.sin_port = htons(port);
 
  379     if(bind(socketD[0] , (
struct sockaddr *)&saddr , 
sizeof(saddr))<0){
 
  384     else{ 
G4cout<<
"G4GainServer waiting at "<<port<<
G4endl; }
 
  386     if(listen(socketD[0],1)<0){
 
  396 void G4UIGainServer::WaitingConnection(){
 
  400     for(
int i=1;i<=2;i++){
 
  401 #if defined __APPLE__ && (__GNUC__<4) 
  402         if((socketD[i] = accept(socketD[0], (
struct sockaddr *)&caddr,(
int *)&
len))<0){
 
  404         if((socketD[i] = accept(socketD[0], (
struct sockaddr *)&caddr,(socklen_t *)&
len))<0){
 
  411                         "Invalid Socket. Cannot establish connection");
 
  422   if( newCommand(0) == 
'/' ) 
 
  423   { tmpString = newCommand; }
 
  424   else if( newCommand(0,3) == 
"../" )
 
  427      unsigned i_direc = 0;
 
  428     while( i_direc < newCommand.length() )
 
  430       if( newCommand(i_direc,3) == 
"../" )
 
  439     tmpString.
append( newCommand( i_direc, newCommand.length()-i_direc ) );
 
  445     tmpString.
append( newCommand );
 
  458 void G4UIGainServer::ShowCurrent(
G4String newCommand){
 
  460   G4String theCommand = GetFullPath(newCommand(1,newCommand.length()-1));
 
  462   if( ! (curV.
isNull()||curV(0)==
'\0' ) ) {
 
  464       G4cout << 
"Current value(s) of the parameter(s) : " << curV << 
G4endl;
 
  471       G4cout << 
"@@ErrResult \"Current value is not available.\"" << 
G4endl;
 
  476 void G4UIGainServer::ChangeDirectory(
G4String newCommand){
 
  479   if( newCommand.length() <= 3 )
 
  483     G4String aNewPrefix = newCommand(3,newCommand.length()-3);
 
  485     if( newPrefix(0) == 
'/' )
 
  487     else if( newPrefix(0) != 
'.' )
 
  492     { 
prefix = ModifyPrefix( newPrefix ); }
 
  496   if( FindDirPath( 
prefix ) == NULL )
 
  503 void G4UIGainServer::ListDirectory(
G4String newCommand){
 
  506   if( newCommand.length() <= 3 )
 
  510     G4String newPrefix = newCommand(3,newCommand.length()-3);
 
  512     if( newPrefix(0) == 
'/' )
 
  513     { targetDir = newPrefix; }
 
  514     else if( newPrefix(0) != 
'.' )
 
  517       targetDir += newPrefix;
 
  520     { targetDir = ModifyPrefix( newPrefix ); }
 
  522   if( targetDir( targetDir.length() - 1 ) != 
'/' )
 
  523   { targetDir += 
"/"; }
 
  525   if( commandTree == NULL )
 
  526   { 
G4cout << 
"Directory <" << targetDir << 
"> is not found." << 
G4endl; }
 
  532 void G4UIGainServer::TerminalHelp(
G4String newCommand){
 
  537     if(i!=std::string::npos){
 
  538         G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
 
  540         if(newValue(0)!=
'/'){
 
  544         if(theCommand !=NULL){
 
  549             G4cout<<
"Command<" << newValue << 
"is not found."<<
G4endl;
 
  557     unsigned prefixIndex = 1;
 
  558     while(prefixIndex<
prefix.length()-1){
 
  559         int ii = 
prefix.index(
"/",prefixIndex);
 
  568         G4cout<<
G4endl <<
"Type the number (0:end, -n:n level back) :"<<std::flush;
 
  572             G4cin.ignore(30,
'\n');
 
  577             if(iFloor <0) iFloor =0;
 
  580         else if(j==0){
break;}
 
  584                 if(j<=n_tree+floor[iFloor]->GetCommandEntry()){
 
  589                 floor[iFloor+1] = floor[iFloor]->
GetTree(j);
 
  598     G4cin.getline(temp,100);
 
  607         if(newCommand(0,2) ==
".."){
 
  609                 G4String tmpString = newPrefix(0,newPrefix.length()-1);
 
  610                 newPrefix = newPrefix(0,tmpString.
last(
'/')+1);
 
  614             newPrefix += newCommand;
 
  617         if(newCommand == 
".." || newCommand == 
"../"){
 
  620         newCommand=newCommand(3,newCommand.length()-3);
 
  630   while( idx < newCommand.length()-1 )
 
  632     int i = newCommand.
index(
"/",idx);
 
  634     if( comTree == NULL )
 
  652     CodeGenJavaTree(tree, 0);
 
  654     CodeGenJavaParams(tree, 0);
 
  666     CodeGenJavaParams(tree, 0);
 
  673   int treeEntry, commandEntry;
 
  678     for(
int i=0; i<commandEntry; i++){
 
  682   if(treeEntry == 0) 
return; 
 
  684   for(
int j=0; j<treeEntry; j++){
 
  685     CodeGenJavaTree(tree->
GetTree(j+1), level+1);
 
  690 void G4UIGainServer::CodeGenJavaParams(
G4UIcommandTree* tree,
int level){
 
  692     int treeEntry,commandEntry,i;
 
  698     for(i=0;i<commandEntry; i++){
 
  701     if(treeEntry ==0) 
return;
 
  703     for(i=0;i<treeEntry; i++){
 
  708         CodeGenJavaParams(treeLink,level+1);
 
  713 void G4UIGainServer::SendAParamProperty(
G4UIcommand* Comp){
 
  715   int guidanceEntry, parameterEntry;
 
  724   for (
int j=0; j<guidanceEntry; j++){
 
  728       for(
int i=0; i< (
int)title.length(); i++){
 
  731         if ( c[0] == 
'\n' || c[0] == 
'\r') {
 
  741   for( 
int par=0; par<parameterEntry; par++) {
 
  757   int treeEntry, commandEntry;
 
  762   for(
int com=0; com<commandEntry; com++) {
 
  768   if( treeEntry == 0 ) 
return;     
 
  770   for( 
int i=0; i<treeEntry; i++) {
 
  771     SendDisableList(tree->
GetTree(i+1), level+1);
 
  781 void G4UIGainServer::UpdateState(
void)
 
  788    if( newState != previousState ) 
 
  791       previousState = newState; 
 
  796 void G4UIGainServer::NotifyStateChange(
void)
 
  804      G4cout << 
"@@State \"" << stateString << 
"\"" << 
G4endl;
 
  806      SendDisableList(tree, 0);
 
  812 void G4UIGainServer::NotifyCommandUpdate(
void)
 
  816   SendCommandProperties(tree);
 
  820 void G4UIGainServer::NotifyParameterUpdate(
G4UIcommand* com)
 
  823     SendAParamProperty(com);
 
  827 int G4UIGainServer::CommandUpdated(
void){
 
  830   int pEntry= previousTreeCommands.size();
 
  831   int nEntry= newTreeCommands.size();
 
  833   for( i=0; i<pEntry; i++) {      
 
  834       for( j=0; j<nEntry; j++) {
 
  835          if( previousTreeCommands[i] == newTreeCommands[j]) 
break;
 
  842   for( i=0; i<nEntry; i++) {      
 
  843       for( j=0; j<pEntry; j++) {
 
  844          if( newTreeCommands[i] == previousTreeCommands[j]) 
break;
 
  858 void G4UIGainServer::GetNewTreeStructure(
G4UIcommandTree * tree, 
int level) { 
 
  867   if( level==0 ) { newTreeCommands.clear();}
 
  868   for(
int com=0; com<commandEntry; com++){
 
  871       newTreeCommands.push_back( commandPath + 
" " + title );
 
  874   if(treeEntry == 0) 
return; 
 
  876   for(
int i=0; i< treeEntry; i++){
 
  880     newTreeCommands.push_back( pathName + 
" " + title );
 
  881     GetNewTreeStructure(t, level+1);
 
  886 void G4UIGainServer::UpdateParamVal(
void) {
 
  891   int pEntry= previousTreeParams.size();
 
  892   int nEntry= newTreeParams.size();
 
  895   if (pEntry != nEntry) 
return; 
 
  896   for( i=0; i<nEntry; i++) {
 
  897     if( previousTreeParams[i] != newTreeParams[i]){
 
  898        Comp = newTreePCP[i];
 
  900             << 
" command is updated." <<
G4endl; 
 
  901        NotifyParameterUpdate(Comp);
 
  907 void G4UIGainServer::GetNewTreeValues( 
G4UIcommandTree * tree, 
int level){ 
 
  919    if( level==0 ) { newTreeParams.clear(); }
 
  920    for(
int com=0; com<commandEntry; com++) {
 
  924       param = commandPath +
" ";
 
  925       for( 
int par=0; par< parameterEntry; par++) {
 
  934      newTreeParams.push_back( param + 
"\n"); 
 
  935      newTreePCP.push_back( Comp ); 
 
  937    if( treeEntry == 0 )  
return;     
 
  938    for( 
int i=0; i< treeEntry; i++) {
 
  940       GetNewTreeValues(t, level+1);
 
G4int GetNumberOfHistory() const 
 
G4String GetParameterCandidates() const 
 
const G4String & GetRange() const 
 
G4String GetPreviousCommand(G4int i) const 
 
G4String & remove(str_size)
 
G4UIcommand * FindPath(const char *commandPath) const 
 
G4String strip(G4int strip_Type=trailing, char c=' ')
 
G4int GetCommandEntry() const 
 
G4String GetParameterName() const 
 
G4UIcommand * GetCommand(G4int i)
 
G4int GetTreeEntry() const 
 
const G4String GetTitle() const 
 
void SetSession(G4UIsession *const value)
 
std::istream & readLine(std::istream &, G4bool skipWhite=true)
 
virtual void PauseSessionStart(const G4String &msg)
 
const G4String & GetGuidanceLine(G4int i) const 
 
static G4UImanager * GetUIpointer()
 
G4bool IsOmittable() const 
 
G4String & prepend(const char *)
 
static G4StateManager * GetStateManager()
 
G4GLOB_DLL std::ostream G4cout
 
str_size index(const char *, G4int pos=0) const 
 
G4UIcommandTree * GetTree(G4int i)
 
virtual G4int ReceiveG4cerr(const G4String &cerrString)
 
virtual G4int ReceiveG4cout(const G4String &coutString)
 
G4UIparameter * GetParameter(G4int i) const 
 
void ListCurrentWithNum() const 
 
typedef int(XMLCALL *XML_NotStandaloneHandler)(void *userData)
 
const G4String & GetCommandPath() const 
 
G4ApplicationState GetCurrentState() const 
 
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *comments)
 
G4String GetCurrentValues(const char *aCommand)
 
const G4String GetPathName() const 
 
const G4String GetTitle() const 
 
G4UIcommandTree * GetTree() const 
 
G4String & append(const G4String &)
 
G4String GetDefaultValue() const 
 
void SetCoutDestination(G4UIsession *const value)
 
std::string::size_type str_size
 
G4String SolveAlias(const char *aCmd)
 
char GetParameterType() const 
 
G4UIsession * SessionStart()
 
const G4String GetParameterGuidance() const 
 
G4String GetParameterRange() const 
 
G4int GetGuidanceEntries() const 
 
G4int GetParameterEntries() const 
 
G4int ApplyCommand(const char *aCommand)
 
G4GLOB_DLL std::ostream G4cerr
 
G4String GetStateString(G4ApplicationState aState) const