Download DSI lab 2 for CSN - London South Bank University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Faculty of ESBE
Distributed Systems and Internetworking
DSI Lab: Distributed Computing With Java RMI
Remote Method Invocation (RMI) is a distributed systems technology that allows one Java Virtual
Machine (JVM) to invoke object methods that will run on another JVM located elsewhere on a
network. This technology is extremely important for the development of large-scale systems, as it
makes it possible to distribute resources and processing load across more than one machine.
RMI applications are divided into two kinds of programs: servers and clients. RMI servers create
some remote objects, and register with a lookup service, to allow clients to find them. Clients use a
remote reference to one or more remote objects in the server and then invokes methods on them.
RMI provides the mechanism by which servers and clients communicate and pass information
back and forth. Such an application is sometimes referred to as a distributed object application.
The following lab work shows you how create a Java RMI application, which perform the
calculation of two numbers.
Exercise 1:

To create a RMI application, the first step is to design an interface. Logon to Windows
2000 system, open a MS-Dos window, change directory to d:\Jwork. Use the notepad
program, create the following Java program and save it as “Calculator.java” in
d:\Jwork directory.
___________________________________________________________
public interface Calculator
extends java.rmi.Remote {
public long add(long a, long b)
throws java.rmi.RemoteException;
public long sub(long a, long b)
throws java.rmi.RemoteException;
public long mul(long a, long b)
throws java.rmi.RemoteException;
public long div(long a, long b)
throws java.rmi.RemoteException;
}
__________________________________________________________
 The second step is to implement the interface. Create the following Java program and save
it as “CalculatorImpl.java”.
___________________________________________________________
public class CalculatorImpl extends
java.rmi.server.UnicastRemoteObject implements Calculator {
// Implementations must have an explicit constructor
// in order to declare the RemoteException exception
public CalculatorImpl()
throws java.rmi.RemoteException {
super();
}
public long add(long a, long b)
Dr. Perry XIAO
Copyright © 2000-2007, London South Bank University
1
Faculty of ESBE
Distributed Systems and Internetworking
throws java.rmi.RemoteException {
return a + b;
}
public long sub(long a, long b)
throws java.rmi.RemoteException {
return a - b;
}
public long mul(long a, long b)
throws java.rmi.RemoteException {
return a * b;
}
public long div(long a, long b)
throws java.rmi.RemoteException {
return a / b;
}
}
__________________________________________________________
 Now you can compile the interface and its implementation. To do this, type “rmic
CalculatorImpl”. It should produce following 4 new files:
CalculatorImpl.class
Calculator.class
CalculatorImpl_Stub.class
CalculatorImpl_Skel.class
 Next, create a RMI server. Create the following Java program and save it as
“CalculatorServer.java”.
__________________________________________________________
import java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.rebind("rmi://localhost:1099/CalculatorService", c);
} catch (Exception e) {
System.out.println("Trouble: " + e);
}
}
public static void main(String args[]) {
new CalculatorServer();
}
}
__________________________________________________________
 Finally, create a RMI client. Type in the following Java program and save it as
“CalculatorClient.java”.
__________________________________________________________
import
import
import
import
java.rmi.Naming;
java.rmi.RemoteException;
java.net.MalformedURLException;
java.rmi.NotBoundException;
public class CalculatorClient {
Dr. Perry XIAO
Copyright © 2000-2007, London South Bank University
2
Faculty of ESBE
Distributed Systems and Internetworking
public static void main(String[] args) {
long num1 = Integer.parseInt(args[0]);
long num2 = Integer.parseInt(args[1]);
try {
Calculator c = (Calculator)
Naming.lookup("rmi://localhost/CalculatorService");
System.out.println( "The substraction of "+num1 +" and "+
num2 +" is: "+ c.sub(num1, num2) );
System.out.println( "The addition of "+num1 +" and "+ num2 +"
is: "+c.add(num1, num2) );
System.out.println( "The multiplication of "+num1 +" and "+
num2 +" is: "+c.mul(num1, num2) );
System.out.println( "The division of "+num1 +" and "+ num2 +"
is: "+c.div(num1, num2) );
}
catch (MalformedURLException murle) {
System.out.println();
System.out.println("MalformedURLException");
System.out.println(murle);
}
catch (RemoteException re) {
System.out.println();
System.out.println("RemoteException");
System.out.println(re);
}
catch (NotBoundException nbe) {
System.out.println();
System.out.println("NotBoundException");
System.out.println(nbe);
}
catch (java.lang.ArithmeticException ae) {
System.out.println();
System.out.println("java.lang.ArithmeticException");
System.out.println(ae);
}
}
}





__________________________________________________________
Type “javac CalculatorServer.java” and “javac CalculatorClient.java” to
compile the client and server programs.
Congratulations! We are now ready to test the RMI applications. Type “rmiregistry” to
start the RMI registry so that objects can be registered (Note: The MS-Dos window will
hang in there, it is ok!)
Open another MS-Dos window, change to the same directory, type “java
CalculatorServer” to run the server program (Note: Again, the MS-Dos window will
hang in there, it is ok!)
Open the third MS-Dos window, change to the same directory, type “java
CalculatorClient 2 4” to run the client program, comment on the results.
Explain the purpose of every single statement in all 4 above Java programs. Write them
down to your logbook.
Dr. Perry XIAO
Copyright © 2000-2007, London South Bank University
3
Faculty of ESBE









Distributed Systems and Internetworking
So far, the client and server programs are all running on the same computer, although they
are running through networking services. Next, is to run the client and server programs on
different computers connected by real networks.
Use FTP program to upload “Calculator.java”, “CalculatorImpl.java”, and
“CalculatorServer.java” to your university Unix account (consult lab instructor, if
you don’t know how to do it).
Open a Telnet window to connect to your university Unix account. Type in following
commands to compile the programs and start the registry.
Open another Telnet window to connect to your university Unix account, type “java
CalculatorServer” to run the server program.
Modify the “CalculatorClient.java” program so that it can connect server program
that runs both in local machine and remote machines (How?).
Type “javac CalculatorClient.java” to compile the modified client program.
Type “java CalculatorClient 5 9” to run the client program, comment on the
results.
Now, upload the “CalculatorClient.java” program to your Unix account. Run the
server program on local Windows machine and client program on remote Unix machine.
Comment the results.
Repeat the above step, but this time run the client program on local Windows machine and
server program on remote Unix machine. Comment the results.
Exercise 2:

Base on the above exercises, design a RMI Lottery application. Each time you run the
client program -- “java LotteryClient n”, the server program “LotteryServer” will
generate n sets of Lottery numbers. Here n is a positive integer, representing the money
you will spend on Lottery in sterling pounds. You should write this program in a proper
engineering manner, i.e. there should be specifications, design (flow chart, FD, or pseudo
code), coding, test/debug, and documentation.
Questions:
1.
2.
3.
4.
5.
6.
7.
What is Java RMI and how does it work?
What are Stub and Skeleton programs in Java RMI?
What level does RMI fit into the TCP/IP 4 layer protocol model?
Which protocol do RMI use, TCP or UDP?
Which port number does RMI use?
What is the difference between RMI and RPC (Remote Procedure Calls)?
What are the advantages of RMI programming comparing with socket programming using
TCP/UDP?
Reference Web Sites:


http://java.sun.com/docs/books/tutorial/rmi/index.html
http://java.sun.com/j2se/1.4/docs/api/overview-summary.html
Dr. Perry XIAO
Copyright © 2000-2007, London South Bank University
4