1 / 25

Distributed Systems with Socket Programming and RMI

Distributed Systems with Socket Programming and RMI.

percy
Download Presentation

Distributed Systems with Socket Programming and 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. Distributed Systems with Socket Programming and RMI What is a distributed system? page 2Sockets and socket programming page 3-6Objects collaborating over the net (RMI) page 7-13RMI, in the depth page 14-16Summary: How to create a simple distributed system page 17-18RMI and applets page 19Deployment diagram page 20-22Distributed system with callback page 23-25 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  2. What Is a Distributed System? • A distributedsystem consists of several programs that are running on several computers and that communicate with each other. • A client is a program or a computer that asks for services from a server, usually over a network. • A server is a program or a computer that performs tasks requested by clients. • Client and server are roles that programs and machines play. • An example: A computer becomes a client if we run a client program on it. • One and the same computer may play both of the roles. • One and the same program may play both of the roles, too. The program receives requests from others, and it may also send requests to others. Match collaboration between objects. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  3. Sockets • Computers communicate with each other when data is sent from one machine to another over a network. • A protocol is a set of rules that tells how this data stream will be sent from the sender and interpreted by the recipient. An example: • The Internet Protocol (IP) describes how computers will communicate with each other over the Internet. • For machines to be able to communicate with each other, they have to be identifiable. Computers connected to the Internet are identified using an IP address, examples: 186.45.34.100 and 156.76.50.237. • To avoid dealing with these numbers, a machine usually also has a name. Examples of names are java.sun.com and mary.marysHome.dk. • If we use the name, the network software in the computer will look up the corresponding IP address on the Internet’s nameservice. A database matching names and IP addresses is distributed on the Internet, and the individual machines know where they should turn for this type of material. • A socket consists of an IP address and a port number, usually separated by a colon (for example, mary.marysHome.dk:100). • We use the portnumber to identify a specific server application that is running on the machine. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  4. A Client Program Sends Data to a Server Program over a Network data 186.45.33.110 A test :160.99.54.340 :35 server program is running, port no. 35 client programis running socket Internet 160.99.54.340 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  5. How to Program the Communication between Programs • The connection between the client- and the server program is established by creating an instance of the java.net.Socket class. • Streams are linked to the Socket object. • If the program are going to send data, it writes to the stream. • If the program are going to receive data, it reads from the stream. • Just in the same way as we work with data files…. • During a test phase, client and server programs can each run in their own Java interpreter on the same machine. • In practice, the machine has to have a network card installed since the software connected to this is also used when both of the programs are running on the same machine. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  6. Communication Betweenthe Programs Show program listing 19.1, pp. 583-585. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  7. Objects That Collaborate over a Network • Repetition from chapter 3: • Client and server are roles that objects play. • Objects cooperate when a client object requests a service by sending a message to a server object. • The server carries out an operation as a reaction to the message. • The server can send responses back to the client. • The objects that communicate with each other can be on different machines figure, page 102 count one vote yes count 25 votes yes Yes No count one vote no count 32 votes yes Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  8. Remote Method Invocation (RMI) • A remoteobject is an object that is running in a Java interpreter on another machine, or in another Java interpreter on the same machine. • RMI is build on socket programming. As programmers, we work with objects and messages, as usual. • A client that is going to send messages to an object has to know the interface for the object. • Up to now, the method heads in the class have functioned as interface. • The interface of remote objects haveto be specified in a Java interface: import java.rmi.*; interface YesNoCounter extends Remote { void increaseNumberOfYes() throws RemoteException; void increaseNumberOfNo() throws RemoteException; void increaseNumberOfYes(int increase) throws RemoteException; void increaseNumberOfNo(int increase) throws RemoteException; int getNumberOfYes() throws RemoteException; int getNumberOfNo() throws RemoteException; } The name of the interface is YesNoCounter. The interface has to be a subinterface of java.rmi.Remote. Every method in the interface may allow java.rmi. RemoteException to be thrown. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  9. import java.rmi.*; import java.rmi.server.*; class YesNoCounterImpl extends UnicastRemoteObject implements YesNoCounter { private int numberOfYes = 0; private int numberOfNo = 0; public YesNoCounterImpl() throws RemoteException { } public synchronized void increaseNumberOfYes() throws RemoteException { System.out.println("The number of yes votes was increased by 1"); numberOfYes++; } public synchronized void increaseNumberOfNo() throws RemoteException { System.out.println("The number of no votes was increased by 1"); numberOfNo++; } public synchronized void increaseNumberOfYes(int increase) throws RemoteException { System.out.println("The number of yes votes was increased by " + increase); numberOfYes += increase; } public synchronized void increaseNumberOfNo(int increase) throws RemoteException { System.out.println("The number of no votes was increased by " + increase); numberOfNo += increase; } public synchronized int getNumberOfYes() throws RemoteException { return numberOfYes; } public synchronized int getNumberOfNo() throws RemoteException { return numberOfNo; } } The Class is the Implementation The name of the class is YesNoCounterImpl. The class has to be a subclass ofjava.rmi.server. UnicastRemoteObject. We always have to create a constructor. The class has to implement the YesNoCounter interface. Methods in mutable classes ought to be synchronized. The println() statements are inserted to log the activities at the server side. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  10. Remote Objects • For an object to be accessible over a network, it has to be an instance of the UnicastRemoteObject class (or a subclass of this class). • An instance of such a class is automatically given its own thread, to keep the object alive indefinitely (or until the program that the object belongs to is aborted). • The object is a server object that waits for queries from potential clients. • The interface of the object is specified by a Java interface, while the implementation is found in a Java class. • Often, the class name is used only after new: YesNoCounter counter = new YesNoCounterImpl(); // like this YesNoCounterImpl counter = new YesNoCounterImpl(); // not like this • In this way we can be sure that we are not sending other messages to the objects than those that are specified in the interface. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  11. The Object Has to be Instantiated by a Program Running at the Server Side import java.rmi.*; class CounterServer { public static void main(String[] args) { try { System.out.println("We'll make a server object"); YesNoCounter counter = new YesNoCounterImpl(); System.out.println("Now it's made!"); Naming.rebind("CountingsLtd", counter); System.out.println("Now we are just waiting for someone to increase our counters..."); } catch (Exception e) { System.out.println("Error: " + e); } } } This thread makes the program run ”for ever”. The object is registered in the bootstrap registry service.. Printout:We'll make a server objectNow it's made!Now we are just waiting for someone to increase our counters...The number of yes votes was increased by 1The number of no votes was increased by 1The number of yes votes was increased by 10The number of no votes was increased by 20 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  12. Here is the Program at the Client Side import java.rmi.*; import java.rmi.server.*; class CounterClient { public static void main(String[] args) { String url = "rmi://localhost/"; try { YesNoCounter counter = (YesNoCounter) Naming.lookup(url + "CountingsLtd"); counter.increaseNumberOfYes(); counter.increaseNumberOfNo(); System.out.println("Number of Yes: " + counter.getNumberOfYes() + " Number of No: " + counter.getNumberOfNo()); counter.increaseNumberOfYes(10); counter.increaseNumberOfNo(20); System.out.println("Number of Yes: " + counter.getNumberOfYes() + " Number of No: " + counter.getNumberOfNo()); } catch (Exception e) { System.out.println("Error: " + e); } } } Printout: Number of Yes: 1 Number of No: 1 Number of Yes: 11 Number of No: 21 Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  13. How to Run the Program System From the MS-DOS Prompt • Download all the java files, including the YesNoCounterImpl_Stub.java file, from the YesNoCounter subdirectory under examples, chapter 19 from [URL Java book]. • Compile the files: • javac *.java • Start the registry service in its own window: • start rmiregistry • Start the server program in its own window: • start java CounterServer • Run the client: • java CounterClient • The rmi registry and the server program have to be stopped by pressing Ctrl+C. Solve the problems, page 593. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  14. What Does Really Happen When a Client Sends Messages to a Remote Object? • The message is sent to a client-side object that functions as a proxy. This object is created automatically. • The message for this proxy is implemented such that the following information is sent over the network: an identification of the remote object, the name of the method that will be called, and the arguments for the method. Here is the link to socket programming. • On the server side, the information is read and the right message is sent to the real object. • If the client is to have a return value, the server will send that to the client-side proxy. • The proxy will send the return value on to the real client. • The proxy object is an instance of the YesNoCounterImpl_Stub class. • The YesNoCounterImpl_Stub.java file is generated by a Java tool called rmic: >rmic –v1.2 YesNoCounterImpl • The stub class is compiled automatically. • The YesNoCounterImpl_Stub implements the YesNoCounter interface. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  15. Passing Arguments • Up to now, the client and the server have run in the same Java interpreter: • The argument values are passed in a method call: • The data type is a primitive data type: The method works with a copy of the argument. • The data type is a reference type: The method gets a copy of the reference, but not of the object itself. The method may change the contents of this object, which usually is an object ”belonging to” the client. • In the same way, values are returned from a non void method. • In an RMI system, arguments will be passed from one Java interpreter to another: • If a remote object is going to be passed, a proxy object is passed. • The recipient can send messages to the actual object through the proxy. • Objects that are instances of “non-remote” classes are passed by serialization. • A client that receives such an object thus receives a copy of the server-side object. • The client can change the object without affecting the server-side object. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  16. When Do We Need Remote Objects, and When Are Serializable Objects Enough? • We have to make remote objects if we want a client to be able to send messages to the object over a network (from one Java interpreter to another). All the clients (and the server) are dealing with the same object. • We can make do with serializable objects if the different Java interpreters can each work with their own copy of the object. Show the Person class from program listing 15.5, pp. 471-475 and program listing 19.4, pp. 596-599. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  17. Checklist: Creating a Simple Distributed System • There are three kinds of classes: • Find out which objects are desirable for a client to be able to send messages to over the network. It’s the classes that these objects are instances of, that have to be handled specially as explained in points 2 and 3 below. • Classes that are used only as parameter type or return type in methods that will be called over the network have to implement java.io.Serializable. • With other classes, we don’t need to do anything special. • For the classes in group 1a) • Make the interface and implementation. Remember the requirements placed on both the interface and implementation class. See slides 8 and 9. Compile. • Run rmic to generate a stub class. Example run: >rmic -v1.2 YesNoCounterImpl • Make the server program. The easiest thing to do is to let it be in the same directory as the interface and implementation. Compile. • Make one or more client programs. A client program needs a compiled interface and a compiled stub class. The easiest thing to do is to have these files in the same directory as the client program. Compile the client program. • Start the registry from the same directory the server program will be running in: >start rmiregistry • Start the server program. • Run any client programs. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  18. Hints for Program Development • Insert a lot of print statements on both the server side and the client side to log the activity. • Restart the registry every time the server program has to be restarted. • Remember to run rmic again if the interface has changed. Solve the problem at pp. 599-600. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  19. RMI and Applets • The client program at slide 12 can be run from any machine that is connected to the Internet. The prerequisite is that a compiled interface and a compiled stub class are accessible on the client side. • What about an applet as a client? • The classes are now distributed by an HTML page containing the name of the applet. • By running in the client’s browser the applet will request compiled interface and stub class. They will automatically be downloded from the same site as the applet. • But – • An applet can only request resources from the computer where it was downloaded from. • The RMI registry and the server objects therefore have to run at this computer. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  20. Deployment Diagrams and UML Components • A distributed system consists of several parts that are running on different computers. • Different parts are dependent on each other such that, for example, the server program has to be started up before a client can run. • In UML, we use a deployment diagram to show these relations. • A UML component is defined as: • “A physical, replaceable part of a system that packages implementation and conforms to and provides the realization of a set of interfaces.” • A UML component has the following characteristics: • In many ways, a component is “bigger” than an object. It usually consists of several objects. • A component is almost independent of other components. It is a physical unit that, together with other components, constitutes a larger system. • A component never works completely alone. It has to be used inside a specific architecture or technology. • A component can be replaced with another component that supports the same interface. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  21. Notation in a Deployment Diagram a node in the network an object at the node component “…realizes the interface…” the arrow shows dependence; here it means that B’s existence depends on A B A Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  22. A Client PC Communicates with an RMI Register and the Component “CountingsLtd” on the Machine server.xxSoft.com Solve the problem at page 602. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  23. A Distributed System with Callback • Every time the number of yes or no quotes are increased, all clients will be alerted. • The server has to keep track of all the clients that are online. • The server has to send messages to the clients (the roles are changed!) – we call it callback. • We must have remote objects on both sides. • We start a server program and two clients: >start rmiregistry >start java CounterServer >start java CounterClient >start java CounterClient • ¨The first dialogs when a client starts: Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  24. Two Clients and a Server Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

  25. Show Program Listings 19.5, 19.6, 19.7 and 19.8 from page 604 and so on. Solve the problems, page 614. Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002The Research Foundation TISIP, http://tisip.no/engelsk/

More Related