source: trunk/source/visualization/FukuiRenderer/src/G4FukuiRenderer.cc @ 834

Last change on this file since 834 was 834, checked in by garnier, 16 years ago

import all except CVS

File size: 7.4 KB
Line 
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,v 1.9 2006/06/29 21:16:58 gunter Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// Satoshi TANAKA
32// FukuiRenderer factory.
33
34//=================//
35#ifdef G4VIS_BUILD_DAWN_DRIVER
36//=================//
37
38//#define DEBUG_FR_SYSTEM
39
40
41#include "G4FukuiRenderer.hh"
42
43#define __G_ANSI_C__
44
45//#include "G4VisFeaturesOfFukuiRenderer.hh"
46#include "G4VSceneHandler.hh"
47#include "G4FukuiRendererSceneHandler.hh"
48#include "G4FukuiRendererViewer.hh"
49#include "G4FRClientServer.hh"
50#include "G4FRConst.hh"
51#include "G4FRFeatures.hh"
52
53        //----- constant
54const int FR_DEFAULT_CONNECTION_TIME = 5  ;
55const char FR_ENV_CONNECTION_TIME [] = "G4DAWN_CONNECTION_TIME";
56const char FR_ENV_DAWN_GUI_ALWAYS [] = "G4DAWN_GUI_ALWAYS";
57
58        //----- G4FukuiRenderer, constructor
59G4FukuiRenderer::G4FukuiRenderer ():
60  G4VGraphicsSystem ("FukuiRenderer",
61                     "DAWN",
62                     FR_DAWNFILE_FEATURES,
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
79G4FukuiRenderer::~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 ()
92G4VSceneHandler* 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 ()
102G4VViewer* 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()
111void G4FukuiRenderer::UseInetDomainAuto()
112{
113        int             pid ;
114
115#if defined DEBUG_FR_SYSTEM
116        G4cerr << "***** 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( getenv( FR_ENV_CONNECTION_TIME ) != NULL  ) {
130                        int sscanf_status = \
131                                sscanf( 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) { 
146          G4cerr << "***** ERROR: Connection failed" << G4endl; 
147        }
148        else { 
149          G4cerr << "***** GEANT4 is connected to FukuiRenderer DAWN ";
150          G4cerr << "in the same host" << G4endl; 
151        }
152
153} //  G4FukuiRenderer::UseInetDomainAuto()
154
155
156        //----- G4FukuiRenderer::UseInetDomain()
157void
158G4FukuiRenderer::UseInetDomain()
159{
160#if defined DEBUG_FR_SYSTEM
161        G4cerr << "***** INET Domain " << G4endl;
162#endif
163        fIPMode = G4FukuiRenderer::IP_INET ;
164
165        this->ConnectPort();
166
167        if(!flag_connected) {
168          G4cerr << "***** ERROR: Connection failed" << G4endl; 
169        }
170        else { 
171          G4cerr << "GEANT4 is connected to FukuiRenderer DAWN via socket" ; 
172          G4cerr << G4endl; 
173        }
174
175} // G4FukuiRenderer::UseInetDomain()
176
177void
178G4FukuiRenderer::ConnectPort( int max_port_incr )
179{
180        //----- establish connection
181  int connect_trial = 0 ;
182  while(1) {
183    if ( ++connect_trial > max_port_incr ) {
184        this->flag_connected = 0 ;
185        G4cerr << "***** INET Connection failed."                << G4endl;
186        G4cerr << "      Maybe, you have not invoked DAWN yet,"  << G4endl;
187        G4cerr << "      or too many DAWN's are already running" << G4endl;
188        G4cerr << "      in the server host."                    << G4endl;
189        fPrimDest.IncrementPortNumber( (- FR_MAX_PORT_INCR) );
190        return ;
191    } else if ( fPrimDest.ConnectINET() ) { 
192            // INET domain connection is established
193        this->flag_connected = 1 ;
194        G4cerr << "***** GEANT4 is connected to port  " ;
195        G4cerr << fPrimDest.GetPortNumber() ; 
196        G4cerr << "  of server" << G4endl;
197        break ; 
198    } else { 
199            // Connection failed. Try the next port.
200      this->flag_connected = 0 ;
201      fPrimDest.IncrementPortNumber();
202      G4cerr << "***** GEANT4 incremented targeting port to " ;
203      G4cerr << fPrimDest.GetPortNumber() << G4endl;
204
205    } // if-elseif-else
206
207  } // while (1)
208}
209
210
211        //----- G4FukuiRenderer::UseBSDUnixDomainAuto()
212void G4FukuiRenderer::UseBSDUnixDomainAuto()
213{
214        int     pid ;
215
216#if defined DEBUG_FR_SYSTEM
217        G4cerr << "***** UseBSDUnixDomainAuto " << G4endl;
218#endif
219        fIPMode = G4FukuiRenderer::IP_UNIX ; // Unix domain
220
221        if( ( pid = fork() ) == 0 ) { // child
222                if ( execlp ( "dawn", "dawn", "-g" , (char *)0 ) < 0 ) 
223                {
224                        perror("dawn") ;
225                }
226        } else { // parent
227
228                        //----- Sleep for a while to make sure that
229                        //..... FukuiRenderer is ready
230                int connection_time = FR_DEFAULT_CONNECTION_TIME  ;
231                if( getenv( FR_ENV_CONNECTION_TIME ) != NULL  ) {
232                        int sscanf_status = \
233                                sscanf( getenv( FR_ENV_CONNECTION_TIME ), "%d", &connection_time) ;
234                        if ( sscanf_status == EOF ) {  connection_time = FR_DEFAULT_CONNECTION_TIME  ;}
235                }
236                sleep(connection_time); 
237
238                        //----- connect GEANT4 to FukuiRenderer
239                this->flag_connected = fPrimDest.ConnectUnix();
240
241                        //----- display status
242                if(!flag_connected) {
243                  G4cerr << "***** ERROR: Connection failed" << G4endl; 
244                } else { 
245                  G4cerr << "*** GEANT4 is connected to FukuiRenderer DAWN ";
246                  G4cerr <<  "in the same host" << G4endl; 
247                }
248
249        } // if--else
250
251}// G4FukuiRenderer::UseBSDUnixDomainAuto()
252
253
254#endif // G4VIS_BUILD_DAWN_DRIVER
Note: See TracBrowser for help on using the repository browser.