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