Teleportation - The Client

     Java 1.1 Source

// The client side of the teleportation pair
// See notes of previous slide
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;

class Application extends PipedWriter
                  implements Runnable, Serializable
{
   PipedReader pipe;
   Thread runner;
   
   public void start ()
   {
      runner = new Thread(this);
      runner.start();
   }
   
   // Connect the pipe to the PipeWriter
   public void connect (PipedReader pipe) throws java.io.IOException
   {
      super.connect(pipe);
   }
   
   public void run ()
   {
      try
      {
         for (int i=0 ; i < 100 ; i++)
         {
            write(String.valueOf(i)+"\n");
         }
      }
      catch (IOException e)
      {
         System.out.println("Cannot open piped connection to agent");
      }
   }
}

// Sends the application to the server
class RVClients implements Runnable
{
   Thread runner = null;
   Socket socket = null;
   BufferedReader in = null;
   ObjectOutputStream out = null;
   RVClient rv;
   String name;
   
   public RVClients(RVClient ca, String nm) { rv = ca; name = nm; }
   
   public void disconnect ()
   {
      try
      {
         socket.close();
      }
      catch (Exception e) { rv.status.setText(String.valueOf(e));  }
      rv.status.setText("Connection Closed");
      rv.client = null;
   }
   
   public void connectit ()
   {
      try
      {
         socket = new Socket("localhost", 8689);
         InputStream ins = socket.getInputStream();
         in = new BufferedReader(new InputStreamReader(ins));
         out = new ObjectOutputStream(socket.getOutputStream());
         rv.status.setText("Ready to send");
         runner = new Thread(this);
         runner.start();
      }
      catch (Exception e)
      {
         rv.status.setText("Connection Refused - Server Running?");
         rv.client = null;    
      }
   }
    
   public void transmitit()
   {
      try
      {
         out.writeObject(new Application());
         rv.status.setText("Sending to server");
      }
      catch (Exception e)
      {
         rv.status.setText("Cannot Send to Server - Server Down?");
         rv.client = null;    
      }
   }
    
   public void run ()
   {
      try
      {
         while (true)
         {
            String str = in.readLine();
            rv.takein.append(str+"\n");
            if (str.equals("") || str == null) break;
         }
      }
      catch (Exception e) { }
      disconnect();
   }
}

public class RVClient extends java.applet.Applet
                      implements ActionListener
{
   Button sendit, leave, openit;
   TextField status;
   TextArea takein;
   RVClients client = null;
   
   public void init ()
   {
      setLayout(new BorderLayout());
      Panel p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new Label("Status"));
      p.add("Center", status = new TextField("No connection yet"));
      add("North", p);
      
      p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new Label("Received From Server"));
      p.add("Center", takein = new TextArea(10,30));
      add("Center", p);

      p = new Panel();
      p.setLayout(new GridLayout(1,3,10,10));
      p.add(sendit = new Button("Send It"));
      p.add(openit = new Button("Connect"));
      p.add(leave = new Button("Close"));
      add("South", p);
   }

   public void start ()
   {
      status.setEditable(false);
      status.setBackground(Color.white);
      takein.setEditable(false);
      takein.setBackground(Color.white);
      sendit.addActionListener(this);
      openit.addActionListener(this);
      leave.addActionListener(this);
   }
   
   public void actionPerformed (ActionEvent evt)
   {
      if (evt.getSource() == sendit && client != null)
         client.transmitit();
      else
      if (evt.getSource() == openit && client == null)
      {
         client = new RVClients(this, "object_client");
         client.connectit();
      }
      else
      if (evt.getSource() == leave && client != null)
      {
         client.disconnect();
         client = null;
      }
   }
}