G4FRClientServer.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // Satoshi TANAKA, Wed Jul  3 14:14:29 JST 1996
00033 
00034 
00035 //=================//
00036 #ifdef G4VIS_BUILD_DAWN_DRIVER
00037 //=================//
00038 
00039 #include "G4VisManager.hh"
00040 #include "G4FRClientServer.hh"
00041 
00042 // #define DEBUG_CLIENT_SERVER
00043 
00044 #include<sys/param.h>
00045 
00046         //----- const   
00047 const  char     DEFAULT_SUN_PATH[]              = "FR_TMP3"        ;
00048 const  int      DEFAULT_PORT_NUMBER             = 40701            ;
00049 //const  char   FR_ENV_SERVER_HOST_NAME[]       = "G4DAWN_HOST_NAME" ; // moved to .hh
00050 const  int      MAX_BINDING_TRIAL               = 10               ;
00051 const  int      BINDING_TRIAL_INTERVAL          = 5                ;
00052 const  int      MAX_CONNECT_TRIAL               = 10               ;
00053 const  char     FR_DEFAULT_HOST_NAME[]          = "localhost"      ;      
00054 
00055         //----- G4FRClientServer::G4FRClientServer ()
00056 G4FRClientServer::G4FRClientServer ( char terminator , char end_line ) :  
00057         TERMINATOR ( terminator ) , 
00058         END_OF_LINE( end_line   ) ,
00059         fSocketFd  ( -1 )           
00060 { 
00061         SetSunPath    ( DEFAULT_SUN_PATH ) ; // for Unix domain
00062         SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
00063         ClearReceivedMessage () ;
00064 }
00065 
00066 
00067         //----- G4FRClientServer::ConnectUnix()
00068 int G4FRClientServer::ConnectUnix()
00069 {
00070                 //----- local
00071         int                     flag_connected = 0 ; 
00072         struct sockaddr_un      server_address ;
00073 
00074                 //----- make socket
00075         fSocketFd = socket( AF_UNIX, SOCK_STREAM, 0 );
00076         if( fSocketFd < 0 ) { Err("G4FRClientServer::ConnectUnix(),socket"); }
00077 
00078                 //----- set server address
00079         memset( (char *)&server_address, '\0', sizeof(server_address)) ;
00080         server_address.sun_family = AF_UNIX ;
00081         strcpy( server_address.sun_path, SUN_PATH );
00082 
00083                 //----- connection
00084         int     connection_status = -1  ;
00085         int     num_trial         = 0 ;
00086         while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
00087                 num_trial++ ; 
00088                 connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
00089                 if( connection_status  <0 ) 
00090                 {
00091 #if defined DEBUG_CLIENT_SERVER
00092                         Err("G4FRClientServer::ConnectUnix(),connect => RETRY");
00093 #endif
00094                         flag_connected = 0 ;
00095                 } else {
00096                         flag_connected = 1 ;
00097                         break ;
00098                 }
00099 
00100                 sleep(1);
00101                 
00102         } // while(connection_status...)
00103 
00104                 //----- return status of connection
00105         return flag_connected ;
00106 
00107 } // G4FRClientServer::ConnectUnix()
00108 
00109 
00110         //----- G4FRClientServer::Receive()
00111 void    G4FRClientServer::Receive()
00112 {
00113                 //-----
00114         ClearReceivedMessage () ;
00115         if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 ) 
00116         {
00117                 Err("G4FRClientServer::Receive(), recv");
00118         }
00119 
00120 #if defined DEBUG_CLIENT_SERVER
00121         if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00122           G4cout << ">>>>> receivedMessage = " << fReceivedMessage << G4endl;
00123 #endif
00124 
00125 }
00126 
00127 
00128         //----- G4FRClientServer::ReceiveLine()
00129 void    G4FRClientServer::ReceiveLine()
00130 {
00131                 //----- local
00132         char    buf[1];
00133         int index = 0 ;
00134 
00135                 //----- receive a line (until newline)
00136         memset(fReceivedMessage, '\0', RECV_BUFMAX) ;   
00137         while( read( fSocketFd, buf, 1 ) == 1 ) {
00138                 fReceivedMessage[index++] = buf[0];
00139                 if( IsEndOfLine(buf[0]) ) { break ;}
00140         }
00141 } // G4FRClientServer::ReceiveLine()
00142 
00143 
00144         //----- G4FRClientServer::Send()
00145 void    G4FRClientServer::Send()
00146 {
00147         if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 ) 
00148         {
00149                 Err("G4FRClientServer::Send(), send");
00150         }
00151 
00152 #if defined DEBUG_CLIENT_SERVER
00153         if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00154           G4cout << "<<<<< SentMessage = " << fSendingMessage << G4endl;
00155 #endif
00156 
00157 } // G4FRClientServer::Send()
00158 
00159 
00160         //----- G4FRClientServer::Send( message )
00161 void    G4FRClientServer::Send( const char* message ) 
00162 {
00163         this->SetSendingMessage( message )      ;
00164         this->Send();
00165 
00166 } // G4FRClientServer::Send( message )
00167 
00168 
00169         //----- G4FRClientServer::SendLine()
00170 void    G4FRClientServer::SendLine( const char* message ) 
00171 {
00172                 //----- local
00173         int     smsg_length ;
00174 
00175                 //----- set message to sending buf
00176         this->SetSendingMessage( message )      ;
00177         smsg_length = GetSendingMessageLength() ;
00178 
00179                 //----- add newline if necessary
00180         if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
00181                 fSendingMessage[ smsg_length ]      = GetEndOfLine() ;
00182                 fSendingMessage[ (smsg_length +1) ] = '\0' ;
00183                 smsg_length = GetSendingMessageLength();
00184         }
00185 
00186                 //----- send
00187         this->Send();
00188 
00189 }// G4FRClientServer::SendLine()
00190 
00191 
00192         //----- G4FRClientServer::DisConnect()
00193 void G4FRClientServer::DisConnect()
00194 {
00195                 //----- close connection
00196         if( shutdown(fSocketFd,2) < 0 ) { 
00197                 Err("G4FRClientServer::DisConnect,shutdown");
00198         }
00199         close( fSocketFd );
00200 
00201         this->Clear();
00202 }
00203 
00204 
00205 
00206         //----- G4FRClientServer::Clear()
00207 void G4FRClientServer::Clear()
00208 {
00209         unlink(SUN_PATH) ;
00210         fSocketFd = -1   ;
00211 }
00212 
00213 
00214         //----- G4FRClientServer::ConnectINET()
00215 int G4FRClientServer::ConnectINET()
00216 {
00217                 //----- local
00218         int                     flag_connected = 0 ;
00219         sockaddr_in             server_address ;
00220         char                    server_hostname[32] ;
00221         hostent*                server_host_p ;
00222 
00223                 //----- make socket
00224         fSocketFd = socket( AF_INET, SOCK_STREAM, 0 );
00225         if( fSocketFd < 0 ) { 
00226 #if defined DEBUG_CLIENT_SERVER
00227           Err("G4FRClientServer::ConnectINET(),socket"); 
00228 #endif
00229         }
00230 
00231                 //----- get IP address of server from its name
00232         if( getenv( FR_ENV_SERVER_HOST_NAME ) != NULL ) 
00233         {
00234                         //----- get server name
00235                 strcpy( server_hostname, getenv( FR_ENV_SERVER_HOST_NAME ) );
00236 
00237                         //----- get IP address of server from its name,
00238                         //..... reading /etc/hosts
00239                 server_host_p = gethostbyname( server_hostname ) ;
00240                         
00241                         //----- If the host specified by FR_ENV_SERVER_HOST_NAME
00242                         //.....  is not written in /etc/hosts, 
00243                         //...... server host is set to the same as the
00244                         //...... client host
00245                 if( !server_host_p ) {
00246 #if defined DEBUG_CLIENT_SERVER
00247                         Err("G4FRClientServer::ConnectINET(), gethostbyname");
00248 #endif
00249                         server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
00250                 }
00251 
00252         } else {
00253                         server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
00254         }
00255 
00256 
00257 
00258 // #if defined DEBUG_CLIENT_SERVER
00259         if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00260           G4cout << "***** Trying connection to  " << server_hostname << G4endl;
00261 // #endif 
00262         
00263 
00264                 //----- connection and binding 
00265         memset( (char *)&server_address, '\0', sizeof(server_address)) ; 
00266                                 // clear server_address
00267         server_address.sin_family = AF_INET ;
00268         server_address.sin_port   = htons( PORT_NUMBER );
00269         memcpy( (char *)(&server_address.sin_addr ), 
00270                 (char *)( server_host_p->h_addr   ), 
00271                 server_host_p->h_length             ); 
00272 
00273         int     connection_status = -1  ;
00274         int     num_trial         = 0 ;
00275         while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
00276                 num_trial++ ; 
00277                 connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
00278                 if( connection_status  <0 ) 
00279                 {
00280 #if defined DEBUG_CLIENT_SERVER
00281                         Err("G4FRClientServer::ConnectINET(),connect => RETRY");
00282 #endif 
00283                         flag_connected  = 0 ;
00284                 } else {
00285                         flag_connected  = 1 ;
00286                         break ;
00287                 }
00288 
00289                 sleep(1);
00290 
00291         } // while(connection_status...)
00292 
00293                 //----- return status of connection
00294         return flag_connected ;
00295 
00296 } // G4FRClientServer::ConnectINET()
00297 
00298 
00299         //----- G4FRClientServer::WaitSendBack()
00300 void    G4FRClientServer::WaitSendBack( const char* command_string ) 
00301 {
00302                 //----- wait for sending back
00303         while(1) { 
00304                 this->ReceiveLine();
00305 
00306                 if( !strncmp(   this->GetReceivedMessage(), \
00307                                 command_string                , \
00308                                 (strlen(command_string))       )   )
00309                 {
00310                         break;
00311                 } else {
00312                         sleep(2);
00313                 }
00314 
00315         } // while
00316 
00317                 //----- clear buffer to receive message
00318         this->ClearReceivedMessage();   
00319 
00320 } // G4FRClientServer::WaitSendBack()
00321 
00322 #endif // G4VIS_BUILD_DAWN_DRIVER

Generated on Mon May 27 17:48:17 2013 for Geant4 by  doxygen 1.4.7