| [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 | //
|
|---|
| 27 | // $Id: g4vrmlview.java,v 1.4 2006/06/29 21:25:17 gunter Exp $
|
|---|
| 28 | // GEANT4 tag $Name: $
|
|---|
| 29 | //
|
|---|
| 30 | import java.io.*;
|
|---|
| 31 | import java.net.*;
|
|---|
| 32 |
|
|---|
| 33 | //------------------//
|
|---|
| 34 | // class g4vrmlview //
|
|---|
| 35 | // ( main() ) //
|
|---|
| 36 | //------------------//
|
|---|
| 37 | public class g4vrmlview
|
|---|
| 38 | {
|
|---|
| 39 | public static void main( String[] args )
|
|---|
| 40 | {
|
|---|
| 41 | try{
|
|---|
| 42 | // CONST
|
|---|
| 43 | final String VERSION = "1.00" ;
|
|---|
| 44 | final String DATE = "August 19, 1997";
|
|---|
| 45 |
|
|---|
| 46 | final int PORT_NO = 40801 ;
|
|---|
| 47 | final String OUTPUT_FILE_HEAD = "g4" ;
|
|---|
| 48 | final String OUTPUT_FILE_EXT = "wrl" ;
|
|---|
| 49 | final int MAX_TRIAL = 10 ;
|
|---|
| 50 |
|
|---|
| 51 | // local
|
|---|
| 52 | int portNo = PORT_NO ;
|
|---|
| 53 |
|
|---|
| 54 | // argument checking
|
|---|
| 55 | if( args.length != 1 && args.length != 2 )
|
|---|
| 56 | {
|
|---|
| 57 | System.out.println( "-------------------------------");
|
|---|
| 58 | System.out.println( " G4VRMLView version " + VERSION );
|
|---|
| 59 | System.out.println( " " + DATE );
|
|---|
| 60 | System.out.println( "-------------------------------");
|
|---|
| 61 | System.out.println( "Usage: java g4vrmlview browser_name [port_number]");
|
|---|
| 62 | System.out.println( " Browser_name: netscape, vrweb, etc, or NONE");
|
|---|
| 63 | return ;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | // VRML browser
|
|---|
| 67 | String browser = new String ( args[0] ) ;
|
|---|
| 68 |
|
|---|
| 69 | // port number
|
|---|
| 70 | if( args.length == 2 )
|
|---|
| 71 | {
|
|---|
| 72 | portNo = Integer.parseInt( args[1] );
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | // make a server socket
|
|---|
| 76 | ServerSocket ss = null ;
|
|---|
| 77 | for ( int i = 0 ; i < MAX_TRIAL ; i++ )
|
|---|
| 78 | {
|
|---|
| 79 | try
|
|---|
| 80 | {
|
|---|
| 81 | ss = new ServerSocket( portNo );
|
|---|
| 82 | System.out.println( "Waiting for requests at port " +portNo + " ...");
|
|---|
| 83 | break ;
|
|---|
| 84 | }
|
|---|
| 85 | catch ( Exception e )
|
|---|
| 86 | {
|
|---|
| 87 | portNo++ ;
|
|---|
| 88 | if( i >= MAX_TRIAL )
|
|---|
| 89 | {
|
|---|
| 90 | System.out.println( "Sockets are not available.");
|
|---|
| 91 | return ;
|
|---|
| 92 | }
|
|---|
| 93 | }
|
|---|
| 94 | } // for
|
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 | // open connection and invoke thread
|
|---|
| 98 | int nSpawn = 0 ;
|
|---|
| 99 | while( true )
|
|---|
| 100 | {
|
|---|
| 101 | Socket socket = ss.accept(); nSpawn++ ;
|
|---|
| 102 |
|
|---|
| 103 | System.out.println( "Connection accepted by thread " + nSpawn );
|
|---|
| 104 |
|
|---|
| 105 | ( new g4vrmlviewThread( socket, OUTPUT_FILE_HEAD, OUTPUT_FILE_EXT , browser )).start() ;
|
|---|
| 106 |
|
|---|
| 107 | } // while
|
|---|
| 108 |
|
|---|
| 109 | }
|
|---|
| 110 | catch ( Exception e )
|
|---|
| 111 | {
|
|---|
| 112 | System.out.println( e.toString() );
|
|---|
| 113 | }
|
|---|
| 114 | } // main()
|
|---|
| 115 |
|
|---|
| 116 | } // g4vrmlview
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 | //------------------------//
|
|---|
| 120 | // class g4vrmlviewThread //
|
|---|
| 121 | //------------------------//
|
|---|
| 122 | class g4vrmlviewThread extends Thread
|
|---|
| 123 | {
|
|---|
| 124 | private final String NONE = "NONE" ; // no browser
|
|---|
| 125 | private Socket m_socket ;
|
|---|
| 126 | private String m_outputFile ;
|
|---|
| 127 | private String m_browser ;
|
|---|
| 128 |
|
|---|
| 129 | // constuctor
|
|---|
| 130 | public g4vrmlviewThread( Socket socket ,
|
|---|
| 131 | String outputFileHead ,
|
|---|
| 132 | String outputFileExt ,
|
|---|
| 133 | String browser )
|
|---|
| 134 | {
|
|---|
| 135 | m_socket = socket ;
|
|---|
| 136 | SetOutputFileName ( outputFileHead , outputFileExt );
|
|---|
| 137 | m_browser = new String ( browser );
|
|---|
| 138 | }
|
|---|
| 139 |
|
|---|
| 140 | private void SetOutputFileName( String outputFileHead,
|
|---|
| 141 | String outputFileExt )
|
|---|
| 142 | {
|
|---|
| 143 | // temporary file name
|
|---|
| 144 | String outputFile_tmp
|
|---|
| 145 | = new String ( outputFileHead +
|
|---|
| 146 | "." +
|
|---|
| 147 | outputFileExt ) ;
|
|---|
| 148 |
|
|---|
| 149 | // for modification of temporary filename
|
|---|
| 150 | int n = 1 ;
|
|---|
| 151 |
|
|---|
| 152 | // make a non-existing filename
|
|---|
| 153 | while ( true )
|
|---|
| 154 | {
|
|---|
| 155 | File file = new File ( outputFile_tmp );
|
|---|
| 156 |
|
|---|
| 157 | if ( !file.exists() )
|
|---|
| 158 | {
|
|---|
| 159 | break ;
|
|---|
| 160 | } else {
|
|---|
| 161 |
|
|---|
| 162 | outputFile_tmp
|
|---|
| 163 | = new String ( outputFileHead +
|
|---|
| 164 | "_" +
|
|---|
| 165 | (n++) +
|
|---|
| 166 | "." +
|
|---|
| 167 | outputFileExt );
|
|---|
| 168 | }
|
|---|
| 169 |
|
|---|
| 170 | } // while
|
|---|
| 171 |
|
|---|
| 172 | // set decided filename to data field
|
|---|
| 173 | m_outputFile = new String ( outputFile_tmp );
|
|---|
| 174 |
|
|---|
| 175 | } // g4vrmlviewThread::setOutputFileName()
|
|---|
| 176 |
|
|---|
| 177 |
|
|---|
| 178 | // run ()
|
|---|
| 179 | public void run ()
|
|---|
| 180 | {
|
|---|
| 181 | try{
|
|---|
| 182 | // get input stream from socket
|
|---|
| 183 | BufferedReader br
|
|---|
| 184 | = new BufferedReader ( new InputStreamReader ( m_socket.getInputStream() ) ) ;
|
|---|
| 185 |
|
|---|
| 186 | // get output stream to file
|
|---|
| 187 | BufferedWriter bw
|
|---|
| 188 | = new BufferedWriter ( new FileWriter ( m_outputFile ) ) ;
|
|---|
| 189 |
|
|---|
| 190 | // socket ==> file
|
|---|
| 191 | String line ;
|
|---|
| 192 | while ( (line = br.readLine()) != null )
|
|---|
| 193 | {
|
|---|
| 194 | bw.write( line );
|
|---|
| 195 | bw.newLine() ;
|
|---|
| 196 | bw.flush () ;
|
|---|
| 197 | }
|
|---|
| 198 | System.out.println( "VRML data is saved to file " +m_outputFile );
|
|---|
| 199 |
|
|---|
| 200 | // close streams and socket
|
|---|
| 201 | br.close();
|
|---|
| 202 | bw.close();
|
|---|
| 203 | m_socket.close() ;
|
|---|
| 204 |
|
|---|
| 205 | // invoke browser
|
|---|
| 206 | if( !m_browser.equals(NONE) )
|
|---|
| 207 | {
|
|---|
| 208 | try
|
|---|
| 209 | {
|
|---|
| 210 |
|
|---|
| 211 | File file = new File ( m_outputFile );
|
|---|
| 212 |
|
|---|
| 213 | // visualize created VRML file with browser
|
|---|
| 214 | if ( file.exists() )
|
|---|
| 215 | {
|
|---|
| 216 | String outputFileAbs = new String ( file.getAbsolutePath() ) ;
|
|---|
| 217 | String[] command = { m_browser, outputFileAbs };
|
|---|
| 218 | System.out.println( "Command: " + command[0] + " " + command[1] );
|
|---|
| 219 |
|
|---|
| 220 | Process exec_process = null ;
|
|---|
| 221 |
|
|---|
| 222 | Runtime runtime = Runtime.getRuntime();
|
|---|
| 223 | exec_process = runtime.exec( command );
|
|---|
| 224 | exec_process.waitFor() ;
|
|---|
| 225 | } else {
|
|---|
| 226 | System.out.println( "Error: Failed to open file" + m_outputFile );
|
|---|
| 227 | }
|
|---|
| 228 | }
|
|---|
| 229 | catch ( Exception e )
|
|---|
| 230 | {
|
|---|
| 231 | System.out.println( e.toString() );
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | }
|
|---|
| 235 | else
|
|---|
| 236 | {
|
|---|
| 237 | System.out.println( "No browser was invoked" );
|
|---|
| 238 | }
|
|---|
| 239 |
|
|---|
| 240 | }
|
|---|
| 241 | catch( Exception e )
|
|---|
| 242 | {
|
|---|
| 243 | System.out.println( e.toString() );
|
|---|
| 244 | }
|
|---|
| 245 | } // run()
|
|---|
| 246 |
|
|---|
| 247 | } // g4vrmlviewThread
|
|---|