Top Banner
versjon 2002-04-17 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 Java the UML Way http://www.tisip.no/JavaTheUmlWay/ Distributed Systems with Socket Programming and RMI What is a distributed system? page 2 Sockets and socket programming page 3-6 Objects collaborating over the net (RMI) page 7-13 RMI, in the depth page 14-16 Summary: How to create a simple distributed system page 17-18 RMI and applets page 19
25

Distributed Systems with Socket Programming and RMI

Jan 12, 2016

Download

Documents

Distributed Systems with Socket Programming and RMI. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Distributed Systems with  Socket Programming and RMI

versjon 2002-04-17Only 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

Java the UML Wayhttp://www.tisip.no/JavaTheUmlWay/

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

Page 2: Distributed Systems with  Socket Programming and RMI

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

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.

Page 3: Distributed Systems with  Socket Programming and RMI

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

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.

Page 4: Distributed Systems with  Socket Programming and RMI

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

A Client Program Sends Data to a Server Program over a Network

Internet

client programis running

server program is running,port no. 35

160.99.54.340

A test :160.99.54.340 :35

socket

data

186.45.33.110

Page 5: Distributed Systems with  Socket Programming and RMI

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

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.

Page 6: Distributed Systems with  Socket Programming and RMI

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

read the name of the server machine from the consoleand set up a connection to the server program

open server socket and wait (server.accept())

open communication streams open communication streams

read aLine from the console

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

Client

Server

receive (read) the introductory text from the server program

send (write) introductory text to the client program

send (write) aLine to the server program

Communication Betweenthe Programs

Show program listing 19.1, pp. 583-585.

Page 7: Distributed Systems with  Socket Programming and RMI

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

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

count one vote no

count one vote yes count 25 votes yes

count 32 votes yes

Page 8: Distributed Systems with  Socket Programming and RMI

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

Every method in the interface may allow java.rmi.

RemoteException to be thrown.

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:

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 interface has to be a subinterface of

java.rmi.Remote.

The name of the interface is

YesNoCounter.

Page 9: Distributed Systems with  Socket Programming and RMI

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 9

The class has to implement the YesNoCounter interface.

The name of the class is

YesNoCounterImpl.

The Class is the Implementationimport 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 has to be a subclassofjava.rmi.server.

UnicastRemoteObject.

Methods in mutable classes ought to be

synchronized.

We always have to create a

constructor.

The println() statements are insertedto log the activities at the server side.

Page 10: Distributed Systems with  Socket Programming and RMI

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

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.

Page 11: Distributed Systems with  Socket Programming and RMI

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

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); } }}

The object is registered in the bootstrap registry

service..

This thread makes theprogram run ”for ever”.

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

Page 12: Distributed Systems with  Socket Programming and RMI

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

Here is the Program at the Client Sideimport 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: 1Number of Yes: 11 Number of No: 21

Page 13: Distributed Systems with  Socket Programming and RMI

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

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.

Page 14: Distributed Systems with  Socket Programming and RMI

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

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.

Page 15: Distributed Systems with  Socket Programming and RMI

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

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.

Page 16: Distributed Systems with  Socket Programming and RMI

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

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.

Page 17: Distributed Systems with  Socket Programming and RMI

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

Checklist: Creating a Simple Distributed System1. 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.

Page 18: Distributed Systems with  Socket Programming and RMI

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

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.

Page 19: Distributed Systems with  Socket Programming and RMI

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

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.

Page 20: Distributed Systems with  Socket Programming and RMI

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

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.

Page 21: Distributed Systems with  Socket Programming and RMI

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

Notation in a Deployment Diagram

a node in the network

an object at the node

component

“…realizes the interface…”

the arrow shows dependence; here itmeans that B’s existence depends on A

B A

Page 22: Distributed Systems with  Socket Programming and RMI

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

Countings Ltd

server.xxSoft.com

:application

:clientPC

YesNoCounter

:Internet

:rmiregistry

:CounterServer

A Client PC Communicates with an RMI Register and the Component “CountingsLtd” on the Machine server.xxSoft.com

Solve the problem at page 602.

Page 23: Distributed Systems with  Socket Programming and RMI

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

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:

Page 24: Distributed Systems with  Socket Programming and RMI

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

Two Clients and a Server

Page 25: Distributed Systems with  Socket Programming and RMI

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

Show Program Listings 19.5, 19.6, 19.7 and 19.8 from page 604 and so on.

Solve the problems, page 614.