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