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
00033
00034
00035
00036 #ifdef G4VIS_BUILD_DAWN_DRIVER
00037
00038
00039 #include "G4VisManager.hh"
00040 #include "G4FRClientServer.hh"
00041
00042
00043
00044 #include<sys/param.h>
00045
00046
00047 const char DEFAULT_SUN_PATH[] = "FR_TMP3" ;
00048 const int DEFAULT_PORT_NUMBER = 40701 ;
00049
00050 const int MAX_BINDING_TRIAL = 10 ;
00051 const int BINDING_TRIAL_INTERVAL = 5 ;
00052 const int MAX_CONNECT_TRIAL = 10 ;
00053 const char FR_DEFAULT_HOST_NAME[] = "localhost" ;
00054
00055
00056 G4FRClientServer::G4FRClientServer ( char terminator , char end_line ) :
00057 TERMINATOR ( terminator ) ,
00058 END_OF_LINE( end_line ) ,
00059 fSocketFd ( -1 )
00060 {
00061 SetSunPath ( DEFAULT_SUN_PATH ) ;
00062 SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
00063 ClearReceivedMessage () ;
00064 }
00065
00066
00067
00068 int G4FRClientServer::ConnectUnix()
00069 {
00070
00071 int flag_connected = 0 ;
00072 struct sockaddr_un server_address ;
00073
00074
00075 fSocketFd = socket( AF_UNIX, SOCK_STREAM, 0 );
00076 if( fSocketFd < 0 ) { Err("G4FRClientServer::ConnectUnix(),socket"); }
00077
00078
00079 memset( (char *)&server_address, '\0', sizeof(server_address)) ;
00080 server_address.sun_family = AF_UNIX ;
00081 strcpy( server_address.sun_path, SUN_PATH );
00082
00083
00084 int connection_status = -1 ;
00085 int num_trial = 0 ;
00086 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
00087 num_trial++ ;
00088 connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
00089 if( connection_status <0 )
00090 {
00091 #if defined DEBUG_CLIENT_SERVER
00092 Err("G4FRClientServer::ConnectUnix(),connect => RETRY");
00093 #endif
00094 flag_connected = 0 ;
00095 } else {
00096 flag_connected = 1 ;
00097 break ;
00098 }
00099
00100 sleep(1);
00101
00102 }
00103
00104
00105 return flag_connected ;
00106
00107 }
00108
00109
00110
00111 void G4FRClientServer::Receive()
00112 {
00113
00114 ClearReceivedMessage () ;
00115 if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 )
00116 {
00117 Err("G4FRClientServer::Receive(), recv");
00118 }
00119
00120 #if defined DEBUG_CLIENT_SERVER
00121 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00122 G4cout << ">>>>> receivedMessage = " << fReceivedMessage << G4endl;
00123 #endif
00124
00125 }
00126
00127
00128
00129 void G4FRClientServer::ReceiveLine()
00130 {
00131
00132 char buf[1];
00133 int index = 0 ;
00134
00135
00136 memset(fReceivedMessage, '\0', RECV_BUFMAX) ;
00137 while( read( fSocketFd, buf, 1 ) == 1 ) {
00138 fReceivedMessage[index++] = buf[0];
00139 if( IsEndOfLine(buf[0]) ) { break ;}
00140 }
00141 }
00142
00143
00144
00145 void G4FRClientServer::Send()
00146 {
00147 if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
00148 {
00149 Err("G4FRClientServer::Send(), send");
00150 }
00151
00152 #if defined DEBUG_CLIENT_SERVER
00153 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00154 G4cout << "<<<<< SentMessage = " << fSendingMessage << G4endl;
00155 #endif
00156
00157 }
00158
00159
00160
00161 void G4FRClientServer::Send( const char* message )
00162 {
00163 this->SetSendingMessage( message ) ;
00164 this->Send();
00165
00166 }
00167
00168
00169
00170 void G4FRClientServer::SendLine( const char* message )
00171 {
00172
00173 int smsg_length ;
00174
00175
00176 this->SetSendingMessage( message ) ;
00177 smsg_length = GetSendingMessageLength() ;
00178
00179
00180 if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
00181 fSendingMessage[ smsg_length ] = GetEndOfLine() ;
00182 fSendingMessage[ (smsg_length +1) ] = '\0' ;
00183 smsg_length = GetSendingMessageLength();
00184 }
00185
00186
00187 this->Send();
00188
00189 }
00190
00191
00192
00193 void G4FRClientServer::DisConnect()
00194 {
00195
00196 if( shutdown(fSocketFd,2) < 0 ) {
00197 Err("G4FRClientServer::DisConnect,shutdown");
00198 }
00199 close( fSocketFd );
00200
00201 this->Clear();
00202 }
00203
00204
00205
00206
00207 void G4FRClientServer::Clear()
00208 {
00209 unlink(SUN_PATH) ;
00210 fSocketFd = -1 ;
00211 }
00212
00213
00214
00215 int G4FRClientServer::ConnectINET()
00216 {
00217
00218 int flag_connected = 0 ;
00219 sockaddr_in server_address ;
00220 char server_hostname[32] ;
00221 hostent* server_host_p ;
00222
00223
00224 fSocketFd = socket( AF_INET, SOCK_STREAM, 0 );
00225 if( fSocketFd < 0 ) {
00226 #if defined DEBUG_CLIENT_SERVER
00227 Err("G4FRClientServer::ConnectINET(),socket");
00228 #endif
00229 }
00230
00231
00232 if( getenv( FR_ENV_SERVER_HOST_NAME ) != NULL )
00233 {
00234
00235 strcpy( server_hostname, getenv( FR_ENV_SERVER_HOST_NAME ) );
00236
00237
00238
00239 server_host_p = gethostbyname( server_hostname ) ;
00240
00241
00242
00243
00244
00245 if( !server_host_p ) {
00246 #if defined DEBUG_CLIENT_SERVER
00247 Err("G4FRClientServer::ConnectINET(), gethostbyname");
00248 #endif
00249 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
00250 }
00251
00252 } else {
00253 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
00254 }
00255
00256
00257
00258
00259 if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00260 G4cout << "***** Trying connection to " << server_hostname << G4endl;
00261
00262
00263
00264
00265 memset( (char *)&server_address, '\0', sizeof(server_address)) ;
00266
00267 server_address.sin_family = AF_INET ;
00268 server_address.sin_port = htons( PORT_NUMBER );
00269 memcpy( (char *)(&server_address.sin_addr ),
00270 (char *)( server_host_p->h_addr ),
00271 server_host_p->h_length );
00272
00273 int connection_status = -1 ;
00274 int num_trial = 0 ;
00275 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
00276 num_trial++ ;
00277 connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
00278 if( connection_status <0 )
00279 {
00280 #if defined DEBUG_CLIENT_SERVER
00281 Err("G4FRClientServer::ConnectINET(),connect => RETRY");
00282 #endif
00283 flag_connected = 0 ;
00284 } else {
00285 flag_connected = 1 ;
00286 break ;
00287 }
00288
00289 sleep(1);
00290
00291 }
00292
00293
00294 return flag_connected ;
00295
00296 }
00297
00298
00299
00300 void G4FRClientServer::WaitSendBack( const char* command_string )
00301 {
00302
00303 while(1) {
00304 this->ReceiveLine();
00305
00306 if( !strncmp( this->GetReceivedMessage(), \
00307 command_string , \
00308 (strlen(command_string)) ) )
00309 {
00310 break;
00311 } else {
00312 sleep(2);
00313 }
00314
00315 }
00316
00317
00318 this->ClearReceivedMessage();
00319
00320 }
00321
00322 #endif // G4VIS_BUILD_DAWN_DRIVER