35 #ifdef G4VIS_BUILD_DAWN_DRIVER
46 const char DEFAULT_SUN_PATH[] =
"FR_TMP3" ;
47 const int DEFAULT_PORT_NUMBER = 40701 ;
49 const int MAX_CONNECT_TRIAL = 10 ;
50 const char FR_DEFAULT_HOST_NAME[] =
"localhost" ;
53 G4FRClientServer::G4FRClientServer (
char terminator ,
char end_line ) :
54 TERMINATOR ( terminator ) ,
55 END_OF_LINE( end_line ) ,
58 SetSunPath ( DEFAULT_SUN_PATH ) ;
59 SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
60 ClearReceivedMessage () ;
65 int G4FRClientServer::ConnectUnix()
68 int flag_connected = 0 ;
69 struct sockaddr_un server_address ;
72 fSocketFd =
socket( AF_UNIX, SOCK_STREAM, 0 );
73 if( fSocketFd < 0 ) { Err(
"G4FRClientServer::ConnectUnix(),socket"); }
76 memset( (
char *)&server_address,
'\0',
sizeof(server_address)) ;
77 server_address.sun_family = AF_UNIX ;
78 strcpy( server_address.sun_path, SUN_PATH );
81 int connection_status = -1 ;
83 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
85 connection_status =
connect( fSocketFd, (
struct sockaddr * )(&server_address),
sizeof( server_address ) ) ;
86 if( connection_status <0 )
88 #if defined DEBUG_CLIENT_SERVER
89 Err(
"G4FRClientServer::ConnectUnix(),connect => RETRY");
102 return flag_connected ;
108 void G4FRClientServer::Receive()
111 ClearReceivedMessage () ;
112 if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 )
114 Err(
"G4FRClientServer::Receive(), recv");
117 #if defined DEBUG_CLIENT_SERVER
119 G4cout <<
">>>>> receivedMessage = " << fReceivedMessage <<
G4endl;
126 void G4FRClientServer::ReceiveLine()
133 memset(fReceivedMessage,
'\0', RECV_BUFMAX) ;
134 while( read( fSocketFd, buf, 1 ) == 1 ) {
135 fReceivedMessage[index++] = buf[0];
136 if( IsEndOfLine(buf[0]) ) { break ;}
142 void G4FRClientServer::Send()
144 if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
146 Err(
"G4FRClientServer::Send(), send");
149 #if defined DEBUG_CLIENT_SERVER
151 G4cout <<
"<<<<< SentMessage = " << fSendingMessage <<
G4endl;
158 void G4FRClientServer::Send(
const char*
message )
160 this->SetSendingMessage( message ) ;
167 void G4FRClientServer::SendLine(
const char*
message )
173 this->SetSendingMessage( message ) ;
174 smsg_length = GetSendingMessageLength() ;
177 if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
178 fSendingMessage[ smsg_length ] = GetEndOfLine() ;
179 fSendingMessage[ (smsg_length +1) ] =
'\0' ;
180 smsg_length = GetSendingMessageLength();
190 void G4FRClientServer::DisConnect()
193 if( shutdown(fSocketFd,2) < 0 ) {
194 Err(
"G4FRClientServer::DisConnect,shutdown");
212 int G4FRClientServer::ConnectINET()
215 int flag_connected = 0 ;
216 sockaddr_in server_address ;
217 char server_hostname[32] ;
218 hostent* server_host_p ;
221 fSocketFd =
socket( AF_INET, SOCK_STREAM, 0 );
222 if( fSocketFd < 0 ) {
223 #if defined DEBUG_CLIENT_SERVER
224 Err(
"G4FRClientServer::ConnectINET(),socket");
229 if( std::getenv( FR_ENV_SERVER_HOST_NAME ) != NULL )
232 strcpy( server_hostname, std::getenv( FR_ENV_SERVER_HOST_NAME ) );
236 server_host_p = gethostbyname( server_hostname ) ;
242 if( !server_host_p ) {
243 #if defined DEBUG_CLIENT_SERVER
244 Err(
"G4FRClientServer::ConnectINET(), gethostbyname");
246 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
250 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
257 G4cout <<
"***** Trying connection to " << server_hostname <<
G4endl;
262 memset( (
char *)&server_address,
'\0',
sizeof(server_address)) ;
264 server_address.sin_family = AF_INET ;
265 server_address.sin_port = htons( PORT_NUMBER );
266 memcpy( (
char *)(&server_address.sin_addr ),
267 (
char *)( server_host_p->h_addr ),
268 server_host_p->h_length );
270 int connection_status = -1 ;
272 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
274 connection_status =
connect( fSocketFd, (
struct sockaddr * )(&server_address),
sizeof( server_address ) ) ;
275 if( connection_status <0 )
277 #if defined DEBUG_CLIENT_SERVER
278 Err(
"G4FRClientServer::ConnectINET(),connect => RETRY");
291 return flag_connected ;
297 void G4FRClientServer::WaitSendBack(
const char* command_string )
303 if( !strncmp( this->GetReceivedMessage(), \
305 (strlen(command_string)) ) )
315 this->ClearReceivedMessage();
319 #endif // G4VIS_BUILD_DAWN_DRIVER