170 likes | 190 Views
Block 19: Developing the Calculator application using RMI. script. Recap last week Give out the references 10 minutes reading Calculator blank sheet, completed CalculatorImp blank, completed CalculatorServer blank, completed CalculatorClient blank, completed.
E N D
Block 19: Developing the Calculator application using RMI Client-server programming
script • Recap last week • Give out the references • 10 minutes reading • Calculator blank sheet, completed • CalculatorImp blank, completed • CalculatorServer blank, completed • CalculatorClient blank, completed Client-server programming
References from last week’s lecture on RMI Client-server programming
Components required • Use a RMI registry at a port • The Server-side Software • The Remote Interface • The Remote Implementation • The Server process • The Client-side Software • The Remote Interface • The client program Client-server programming
Steps in creating RMI applications • Declare the remote interface • Create the class that implements the remote interface • Create the stubs and skeletons (not for java 5.0) • Create the server process to export the remote object to RMI Registry • Start the RMI Registry • Create the client application • Run the application Client-server programming
handouts • Publisher examples code as a reference • In PublisherRMI.rtf Client-server programming
Calculator • Server provides services for a simpler calculator: add(x,y), sub(x,y) • Client get user’s inputs of the operator and the two numbers and call the approporiate remote method. Client-server programming
Developing the Calculator application Client-server programming
List the components you need to develop • Server side: • Client side: Client-server programming
List the components you need to develop • Server side: • Calculator interface • Calculator implementation • Calculator server • Client side • Client program Client-server programming
Calculator interface public interface Calculator extends Remote{ public int exp(int x, int y) throws java.rmi.RemoteException; public int add(int x, int y) throws java.rmi.RemoteException; public int sub(int x, int y) throws java.rmi.RemoteException; } Client-server programming
Calculator implementation public class CalculatorImp extends UnicastRemoteObject implements Calculator { public Calculator throws RemoteException { super( ); } public int add(int x, int y) throws java.rmi.RemoteException{ return x+y; } ….. ….. } // end class Client-server programming
CalculatorServer public class CalculatorServer { public static void main(String args[]) { int portNum=?; String registryURL=?; CalculatorImp calculator; try{ // start rmi registry // create instance of CalculatorImpl // export object }// end try catch (Exception re) {...} // end catch } // end main Client-server programming
CalculatorServer – try block try { LocateRegistry.createRegistry(RMIPort); calculator = new CalculatorImp(); Naming.rebind(registryURL, calculator); } catch (RemoteException ex) { ………..} Client-server programming
Client program: look up the remote object public static void main(String[] args) { String menu = "1: add\n 2: sub\n 9: quit\n"; Scanner scan = new Scanner(System.in); int operator, result, x, y; int port = 1111; String registryURL = "rmi://localhost:"+port + "/cal"; // get the remote Calculator object Calculator c; try { c = (Calculator) Naming.lookup(registryURL); //... cont from next slides Client-server programming
Client program: send message to remote object …… cont from previous slide: System.out.println(menu); operator = scan.nextInt(); while (operator != 9) { switch (operator) { case 1://add x = scan.nextInt(); y = scan.nextInt(); result = c.add(x, y); case 2: //sub ... }//switch System.out.println(menu); operator = scan.nextInt(); } //while } } Client-server programming
Exercises in the practicals • Define the Calculator remote object • Define a Timer remote object that provides a service that allows clients to find out the time of the day on the server • Define a server process exporting these two objects • Define a client programme that is similar to the Calculator client, in addition, it prints out the times of the day at the start of the program and at the end of the program. Client-server programming