36#ifdef G4VIS_BUILD_DAWN_DRIVER
47const char DEFAULT_SUN_PATH[] =
"FR_TMP3" ;
48const int DEFAULT_PORT_NUMBER = 40701 ;
50const int MAX_BINDING_TRIAL = 10 ;
51const int BINDING_TRIAL_INTERVAL = 5 ;
52const int MAX_CONNECT_TRIAL = 10 ;
53const char FR_DEFAULT_HOST_NAME[] =
"localhost" ;
56G4FRClientServer::G4FRClientServer (
char terminator ,
char end_line ) :
57 TERMINATOR ( terminator ) ,
58 END_OF_LINE( end_line ) ,
61 SetSunPath ( DEFAULT_SUN_PATH ) ;
62 SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
63 ClearReceivedMessage () ;
68int G4FRClientServer::ConnectUnix()
71 int flag_connected = 0 ;
72 struct sockaddr_un server_address ;
75 fSocketFd = socket( AF_UNIX, SOCK_STREAM, 0 );
76 if( fSocketFd < 0 ) { Err(
"G4FRClientServer::ConnectUnix(),socket"); }
79 memset( (
char *)&server_address,
'\0',
sizeof(server_address)) ;
80 server_address.sun_family = AF_UNIX ;
81 strcpy( server_address.sun_path, SUN_PATH );
84 int connection_status = -1 ;
86 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
88 connection_status = connect( fSocketFd, (
struct sockaddr * )(&server_address),
sizeof( server_address ) ) ;
89 if( connection_status <0 )
91#if defined DEBUG_CLIENT_SERVER
92 Err(
"G4FRClientServer::ConnectUnix(),connect => RETRY");
105 return flag_connected ;
111void G4FRClientServer::Receive()
114 ClearReceivedMessage () ;
115 if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 )
117 Err(
"G4FRClientServer::Receive(), recv");
120#if defined DEBUG_CLIENT_SERVER
122 G4cout <<
">>>>> receivedMessage = " << fReceivedMessage <<
G4endl;
129void G4FRClientServer::ReceiveLine()
136 memset(fReceivedMessage,
'\0', RECV_BUFMAX) ;
137 while( read( fSocketFd, buf, 1 ) == 1 ) {
138 fReceivedMessage[index++] = buf[0];
139 if( IsEndOfLine(buf[0]) ) { break ;}
145void G4FRClientServer::Send()
147 if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
149 Err(
"G4FRClientServer::Send(), send");
152#if defined DEBUG_CLIENT_SERVER
154 G4cout <<
"<<<<< SentMessage = " << fSendingMessage <<
G4endl;
161void G4FRClientServer::Send(
const char* message )
163 this->SetSendingMessage( message ) ;
170void G4FRClientServer::SendLine(
const char* message )
176 this->SetSendingMessage( message ) ;
177 smsg_length = GetSendingMessageLength() ;
180 if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
181 fSendingMessage[ smsg_length ] = GetEndOfLine() ;
182 fSendingMessage[ (smsg_length +1) ] =
'\0' ;
183 smsg_length = GetSendingMessageLength();
193void G4FRClientServer::DisConnect()
196 if( shutdown(fSocketFd,2) < 0 ) {
197 Err(
"G4FRClientServer::DisConnect,shutdown");
207void G4FRClientServer::Clear()
215int G4FRClientServer::ConnectINET()
218 int flag_connected = 0 ;
219 sockaddr_in server_address ;
220 char server_hostname[32] ;
221 hostent* server_host_p ;
224 fSocketFd = socket( AF_INET, SOCK_STREAM, 0 );
225 if( fSocketFd < 0 ) {
226#if defined DEBUG_CLIENT_SERVER
227 Err(
"G4FRClientServer::ConnectINET(),socket");
232 if( getenv( FR_ENV_SERVER_HOST_NAME ) != NULL )
235 strcpy( server_hostname, getenv( FR_ENV_SERVER_HOST_NAME ) );
239 server_host_p = gethostbyname( server_hostname ) ;
245 if( !server_host_p ) {
246#if defined DEBUG_CLIENT_SERVER
247 Err(
"G4FRClientServer::ConnectINET(), gethostbyname");
249 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
253 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
260 G4cout <<
"***** Trying connection to " << server_hostname <<
G4endl;
265 memset( (
char *)&server_address,
'\0',
sizeof(server_address)) ;
267 server_address.sin_family = AF_INET ;
268 server_address.sin_port = htons( PORT_NUMBER );
269 memcpy( (
char *)(&server_address.sin_addr ),
270 (
char *)( server_host_p->h_addr ),
271 server_host_p->h_length );
273 int connection_status = -1 ;
275 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
277 connection_status = connect( fSocketFd, (
struct sockaddr * )(&server_address),
sizeof( server_address ) ) ;
278 if( connection_status <0 )
280#if defined DEBUG_CLIENT_SERVER
281 Err(
"G4FRClientServer::ConnectINET(),connect => RETRY");
294 return flag_connected ;
300void G4FRClientServer::WaitSendBack(
const char* command_string )
306 if( !strncmp( this->GetReceivedMessage(), \
308 (strlen(command_string)) ) )
318 this->ClearReceivedMessage();
G4DLLIMPORT std::ostream G4cout
static Verbosity GetVerbosity()