G4FukuiRenderer.cc

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 //
00029 // 
00030 // Satoshi TANAKA
00031 // FukuiRenderer factory.
00032 
00033 //=================//
00034 #ifdef G4VIS_BUILD_DAWN_DRIVER
00035 //=================//
00036 
00037 //#define DEBUG_FR_SYSTEM
00038 
00039 
00040 #include "G4FukuiRenderer.hh"
00041 
00042 #define __G_ANSI_C__
00043 
00044 #include "G4VisManager.hh"
00045 //#include "G4VisFeaturesOfFukuiRenderer.hh"
00046 #include "G4VSceneHandler.hh"
00047 #include "G4FukuiRendererSceneHandler.hh"
00048 #include "G4FukuiRendererViewer.hh"
00049 #include "G4FRClientServer.hh"
00050 #include "G4FRConst.hh"
00051 #include "G4FRFeatures.hh"
00052 
00053         //----- constant
00054 const int FR_DEFAULT_CONNECTION_TIME = 5  ;
00055 const char FR_ENV_CONNECTION_TIME [] = "G4DAWN_CONNECTION_TIME";
00056 const char FR_ENV_DAWN_GUI_ALWAYS [] = "G4DAWN_GUI_ALWAYS";
00057 
00058         //----- G4FukuiRenderer, constructor
00059 G4FukuiRenderer::G4FukuiRenderer ():
00060   G4VGraphicsSystem ("FukuiRenderer",
00061                      "DAWN",
00062                      FR_DAWNFILE_FEATURES,
00063                      G4VGraphicsSystem::threeD),
00064   fPrimDest() ,
00065   fIPMode( G4FukuiRenderer::IP_UNIX ),
00066   flag_use_gui     (false) ,
00067   flag_connected   (0) 
00068 {
00069                 //----- sorting of objects
00070         if( ( getenv( FR_ENV_DAWN_GUI_ALWAYS ) != NULL        ) && \
00071             ( strcmp( getenv( FR_ENV_DAWN_GUI_ALWAYS ),"0"  ) )      ) 
00072         {
00073                 flag_use_gui =  true ;
00074         } 
00075 }
00076 
00077 
00078         //----- G4FukuiRenderer, destructor
00079 G4FukuiRenderer::~G4FukuiRenderer () 
00080 {
00081         if( flag_connected ) {
00082                         //----- Terminate FukuiRenderer
00083                 fPrimDest.SendLine( FR_TERMINATE_DAWND ) ; 
00084 
00085                         //----- disconnection
00086                 fPrimDest.DisConnect(); 
00087                 flag_connected = 0 ;
00088         }
00089 }
00090 
00091         //-----  G4FukuiRenderer::CreateSceneHandler () 
00092 G4VSceneHandler* G4FukuiRenderer::CreateSceneHandler (const G4String& name) 
00093 {
00094         G4VSceneHandler* p = new G4FukuiRendererSceneHandler (*this, name);
00095 
00096         if(!flag_connected) { delete p ;  p = NULL ; }
00097 
00098         return p;
00099 }
00100 
00101         //-----  G4FukuiRenderer::CreateViewer () 
00102 G4VViewer* G4FukuiRenderer::CreateViewer (G4VSceneHandler& scene, const G4String& name) 
00103 {
00104         if(!flag_connected) return NULL;
00105         G4VViewer* pView = 
00106           new G4FukuiRendererViewer ((G4FukuiRendererSceneHandler&) scene, name);
00107         return pView;
00108 }
00109 
00110         //----- G4FukuiRenderer::UseInetDomainAuto()
00111 void G4FukuiRenderer::UseInetDomainAuto()
00112 {
00113         int             pid ;
00114 
00115 #if defined DEBUG_FR_SYSTEM
00116         if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00117           G4cout << "***** Unix Inet Domain (AUTO mode)" << G4endl;
00118 #endif
00119         fIPMode = G4FukuiRenderer::IP_UNIX ;
00120 
00121         if( ( pid = fork() ) == 0 ) { // child
00122                 if ( execlp ( "dawn", "dawn", "-G" , (char *)0 ) < 0 ) 
00123                 {
00124                         perror("dawn") ;
00125                 }
00126         } else { // parent 
00127 
00128                         //----- Set waiting time to ensure connection
00129                 int connection_time = FR_DEFAULT_CONNECTION_TIME  ;
00130                 if( getenv( FR_ENV_CONNECTION_TIME ) != NULL  ) {
00131                         int sscanf_status = \
00132                                 sscanf( getenv( FR_ENV_CONNECTION_TIME ), "%d", &connection_time) ;
00133                         if ( sscanf_status == EOF ) {  
00134                           connection_time = FR_DEFAULT_CONNECTION_TIME  ;
00135                         }
00136                 }
00137 
00138 
00139                         //----- Wait for starting up of FukuiRenderer
00140                 sleep(connection_time); 
00141 
00142                         //----- establish connection
00143                 this->ConnectPort();
00144         }
00145 
00146         if(!flag_connected) { 
00147           if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00148             G4cout << "***** ERROR: Connection failed" << G4endl; 
00149         }
00150         else { 
00151           if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00152             G4cout << "***** GEANT4 is connected to FukuiRenderer DAWN ";
00153             G4cout << "in the same host" << G4endl; 
00154           }
00155         }
00156 
00157 } //  G4FukuiRenderer::UseInetDomainAuto()
00158 
00159 
00160         //----- G4FukuiRenderer::UseInetDomain()
00161 void
00162 G4FukuiRenderer::UseInetDomain()
00163 {
00164 #if defined DEBUG_FR_SYSTEM
00165   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00166         G4cout << "***** INET Domain " << G4endl;
00167 #endif
00168         fIPMode = G4FukuiRenderer::IP_INET ;
00169 
00170         this->ConnectPort();
00171 
00172         if(!flag_connected) {
00173           if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00174             G4cout << "***** ERROR: Connection failed" << G4endl; 
00175         }
00176         else { 
00177           if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00178             G4cout << "GEANT4 is connected to FukuiRenderer DAWN via socket" ; 
00179             G4cout << G4endl; 
00180           }
00181         }
00182 
00183 } // G4FukuiRenderer::UseInetDomain()
00184 
00185 void
00186 G4FukuiRenderer::ConnectPort( int max_port_incr )
00187 {
00188         //----- establish connection
00189   int connect_trial = 0 ;
00190   while(1) {
00191     if ( ++connect_trial > max_port_incr ) {
00192         this->flag_connected = 0 ;
00193         if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00194           G4cout << "***** INET Connection failed."                << G4endl;
00195           G4cout << "      Maybe, you have not invoked DAWN yet,"  << G4endl;
00196           G4cout << "      or too many DAWN's are already running" << G4endl;
00197           G4cout << "      in the server host."                    << G4endl;
00198         }
00199         fPrimDest.IncrementPortNumber( (- FR_MAX_PORT_INCR) );
00200         return ;
00201     } else if ( fPrimDest.ConnectINET() ) { 
00202             // INET domain connection is established
00203         this->flag_connected = 1 ;
00204         if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00205           G4cout << "***** GEANT4 is connected to port  " ;
00206           G4cout << fPrimDest.GetPortNumber() ; 
00207           G4cout << "  of server" << G4endl;
00208         }
00209         break ; 
00210     } else { 
00211             // Connection failed. Try the next port.
00212       this->flag_connected = 0 ;
00213       fPrimDest.IncrementPortNumber();
00214       if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00215         G4cout << "***** GEANT4 incremented targeting port to " ;
00216         G4cout << fPrimDest.GetPortNumber() << G4endl;
00217       }
00218 
00219     } // if-elseif-else
00220 
00221   } // while (1) 
00222 }
00223 
00224 
00225         //----- G4FukuiRenderer::UseBSDUnixDomainAuto()
00226 void G4FukuiRenderer::UseBSDUnixDomainAuto()
00227 {
00228         int     pid ;
00229 
00230 #if defined DEBUG_FR_SYSTEM
00231         if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00232           G4cout << "***** UseBSDUnixDomainAuto " << G4endl;
00233 #endif
00234         fIPMode = G4FukuiRenderer::IP_UNIX ; // Unix domain
00235 
00236         if( ( pid = fork() ) == 0 ) { // child
00237                 if ( execlp ( "dawn", "dawn", "-g" , (char *)0 ) < 0 ) 
00238                 {
00239                         perror("dawn") ;
00240                 }
00241         } else { // parent 
00242 
00243                         //----- Sleep for a while to make sure that 
00244                         //..... FukuiRenderer is ready
00245                 int connection_time = FR_DEFAULT_CONNECTION_TIME  ;
00246                 if( getenv( FR_ENV_CONNECTION_TIME ) != NULL  ) {
00247                         int sscanf_status = \
00248                                 sscanf( getenv( FR_ENV_CONNECTION_TIME ), "%d", &connection_time) ;
00249                         if ( sscanf_status == EOF ) {  connection_time = FR_DEFAULT_CONNECTION_TIME  ;}
00250                 }
00251                 sleep(connection_time); 
00252 
00253                         //----- connect GEANT4 to FukuiRenderer
00254                 this->flag_connected = fPrimDest.ConnectUnix();
00255 
00256                         //----- display status
00257                 if(!flag_connected) {
00258                   if (G4VisManager::GetVerbosity() >= G4VisManager::errors)
00259                     G4cout << "***** ERROR: Connection failed" << G4endl; 
00260                 } else { 
00261                   if (G4VisManager::GetVerbosity() >= G4VisManager::errors) {
00262                     G4cout << "*** GEANT4 is connected to FukuiRenderer DAWN ";
00263                     G4cout <<  "in the same host" << G4endl; 
00264                   }
00265                 }
00266 
00267         } // if--else
00268 
00269 }// G4FukuiRenderer::UseBSDUnixDomainAuto()
00270 
00271 
00272 #endif // G4VIS_BUILD_DAWN_DRIVER

Generated on Mon May 27 17:48:18 2013 for Geant4 by  doxygen 1.4.7