/** * VECR IO Applet * * R. Perry, Oct. 2004 */ import java.io.*; import java.net.*; import java.awt.*; import java.awt.event.*; import java.applet.*; public class IOapplet extends Applet implements Runnable, ActionListener { // IO protocol // static final byte DATA = 0; static final byte EOF = 1; static final byte STOP = 2; static final byte SAVE = 3; // GUI // Button bEOF, bSAVE, bSTOP, bRESTART, bPFONT, bMFONT; TextArea diag, output; TextField input; Thread listener; Font font; String font_name; int font_style; int font_size; // network // String host; Socket s; BufferedInputStream in; DataOutputStream out; String newline; // "\n" ? public void init() { newline = System.getProperty("line.separator"); host = getCodeBase().getHost(); diag = new TextArea( "", 13, 60); diag.setEditable( false); diag.setBackground( Color.white); output = new TextArea( ""); output.setEditable( false); output.setBackground( Color.white); input = new TextField(); bEOF = new Button( "EOF"); bSAVE = new Button( "Save"); bSTOP = new Button( "Stop"); bRESTART = new Button( "Restart"); bPFONT = new Button( "++Font"); bMFONT = new Button( "--Font"); GridBagLayout gridBag = new GridBagLayout(); setLayout( gridBag); GridBagConstraints c = new GridBagConstraints(); c.fill = GridBagConstraints.HORIZONTAL; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 0.0; gridBag.setConstraints( diag, c); add( diag); c.fill = GridBagConstraints.BOTH; c.gridwidth = GridBagConstraints.REMAINDER; c.weightx = 1.0; c.weighty = 1.0; gridBag.setConstraints( output, c); add( output); c.fill = GridBagConstraints.HORIZONTAL; c.weighty = 0.0; gridBag.setConstraints( input, c); add( input); c.gridwidth = 1; gridBag.setConstraints( bEOF, c); add( bEOF); gridBag.setConstraints( bSTOP, c); add( bSTOP); gridBag.setConstraints( bRESTART, c); add( bRESTART); gridBag.setConstraints( bPFONT, c); add( bPFONT); gridBag.setConstraints( bMFONT, c); add( bMFONT); gridBag.setConstraints( bSAVE, c); add( bSAVE); validate(); font = output.getFont(); // font_name = font.getName(); font_name = "Monospaced"; font_style = font.getStyle(); font_size = font.getSize(); font = new Font( font_name, font_style, font_size); diag.setFont( font); output.setFont( font); // repaint(); input.addActionListener( this); bSAVE.addActionListener(this); bEOF.addActionListener(this); bSTOP.addActionListener(this); bRESTART.addActionListener(this); bPFONT.addActionListener(this); bMFONT.addActionListener(this); input.requestFocus(); } public void start() { bEOF.setEnabled( true); bSTOP.setEnabled( true); bSAVE.setEnabled( true); bRESTART.setEnabled( true); bPFONT.setEnabled( true); bMFONT.setEnabled( true); input.setEnabled( true); input.requestFocus(); try { int port = 80; // // int port = getCodeBase().getPort(); -- only if a port was specified in the URL // int defaultport = getCodeBase().getDefaultPort(); -- only in newer JREs diag.append( "* Connecting to " + host + ":" + port + newline); diag.setCaretPosition(diag.getText().length()); s = new Socket( host, port); s.setSoTimeout( 1800000); // 30 minutes in = new BufferedInputStream( s.getInputStream()); out = new DataOutputStream( new BufferedOutputStream( s.getOutputStream())); // initialize the proxy connection // String connect = "CONNECT localhost:7\n"; diag.append( "* Sending proxy request: "); diag.append( connect); diag.setCaretPosition(diag.getText().length()); out.writeBytes( connect); out.flush(); read_diag(); // send authentication data // String user = getParameter( "user"); diag.append( "* Sending authentication data for "); diag.append( user); diag.append( newline); diag.setCaretPosition(diag.getText().length()); out.writeUTF( user); out.writeUTF( getParameter( "uid")); out.writeUTF( getParameter( "time")); out.writeUTF( getParameter( "ticket")); out.writeUTF( getParameter( "dir")); out.writeUTF( getParameter( "prog")); out.writeUTF( getParameter( "args")); out.flush(); // start listening // listener = new Thread( this); listener.start(); // invokes run() } catch (Exception ex) { ByteArrayOutputStream error = new ByteArrayOutputStream(); ex.printStackTrace( new PrintStream( error)); diag.append( error.toString()); diag.setCaretPosition(diag.getText().length()); } } public void close_all() { if( listener != null) try { listener.interrupt(); listener = null; } catch (Exception ex) { } if( in != null) try { in.close(); in = null; } catch (Exception ex) { } if( out != null) try { out.close(); out = null; } catch (Exception ex) { } if( s != null) try { s.close(); s = null; } catch (Exception ex) { } } public void stop() { if( s != null) { diag.append( "* Disconnecting"); diag.append( newline); diag.setCaretPosition(diag.getText().length()); } bEOF.setEnabled( false); bSTOP.setEnabled( false); bSAVE.setEnabled( false); input.setEnabled( false); close_all(); } // read initial connection diagnostics // public void read_diag() throws Exception { int n; int count = 0; byte buf[] = new byte[2048]; while( in.available() == 0) { if( ++count > 50 ) throw new Exception( "No response from server"); Thread.sleep(100); } do { n = in.read( buf); if( n <= 0) break; diag.append( new String( buf, 0, n) ); diag.setCaretPosition(diag.getText().length()); // Thread.yield(); } while( in.available() > 0); } public void run() { int n; byte buf[] = new byte[2048]; try { while( true) { n = in.read( buf); if( n <= 0) break; output.append( new String( buf, 0, n) ); output.setCaretPosition(output.getText().length()); input.requestFocus(); } } catch (Exception ex) { } } // handle action events // public void actionPerformed( ActionEvent e) { Object source = e.getSource(); try { if( source == bRESTART) { stop(); diag.append( "* Restarting"); diag.append( newline); diag.setCaretPosition(diag.getText().length()); start(); } else if( source == bPFONT) { int new_font_size = font_size + 4; diag.append( "* Changing font size from " + font_size + " to " + new_font_size); diag.append( newline); font = new Font( font_name, font_style, new_font_size); font_size = new_font_size; diag.setFont( font); diag.setCaretPosition(diag.getText().length()); output.setFont( font); output.setCaretPosition(output.getText().length()); } else if( source == bMFONT) { int new_font_size = font_size - 4; if( new_font_size < 4) { diag.append( "* Sorry, new font size would be too small"); diag.append( newline); diag.setCaretPosition(diag.getText().length()); } else { diag.append( "* Changing font size from " + font_size + " to " + new_font_size); diag.append( newline); font = new Font( font_name, font_style, new_font_size); font_size = new_font_size; diag.setFont( font); diag.setCaretPosition(diag.getText().length()); output.setFont( font); output.setCaretPosition(output.getText().length()); } } else if( listener != null) { if (source == input) // for input TextField action events { String line = input.getText(); input.setText(""); output.append( line); output.append( newline); output.setCaretPosition(output.getText().length()); out.writeByte( DATA); out.writeUTF( line); out.flush(); } else if( source == bEOF) { diag.append( "* Sending EOF"); diag.append( newline); diag.setCaretPosition(diag.getText().length()); out.writeByte( EOF); out.flush(); } else if( source == bSTOP) { out.writeByte( STOP); out.flush(); Thread.sleep(100); // wait for server response stop(); } else if (source == bSAVE) { diag.append( "* Saving output.txt on server"); diag.append( newline); diag.setCaretPosition(diag.getText().length()); out.writeByte( SAVE); out.writeUTF( output.getText()); out.flush(); } } // if( listener != null) input.requestFocus(); } catch (Exception ex) { ByteArrayOutputStream error = new ByteArrayOutputStream(); ex.printStackTrace( new PrintStream( error)); diag.append( error.toString()); diag.setCaretPosition(diag.getText().length()); } } }