45 TVersion =
"T1.0a"; JVersion=
"J1.0a";
49 while(SetUPServer() ==
false){
50 G4cout<<
"can't get the port no. "<<port<<
" Now, try to get the next port "<<port+1<<
G4endl;
56 UI-> SetSession(
this);
57 UI-> SetCoutDestination(
this);
67 GetNewTreeStructure(tree,0);
68 GetNewTreeValues(tree,0);
69 previousTreeCommands = newTreeCommands;
70 previousTreeParams = newTreeParams;
71 previousTreePCP = newTreePCP;
81 UI-> SetSession(NULL);
82 UI-> SetCoutDestination(NULL);
106 ExecuteCommand(newCommand);
115 promptCharacter = msg;
122 ExecuteCommand(newCommand);
124 strcpy(buf,
"nowIdle");
125 ssize_t rc = write(socketD[1],buf,strlen(buf));
131void 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 ssize_t rc = read(socketD[1],buf,1024);
214 if (!
G4cin.good()) {
G4cin.clear(); newCommand = nullString; iExit=
false;
break;}
217 if( newCommand.length() < 1) {
break; }
219 while( newCommand(newCommand.length()-1) ==
'_' )
222 newCommand.
remove(newCommand.length()-1);
224 if (!
G4cin.good()) {
G4cin.clear(); newCommand = nullString; iExit=
false;
break;}
225 newCommand.
append(newLine);
229 if( nC.length() < 1) {
break; }
232 if( nC ==
"@@GainmodeJAVA" ) {
235 SendCommandProperties(tree);
238 else if( nC ==
"@@GainmodeTcl" ) {
241 SendCommandProperties(tree);
244 else if( nC(0) ==
'#' )
247 else if( nC ==
"ls" || nC(0,3) ==
"ls " )
248 { ListDirectory( nC ); }
249 else if( nC ==
"pwd" )
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")
260 for(
int i=0;i<nh;i++)
263 else if( nC(0) ==
'!' )
268 std::istringstream is((
char*)tt);
278 {
G4cerr <<
"history " << vl <<
" is not found." <<
G4endl; }
280 else if( nC(0,4) ==
"exit" )
286 G4cerr <<
"Please abort it using \"/run/abort\" command first" <<
G4endl;
287 G4cerr <<
" and use \"continue\" command until the application" <<
G4endl;
290 G4cout <<
"@@ErrResult \"You are now processing RUN.\"" <<
G4endl;
298 newCommand = nullString;
302 else if( nC ==
"cont" || nC ==
"continue" )
305 newCommand = nullString;
311 return GetFullPath(newCommand);
319 ssize_t rc = write(socketD[1],coutString,coutString.length());
333 ssize_t rc = write(socketD[2],cerrString,cerrString.length());
349 G4cin.ignore(30,
'\n');
356void G4UIGainServer::ExitHelp() const
360 G4cin.getline(temp, 100);
364bool G4UIGainServer::SetUPServer(){
367 socketD[0] = socket(AF_INET,SOCK_STREAM,0);
370 perror(
"server:socket");
375 memset( (
char *)&saddr,
'\0',
sizeof(saddr)) ;
377 saddr.sin_family = AF_INET;
378 saddr.sin_addr.s_addr = INADDR_ANY;
379 saddr.sin_port = htons(port);
382 if(bind(socketD[0] , (
struct sockaddr *)&saddr ,
sizeof(saddr))<0){
387 else{
G4cout<<
"G4GainServer waiting at "<<port<<
G4endl; }
389 if(listen(socketD[0],1)<0){
399void G4UIGainServer::WaitingConnection(){
403 for(
int i=1;i<=2;i++){
404#if defined __APPLE__ && (__GNUC__<4)
405 if((socketD[i] = accept(socketD[0], (
struct sockaddr *)&caddr,(
int *)&len))<0){
407 if((socketD[i] = accept(socketD[0], (
struct sockaddr *)&caddr,(socklen_t *)&len))<0){
414 "Invalid Socket. Cannot establish connection");
425 if( newCommand(0) ==
'/' )
426 { tmpString = newCommand; }
427 else if( newCommand(0,3) ==
"../" )
430 unsigned i_direc = 0;
431 while( i_direc < newCommand.length() )
433 if( newCommand(i_direc,3) ==
"../" )
442 tmpString.
append( newCommand( i_direc, newCommand.length()-i_direc ) );
448 tmpString.
append( newCommand );
461void G4UIGainServer::ShowCurrent(
G4String newCommand){
463 G4String theCommand = GetFullPath(newCommand(1,newCommand.length()-1));
465 if( ! (curV.
isNull()||curV(0)==
'\0' ) ) {
467 G4cout <<
"Current value(s) of the parameter(s) : " << curV <<
G4endl;
474 G4cout <<
"@@ErrResult \"Current value is not available.\"" <<
G4endl;
479void G4UIGainServer::ChangeDirectory(
G4String newCommand){
482 if( newCommand.length() <= 3 )
486 G4String aNewPrefix = newCommand(3,newCommand.length()-3);
488 if( newPrefix(0) ==
'/' )
490 else if( newPrefix(0) !=
'.' )
495 {
prefix = ModifyPrefix( newPrefix ); }
499 if( FindDirPath(
prefix ) == NULL )
506void G4UIGainServer::ListDirectory(
G4String newCommand){
509 if( newCommand.length() <= 3 )
513 G4String newPrefix = newCommand(3,newCommand.length()-3);
515 if( newPrefix(0) ==
'/' )
516 { targetDir = newPrefix; }
517 else if( newPrefix(0) !=
'.' )
520 targetDir += newPrefix;
523 { targetDir = ModifyPrefix( newPrefix ); }
525 if( targetDir( targetDir.length() - 1 ) !=
'/' )
526 { targetDir +=
"/"; }
528 if( commandTree == NULL )
529 {
G4cout <<
"Directory <" << targetDir <<
"> is not found." <<
G4endl; }
535void G4UIGainServer::TerminalHelp(
G4String newCommand){
540 if(i!=std::string::npos){
541 G4String newValue = newCommand(i+1,newCommand.length()-(i+1));
543 if(newValue(0)!=
'/'){
547 if(theCommand !=NULL){
552 G4cout<<
"Command<" << newValue <<
"is not found."<<
G4endl;
560 unsigned prefixIndex = 1;
561 while(prefixIndex<
prefix.length()-1){
562 int ii =
prefix.index(
"/",prefixIndex);
571 G4cout<<
G4endl <<
"Type the number (0:end, -n:n level back) :"<<std::flush;
575 G4cin.ignore(30,
'\n');
580 if(iFloor <0) iFloor =0;
583 else if(j==0){
break;}
587 if(j<=n_tree+floor[iFloor]->GetCommandEntry()){
592 floor[iFloor+1] = floor[iFloor]->
GetTree(j);
601 G4cin.getline(temp,100);
610 if(newCommand(0,2) ==
".."){
612 G4String tmpString = newPrefix(0,newPrefix.length()-1);
613 newPrefix = newPrefix(0,tmpString.
last(
'/')+1);
617 newPrefix += newCommand;
620 if(newCommand ==
".." || newCommand ==
"../"){
623 newCommand=newCommand(3,newCommand.length()-3);
633 while( idx < newCommand.length()-1 )
635 int i = newCommand.
index(
"/",idx);
637 if( comTree == NULL )
655 CodeGenJavaTree(tree, 0);
657 CodeGenJavaParams(tree, 0);
669 CodeGenJavaParams(tree, 0);
676 int treeEntry, commandEntry;
681 for(
int i=0; i<commandEntry; i++){
685 if(treeEntry == 0)
return;
687 for(
int j=0; j<treeEntry; j++){
688 CodeGenJavaTree(tree->
GetTree(j+1), level+1);
693void G4UIGainServer::CodeGenJavaParams(
G4UIcommandTree* tree,
int level){
695 int treeEntry,commandEntry,i;
701 for(i=0;i<commandEntry; i++){
704 if(treeEntry ==0)
return;
706 for(i=0;i<treeEntry; i++){
711 CodeGenJavaParams(treeLink,level+1);
716void G4UIGainServer::SendAParamProperty(
G4UIcommand* Comp){
718 int guidanceEntry, parameterEntry;
727 for (
int j=0; j<guidanceEntry; j++){
731 for(
int i=0; i< (int)title.length(); i++){
734 if ( c[0] ==
'\n' || c[0] ==
'\r') {
744 for(
int par=0; par<parameterEntry; par++) {
760 int treeEntry, commandEntry;
765 for(
int com=0; com<commandEntry; com++) {
771 if( treeEntry == 0 )
return;
773 for(
int i=0; i<treeEntry; i++) {
774 SendDisableList(tree->
GetTree(i+1), level+1);
784void G4UIGainServer::UpdateState(
void)
791 if( newState != previousState )
794 previousState = newState;
799void G4UIGainServer::NotifyStateChange(
void)
807 G4cout <<
"@@State \"" << stateString <<
"\"" <<
G4endl;
809 SendDisableList(tree, 0);
815void G4UIGainServer::NotifyCommandUpdate(
void)
819 SendCommandProperties(tree);
823void G4UIGainServer::NotifyParameterUpdate(
G4UIcommand* com)
826 SendAParamProperty(com);
830int G4UIGainServer::CommandUpdated(
void){
833 int pEntry= previousTreeCommands.size();
834 int nEntry= newTreeCommands.size();
836 for( i=0; i<pEntry; i++) {
837 for( j=0; j<nEntry; j++) {
838 if( previousTreeCommands[i] == newTreeCommands[j])
break;
845 for( i=0; i<nEntry; i++) {
846 for( j=0; j<pEntry; j++) {
847 if( newTreeCommands[i] == previousTreeCommands[j])
break;
861void G4UIGainServer::GetNewTreeStructure(
G4UIcommandTree * tree,
int level) {
870 if( level==0 ) { newTreeCommands.clear();}
871 for(
int com=0; com<commandEntry; com++){
874 newTreeCommands.push_back( commandPath +
" " + title );
877 if(treeEntry == 0)
return;
879 for(
int i=0; i< treeEntry; i++){
883 newTreeCommands.push_back( pathName +
" " + title );
884 GetNewTreeStructure(t, level+1);
889void G4UIGainServer::UpdateParamVal(
void) {
894 int pEntry= previousTreeParams.size();
895 int nEntry= newTreeParams.size();
898 if (pEntry != nEntry)
return;
899 for( i=0; i<nEntry; i++) {
900 if( previousTreeParams[i] != newTreeParams[i]){
901 Comp = newTreePCP[i];
903 <<
" command is updated." <<
G4endl;
904 NotifyParameterUpdate(Comp);
910void G4UIGainServer::GetNewTreeValues(
G4UIcommandTree * tree,
int level){
922 if( level==0 ) { newTreeParams.clear(); }
923 for(
int com=0; com<commandEntry; com++) {
927 param = commandPath +
" ";
928 for(
int par=0; par< parameterEntry; par++) {
937 newTreeParams.push_back( param +
"\n");
938 newTreePCP.push_back( Comp );
940 if( treeEntry == 0 )
return;
941 for(
int i=0; i< treeEntry; i++) {
943 GetNewTreeValues(t, level+1);
void G4Exception(const char *originOfException, const char *exceptionCode, G4ExceptionSeverity severity, const char *description)
std::string::size_type str_size
@ fParameterOutOfCandidates
@ fIllegalApplicationState
G4GLOB_DLL std::ostream G4cerr
G4GLOB_DLL std::ostream G4cout
const G4ApplicationState & GetCurrentState() const
G4String GetStateString(const G4ApplicationState &aState) const
static G4StateManager * GetStateManager()
G4String & remove(str_size)
G4String & append(const G4String &)
G4String & prepend(const char *)
str_size index(const char *, G4int pos=0) const
std::size_t last(char) const
G4String strip(G4int strip_Type=trailing, char c=' ')
std::istream & readLine(std::istream &, G4bool skipWhite=true)
virtual G4int ReceiveG4cout(const G4String &coutString)
G4UIsession * SessionStart()
virtual void PauseSessionStart(const G4String &msg)
virtual G4int ReceiveG4cerr(const G4String &cerrString)
G4int GetCommandEntry() const
G4UIcommand * GetCommand(G4int i)
const G4String & GetPathName() const
G4int GetTreeEntry() const
G4UIcommandTree * GetTree(G4int i)
void ListCurrentWithNum() const
const G4String GetTitle() const
G4UIcommand * FindPath(const char *commandPath) const
std::size_t GetParameterEntries() const
const G4String & GetGuidanceLine(G4int i) const
G4UIparameter * GetParameter(G4int i) const
const G4String GetTitle() const
const G4String & GetCommandPath() const
std::size_t GetGuidanceEntries() const
const G4String & GetRange() const
void SetCoutDestination(G4UIsession *const value)
G4UIcommandTree * GetTree() const
G4int ApplyCommand(const char *aCommand)
G4int GetNumberOfHistory() const
G4String GetPreviousCommand(G4int i) const
G4String GetCurrentValues(const char *aCommand)
G4String SolveAlias(const char *aCmd)
static G4UImanager * GetUIpointer()
void SetSession(G4UIsession *const value)
const G4String & GetParameterCandidates() const
const G4String & GetParameterGuidance() const
G4bool IsOmittable() const
const G4String & GetParameterRange() const
char GetParameterType() const
const G4String & GetParameterName() const
const G4String & GetDefaultValue() const