// // WaveTest.java Steve Lang & Jim Phillips // // This example creates a simple interface in which you can type // Wave commands and see the text and graphic output produced by // Wave. This is accomplished by communicating with a Wave server // running on the machine which is also serving the Java applet // (e.g. the web server machine). A CGI script is used as the // method of communication between the applet and the Wave server // because this will work reliably across firewalls, where a // direct socket connection between the applet and Wave might not. // import java.io.*; import java.awt.*; import java.applet.*; import java.awt.image.*; import java.net.*; import sun.awt.image.URLImageSource; // // The WaveTest Applet // public class WaveTest extends Applet { WaveControls controls; public void init() { // // Create a canvas for Wave graphics and center // it by placing an empty label on the left // (inelegant but does the trick). // setLayout(new BorderLayout()); WaveCanvas c = new WaveCanvas(this); add("West",new Label(" ")); add("Center", c); // // Add the text input and output areas // add("South", controls = new WaveControls(c,this)); } public void start() { controls.enable(); } public void stop() { controls.disable(); } public boolean handleEvent(Event e) { if (e.id == Event.WINDOW_DESTROY) { System.exit(0); } return false; } public static void main(String args[]) { Frame f = new Frame("WaveTest"); WaveTest waveTest = new WaveTest(); f.add("Center", waveTest); f.resize(400, 350); f.show(); } } // // Create a graphics area and set the paint method to // retrieve a gif file from the web server. // class WaveCanvas extends Canvas { WaveTest par; Image img; public WaveCanvas (WaveTest parent) { par=parent; } public void paint(Graphics g) { img = par.getImage(par.getDocumentBase(),"java.gif"); g.drawImage(img,0,0,400,350,this); } public void flush() { img.flush(); } public void update(Graphics g) { paint(g); } public void redraw() { repaint(); } } // // Create a text area for input of Wave commands and a text // region to display the output of Wave. Also include a button // to refresh the graphic area (although this should be unnecessary // because it will be done automatically when you issue a command // that changes the graphic area.) // class WaveControls extends Panel { TextField cmd; TextArea res; WaveCanvas canvas; WaveTest par; String msg; public WaveControls(WaveCanvas canvas, WaveTest parent) { this.canvas = canvas; par = parent; boolean autoflush = true; // // Create the Wave command input field and the Redraw button // Panel towave = new Panel(); towave.setLayout(new BorderLayout()); towave.add ("West",new Label("WAVE> ")); towave.add ("Center",cmd = new TextField("",40)); towave.add ("East",new Button("Redraw")); // // Create the text area to show Wave's stdout // Panel frmwave = new Panel(); frmwave.setLayout(new BorderLayout()); frmwave.add ("West",new Label(" ")); frmwave.add ("Center",res = new TextArea(10,40)); setLayout(new BorderLayout()); add("North",towave); add("South",frmwave); } // // Handle the events from the applet // public boolean action(Event ev, Object arg) { // // If Redraw is pressed, send Wave the Redraw command // (which is part of the java_server.pro) which causes // Wave to copy it's internal Z buffer window to a // GIF file. Finally flush the canvas in the applet // which causes it to get a fresh copy of the graphic // from the server. // if (ev.target instanceof Button) { //System.out.println("Sending Redraw request"); //par.showStatus("Requesting Graphics Update..."); //par.show(); send_msg("REDRAW"); canvas.flush(); canvas.repaint(); //par.showStatus(""); return true; } // // Get the text from the command area, erase the command // area, and send the command to Wave to be executed. // if (ev.target instanceof TextField) { //System.out.println("Sending Command"); //par.showStatus("Sending Command to Wave..."); //par.show(); String s = cmd.getText(); cmd.setText(""); send_msg(s); //par.showStatus(""); return true; } return false; } // // This method actually makes the connection with Wave. // It does this by executing a CGI script that communicates // with Wave via an RPC client. Wave is assumed to be running // as an RPC server, but the client knows to restart the server // if it is not running for any reason. // public void send_msg(String s) { try { // // Open a URL connection to the CGI script // // We could get the URL of the server something like this: //res.appendText("codebase: " + par.getCodeBase() + "\n" ); //URL url = new URL(par.getCodeBase(), "../cgi-test/call_wave"); // Here the URL is absolute: URL url = new URL("http://www.jwave.vt.edu/cgi-bin/call_wave"); URLConnection connection = url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); // // Send the command to Wave // PrintStream outStream = new PrintStream(connection.getOutputStream(),true); outStream.println(s); outStream.close(); // // Wait for response // DataInputStream inStream = new DataInputStream(connection.getInputStream()); while (null != (msg = inStream.readLine())) { // // If Wave sends back REDRAW get a new copy // of the GIF file for the graphic display. // if (msg.equals("REDRAW")) { canvas.flush(); canvas.repaint(); } else { // // Add the text from Wave to the text area // res.appendText(msg + "\n" ); } } inStream.close(); // // Handle exceptions // } catch (MalformedURLException e) { res.appendText("Malformed send_msg: " + e + "\n" ); return; } catch (IOException e) { res.appendText("IOException in send_msg: " + e + "\n" ); return; } } }