The K Zone, Home

About Me

K View

Projects

Papers

Gallery

Email

 

 

 
 

Evaluation of RMI - Page 12

 

Back to Contents Page

 
 

 

Appendix A - RMI Implementation Code

Interface

1. import java.rmi.*;
2. public interface NumFun extends Remote {
3. public boolean isPrime(long n) throws RemoteException;
4. public long gcd(long a, long b) throws RemoteException;
5. public long lcm(long a, long b) throws RemoteException;
6. }

Implementation of the Interface


1. import java.rmi.*;
2. public class NumFunIm extends java.rmi.server.UnicastRemoteObject
3. implements NumFun {
4. public NumFunIm() throws RemoteException { super(); }
5. public boolean isPrime(long n) throws RemoteException {
6. if (n < 2 || n > 2 && n%2 == 0) return false;
7. long m = (long)Math.sqrt(n) + 1;
8. for (long i = 3; i <= m; i += 2)
9. if (n%i == 0) return false;
10. return true;
11. }
12. public long gcd(long u, long v) throws RemoteException {
13. for ( ; ; ) {
14. long r = u % v;
15. if (r == 0) return v;
16. u = v;
17. v = r;
18. }
19. }
20. public long lcm(long u, long v) throws RemoteException {
21. return u*v/gcd(u,v);
22. }
23. }

 

Implementation of the Server

1. public class NFServer {
2. public static void main(String [] args) {
3. try {
4. NumFun nf = new NumFunIm();
5. java.rmi.Naming.rebind("rmi://localhost/NFService", nf);
6. System.out.println("NFServer bound");
7. } catch (Exception e) { System.out.println(e); }
8. }
9. }

 

Implementation of the Client

1. public class NFClient {
2. public static void main(String [] args) {
3. long n = args.length > 0 ? Long.parseLong(args[0]) : 17;
4. long u = args.length > 1 ? Long.parseLong(args[1]) : 12;
5. long v = args.length > 2 ? Long.parseLong(args[2]) : 18;
6. try {
7. NumFun nf =
8. (NumFun)java.rmi.Naming.lookup("rmi://localhost/NFService");
9. System.out.println(
10. n + " is " + (nf.isPrime(n) ? "" : "not ") + "a prime" +
11. "\nThe GCD of " + u + " and " + v + " is " + nf.gcd(u,v) +
12. "\nThe LCM of " + u + " and " + v + " is " + nf.lcm(u,v) );
13. } catch (Exception e) { System.out.println(e); }
14. }
15. }


Running the System
Open three DOS windows:
1. Compile Server (Window 1)

...\rmiNFS\server>javac NFServer.java

2. Compile the implementation (Window 2)

...\rmiNFS\server>rmic NumFunIm

3. Compile the Client (Window 3)

...\rmiNFC\client>javac NFClient.java

4. Start the RMI registry (Window 2)

...\rmiNFC\client>rmiregistry

5. Run the server (Window 1)

...\rmiNFS\server>java NFServer

6. Run the client (Window 2)

...\rmiNFC\client>java NFClient


Default Output
Using the clients default arguments, 17, 12 and 18 produces the following output:

17 is a prime
The GCD of 12 and 18 is 6
The LCM of 12 and 18 is 36

Next Page