37 public static void main( String[] args )
41 final String VERSION =
"1.00" ;
42 final String
DATE =
"August 19, 1997";
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 ;
50 int portNo = PORT_NO ;
53 if( args.length != 1 && args.length != 2 )
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");
65 String browser =
new String ( args[0] ) ;
68 if( args.length == 2 )
70 portNo = Integer.parseInt( args[1] );
74 ServerSocket ss = null ;
75 for (
int i = 0 ; i < MAX_TRIAL ; i++ )
79 ss =
new ServerSocket( portNo );
80 System.out.println(
"Waiting for requests at port " +portNo +
" ...");
88 System.out.println(
"Sockets are not available.");
99 Socket
socket = ss.accept(); nSpawn++ ;
101 System.out.println(
"Connection accepted by thread " + nSpawn );
103 (
new g4vrmlviewThread( socket, OUTPUT_FILE_HEAD, OUTPUT_FILE_EXT , browser )).
start() ;
108 catch ( Exception
e )
110 System.out.println( e.toString() );
120 class g4vrmlviewThread
extends Thread
122 private final String
NONE =
"NONE" ;
123 private Socket m_socket ;
124 private String m_outputFile ;
125 private String m_browser ;
128 public g4vrmlviewThread( Socket
socket ,
129 String outputFileHead ,
130 String outputFileExt ,
134 SetOutputFileName ( outputFileHead , outputFileExt );
135 m_browser =
new String ( browser );
138 private void SetOutputFileName( String outputFileHead,
139 String outputFileExt )
142 String outputFile_tmp
143 =
new String ( outputFileHead +
153 File
file =
new File ( outputFile_tmp );
155 if ( !file.exists() )
161 =
new String ( outputFileHead +
171 m_outputFile =
new String ( outputFile_tmp );
182 =
new BufferedReader (
new InputStreamReader ( m_socket.getInputStream() ) ) ;
186 =
new BufferedWriter (
new FileWriter ( m_outputFile ) ) ;
190 while ( (line = br.readLine()) != null )
196 System.out.println(
"VRML data is saved to file " +m_outputFile );
204 if( !m_browser.equals(
NONE) )
209 File file =
new File ( m_outputFile );
214 String outputFileAbs =
new String ( file.getAbsolutePath() ) ;
215 String[] command = { m_browser, outputFileAbs };
216 System.out.println(
"Command: " + command[0] +
" " + command[1] );
218 Process exec_process = null ;
220 Runtime runtime = Runtime.getRuntime();
221 exec_process = runtime.exec( command );
222 exec_process.waitFor() ;
224 System.out.println(
"Error: Failed to open file" + m_outputFile );
227 catch ( Exception
e )
229 System.out.println( e.toString() );
235 System.out.println(
"No browser was invoked" );
241 System.out.println( e.toString() );