| 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.7 2010/11/11 01:13:42 akimura Exp $
|
|---|
| 28 | // GEANT4 tag $Name: geant4-09-04-ref-00 $
|
|---|
| 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 "G4VisManager.hh"
|
|---|
| 41 | #include "G4FRClientServer.hh"
|
|---|
| 42 |
|
|---|
| 43 | // #define DEBUG_CLIENT_SERVER
|
|---|
| 44 |
|
|---|
| 45 | #include<sys/param.h>
|
|---|
| 46 |
|
|---|
| 47 | //----- const
|
|---|
| 48 | const char DEFAULT_SUN_PATH[] = "FR_TMP3" ;
|
|---|
| 49 | const int DEFAULT_PORT_NUMBER = 40701 ;
|
|---|
| 50 | //const char FR_ENV_SERVER_HOST_NAME[] = "G4DAWN_HOST_NAME" ; // moved to .hh
|
|---|
| 51 | const int MAX_BINDING_TRIAL = 10 ;
|
|---|
| 52 | const int BINDING_TRIAL_INTERVAL = 5 ;
|
|---|
| 53 | const int MAX_CONNECT_TRIAL = 10 ;
|
|---|
| 54 | const char FR_DEFAULT_HOST_NAME[] = "localhost" ;
|
|---|
| 55 |
|
|---|
| 56 | //----- G4FRClientServer::G4FRClientServer ()
|
|---|
| 57 | G4FRClientServer::G4FRClientServer ( char terminator , char end_line ) :
|
|---|
| 58 | TERMINATOR ( terminator ) ,
|
|---|
| 59 | END_OF_LINE( end_line ) ,
|
|---|
| 60 | fSocketFd ( -1 )
|
|---|
| 61 | {
|
|---|
| 62 | SetSunPath ( DEFAULT_SUN_PATH ) ; // for Unix domain
|
|---|
| 63 | SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
|
|---|
| 64 | ClearReceivedMessage () ;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 | //----- G4FRClientServer::ConnectUnix()
|
|---|
| 69 | int G4FRClientServer::ConnectUnix()
|
|---|
| 70 | {
|
|---|
| 71 | //----- local
|
|---|
| 72 | int flag_connected = 0 ;
|
|---|
| 73 | struct sockaddr_un server_address ;
|
|---|
| 74 |
|
|---|
| 75 | //----- make socket
|
|---|
| 76 | fSocketFd = socket( AF_UNIX, SOCK_STREAM, 0 );
|
|---|
| 77 | if( fSocketFd < 0 ) { Err("G4FRClientServer::ConnectUnix(),socket"); }
|
|---|
| 78 |
|
|---|
| 79 | //----- set server address
|
|---|
| 80 | memset( (char *)&server_address, '\0', sizeof(server_address)) ;
|
|---|
| 81 | server_address.sun_family = AF_UNIX ;
|
|---|
| 82 | strcpy( server_address.sun_path, SUN_PATH );
|
|---|
| 83 |
|
|---|
| 84 | //----- connection
|
|---|
| 85 | int connection_status = -1 ;
|
|---|
| 86 | int num_trial = 0 ;
|
|---|
| 87 | while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
|
|---|
| 88 | num_trial++ ;
|
|---|
| 89 | connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
|
|---|
| 90 | if( connection_status <0 )
|
|---|
| 91 | {
|
|---|
| 92 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 93 | Err("G4FRClientServer::ConnectUnix(),connect => RETRY");
|
|---|
| 94 | #endif
|
|---|
| 95 | flag_connected = 0 ;
|
|---|
| 96 | } else {
|
|---|
| 97 | flag_connected = 1 ;
|
|---|
| 98 | break ;
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | sleep(1);
|
|---|
| 102 |
|
|---|
| 103 | } // while(connection_status...)
|
|---|
| 104 |
|
|---|
| 105 | //----- return status of connection
|
|---|
| 106 | return flag_connected ;
|
|---|
| 107 |
|
|---|
| 108 | } // G4FRClientServer::ConnectUnix()
|
|---|
| 109 |
|
|---|
| 110 |
|
|---|
| 111 | //----- G4FRClientServer::Receive()
|
|---|
| 112 | void G4FRClientServer::Receive()
|
|---|
| 113 | {
|
|---|
| 114 | //-----
|
|---|
| 115 | ClearReceivedMessage () ;
|
|---|
| 116 | if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 )
|
|---|
| 117 | {
|
|---|
| 118 | Err("G4FRClientServer::Receive(), recv");
|
|---|
| 119 | }
|
|---|
| 120 |
|
|---|
| 121 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 122 | if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
|
|---|
| 123 | G4cout << ">>>>> receivedMessage = " << fReceivedMessage << G4endl;
|
|---|
| 124 | #endif
|
|---|
| 125 |
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 |
|
|---|
| 129 | //----- G4FRClientServer::ReceiveLine()
|
|---|
| 130 | void G4FRClientServer::ReceiveLine()
|
|---|
| 131 | {
|
|---|
| 132 | //----- local
|
|---|
| 133 | char buf[1];
|
|---|
| 134 | int index = 0 ;
|
|---|
| 135 |
|
|---|
| 136 | //----- receive a line (until newline)
|
|---|
| 137 | memset(fReceivedMessage, '\0', RECV_BUFMAX) ;
|
|---|
| 138 | while( read( fSocketFd, buf, 1 ) == 1 ) {
|
|---|
| 139 | fReceivedMessage[index++] = buf[0];
|
|---|
| 140 | if( IsEndOfLine(buf[0]) ) { break ;}
|
|---|
| 141 | }
|
|---|
| 142 | } // G4FRClientServer::ReceiveLine()
|
|---|
| 143 |
|
|---|
| 144 |
|
|---|
| 145 | //----- G4FRClientServer::Send()
|
|---|
| 146 | void G4FRClientServer::Send()
|
|---|
| 147 | {
|
|---|
| 148 | if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
|
|---|
| 149 | {
|
|---|
| 150 | Err("G4FRClientServer::Send(), send");
|
|---|
| 151 | }
|
|---|
| 152 |
|
|---|
| 153 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 154 | if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
|
|---|
| 155 | G4cout << "<<<<< SentMessage = " << fSendingMessage << G4endl;
|
|---|
| 156 | #endif
|
|---|
| 157 |
|
|---|
| 158 | } // G4FRClientServer::Send()
|
|---|
| 159 |
|
|---|
| 160 |
|
|---|
| 161 | //----- G4FRClientServer::Send( message )
|
|---|
| 162 | void G4FRClientServer::Send( const char* message )
|
|---|
| 163 | {
|
|---|
| 164 | this->SetSendingMessage( message ) ;
|
|---|
| 165 | this->Send();
|
|---|
| 166 |
|
|---|
| 167 | } // G4FRClientServer::Send( message )
|
|---|
| 168 |
|
|---|
| 169 |
|
|---|
| 170 | //----- G4FRClientServer::SendLine()
|
|---|
| 171 | void G4FRClientServer::SendLine( const char* message )
|
|---|
| 172 | {
|
|---|
| 173 | //----- local
|
|---|
| 174 | int smsg_length ;
|
|---|
| 175 |
|
|---|
| 176 | //----- set message to sending buf
|
|---|
| 177 | this->SetSendingMessage( message ) ;
|
|---|
| 178 | smsg_length = GetSendingMessageLength() ;
|
|---|
| 179 |
|
|---|
| 180 | //----- add newline if necessary
|
|---|
| 181 | if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
|
|---|
| 182 | fSendingMessage[ smsg_length ] = GetEndOfLine() ;
|
|---|
| 183 | fSendingMessage[ (smsg_length +1) ] = '\0' ;
|
|---|
| 184 | smsg_length = GetSendingMessageLength();
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | //----- send
|
|---|
| 188 | this->Send();
|
|---|
| 189 |
|
|---|
| 190 | }// G4FRClientServer::SendLine()
|
|---|
| 191 |
|
|---|
| 192 |
|
|---|
| 193 | //----- G4FRClientServer::DisConnect()
|
|---|
| 194 | void G4FRClientServer::DisConnect()
|
|---|
| 195 | {
|
|---|
| 196 | //----- close connection
|
|---|
| 197 | if( shutdown(fSocketFd,2) < 0 ) {
|
|---|
| 198 | Err("G4FRClientServer::DisConnect,shutdown");
|
|---|
| 199 | }
|
|---|
| 200 | close( fSocketFd );
|
|---|
| 201 |
|
|---|
| 202 | this->Clear();
|
|---|
| 203 | }
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 |
|
|---|
| 207 | //----- G4FRClientServer::Clear()
|
|---|
| 208 | void G4FRClientServer::Clear()
|
|---|
| 209 | {
|
|---|
| 210 | unlink(SUN_PATH) ;
|
|---|
| 211 | fSocketFd = -1 ;
|
|---|
| 212 | }
|
|---|
| 213 |
|
|---|
| 214 |
|
|---|
| 215 | //----- G4FRClientServer::ConnectINET()
|
|---|
| 216 | int G4FRClientServer::ConnectINET()
|
|---|
| 217 | {
|
|---|
| 218 | //----- local
|
|---|
| 219 | int flag_connected = 0 ;
|
|---|
| 220 | sockaddr_in server_address ;
|
|---|
| 221 | char server_hostname[32] ;
|
|---|
| 222 | hostent* server_host_p ;
|
|---|
| 223 |
|
|---|
| 224 | //----- make socket
|
|---|
| 225 | fSocketFd = socket( AF_INET, SOCK_STREAM, 0 );
|
|---|
| 226 | if( fSocketFd < 0 ) {
|
|---|
| 227 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 228 | Err("G4FRClientServer::ConnectINET(),socket");
|
|---|
| 229 | #endif
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | //----- get IP address of server from its name
|
|---|
| 233 | if( getenv( FR_ENV_SERVER_HOST_NAME ) != NULL )
|
|---|
| 234 | {
|
|---|
| 235 | //----- get server name
|
|---|
| 236 | strcpy( server_hostname, getenv( FR_ENV_SERVER_HOST_NAME ) );
|
|---|
| 237 |
|
|---|
| 238 | //----- get IP address of server from its name,
|
|---|
| 239 | //..... reading /etc/hosts
|
|---|
| 240 | server_host_p = gethostbyname( server_hostname ) ;
|
|---|
| 241 |
|
|---|
| 242 | //----- If the host specified by FR_ENV_SERVER_HOST_NAME
|
|---|
| 243 | //..... is not written in /etc/hosts,
|
|---|
| 244 | //...... server host is set to the same as the
|
|---|
| 245 | //...... client host
|
|---|
| 246 | if( !server_host_p ) {
|
|---|
| 247 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 248 | Err("G4FRClientServer::ConnectINET(), gethostbyname");
|
|---|
| 249 | #endif
|
|---|
| 250 | server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
|
|---|
| 251 | }
|
|---|
| 252 |
|
|---|
| 253 | } else {
|
|---|
| 254 | server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
|
|---|
| 255 | }
|
|---|
| 256 |
|
|---|
| 257 |
|
|---|
| 258 |
|
|---|
| 259 | // #if defined DEBUG_CLIENT_SERVER
|
|---|
| 260 | if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
|
|---|
| 261 | G4cout << "***** Trying connection to " << server_hostname << G4endl;
|
|---|
| 262 | // #endif
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 | //----- connection and binding
|
|---|
| 266 | memset( (char *)&server_address, '\0', sizeof(server_address)) ;
|
|---|
| 267 | // clear server_address
|
|---|
| 268 | server_address.sin_family = AF_INET ;
|
|---|
| 269 | server_address.sin_port = htons( PORT_NUMBER );
|
|---|
| 270 | memcpy( (char *)(&server_address.sin_addr ),
|
|---|
| 271 | (char *)( server_host_p->h_addr ),
|
|---|
| 272 | server_host_p->h_length );
|
|---|
| 273 |
|
|---|
| 274 | int connection_status = -1 ;
|
|---|
| 275 | int num_trial = 0 ;
|
|---|
| 276 | while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
|
|---|
| 277 | num_trial++ ;
|
|---|
| 278 | connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
|
|---|
| 279 | if( connection_status <0 )
|
|---|
| 280 | {
|
|---|
| 281 | #if defined DEBUG_CLIENT_SERVER
|
|---|
| 282 | Err("G4FRClientServer::ConnectINET(),connect => RETRY");
|
|---|
| 283 | #endif
|
|---|
| 284 | flag_connected = 0 ;
|
|---|
| 285 | } else {
|
|---|
| 286 | flag_connected = 1 ;
|
|---|
| 287 | break ;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | sleep(1);
|
|---|
| 291 |
|
|---|
| 292 | } // while(connection_status...)
|
|---|
| 293 |
|
|---|
| 294 | //----- return status of connection
|
|---|
| 295 | return flag_connected ;
|
|---|
| 296 |
|
|---|
| 297 | } // G4FRClientServer::ConnectINET()
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 | //----- G4FRClientServer::WaitSendBack()
|
|---|
| 301 | void G4FRClientServer::WaitSendBack( const char* command_string )
|
|---|
| 302 | {
|
|---|
| 303 | //----- wait for sending back
|
|---|
| 304 | while(1) {
|
|---|
| 305 | this->ReceiveLine();
|
|---|
| 306 |
|
|---|
| 307 | if( !strncmp( this->GetReceivedMessage(), \
|
|---|
| 308 | command_string , \
|
|---|
| 309 | (strlen(command_string)) ) )
|
|---|
| 310 | {
|
|---|
| 311 | break;
|
|---|
| 312 | } else {
|
|---|
| 313 | sleep(2);
|
|---|
| 314 | }
|
|---|
| 315 |
|
|---|
| 316 | } // while
|
|---|
| 317 |
|
|---|
| 318 | //----- clear buffer to receive message
|
|---|
| 319 | this->ClearReceivedMessage();
|
|---|
| 320 |
|
|---|
| 321 | } // G4FRClientServer::WaitSendBack()
|
|---|
| 322 |
|
|---|
| 323 | #endif // G4VIS_BUILD_DAWN_DRIVER
|
|---|