Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java the UML Way http://www.tisip.no/JavaTheUmlWay/ Distributed Systems with Socket Programming and RMI What is a distributed system? Sockets and socket programming Objects collaborating over the net (RMI) RMI, in the depth Summary: How to create a simple distributed system RMI and applets Deployment diagram Distributed system with callback versjon 2002-04-17 page 2 page 3-6 page 7-13 page 14-16 page 17-18 page 19 page 20-22 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19 What Is a Distributed System? • A distributed system 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 2 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 name service. 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 port number 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 3 A Client Program Sends Data to a Server Program over a Network 186.45.33.110 server program is running, port no. 35 client program is running 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 4 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 5 Communication Between the Programs Client Server open server socket and wait (server.accept()) read the name of the server machine from the console and set up a connection to the server program open communication streams open communication streams send (write) introductory text to the client program receive (read) the introductory text from the server program read aLine from the console send (write) aLine to the server program receive (read) aLine from the client print aLine to the console send (write) answer to the client program receive (read) response from the server program 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 6 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 Yes No 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 7 Remote Method Invocation (RMI) • • • • • A remote object 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 have to be specified in a Java interface: The name of the interface is YesNoCounter. The interface has to be a subinterface of java.rmi.Remote. 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; Every method in the interface int getNumberOfNo() throws RemoteException; 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 8 import java.rmi.*; The Class is the Implementation import java.rmi.server.*; class YesNoCounterImpl extends UnicastRemoteObject implements YesNoCounter { private int numberOfYes = 0; The name of the class private int numberOfNo = 0; is public YesNoCounterImpl() throws RemoteException { YesNoCounterImpl. } public synchronized void increaseNumberOfYes() throws RemoteException { System.out.println("The number of yes votes was increased by 1"); The class has to be a subclass numberOfYes++; ofjava.rmi.server. } UnicastRemoteObject. public synchronized void increaseNumberOfNo() throws RemoteException { System.out.println("The number of no votes was increased by 1"); numberOfNo++; } We always have to public synchronized void increaseNumberOfYes(int increase) throws RemoteException { create a System.out.println("The number of yes votes was increased by " + increase); constructor. numberOfYes += increase; } public synchronized void increaseNumberOfNo(int increase) throws RemoteException { System.out.println("The number of no votes was increased by " + increase); The class has to implement the numberOfNo += increase; YesNoCounter interface. } public synchronized int getNumberOfYes() throws RemoteException { return numberOfYes; Methods in mutable } The println() statements are inserted classes ought to be public synchronized int getNumberOfNo() throws RemoteException { to log the activities at the server side. synchronized. return numberOfNo; } Only to be used in connection with the book "Java the UML Way", by Else Lervik and Vegard B. Havdal. Chapter 19, page 9 } ISBN 0-470-84386-1, John Wiley & Sons Ltd 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 10 The Object Has to be Instantiated by a Program Running at the Server Side import java.rmi.*; class CounterServer { This thread makes the public static void main(String[] args) { program run ”for ever”. try { System.out.println("We'll make a server object"); The object is registered YesNoCounter counter = new YesNoCounterImpl(); in the bootstrap registry System.out.println("Now it's made!"); service.. Naming.rebind("CountingsLtd", counter); System.out.println("Now we are just waiting for someone to increase our counters..."); Printout: } catch (Exception e) { We'll make a server object System.out.println("Error: " + e); Now it's made! Now we are just waiting for someone to increase } our counters... } The number of yes votes was increased by 1 The number of no votes was increased by 1 } The number of yes votes was increased by 10 The 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 11 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 12 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 13 What Does Really Happen When a Client Sends Messages to a Remote Object? 1. The message is sent to a client-side object that functions as a proxy. This object is created automatically. 2. 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. 3. On the server side, the information is read and the right message is sent to the real object. 4. If the client is to have a return value, the server will send that to the client-side proxy. 5. 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 14 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 15 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 16 Checklist: Creating a Simple Distributed System 1. There are three kinds of classes: a) 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. b) 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. c) With other classes, we don’t need to do anything special. 2. For the classes in group 1a) a) Make the interface and implementation. Remember the requirements placed on both the interface and implementation class. See slides 8 and 9. Compile. b) Run rmic to generate a stub class. Example run: >rmic -v1.2 YesNoCounterImpl 3. Make the server program. The easiest thing to do is to let it be in the same directory as the interface and implementation. Compile. 4. 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. 5. Start the registry from the same directory the server program will be running in: >start rmiregistry 6. Start the server program. 7. 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 17 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 18 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 19 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 20 Notation in a Deployment Diagram a node in the network an object at the node component “…realizes the interface…” B A the arrow shows dependence; here it means that B’s existence depends on 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 21 A Client PC Communicates with an RMI Register and the Component “CountingsLtd” on the Machine server.xxSoft.com server.xxSoft.com :CounterServer Countings Ltd YesNoCounter :rmiregistry :Internet :clientPC :application 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 22 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 23 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 24 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 2002 The Research Foundation TISIP, http://tisip.no/engelsk/ Chapter 19, page 25