20-CS-694 Advanced Programming Techniques Spring 2012
Networking

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

     Previous      Next     FarOutDate.java     FarOutDateClient.java     DateServer.java     DateImpl.java      All lectures

Remote Method Invocation

Remote Method Invocation: a server makes a collection of methods under an interface available to clients. The clients use the methods as though they exists on their machines but actually all method invocations take place on the server and return values are transmitted back to the client. Thus, for example, proprietary information may be used in a company demo with little prospect for competitors to gain direct access to that information. Requirements are an interface that prototypes the methods, procedures that implement the methods on the server, procedures that serve the methods, and client procedures for invoking them. This slide contains an example showing a single remote method that returns a date when it is called.

The interface: (FarOutDate.java)

   import java.rmi.*;
   import java.util.*;

   public interface FarOutDate extends Remote {
      Date getDate() throws RemoteException;
   }

The Server: (DateServer.java)

   import java.util.*;
   import java.rmi.*;
   import java.rmi.server.*;

   public class DateServer {
      public static void main (String args[]) {
         System.setSecurityManager(new RMISecurityManager());
         try {
            FarOutDate date_server = new DateImpl();
            Naming.rebind("DateServer", date_server);
            System.out.println("DateServer serving");
         }
         catch (Exception e) {
            System.out.println(e.toString());
         }
      }
   }

The Implemented Classes: (DateImpl.java)

   import java.util.*;
   import java.rmi.*;
   import java.rmi.server.*;

   public class DateImpl extends UnicastRemoteObject implements FarOutDate {
      public DateImpl () throws RemoteException { }
   
      public Date getDate() throws RemoteException {
         return new Date();
      }
   }

A Simple Client: (FarOutDateClient.java)

   import java.rmi.*;
   import java.awt.*;
   import java.awt.event.*;
	import java.applet.*;
	import javax.swing.*;

   public class FarOutDateClient extends Applet implements ActionListener {
      JTextField text;
      JButton getit;
      JTextArea area;
   
      public void init () {
         setLayout(new BorderLayout(10,10));
         add ("North", text = new JTextField());
         add ("Center", new JScrollPane(area = new JTextArea()));
         Panel p = new Panel();
         p.setLayout(new GridLayout(1,3));
         p.add(new Label());
         p.add(getit = Jnew Button("Get Date"));
         add ("South", p);
      }
   
      public void start () {
         getit.addActionListener(this);
      }

      public void actionPerformed (ActionEvent evt) {
         if (evt.getSource() == getit) {
            try {
               String server_object = "rmi://localhost/DateServer";
               FarOutDate date = (FarOutDate)Naming.lookup(server_object);
               area.append(date.getDate()+"\n");
            }
            catch (Exception e) {
               text.setText(e.toString());
            }
         }
      }
   }

HTML file to start the client: (date.html)

   < applet code="FarOutDateClient.class" width=300 height=350 >
   < /applet >

Instructions:

  1. Edit and compile the code above in your "home" directory
  2. Run "rmic DateImpl"
  3. Transfer non-client classes to /usr/local/java/lib
  4. Run "rmiregistry &"
  5. From /usr/local/java/lib run "java DateServer"
  6. Wait for "DateServer serving"
  7. From "home" directory run "appletviewer date.html"