// File: PVWaveAccessServer.java // // Author: Francois Coupleux (coupleux@eleve.emn.fr) // // Date: 7-29-1996 // // Description: Server which sends commands to PV-Wave. import java.net.*; import java.io.*; import java.util.*; class PVWaveAccessServer { public static void main(String arg[]) { ServerSocket serverSocket; Socket clientSocket; DataInputStream dataInputStream , currentInputStream; PrintStream dataOutputStream , currentOutputStream; Vector ids , currentVector , x1 , y1 , x2 , y2; String message , currentClientId , command; StringTokenizer stringTokenizer; URL pvWaveServerURL; URLConnection pvWaveServerConnection; File imageFile , standardImageFile; int i; try { // Creates vectors ids = new Vector(); x1 = new Vector(); y1 = new Vector(); x2 = new Vector(); y2 = new Vector(); pvWaveServerConnection = null; // Creates sockets serverSocket = new ServerSocket(7000); while(true) { // Waits for a client clientSocket = serverSocket.accept(); // Creates streams dataInputStream = new DataInputStream(clientSocket.getInputStream()); dataOutputStream = new PrintStream(clientSocket.getOutputStream()); while(true) { // Waits for a message from the client while((message = dataInputStream.readLine()) == null) { ; } stringTokenizer = new StringTokenizer(message , "&"); currentClientId = stringTokenizer.nextToken(); // Declares a new client for(i = 0 ; i < ids.size() ; i++) { if(((String)ids.elementAt(i)).equals(currentClientId)) { break; } } if(i == ids.size()) { ids.addElement(currentClientId); x1.addElement("0"); y1.addElement("0"); x2.addElement("0"); y2.addElement("0"); } // Creates streams to the PV-Wave server pvWaveServerURL = new URL("http://www.jwave.vt.edu/cgi-bin/call_wave_visualizer"); pvWaveServerConnection = pvWaveServerURL.openConnection(); currentOutputStream = new PrintStream(pvWaveServerConnection.getOutputStream()); command = stringTokenizer.nextToken(); // If the comman is an OPLOT command this sets the clipping area if(command.startsWith("OPLOT")) { for(i = 0 ; i < ids.size() ; i++) { if(((String)ids.elementAt(i)).equals(currentClientId)) { break; } } currentOutputStream.println("clipping_area(0)=" + (String)x1.elementAt(i)); currentOutputStream.close(); currentInputStream = new DataInputStream(pvWaveServerConnection.getInputStream()); while(currentInputStream.readLine() == null) { ; } currentInputStream.close(); pvWaveServerURL = new URL("http://www.jwave.vt.edu/cgi-bin/call_wave_visualizer"); pvWaveServerConnection = pvWaveServerURL.openConnection(); currentOutputStream = new PrintStream(pvWaveServerConnection.getOutputStream()); currentOutputStream.println("clipping_area(1)=" + (String)y1.elementAt(i)); currentOutputStream.close(); currentInputStream = new DataInputStream(pvWaveServerConnection.getInputStream()); while(currentInputStream.readLine() == null) { ; } currentInputStream.close(); pvWaveServerURL = new URL("http://www.jwave.vt.edu/cgi-bin/call_wave_visualizer"); pvWaveServerConnection = pvWaveServerURL.openConnection(); currentOutputStream = new PrintStream(pvWaveServerConnection.getOutputStream()); currentOutputStream.println("clipping_area(2)=" + (String)x2.elementAt(i)); currentOutputStream.close(); currentInputStream = new DataInputStream(pvWaveServerConnection.getInputStream()); while(currentInputStream.readLine() == null) { ; } currentInputStream.close(); pvWaveServerURL = new URL("http://www.jwave.vt.edu/cgi-bin/call_wave_visualizer"); pvWaveServerConnection = pvWaveServerURL.openConnection(); currentOutputStream = new PrintStream(pvWaveServerConnection.getOutputStream()); currentOutputStream.println("clipping_area(3)=" + (String)y2.elementAt(i)); currentOutputStream.close(); currentInputStream = new DataInputStream(pvWaveServerConnection.getInputStream()); while(currentInputStream.readLine() == null) { ; } currentInputStream.close(); pvWaveServerURL = new URL("http://www.jwave.vt.edu/cgi-bin/call_wave_visualizer"); pvWaveServerConnection = pvWaveServerURL.openConnection(); currentOutputStream = new PrintStream(pvWaveServerConnection.getOutputStream()); } // Sends the message to PV-Wave currentOutputStream.println(command); currentOutputStream.close(); currentInputStream = new DataInputStream(pvWaveServerConnection.getInputStream()); // Waits for the PV_Wave response while((message = currentInputStream.readLine()) == null) { ; } if(message.charAt(0) == '[') { while((message = currentInputStream.readLine()) == null) { ; } } // If it's an EXIT or a QUIT message if(message.equals("Bye-bye, leaving PV-WAVE...")) { // Deletes the image imageFile = new File("/home3/httpd/htdocs/javaprj/viscprj/visualizer/applet/PVWaveGraphs" , currentClientId); imageFile.delete(); // Sends the message to the applet dataOutputStream.println(message); // Closes connections to the PV-Wave server currentInputStream.close(); break; } // If it's a message about clipping area else if(message.startsWith("WAVE> PRINT")) { // Gets the correct vector for the clipping value stringTokenizer = new StringTokenizer(message , "("); stringTokenizer.nextToken(); switch(stringTokenizer.nextToken().charAt(0)) { case '0': currentVector = x1; break; case '1': currentVector = y1; break; case '2': currentVector = x2; break; default: currentVector = y2; break; } // Puts the clipping value in the vector for(i = 0 ; i < ids.size() ; i++) { if(((String)ids.elementAt(i)).equals(currentClientId)) { break; } } stringTokenizer = new StringTokenizer(currentInputStream.readLine() , " "); currentVector.setElementAt(stringTokenizer.nextToken() , i); // Sends the message to the applet dataOutputStream.println(message); } else { // If it's the REDRAW message if(message.startsWith("WAVE> PLOT") || message.startsWith("WAVE> OPLOT")) { // Renames the file "graph.gif" imageFile = new File("/home3/httpd/htdocs/javaprj/viscprj/visualizer/applet/PVWaveGraphs" , currentClientId); imageFile.delete(); standardImageFile = new File("/home3/httpd/htdocs/javaprj/viscprj/visualizer/applet/PVWaveGraphs" , "graph.gif"); standardImageFile.renameTo(imageFile); } // Sends the message to the applet dataOutputStream.println(message); if(message.startsWith("WAVE> PLOT") || message.startsWith("WAVE> OPLOT")) { break; } } } // Closes connection to the client and client's socket dataInputStream.close(); clientSocket.close(); } } catch(Exception exception) { System.out.println("Error: Impossible to run the PVWaveAccessServer."); } } }