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