Program 3: RMI Programming Assignment

In this assignment you are to modify the Calculator example presented in class. You are provided with initial code and a separate (more complicated) Score example. The separate example is an unrelated application, but illustrates using a security manager, and how to programmatically set the security policy and codebase. Recall the codebase setting is necessary for automatic downloading of classes from a server to a client that are needed by the client (e.g., stub classes representing remote server objects).

There are three modifications you will need to make to the Calculator application code provided.

Compiling and running the initial code

  1. The code is in the zip file calculator.zip. Unzip this file in some directory. All the files are in a subdirectory named rmi.
  2. Compile all the java files from the parent directory of rmi:
    javac rmi/*.java
    
  3. Run the RMI compiler on the server .class file:
     rmic rmi.CalculatorImpl
    
  4. Start the rmiregistry (but not in the directory containing the package directory, rmi).
  5. Start the server:
     java -Djava.rmi.server.codebase=file:/.../ rmi.CalculatorImpl
    
    where ... should be replaced by the directory path that contains the
    rmi package. It should end with /. 
    
    E.g., in Windows if rmi is a subdirectory of c:\myfiles\hw, then the
    server could be run by 
    
     java -Djava.rmi.server.codebase=file:/c:/myfiles/hw/ rmi.CalculatorImpl
    
    
  6. Start the client
     java rmi.CalculatorClient
    

Securtiy Managers

First add a security manager for both the server and the client. The provided code has all the files in one directory. Since all class files including stub classes are in the same directory, the client does not have to download classes remotely from the server. Consequently, no security manager is required in the provided code. In the modified code, the client will need to download classes remotely so a security manager will be needed.

When a securtiy manager is set, a policy file must also be given specifying what download, socket, etc. permissions are granted when the application executes.

Add code to both the client and server to set the security policy using a default policy file (in the current directory of the client and the current directory of the server, respectively). If the client or server is started with a command line option for a security policy file, the command line specified file should override the default.

See the Score sample application for an example of how to do this.




java -Djava.security.policy=otherpolicy.txt SomeServer

(This should override the security policy file provided by your code.)

New remote method and its new Serializable argument

Consider the following Java Class representing a List of Integers.

package rmi;
import java.util.*;

public class IntList  {

	private LinkedList V;

	/**
	 * Constructor for objects of class IntList
	 */
	public IntList(){
		V = new LinkedList();
	}
               private void setV(LinkedList V){
                   this.V = V;
               }
	public void addBack(int i) {
	   V.addLast(new Integer(i));
	}
	
	public void addFront(int i){
	   V.addFirst(new Integer(i));
	}
	
	public int removeFront(){
	   return ((Integer)(V.removeFirst())).intValue();
	}

    public int removeBack(){
       return ((Integer)(V.removeLast())).intValue();
    }
    
    public boolean isEmpty(){
       return (V.size() == 0);
    }
    public IntList copy(){
       IntList Vpr = new IntList();
       Vpr.setV((LinkedList)(V.clone()));
       return Vpr;
     }

}
  1. Modify the above class to implement the Serializable API.
  2. Modify the Calculator example discussed in class as follows.
    1. Add the method:

          public int sumList(IntLIst L) throws RemoteException;
      

      to the Calculator interface.

    2. Implement the method

          public int sumList(IntLIst L) throws RemoteException
      

      in CalculatorImpl. It should sum up the numbers in the list L and return the result.

      For example,

      int sum =0;
      while (!L.isEmpty()){
                 sum = sum + L.removeFront();
              }
      
      
    3. In the CalculatorClient, replace the method g() by:
      private void go ()
          throws ArithmeticException, MalformedURLException, 
                 NotBoundException, RemoteException
        {
          Calculator calc =
            (Calculator) Naming.lookup ("rmi://127.0.0.1/Calculator");
      
          IntList L = new IntList();
              
              L.addFront(1);
              L.addFront(2);
              L.addFront(3);
              L.addFront(4);
              System.out.println (calc.sumList(L));
        }
      

Separate Server and Client and add default codebase

Separate the server, client files into 3 separate directories.

The Score sample example illustrates this. In that example

As for the policy file, you will need to add code to the CalculatorServer (but not the CalculatorClient) to set a default codebase to be its package directory. If the server is started with a command line option for a codebase url, the command line specified file should override the default.

Again, see the Score sample application for an example of how to do this.




java -Djava.rmi.server.codebase=file:/c:/hw/mserver/  server.SomeServer

(This should override the server codebase provided by your code.)

What to submit

You should zip (or create a jar file) of the server and client directories together with their policy files. Name the zip (or jar file) MyCalculator.zip ( or MyCalculator.jar ), and submit that file to the Course Online site for program 3. Make sure that your zip or jar file contains all the .java files and the policy files.