Geant4  10.00.p03
G4FukuiRenderer.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: G4FukuiRenderer.cc 66870 2013-01-14 23:38:59Z adotti $
28 //
29 //
30 // Satoshi TANAKA
31 // FukuiRenderer factory.
32 
33 //=================//
34 #ifdef G4VIS_BUILD_DAWN_DRIVER
35 //=================//
36 
37 //#define DEBUG_FR_SYSTEM
38 
39 
40 #include "G4FukuiRenderer.hh"
41 
42 #define __G_ANSI_C__
43 
44 #include "G4VisManager.hh"
45 //#include "G4VisFeaturesOfFukuiRenderer.hh"
46 #include "G4VSceneHandler.hh"
48 #include "G4FukuiRendererViewer.hh"
49 #include "G4FRClientServer.hh"
50 #include "G4FRConst.hh"
51 #include "G4FRFeatures.hh"
52 
53  //----- constant
54 const int FR_DEFAULT_CONNECTION_TIME = 5 ;
55 const char FR_ENV_CONNECTION_TIME [] = "G4DAWN_CONNECTION_TIME";
56 const char FR_ENV_DAWN_GUI_ALWAYS [] = "G4DAWN_GUI_ALWAYS";
57 
58  //----- G4FukuiRenderer, constructor
59 G4FukuiRenderer::G4FukuiRenderer ():
60  G4VGraphicsSystem ("FukuiRenderer",
61  "DAWN",
63  G4VGraphicsSystem::threeD),
64  fPrimDest() ,
65  fIPMode( G4FukuiRenderer::IP_UNIX ),
66  flag_use_gui (false) ,
67  flag_connected (0)
68 {
69  //----- sorting of objects
70  if( ( getenv( FR_ENV_DAWN_GUI_ALWAYS ) != NULL ) && \
71  ( strcmp( getenv( FR_ENV_DAWN_GUI_ALWAYS ),"0" ) ) )
72  {
73  flag_use_gui = true ;
74  }
75 }
76 
77 
78  //----- G4FukuiRenderer, destructor
79 G4FukuiRenderer::~G4FukuiRenderer ()
80 {
81  if( flag_connected ) {
82  //----- Terminate FukuiRenderer
83  fPrimDest.SendLine( FR_TERMINATE_DAWND ) ;
84 
85  //----- disconnection
86  fPrimDest.DisConnect();
87  flag_connected = 0 ;
88  }
89 }
90 
91  //----- G4FukuiRenderer::CreateSceneHandler ()
92 G4VSceneHandler* G4FukuiRenderer::CreateSceneHandler (const G4String& name)
93 {
94  G4VSceneHandler* p = new G4FukuiRendererSceneHandler (*this, name);
95 
96  if(!flag_connected) { delete p ; p = NULL ; }
97 
98  return p;
99 }
100 
101  //----- G4FukuiRenderer::CreateViewer ()
102 G4VViewer* G4FukuiRenderer::CreateViewer (G4VSceneHandler& scene, const G4String& name)
103 {
104  if(!flag_connected) return NULL;
105  G4VViewer* pView =
106  new G4FukuiRendererViewer ((G4FukuiRendererSceneHandler&) scene, name);
107  return pView;
108 }
109 
110  //----- G4FukuiRenderer::UseInetDomainAuto()
111 void G4FukuiRenderer::UseInetDomainAuto()
112 {
113  int pid ;
114 
115 #if defined DEBUG_FR_SYSTEM
117  G4cout << "***** Unix Inet Domain (AUTO mode)" << G4endl;
118 #endif
119  fIPMode = G4FukuiRenderer::IP_UNIX ;
120 
121  if( ( pid = fork() ) == 0 ) { // child
122  if ( execlp ( "dawn", "dawn", "-G" , (char *)0 ) < 0 )
123  {
124  perror("dawn") ;
125  }
126  } else { // parent
127 
128  //----- Set waiting time to ensure connection
129  int connection_time = FR_DEFAULT_CONNECTION_TIME ;
130  if( getenv( FR_ENV_CONNECTION_TIME ) != NULL ) {
131  int sscanf_status = \
132  sscanf( getenv( FR_ENV_CONNECTION_TIME ), "%d", &connection_time) ;
133  if ( sscanf_status == EOF ) {
134  connection_time = FR_DEFAULT_CONNECTION_TIME ;
135  }
136  }
137 
138 
139  //----- Wait for starting up of FukuiRenderer
140  sleep(connection_time);
141 
142  //----- establish connection
143  this->ConnectPort();
144  }
145 
146  if(!flag_connected) {
148  G4cout << "***** ERROR: Connection failed" << G4endl;
149  }
150  else {
152  G4cout << "***** GEANT4 is connected to FukuiRenderer DAWN ";
153  G4cout << "in the same host" << G4endl;
154  }
155  }
156 
157 } // G4FukuiRenderer::UseInetDomainAuto()
158 
159 
160  //----- G4FukuiRenderer::UseInetDomain()
161 void
162 G4FukuiRenderer::UseInetDomain()
163 {
164 #if defined DEBUG_FR_SYSTEM
166  G4cout << "***** INET Domain " << G4endl;
167 #endif
168  fIPMode = G4FukuiRenderer::IP_INET ;
169 
170  this->ConnectPort();
171 
172  if(!flag_connected) {
174  G4cout << "***** ERROR: Connection failed" << G4endl;
175  }
176  else {
178  G4cout << "GEANT4 is connected to FukuiRenderer DAWN via socket" ;
179  G4cout << G4endl;
180  }
181  }
182 
183 } // G4FukuiRenderer::UseInetDomain()
184 
185 void
186 G4FukuiRenderer::ConnectPort( int max_port_incr )
187 {
188  //----- establish connection
189  int connect_trial = 0 ;
190  while(1) {
191  if ( ++connect_trial > max_port_incr ) {
192  this->flag_connected = 0 ;
194  G4cout << "***** INET Connection failed." << G4endl;
195  G4cout << " Maybe, you have not invoked DAWN yet," << G4endl;
196  G4cout << " or too many DAWN's are already running" << G4endl;
197  G4cout << " in the server host." << G4endl;
198  }
199  fPrimDest.IncrementPortNumber( (- FR_MAX_PORT_INCR) );
200  return ;
201  } else if ( fPrimDest.ConnectINET() ) {
202  // INET domain connection is established
203  this->flag_connected = 1 ;
205  G4cout << "***** GEANT4 is connected to port " ;
206  G4cout << fPrimDest.GetPortNumber() ;
207  G4cout << " of server" << G4endl;
208  }
209  break ;
210  } else {
211  // Connection failed. Try the next port.
212  this->flag_connected = 0 ;
213  fPrimDest.IncrementPortNumber();
215  G4cout << "***** GEANT4 incremented targeting port to " ;
216  G4cout << fPrimDest.GetPortNumber() << G4endl;
217  }
218 
219  } // if-elseif-else
220 
221  } // while (1)
222 }
223 
224 
225  //----- G4FukuiRenderer::UseBSDUnixDomainAuto()
226 void G4FukuiRenderer::UseBSDUnixDomainAuto()
227 {
228  int pid ;
229 
230 #if defined DEBUG_FR_SYSTEM
232  G4cout << "***** UseBSDUnixDomainAuto " << G4endl;
233 #endif
234  fIPMode = G4FukuiRenderer::IP_UNIX ; // Unix domain
235 
236  if( ( pid = fork() ) == 0 ) { // child
237  if ( execlp ( "dawn", "dawn", "-g" , (char *)0 ) < 0 )
238  {
239  perror("dawn") ;
240  }
241  } else { // parent
242 
243  //----- Sleep for a while to make sure that
244  //..... FukuiRenderer is ready
245  int connection_time = FR_DEFAULT_CONNECTION_TIME ;
246  if( getenv( FR_ENV_CONNECTION_TIME ) != NULL ) {
247  int sscanf_status = \
248  sscanf( getenv( FR_ENV_CONNECTION_TIME ), "%d", &connection_time) ;
249  if ( sscanf_status == EOF ) { connection_time = FR_DEFAULT_CONNECTION_TIME ;}
250  }
251  sleep(connection_time);
252 
253  //----- connect GEANT4 to FukuiRenderer
254  this->flag_connected = fPrimDest.ConnectUnix();
255 
256  //----- display status
257  if(!flag_connected) {
259  G4cout << "***** ERROR: Connection failed" << G4endl;
260  } else {
262  G4cout << "*** GEANT4 is connected to FukuiRenderer DAWN ";
263  G4cout << "in the same host" << G4endl;
264  }
265  }
266 
267  } // if--else
268 
269 }// G4FukuiRenderer::UseBSDUnixDomainAuto()
270 
271 
272 #endif // G4VIS_BUILD_DAWN_DRIVER
const char FR_TERMINATE_DAWND[]
Definition: G4FRConst.hh:52
G4String name
Definition: TRTMaterials.hh:40
G4GLOB_DLL std::ostream G4cout
const char FR_DAWNFILE_FEATURES[]
Definition: G4FRFeatures.hh:46
static Verbosity GetVerbosity()
#define G4endl
Definition: G4ios.hh:61