Geant4  10.01
G4FRClientServer.cc
Go to the documentation of this file.
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 79795 2014-03-14 10:13:31Z gcosmo $
28 //
29 // Satoshi TANAKA, Wed Jul 3 14:14:29 JST 1996
33 
34 
35 //=================//
36 #ifdef G4VIS_BUILD_DAWN_DRIVER
37 //=================//
38 
39 #include "G4VisManager.hh"
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_CONNECT_TRIAL = 10 ;
51 const char FR_DEFAULT_HOST_NAME[] = "localhost" ;
52 
53  //----- G4FRClientServer::G4FRClientServer ()
54 G4FRClientServer::G4FRClientServer ( char terminator , char end_line ) :
55  TERMINATOR ( terminator ) ,
56  END_OF_LINE( end_line ) ,
57  fSocketFd ( -1 )
58 {
59  SetSunPath ( DEFAULT_SUN_PATH ) ; // for Unix domain
60  SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
61  ClearReceivedMessage () ;
62 }
63 
64 
65  //----- G4FRClientServer::ConnectUnix()
66 int G4FRClientServer::ConnectUnix()
67 {
68  //----- local
69  int flag_connected = 0 ;
70  struct sockaddr_un server_address ;
71 
72  //----- make socket
73  fSocketFd = socket( AF_UNIX, SOCK_STREAM, 0 );
74  if( fSocketFd < 0 ) { Err("G4FRClientServer::ConnectUnix(),socket"); }
75 
76  //----- set server address
77  memset( (char *)&server_address, '\0', sizeof(server_address)) ;
78  server_address.sun_family = AF_UNIX ;
79  strcpy( server_address.sun_path, SUN_PATH );
80 
81  //----- connection
82  int connection_status = -1 ;
83  int num_trial = 0 ;
84  while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
85  num_trial++ ;
86  connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
87  if( connection_status <0 )
88  {
89 #if defined DEBUG_CLIENT_SERVER
90  Err("G4FRClientServer::ConnectUnix(),connect => RETRY");
91 #endif
92  flag_connected = 0 ;
93  } else {
94  flag_connected = 1 ;
95  break ;
96  }
97 
98  sleep(1);
99 
100  } // while(connection_status...)
101 
102  //----- return status of connection
103  return flag_connected ;
104 
105 } // G4FRClientServer::ConnectUnix()
106 
107 
108  //----- G4FRClientServer::Receive()
109 void G4FRClientServer::Receive()
110 {
111  //-----
112  ClearReceivedMessage () ;
113  if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 )
114  {
115  Err("G4FRClientServer::Receive(), recv");
116  }
117 
118 #if defined DEBUG_CLIENT_SERVER
120  G4cout << ">>>>> receivedMessage = " << fReceivedMessage << G4endl;
121 #endif
122 
123 }
124 
125 
126  //----- G4FRClientServer::ReceiveLine()
127 void G4FRClientServer::ReceiveLine()
128 {
129  //----- local
130  char buf[1];
131  int index = 0 ;
132 
133  //----- receive a line (until newline)
134  memset(fReceivedMessage, '\0', RECV_BUFMAX) ;
135  while( read( fSocketFd, buf, 1 ) == 1 ) {
136  fReceivedMessage[index++] = buf[0];
137  if( IsEndOfLine(buf[0]) ) { break ;}
138  }
139 } // G4FRClientServer::ReceiveLine()
140 
141 
142  //----- G4FRClientServer::Send()
143 void G4FRClientServer::Send()
144 {
145  if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
146  {
147  Err("G4FRClientServer::Send(), send");
148  }
149 
150 #if defined DEBUG_CLIENT_SERVER
152  G4cout << "<<<<< 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()
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
258  G4cout << "***** Trying connection to " << server_hostname << G4endl;
259 // #endif
260 
261 
262  //----- connection and binding
263  memset( (char *)&server_address, '\0', sizeof(server_address)) ;
264  // clear 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 );
270 
271  int connection_status = -1 ;
272  int num_trial = 0 ;
273  while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
274  num_trial++ ;
275  connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
276  if( connection_status <0 )
277  {
278 #if defined DEBUG_CLIENT_SERVER
279  Err("G4FRClientServer::ConnectINET(),connect => RETRY");
280 #endif
281  flag_connected = 0 ;
282  } else {
283  flag_connected = 1 ;
284  break ;
285  }
286 
287  sleep(1);
288 
289  } // while(connection_status...)
290 
291  //----- return status of connection
292  return flag_connected ;
293 
294 } // G4FRClientServer::ConnectINET()
295 
296 
297  //----- G4FRClientServer::WaitSendBack()
298 void G4FRClientServer::WaitSendBack( const char* command_string )
299 {
300  //----- wait for sending back
301  while(1) {
302  this->ReceiveLine();
303 
304  if( !strncmp( this->GetReceivedMessage(), \
305  command_string , \
306  (strlen(command_string)) ) )
307  {
308  break;
309  } else {
310  sleep(2);
311  }
312 
313  } // while
314 
315  //----- clear buffer to receive message
316  this->ClearReceivedMessage();
317 
318 } // G4FRClientServer::WaitSendBack()
319 
320 #endif // G4VIS_BUILD_DAWN_DRIVER
void Clear(Node *)
G4GLOB_DLL std::ostream G4cout
static Verbosity GetVerbosity()
#define G4endl
Definition: G4ios.hh:61