Geant4 9.6.0
Toolkit for the simulation of the passage of particles through matter
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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$
28//
29// Satoshi TANAKA, Wed Jul 3 14:14:29 JST 1996
30////////////////////////////////
31///// G4FRClientServer.cc /////
32////////////////////////////////
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
47const char DEFAULT_SUN_PATH[] = "FR_TMP3" ;
48const int DEFAULT_PORT_NUMBER = 40701 ;
49//const char FR_ENV_SERVER_HOST_NAME[] = "G4DAWN_HOST_NAME" ; // moved to .hh
50const int MAX_BINDING_TRIAL = 10 ;
51const int BINDING_TRIAL_INTERVAL = 5 ;
52const int MAX_CONNECT_TRIAL = 10 ;
53const char FR_DEFAULT_HOST_NAME[] = "localhost" ;
54
55 //----- G4FRClientServer::G4FRClientServer ()
56G4FRClientServer::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()
68int 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()
111void 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
122 G4cout << ">>>>> receivedMessage = " << fReceivedMessage << G4endl;
123#endif
124
125}
126
127
128 //----- G4FRClientServer::ReceiveLine()
129void G4FRClientServer::ReceiveLine()
130{
131 //----- local
132 char buf[1];
133 int index = 0 ;
134
135 //----- receive a line (until newline)
136 memset(fReceivedMessage, '\0', RECV_BUFMAX) ;
137 while( read( fSocketFd, buf, 1 ) == 1 ) {
138 fReceivedMessage[index++] = buf[0];
139 if( IsEndOfLine(buf[0]) ) { break ;}
140 }
141} // G4FRClientServer::ReceiveLine()
142
143
144 //----- G4FRClientServer::Send()
145void G4FRClientServer::Send()
146{
147 if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
148 {
149 Err("G4FRClientServer::Send(), send");
150 }
151
152#if defined DEBUG_CLIENT_SERVER
154 G4cout << "<<<<< SentMessage = " << fSendingMessage << G4endl;
155#endif
156
157} // G4FRClientServer::Send()
158
159
160 //----- G4FRClientServer::Send( message )
161void G4FRClientServer::Send( const char* message )
162{
163 this->SetSendingMessage( message ) ;
164 this->Send();
165
166} // G4FRClientServer::Send( message )
167
168
169 //----- G4FRClientServer::SendLine()
170void G4FRClientServer::SendLine( const char* message )
171{
172 //----- local
173 int smsg_length ;
174
175 //----- set message to sending buf
176 this->SetSendingMessage( message ) ;
177 smsg_length = GetSendingMessageLength() ;
178
179 //----- add newline if necessary
180 if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
181 fSendingMessage[ smsg_length ] = GetEndOfLine() ;
182 fSendingMessage[ (smsg_length +1) ] = '\0' ;
183 smsg_length = GetSendingMessageLength();
184 }
185
186 //----- send
187 this->Send();
188
189}// G4FRClientServer::SendLine()
190
191
192 //----- G4FRClientServer::DisConnect()
193void G4FRClientServer::DisConnect()
194{
195 //----- close connection
196 if( shutdown(fSocketFd,2) < 0 ) {
197 Err("G4FRClientServer::DisConnect,shutdown");
198 }
199 close( fSocketFd );
200
201 this->Clear();
202}
203
204
205
206 //----- G4FRClientServer::Clear()
207void G4FRClientServer::Clear()
208{
209 unlink(SUN_PATH) ;
210 fSocketFd = -1 ;
211}
212
213
214 //----- G4FRClientServer::ConnectINET()
215int G4FRClientServer::ConnectINET()
216{
217 //----- local
218 int flag_connected = 0 ;
219 sockaddr_in server_address ;
220 char server_hostname[32] ;
221 hostent* server_host_p ;
222
223 //----- make socket
224 fSocketFd = socket( AF_INET, SOCK_STREAM, 0 );
225 if( fSocketFd < 0 ) {
226#if defined DEBUG_CLIENT_SERVER
227 Err("G4FRClientServer::ConnectINET(),socket");
228#endif
229 }
230
231 //----- get IP address of server from its name
232 if( getenv( FR_ENV_SERVER_HOST_NAME ) != NULL )
233 {
234 //----- get server name
235 strcpy( server_hostname, getenv( FR_ENV_SERVER_HOST_NAME ) );
236
237 //----- get IP address of server from its name,
238 //..... reading /etc/hosts
239 server_host_p = gethostbyname( server_hostname ) ;
240
241 //----- If the host specified by FR_ENV_SERVER_HOST_NAME
242 //..... is not written in /etc/hosts,
243 //...... server host is set to the same as the
244 //...... client host
245 if( !server_host_p ) {
246#if defined DEBUG_CLIENT_SERVER
247 Err("G4FRClientServer::ConnectINET(), gethostbyname");
248#endif
249 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
250 }
251
252 } else {
253 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
254 }
255
256
257
258// #if defined DEBUG_CLIENT_SERVER
260 G4cout << "***** Trying connection to " << server_hostname << G4endl;
261// #endif
262
263
264 //----- connection and binding
265 memset( (char *)&server_address, '\0', sizeof(server_address)) ;
266 // clear server_address
267 server_address.sin_family = AF_INET ;
268 server_address.sin_port = htons( PORT_NUMBER );
269 memcpy( (char *)(&server_address.sin_addr ),
270 (char *)( server_host_p->h_addr ),
271 server_host_p->h_length );
272
273 int connection_status = -1 ;
274 int num_trial = 0 ;
275 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
276 num_trial++ ;
277 connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
278 if( connection_status <0 )
279 {
280#if defined DEBUG_CLIENT_SERVER
281 Err("G4FRClientServer::ConnectINET(),connect => RETRY");
282#endif
283 flag_connected = 0 ;
284 } else {
285 flag_connected = 1 ;
286 break ;
287 }
288
289 sleep(1);
290
291 } // while(connection_status...)
292
293 //----- return status of connection
294 return flag_connected ;
295
296} // G4FRClientServer::ConnectINET()
297
298
299 //----- G4FRClientServer::WaitSendBack()
300void G4FRClientServer::WaitSendBack( const char* command_string )
301{
302 //----- wait for sending back
303 while(1) {
304 this->ReceiveLine();
305
306 if( !strncmp( this->GetReceivedMessage(), \
307 command_string , \
308 (strlen(command_string)) ) )
309 {
310 break;
311 } else {
312 sleep(2);
313 }
314
315 } // while
316
317 //----- clear buffer to receive message
318 this->ClearReceivedMessage();
319
320} // G4FRClientServer::WaitSendBack()
321
322#endif // G4VIS_BUILD_DAWN_DRIVER
#define G4endl
Definition: G4ios.hh:52
G4DLLIMPORT std::ostream G4cout
static Verbosity GetVerbosity()
void Clear(Node *)