Geant4 10.7.0
Toolkit for the simulation of the passage of particles through matter
Loading...
Searching...
No Matches
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//
28// Satoshi TANAKA, Wed Jul 3 14:14:29 JST 1996
29////////////////////////////////
30///// G4FRClientServer.cc /////
31////////////////////////////////
32
33
34//=================//
35#ifdef G4VIS_BUILD_DAWN_DRIVER
36//=================//
37
38#include "G4VisManager.hh"
39#include "G4FRClientServer.hh"
40
41// #define DEBUG_CLIENT_SERVER
42
43#include<sys/param.h>
44
45 //----- const
46const char DEFAULT_SUN_PATH[] = "FR_TMP3" ;
47const int DEFAULT_PORT_NUMBER = 40701 ;
48//const char FR_ENV_SERVER_HOST_NAME[] = "G4DAWN_HOST_NAME" ; // moved to .hh
49const int MAX_CONNECT_TRIAL = 10 ;
50const char FR_DEFAULT_HOST_NAME[] = "localhost" ;
51
52 //----- G4FRClientServer::G4FRClientServer ()
53G4FRClientServer::G4FRClientServer ( char terminator , char end_line ) :
54 TERMINATOR ( terminator ) ,
55 END_OF_LINE( end_line ) ,
56 fSocketFd ( -1 )
57{
58 SetSunPath ( DEFAULT_SUN_PATH ) ; // for Unix domain
59 SetPortNumber ( DEFAULT_PORT_NUMBER ) ;
60 ClearReceivedMessage () ;
61}
62
63
64 //----- G4FRClientServer::ConnectUnix()
65int G4FRClientServer::ConnectUnix()
66{
67 //----- local
68 int flag_connected = 0 ;
69 struct sockaddr_un server_address ;
70
71 //----- make socket
72 fSocketFd = socket( AF_UNIX, SOCK_STREAM, 0 );
73 if( fSocketFd < 0 ) { Err("G4FRClientServer::ConnectUnix(),socket"); }
74
75 //----- set server address
76 memset( (char *)&server_address, '\0', sizeof(server_address)) ;
77 server_address.sun_family = AF_UNIX ;
78 strcpy( server_address.sun_path, SUN_PATH );
79
80 //----- connection
81 int connection_status = -1 ;
82 int num_trial = 0 ;
83 while( connection_status < 0 && num_trial <= MAX_CONNECT_TRIAL ) {
84 num_trial++ ;
85 connection_status = connect( fSocketFd, (struct sockaddr * )(&server_address), sizeof( server_address ) ) ;
86 if( connection_status <0 )
87 {
88#if defined DEBUG_CLIENT_SERVER
89 Err("G4FRClientServer::ConnectUnix(),connect => RETRY");
90#endif
91 flag_connected = 0 ;
92 } else {
93 flag_connected = 1 ;
94 break ;
95 }
96
97 sleep(1);
98
99 } // while(connection_status...)
100
101 //----- return status of connection
102 return flag_connected ;
103
104} // G4FRClientServer::ConnectUnix()
105
106
107 //----- G4FRClientServer::Receive()
108void G4FRClientServer::Receive()
109{
110 //-----
111 ClearReceivedMessage () ;
112 if( recv( fSocketFd, fReceivedMessage, G4FRClientServer::RECV_BUFMAX , 0 ) < 0 )
113 {
114 Err("G4FRClientServer::Receive(), recv");
115 }
116
117#if defined DEBUG_CLIENT_SERVER
119 G4cout << ">>>>> receivedMessage = " << fReceivedMessage << G4endl;
120#endif
121
122}
123
124
125 //----- G4FRClientServer::ReceiveLine()
126void G4FRClientServer::ReceiveLine()
127{
128 //----- local
129 char buf[1];
130 int index = 0 ;
131
132 //----- receive a line (until newline)
133 memset(fReceivedMessage, '\0', RECV_BUFMAX) ;
134 while( read( fSocketFd, buf, 1 ) == 1 ) {
135 fReceivedMessage[index++] = buf[0];
136 if( IsEndOfLine(buf[0]) ) { break ;}
137 }
138} // G4FRClientServer::ReceiveLine()
139
140
141 //----- G4FRClientServer::Send()
142void G4FRClientServer::Send()
143{
144 if( send( fSocketFd, fSendingMessage, strlen(fSendingMessage) , 0 ) < 0 )
145 {
146 Err("G4FRClientServer::Send(), send");
147 }
148
149#if defined DEBUG_CLIENT_SERVER
151 G4cout << "<<<<< SentMessage = " << fSendingMessage << G4endl;
152#endif
153
154} // G4FRClientServer::Send()
155
156
157 //----- G4FRClientServer::Send( message )
158void G4FRClientServer::Send( const char* message )
159{
160 this->SetSendingMessage( message ) ;
161 this->Send();
162
163} // G4FRClientServer::Send( message )
164
165
166 //----- G4FRClientServer::SendLine()
167void G4FRClientServer::SendLine( const char* message )
168{
169 //----- local
170 int smsg_length ;
171
172 //----- set message to sending buf
173 this->SetSendingMessage( message ) ;
174 smsg_length = GetSendingMessageLength() ;
175
176 //----- add newline if necessary
177 if( !IsEndOfLine( fSendingMessage[ (smsg_length - 1)] ) ) {
178 fSendingMessage[ smsg_length ] = GetEndOfLine() ;
179 fSendingMessage[ (smsg_length +1) ] = '\0' ;
180 smsg_length = GetSendingMessageLength();
181 }
182
183 //----- send
184 this->Send();
185
186}// G4FRClientServer::SendLine()
187
188
189 //----- G4FRClientServer::DisConnect()
190void G4FRClientServer::DisConnect()
191{
192 //----- close connection
193 if( shutdown(fSocketFd,2) < 0 ) {
194 Err("G4FRClientServer::DisConnect,shutdown");
195 }
196 close( fSocketFd );
197
198 this->Clear();
199}
200
201
202
203 //----- G4FRClientServer::Clear()
204void G4FRClientServer::Clear()
205{
206 unlink(SUN_PATH) ;
207 fSocketFd = -1 ;
208}
209
210
211 //----- G4FRClientServer::ConnectINET()
212int G4FRClientServer::ConnectINET()
213{
214 //----- local
215 int flag_connected = 0 ;
216 sockaddr_in server_address ;
217 char server_hostname[32] ;
218 hostent* server_host_p ;
219
220 //----- make socket
221 fSocketFd = socket( AF_INET, SOCK_STREAM, 0 );
222 if( fSocketFd < 0 ) {
223#if defined DEBUG_CLIENT_SERVER
224 Err("G4FRClientServer::ConnectINET(),socket");
225#endif
226 }
227
228 //----- get IP address of server from its name
229 if( std::getenv( FR_ENV_SERVER_HOST_NAME ) != NULL )
230 {
231 //----- get server name
232 strcpy( server_hostname, std::getenv( FR_ENV_SERVER_HOST_NAME ) );
233
234 //----- get IP address of server from its name,
235 //..... reading /etc/hosts
236 server_host_p = gethostbyname( server_hostname ) ;
237
238 //----- If the host specified by FR_ENV_SERVER_HOST_NAME
239 //..... is not written in /etc/hosts,
240 //...... server host is set to the same as the
241 //...... client host
242 if( !server_host_p ) {
243#if defined DEBUG_CLIENT_SERVER
244 Err("G4FRClientServer::ConnectINET(), gethostbyname");
245#endif
246 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
247 }
248
249 } else {
250 server_host_p = gethostbyname( FR_DEFAULT_HOST_NAME ) ;
251 }
252
253
254
255// #if defined DEBUG_CLIENT_SERVER
257 G4cout << "***** 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()
297void 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
#define G4endl
Definition: G4ios.hh:57
G4GLOB_DLL std::ostream G4cout
static Verbosity GetVerbosity()
void Clear(Node *)