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