1 / 17

Block 19: Developing the Calculator application using RMI

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.

marmijo
Download Presentation

Block 19: Developing the Calculator application using RMI

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Block 19: Developing the Calculator application using RMI Client-server programming

  2. 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

  3. References from last week’s lecture on RMI Client-server programming

  4. 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

  5. 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

  6. handouts • Publisher examples code as a reference • In PublisherRMI.rtf Client-server programming

  7. 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

  8. Developing the Calculator application Client-server programming

  9. List the components you need to develop • Server side: • Client side: Client-server programming

  10. List the components you need to develop • Server side: • Calculator interface • Calculator implementation • Calculator server • Client side • Client program Client-server programming

  11. 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

  12. 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

  13. 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

  14. CalculatorServer – try block try { LocateRegistry.createRegistry(RMIPort); calculator = new CalculatorImp(); Naming.rebind(registryURL, calculator); } catch (RemoteException ex) { ………..} Client-server programming

  15. 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

  16. 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

  17. 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

More Related