36 #ifdef G4VIS_BUILD_DAWN_DRIVER
47 const char DEFAULT_SUN_PATH[] =
"FR_TMP3" ;
48 const int DEFAULT_PORT_NUMBER = 40701 ;
50 const int MAX_CONNECT_TRIAL = 10 ;
51 const char FR_DEFAULT_HOST_NAME[] =
"localhost" ;
54 G4FRClientServer::G4FRClientServer (
char terminator ,
char end_line ) :
55 TERMINATOR ( terminator ) ,
56 END_OF_LINE( end_line ) ,
59 SetSunPath ( DEFAULT_SUN_PATH ) ;
60 SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
61 ClearReceivedMessage () ;
66 int G4FRClientServer::ConnectUnix()
69 int flag_connected = 0 ;
70 struct sockaddr_un server_address ;
73 fSocketFd = socket( AF_UNIX, SOCK_STREAM, 0 );
74 if( fSocketFd < 0 ) { Err(
"G4FRClientServer::ConnectUnix(),socket"); }
77 memset( (
char *)&server_address,
'\0',
sizeof(server_address)) ;
78 server_address.sun_family = AF_UNIX ;
79 strcpy( server_address.sun_path, SUN_PATH );
82 int connection_status = -1 ;
84 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
86 connection_status = connect( fSocketFd, (
struct sockaddr * )(&server_address),
sizeof( server_address ) ) ;
87 if( connection_status <0 )
89 #if defined DEBUG_CLIENT_SERVER
90 Err(
"G4FRClientServer::ConnectUnix(),connect => RETRY");
103 return flag_connected ;
109 void G4FRClientServer::Receive()
112 ClearReceivedMessage () ;
113 if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 )
115 Err(
"G4FRClientServer::Receive(), recv");
118 #if defined DEBUG_CLIENT_SERVER
120 G4cout <<
">>>>> receivedMessage = " << fReceivedMessage <<
G4endl;
127 void G4FRClientServer::ReceiveLine()
134 memset(fReceivedMessage,
'\0', RECV_BUFMAX) ;
135 while( read( fSocketFd, buf, 1 ) == 1 ) {
136 fReceivedMessage[index++] = buf[0];
137 if( IsEndOfLine(buf[0]) ) { break ;}
143 void G4FRClientServer::Send()
145 if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
147 Err(
"G4FRClientServer::Send(), send");
150 #if defined DEBUG_CLIENT_SERVER
152 G4cout <<
"<<<<< SentMessage = " << fSendingMessage <<
G4endl;
159 void G4FRClientServer::Send(
const char*
message )
161 this->SetSendingMessage( message ) ;
168 void G4FRClientServer::SendLine(
const char*
message )
174 this->SetSendingMessage( message ) ;
175 smsg_length = GetSendingMessageLength() ;
178 if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
179 fSendingMessage[ smsg_length ] = GetEndOfLine() ;
180 fSendingMessage[ (smsg_length +1) ] =
'\0' ;
181 smsg_length = GetSendingMessageLength();
191 void G4FRClientServer::DisConnect()
194 if( shutdown(fSocketFd,2) < 0 ) {
195 Err(
"G4FRClientServer::DisConnect,shutdown");
213 int G4FRClientServer::ConnectINET()
216 int flag_connected = 0 ;
217 sockaddr_in server_address ;
218 char server_hostname[32] ;
219 hostent* server_host_p ;
222 fSocketFd = socket( AF_INET, SOCK_STREAM, 0 );
223 if( fSocketFd < 0 ) {
224 #if defined DEBUG_CLIENT_SERVER
225 Err(
"G4FRClientServer::ConnectINET(),socket");
230 if( getenv( FR_ENV_SERVER_HOST_NAME ) != NULL )
233 strcpy( server_hostname, getenv( FR_ENV_SERVER_HOST_NAME ) );
237 server_host_p = gethostbyname( server_hostname ) ;
243 if( !server_host_p ) {
244 #if defined DEBUG_CLIENT_SERVER
245 Err(
"G4FRClientServer::ConnectINET(), gethostbyname");
247 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
251 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
258 G4cout <<
"***** Trying connection to " << server_hostname <<
G4endl;
263 memset( (
char *)&server_address,
'\0',
sizeof(server_address)) ;
265 server_address.sin_family = AF_INET ;
266 server_address.sin_port = htons( PORT_NUMBER );
267 memcpy( (
char *)(&server_address.sin_addr ),
268 (
char *)( server_host_p->h_addr ),
269 server_host_p->h_length );
271 int connection_status = -1 ;
273 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
275 connection_status = connect( fSocketFd, (
struct sockaddr * )(&server_address),
sizeof( server_address ) ) ;
276 if( connection_status <0 )
278 #if defined DEBUG_CLIENT_SERVER
279 Err(
"G4FRClientServer::ConnectINET(),connect => RETRY");
292 return flag_connected ;
298 void G4FRClientServer::WaitSendBack(
const char* command_string )
304 if( !strncmp( this->GetReceivedMessage(), \
306 (strlen(command_string)) ) )
316 this->ClearReceivedMessage();
320 #endif // G4VIS_BUILD_DAWN_DRIVER
void message(RunManager *runmanager)
static Verbosity GetVerbosity()
G4GLOB_DLL std::ostream G4cout