| [834] | 1 | //
|
|---|
| 2 | // ********************************************************************
|
|---|
| 3 | // * License and Disclaimer *
|
|---|
| 4 | // * *
|
|---|
| 5 | // * The Geant4 software is copyright of the Copyright Holders of *
|
|---|
| 6 | // * the Geant4 Collaboration. It is provided under the terms and *
|
|---|
| 7 | // * conditions of the Geant4 Software License, included in the file *
|
|---|
| 8 | // * LICENSE and available at http://cern.ch/geant4/license . These *
|
|---|
| 9 | // * include a list of copyright holders. *
|
|---|
| 10 | // * *
|
|---|
| 11 | // * Neither the authors of this software system, nor their employing *
|
|---|
| 12 | // * institutes,nor the agencies providing financial support for this *
|
|---|
| 13 | // * work make any representation or warranty, express or implied, *
|
|---|
| 14 | // * regarding this software system or assume any liability for its *
|
|---|
| 15 | // * use. Please see the license in the file LICENSE and URL above *
|
|---|
| 16 | // * for the full disclaimer and the limitation of liability. *
|
|---|
| 17 | // * *
|
|---|
| 18 | // * This code implementation is the result of the scientific and *
|
|---|
| 19 | // * technical work of the GEANT4 collaboration. *
|
|---|
| 20 | // * By using, copying, modifying or distributing the software (or *
|
|---|
| 21 | // * any work based on the software) you agree to acknowledge its *
|
|---|
| 22 | // * use in resulting scientific publications, and indicate your *
|
|---|
| 23 | // * acceptance of all terms of the Geant4 Software license. *
|
|---|
| 24 | // ********************************************************************
|
|---|
| 25 | //
|
|---|
| 26 | //
|
|---|
| 27 | // $Id: G4FRClientServer.cc,v 1.6 2006/06/29 21:16:56 gunter Exp $
|
|---|
| [1337] | 28 | // GEANT4 tag $Name: geant4-09-04-beta-01 $
|
|---|
| [834] | 29 | //
|
|---|
| 30 | // Satoshi TANAKA, Wed Jul 3 14:14:29 JST 1996
|
|---|
| 31 | ////////////////////////////////
|
|---|
| 32 | ///// G4FRClientServer.cc /////
|
|---|
| 33 | ////////////////////////////////
|
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 | //=================//
|
|---|
| 37 | #ifdef G4VIS_BUILD_DAWN_DRIVER
|
|---|
| 38 | //=================//
|
|---|
| 39 |
|
|---|
| 40 | #include "G4FRClientServer.hh"
|
|---|
| 41 |
|
|---|
| 42 | // #define DEBUG_CLIENT_SERVER
|
|---|
| 43 |
|
|---|
| 44 | #include<sys/param.h>
|
|---|
| 45 |
|
|---|
| 46 | //----- const
|
|---|
| 47 | const char DEFAULT_SUN_PATH[] = "FR_TMP3" ;
|
|---|
| 48 | const int DEFAULT_PORT_NUMBER = 40701 ;
|
|---|
| 49 | //const char FR_ENV_SERVER_HOST_NAME[] = "G4DAWN_HOST_NAME" ; // moved to .hh
|
|---|
| 50 | const int MAX_BINDING_TRIAL = 10 ;
|
|---|
| 51 | const int BINDING_TRIAL_INTERVAL = 5 ;
|
|---|
| 52 | const int MAX_CONNECT_TRIAL = 10 ;
|
|---|
| 53 | const char FR_DEFAULT_HOST_NAME[] = "localhost" ;
|
|---|
| 54 |
|
|---|
| 55 | //----- G4FRClientServer::G4FRClientServer ()
|
|---|
| 56 | G4FRClientServer::G4FRClientServer ( char terminator , char end_line ) :
|
|---|
| 57 | TERMINATOR ( terminator ) ,
|
|---|
| 58 | END_OF_LINE( end_line ) ,
|
|---|
| 59 | fSocketFd ( -1 )
|
|---|
| 60 | {
|
|---|
| 61 | SetSunPath ( DEFAULT_SUN_PATH ) ; // for Unix domain
|
|---|
| 62 | SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
|
|---|
| 63 | ClearReceivedMessage () ;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | //----- G4FRClientServer::ConnectUnix()
|
|---|
| 68 | int G4FRClientServer::ConnectUnix()
|
|---|
| 69 | {
|
|---|
| 70 | //----- local
|
|---|
| 71 | int flag_connected = 0 ;
|
|---|
| 72 | struct sockaddr_un server_address ;
|
|---|
| 73 |
|
|---|
| 74 | //----- make socket
|
|---|
| 75 | fSocketFd = socket( AF_UNIX, SOCK_STREAM, 0 );
|
|---|
| 76 | if( fSocketFd < 0 ) { Err("G4FRClientServer::ConnectUnix(),socket"); }
|
|---|
| 77 |
|
|---|
| 78 | //----- set server address
|
|---|
| 79 | memset( (char *)&server_address, '\0', sizeof(server_address)) ;
|
|---|
| 80 | server_address.sun_family = AF_UNIX ;
|
|---|
| 81 | strcpy( server_address.sun_path, SUN_PATH );
|
|---|
| 82 |
|
|---|
| 83 | //----- connection
|
|---|
| 84 | int connection_status = -1 ;
|
|---|
| 85 | int num_trial = 0 ;
|
|---|
| 86 | while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
|
|---|
| 87 | num_trial++ ;
|
|---|
| 88 | connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
|
|---|
| 89 | if( connection_status <0 )
|
|---|
| 90 | {
|
|---|
| 91 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 92 | Err("G4FRClientServer::ConnectUnix(),connect => RETRY");
|
|---|
| 93 | #endif
|
|---|
| 94 | flag_connected = 0 ;
|
|---|
| 95 | } else {
|
|---|
| 96 | flag_connected = 1 ;
|
|---|
| 97 | break ;
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | sleep(1);
|
|---|
| 101 |
|
|---|
| 102 | } // while(connection_status...)
|
|---|
| 103 |
|
|---|
| 104 | //----- return status of connection
|
|---|
| 105 | return flag_connected ;
|
|---|
| 106 |
|
|---|
| 107 | } // G4FRClientServer::ConnectUnix()
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | //----- G4FRClientServer::Receive()
|
|---|
| 111 | void G4FRClientServer::Receive()
|
|---|
| 112 | {
|
|---|
| 113 | //-----
|
|---|
| 114 | ClearReceivedMessage () ;
|
|---|
| 115 | if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 )
|
|---|
| 116 | {
|
|---|
| 117 | Err("G4FRClientServer::Receive(), recv");
|
|---|
| 118 | }
|
|---|
| 119 |
|
|---|
| 120 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 121 | G4cerr << ">>>>> receivedMessage = " << fReceivedMessage << G4endl;
|
|---|
| 122 | #endif
|
|---|
| 123 |
|
|---|
| 124 | }
|
|---|
| 125 |
|
|---|
| 126 |
|
|---|
| 127 | //----- G4FRClientServer::ReceiveLine()
|
|---|
| 128 | void G4FRClientServer::ReceiveLine()
|
|---|
| 129 | {
|
|---|
| 130 | //----- local
|
|---|
| 131 | char buf[1];
|
|---|
| 132 | int index = 0 ;
|
|---|
| 133 |
|
|---|
| 134 | //----- receive a line (until newline)
|
|---|
| 135 | memset(fReceivedMessage, '\0', RECV_BUFMAX) ;
|
|---|
| 136 | while( read( fSocketFd, buf, 1 ) == 1 ) {
|
|---|
| 137 | fReceivedMessage[index++] = buf[0];
|
|---|
| 138 | if( IsEndOfLine(buf[0]) ) { break ;}
|
|---|
| 139 | }
|
|---|
| 140 | } // G4FRClientServer::ReceiveLine()
|
|---|
| 141 |
|
|---|
| 142 |
|
|---|
| 143 | //----- G4FRClientServer::Send()
|
|---|
| 144 | void G4FRClientServer::Send()
|
|---|
| 145 | {
|
|---|
| 146 | if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
|
|---|
| 147 | {
|
|---|
| 148 | Err("G4FRClientServer::Send(), send");
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 152 | G4cerr << "<<<<< SentMessage = " << fSendingMessage << G4endl;
|
|---|
| 153 | #endif
|
|---|
| 154 |
|
|---|
| 155 | } // G4FRClientServer::Send()
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 | //----- G4FRClientServer::Send( message )
|
|---|
| 159 | void G4FRClientServer::Send( const char* message )
|
|---|
| 160 | {
|
|---|
| 161 | this->SetSendingMessage( message ) ;
|
|---|
| 162 | this->Send();
|
|---|
| 163 |
|
|---|
| 164 | } // G4FRClientServer::Send( message )
|
|---|
| 165 |
|
|---|
| 166 |
|
|---|
| 167 | //----- G4FRClientServer::SendLine()
|
|---|
| 168 | void G4FRClientServer::SendLine( const char* message )
|
|---|
| 169 | {
|
|---|
| 170 | //----- local
|
|---|
| 171 | int smsg_length ;
|
|---|
| 172 |
|
|---|
| 173 | //----- set message to sending buf
|
|---|
| 174 | this->SetSendingMessage( message ) ;
|
|---|
| 175 | smsg_length = GetSendingMessageLength() ;
|
|---|
| 176 |
|
|---|
| 177 | //----- add newline if necessary
|
|---|
| 178 | if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
|
|---|
| 179 | fSendingMessage[ smsg_length ] = GetEndOfLine() ;
|
|---|
| 180 | fSendingMessage[ (smsg_length +1) ] = '\0' ;
|
|---|
| 181 | smsg_length = GetSendingMessageLength();
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | //----- send
|
|---|
| 185 | this->Send();
|
|---|
| 186 |
|
|---|
| 187 | }// G4FRClientServer::SendLine()
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | //----- G4FRClientServer::DisConnect()
|
|---|
| 191 | void G4FRClientServer::DisConnect()
|
|---|
| 192 | {
|
|---|
| 193 | //----- close connection
|
|---|
| 194 | if( shutdown(fSocketFd,2) < 0 ) {
|
|---|
| 195 | Err("G4FRClientServer::DisConnect,shutdown");
|
|---|
| 196 | }
|
|---|
| 197 | close( fSocketFd );
|
|---|
| 198 |
|
|---|
| 199 | this->Clear();
|
|---|
| 200 | }
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 |
|
|---|
| 204 | //----- G4FRClientServer::Clear()
|
|---|
| 205 | void G4FRClientServer::Clear()
|
|---|
| 206 | {
|
|---|
| 207 | unlink(SUN_PATH) ;
|
|---|
| 208 | fSocketFd = -1 ;
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 |
|
|---|
| 212 | //----- G4FRClientServer::ConnectINET()
|
|---|
| 213 | int G4FRClientServer::ConnectINET()
|
|---|
| 214 | {
|
|---|
| 215 | //----- local
|
|---|
| 216 | int flag_connected = 0 ;
|
|---|
| 217 | sockaddr_in server_address ;
|
|---|
| 218 | char server_hostname[32] ;
|
|---|
| 219 | hostent* server_host_p ;
|
|---|
| 220 |
|
|---|
| 221 | //----- make socket
|
|---|
| 222 | fSocketFd = socket( AF_INET, SOCK_STREAM, 0 );
|
|---|
| 223 | if( fSocketFd < 0 ) {
|
|---|
| 224 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 225 | Err("G4FRClientServer::ConnectINET(),socket");
|
|---|
| 226 | #endif
|
|---|
| 227 | }
|
|---|
| 228 |
|
|---|
| 229 | //----- get IP address of server from its name
|
|---|
| 230 | if( getenv( FR_ENV_SERVER_HOST_NAME ) != NULL )
|
|---|
| 231 | {
|
|---|
| 232 | //----- get server name
|
|---|
| 233 | strcpy( server_hostname, getenv( FR_ENV_SERVER_HOST_NAME ) );
|
|---|
| 234 |
|
|---|
| 235 | //----- get IP address of server from its name,
|
|---|
| 236 | //..... reading /etc/hosts
|
|---|
| 237 | server_host_p = gethostbyname( server_hostname ) ;
|
|---|
| 238 |
|
|---|
| 239 | //----- If the host specified by FR_ENV_SERVER_HOST_NAME
|
|---|
| 240 | //..... is not written in /etc/hosts,
|
|---|
| 241 | //...... server host is set to the same as the
|
|---|
| 242 | //...... client host
|
|---|
| 243 | if( !server_host_p ) {
|
|---|
| 244 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 245 | Err("G4FRClientServer::ConnectINET(), gethostbyname");
|
|---|
| 246 | #endif
|
|---|
| 247 | server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
|
|---|
| 248 | }
|
|---|
| 249 |
|
|---|
| 250 | } else {
|
|---|
| 251 | server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
|
|---|
| 252 | }
|
|---|
| 253 |
|
|---|
| 254 |
|
|---|
| 255 |
|
|---|
| 256 | // #if defined DEBUG_CLIENT_SERVER
|
|---|
| 257 | G4cerr << "***** Trying connection to " << server_hostname << G4endl;
|
|---|
| 258 | // #endif
|
|---|
| 259 |
|
|---|
| 260 |
|
|---|
| 261 | //----- connection and binding
|
|---|
| 262 | memset( (char *)&server_address, '\0', sizeof(server_address)) ;
|
|---|
| 263 | // clear 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 );
|
|---|
| 269 |
|
|---|
| 270 | int connection_status = -1 ;
|
|---|
| 271 | int num_trial = 0 ;
|
|---|
| 272 | while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
|
|---|
| 273 | num_trial++ ;
|
|---|
| 274 | connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
|
|---|
| 275 | if( connection_status <0 )
|
|---|
| 276 | {
|
|---|
| 277 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 278 | Err("G4FRClientServer::ConnectINET(),connect => RETRY");
|
|---|
| 279 | #endif
|
|---|
| 280 | flag_connected = 0 ;
|
|---|
| 281 | } else {
|
|---|
| 282 | flag_connected = 1 ;
|
|---|
| 283 | break ;
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | sleep(1);
|
|---|
| 287 |
|
|---|
| 288 | } // while(connection_status...)
|
|---|
| 289 |
|
|---|
| 290 | //----- return status of connection
|
|---|
| 291 | return flag_connected ;
|
|---|
| 292 |
|
|---|
| 293 | } // G4FRClientServer::ConnectINET()
|
|---|
| 294 |
|
|---|
| 295 |
|
|---|
| 296 | //----- G4FRClientServer::WaitSendBack()
|
|---|
| 297 | void G4FRClientServer::WaitSendBack( const char* command_string )
|
|---|
| 298 | {
|
|---|
| 299 | //----- wait for sending back
|
|---|
| 300 | while(1) {
|
|---|
| 301 | this->ReceiveLine();
|
|---|
| 302 |
|
|---|
| 303 | if( !strncmp( this->GetReceivedMessage(), \
|
|---|
| 304 | command_string , \
|
|---|
| 305 | (strlen(command_string)) ) )
|
|---|
| 306 | {
|
|---|
| 307 | break;
|
|---|
| 308 | } else {
|
|---|
| 309 | sleep(2);
|
|---|
| 310 | }
|
|---|
| 311 |
|
|---|
| 312 | } // while
|
|---|
| 313 |
|
|---|
| 314 | //----- clear buffer to receive message
|
|---|
| 315 | this->ClearReceivedMessage();
|
|---|
| 316 |
|
|---|
| 317 | } // G4FRClientServer::WaitSendBack()
|
|---|
| 318 |
|
|---|
| 319 | #endif // G4VIS_BUILD_DAWN_DRIVER
|
|---|