20-CS-694 Advanced Programming Techniques Spring 2012
Networking

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

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

Teleportation II - The Client

See notes on the previous slide.
import java.awt.*;
import java.io.*;
import java.net.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;

abstract class Stream implements Runnable, Serializable {
   Object value;
   Thread runner = null, nextThread = null;
   
   public Stream ()  {  }
   
   private void writeObject(java.io.ObjectOutputStream out)
        throws IOException   { }
   
   private void readObject(java.io.ObjectInputStream in)
        throws IOException, ClassNotFoundException { }
   
   synchronized public void putIt (Object t) {
      value = t;
      notify();
      try { wait(); } catch (Exception e) {  }
   }

   synchronized public Object next () {
      if (runner != null)  notify();
      else {
         runner = new Thread(this);
         runner.start();
      }

      try { wait(); } catch (Exception e) {  }

      return value;
   }
}

class IntObject implements Serializable {
   int number;
   
   public IntObject (int n) { number = n; }
   
   public int valueOf () { return number; }
}

class Times extends Stream {
   IntObject first;
   int number;
   Stream stream;
   
   public Times (int n, Stream s) {
      stream = s;
      number = n;
   }
   
   public void run () {
      first = new IntObject(number*((IntObject)stream.next()).valueOf());
      while (true) {
         putIt(first);
         first = new IntObject(number*((IntObject)(stream.next())).valueOf());
      }
   }
}   

class Merge extends Stream {
   IntObject val1, val2, first;
   Stream stream1;
   Stream stream2;
   
   public Merge (Stream s1, Stream s2) {
      stream1 = s1; 
      stream2 = s2;
   }

   public void run () {
      val1 = (IntObject)stream1.next();
      val2 = (IntObject)stream2.next();
      
      while (true) {
         if (val1 == null && val2 == null) putIt(null);
         else
         if (val1 == null) {
            putIt(val2);
            val2 = (IntObject)stream2.next();
         }
         else
         if (val2 == null) {
            putIt(val1);
            val1 = (IntObject)stream1.next();
         }
         else
         if (((IntObject)(val1)).valueOf() < ((IntObject)(val2)).valueOf()) {
            putIt(val1);
            val1 = (IntObject)stream1.next();
         }
         else {
            putIt(val2);
            val2 = (IntObject)stream2.next();
         }
      }
   }
}

class Successor extends Stream {
   int number;
   IntObject first;
   
   public Successor (int n) {
      number = n;
   }

   public void run () {
      first = new IntObject(number);
      while (true) {
         putIt(first);
         number++;
         first = new IntObject(number);
      }
   }
}

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 {
         Times times5 = new Times(5, new Successor(1));
         Times times3 = new Times(3, new Successor(1));
         Merge merge = new Merge(times5, times3);
         for (int i=1 ; i < 10 ; i++) {
            int number = ((IntObject)(merge.next())).valueOf();
            rv.monitor.append(String.valueOf(number));
            if ((i % 5) == 0) rv.monitor.append("\n");
            else rv.monitor.append("  ");
         }
         out.writeObject(merge);
         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 Applet implements ActionListener {
   JButton sendit, leave, openit;
   JTextField status;
   JTextArea takein, monitor;
   RVClients client = 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("No connection yet"));
      add("North", p);

      Panel q = new Panel();
      q.setLayout(new GridLayout(2,1));
      p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new JLabel("Received From Server"));
      p.add("Center", new JScrollPane(takein = new JTextArea(10,30)));
      q.add(p);
      p = new Panel();
      p.setLayout(new BorderLayout());
      p.add("North", new JLabel("Running locally..."));
      p.add("Center", new JScrollPane(monitor = new JTextArea(10,30)));
      q.add(p);
      add("Center", q);

      p = new Panel();
      p.setLayout(new GridLayout(1,3,10,10));
      p.add(sendit = new JButton("Send It"));
      p.add(openit = new JButton("Connect"));
      p.add(leave = new JButton("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;
      }
   }
}