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      All lectures

Remote Method Invocation

import java.rmi.*;
import java.rmi.RemoteException;
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;
   Stream process;
   Socket incoming;
   int counter;
   RVer server;

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

   public void run() {
      try {
         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" );
         server.applet.status.setText("Connection open from server "+counter);

         process = (Stream)in.readObject();
         try {
            for (int i=1 ; i <= 20 ; i++) {
               int number = ((IntObject)(process.next())).valueOf();
               server.applet.takein.append (String.valueOf(number));
               if ((i % 5) == 0)
                  server.applet.takein.append ("\n");
               else
                  server.applet.takein.append ("  ");
            }
            server.applet.takein.append("--------------------\n");
         }
         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(8689);
         applet.status.setText("Server listening on port 8689");
    
         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!!");
      }
   }
}