20-CS-694 Advanced Programming Techniques Spring 2012
Networking

Interfaces, Exceptions, Graphics, Animation, Threads, Reflection, Networking, RMI, JDBC, JNI

     Previous     Next     RVServer.java     RVClient.java     Serializable class      All lectures

Teleportation

An "Application" object is sent by the client process of the next slide to this server. A pipeline is connected to the application and then the application is executed. The output from the application is viewed, via the pipeline, in a JTextArea on the server. The application object is threaded and can be immediately started when it arrives at the server. The application object extends PipedWriter to allow a simple connection to be made between it and the server.
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

class RVHandler extends Thread {
   String processid = null;
   Application process;
   Socket incoming;
   int counter;
   RVer server;

   RVHandler(Socket i, int c, RVer svr) {
      incoming = i;
      counter = c;
      server = svr;
   }

   public void run() {
      try {
         PipedReader reader = new PipedReader();
         InputStream ins = incoming.getInputStream();
         ObjectInputStream in = new ObjectInputStream(ins);
         PrintWriter out = new PrintWriter(incoming.getOutputStream(), true);

         out.println( "Hey! Send Me Your Serialized Thread Object!!!\r" );
         process = (Application)in.readObject();
         server.applet.status.setText("Connection open from server "+counter);
         process.connect(reader);
         process.start();
         BufferedReader br = new BufferedReader(reader);
         try {
            int i=1;
            while (true) {
               server.applet.takein.append (br.readLine());
               if ((i++ % 5) == 0)
                  server.applet.takein.append ("\n");
               else
                  server.applet.takein.append ("  ");
            }
         } catch (Exception ee) { }
         incoming.close();
         server.applet.status.setText("Server "+counter+" closed connection");
         server.resetID(counter);
      } catch (Exception e) { server.applet.status.setText(String.valueOf(e)); }
   } 
}

class RVer implements Runnable {
   Thread runner;
   int pid[];
   RVServer applet;
   
   RVer (RVServer applet) { this.applet = applet; }

   public int unusedID() {
      for (int i=0 ; i < 100 ; i++)
         if (pid[i] == 0) { pid[i] = 1;  return i; }
      return -1;
   }
   
   public void resetID(int i) { pid[i] = 0; }
   
   public void initID() {
      pid = new int[100];
      for (int i=0 ; i < 100 ; i++) pid[i] = 0;
   }

   public void start () {
      runner = new Thread(this);
      runner.start();
   }

   public void run () {
      int i;
      initID();
      
      try {
         ServerSocket s = new ServerSocket(8789);
         applet.status.setText("Server listening on port 8789");
    
         while (true) {
            Socket incoming = s.accept( );
            if ((i = unusedID()) != -1)
              new RVHandler(incoming, i, this).start();
            else
              applet.status.setText("No available Servers");
         }
      } catch (Exception f) {}
   }
}

public class RVServer extends Applet implements ActionListener {
   JButton doit;
   JTextField status;
   JTextArea takein;
   RVer server = null;
   
   public void init () {
      setLayout(new BorderLayout());
      Panel p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new JLabel("Status"));
      p.add("Center", status = new JTextField());
      p.add("East", doit = new JButton("Start Server"));
      add("North", p);
      
      p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new JLabel("Output of Agent"));
      p.add("Center", new JScrollPane(takein = new JTextArea(10,30)));
      add("Center", p);
   }
   
   public void start () {
      takein.setEditable(false);
      takein.setBackground(Color.white);
      status.setEditable(false);
      status.setBackground(Color.white);
      doit.addActionListener(this);
   }
   
   public void actionPerformed (ActionEvent evt) {
      if (evt.getSource() == doit) {
         if (server == null) {
            server = new RVer(this);
            server.start();
         }
         else
            status.setText("Server already started!!");
      }
   }
}