© lethbridge/laganière 2001 chap. 3: basing development on reusable technology 1 let’s get...

24
© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusa ble Technology 1 Let’s get started. Let’s start by selecting an architecture from among many… We will use a client server architecture Select client-server architecture because we are going to build a client-server framework upon which we can later build applications. Once we build a client-server framework, we can then SEE how the framework works. Once we see how the framework workds, we can then REUSE this framework to build applications.

Post on 21-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

1

Let’s get started.

Let’s start by selecting an architecture from among many…

We will use a client server architecture

Select client-server architecture because we are going to build a client-server framework upon which we can later build applications.

Once we build a client-server framework, we can then SEE how the framework works.

Once we see how the framework workds, we can then REUSE

this framework to build applications.

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

2

3.4 The Client-Server Architecture

A distributed system is a system in which: • computations are performed by separate programs• … normally running on separate pieces of hardware• … that co-operate to perform the task of the system.

Server:• A program that provides a service for other programs

that connect to it using a communication channelClient

• A program that accesses a server (or several servers) to obtain services

• A server may be accessed by many clients simultaneously

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

3

Sequence of activities in a client-server system

1. The server starts running 2. The server waits for clients to connect. (listening) 3. Clients start running and perform operations

— Some operations involve requests to the server4. When a client attempts to connect, the server accepts

the connection (if it is willing) 5. The server waits for messages to arrive from

connected clients6. When a message from a client arrives, the server

takes some action in response, then resumes waiting 7. Clients and servers continue functioning in this

manner until they decide to shut down or disconnect

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

4

A server program communicating with two client programs

disconnect

send message

disconnect

send reply

listen for connections

stop listening

connect

Client 2

send message

connect

Client 1Server

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

5

Note:

Certainly possible for the same program to be both a client as well as a server at the same time.

A database server might need to be connected to another server in order to get more data.

Here it is both server and a client of other server.

Also possible for clients and servers to share the same box.

More likely, separate computers and generally in different locations.

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

6

Alternatives to the client server architecture

• Have a single program on one computer that does everything

• Have no communication

— Each computer performs the work separately- Could have common communications areas.

• Have some mechanism other than client-server communication for exchanging information

—E.g. one program writes to a database; the other reads from the database

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

7

Advantages of client-server systems

• The work can be distributed among different machines • The clients can access the server’s functionality from a

distance • The client and server can be designed separately• They can both be simpler• All the data can be kept centrally at the server • Conversely, data can be distributed among many

different geographically-distributed clients or servers• The server can be accessed simultaneously by many

clients • Competing clients can be written to communicate with

the same server, and vice-versa

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

8

Example of client-server systems

• The World Wide Web

• Email

• Network File System

• Transaction Processing System

• Remote Display System

• Communication System

• Database System

In order to understand the framework we are about to build, let’s examine the required functionality.

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

9

Activities of a server

1. Initializes itself

2. Starts listening for clients

3. Handles the following types of events originating from clients

1. accepts connections

2. responds to messages

3. handles client disconnection

4. May stop listening

too many clients;

periodic maintenance…

5. Must cleanly terminate

Waiting for Connections

Handling a Connection

do: react to messages

handle disconnection

Waiting

start listening stop listening

For each connection:

For the server as a whole:

accept connection

Initializing

terminate

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

10

Activities of a client

1. Initializes itself

2. Initiates a connection

3. Sends messages

4. Handles the following types of events originating from the server

1. responds to messages

2. handles server disconnection

5. Must cleanly terminate

initiate a connection to a server

respond to events triggered by the server

do: respond to messages andhandle server disconnection

interact with the user, sending messages to the server as necessary

terminate

initialize

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

11

Threads in a client-server system – really, both must have concurrent threads of control.

kill clientdisconnect

reply to message

create

reply to message

connect

display disconnect

display reply

display reply

create

wait for connections

interact with user

wait for server events

wait for messages:

client A

interact with server user

wait for messages:

client B

send message

send message

Client Side (Client A) Server Side

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

12

Thin- versus fat-client systems

Thin-client system (a)

• Client is made as small as possible

• Most of the work is done in the server.

• Client easy to download over the network

• Client machines are usually inexpensive

• But there are downsides:

—Bandwidth required may be prohibitive for large n.Light computation

Light computation

Heavy computation

Heavy computation

resultsfor display

simplecommands

requestsfor services

resultsof requests

a b

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

13

Thin- versus fat-client systems

Fat-client system (b)

• As much work as possible is delegated to the clients.

• Server can handle more clients

• Bandwidth reduced

Downsides: 1. more expensive client machines;

2. all copies of software need to be upgraded on all clients when changes made

3. Maintenance made more difficult.Light computation

Light computation

Heavy computation

Heavy computation

resultsfor display

simplecommands

requestsfor services

resultsof requests

a b

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

14

Communications protocols

• The messages the client sends to the server form a language.

— The server has to be programmed to understand that language.

• The messages the server sends to the client also form a language.

— The client has to be programmed to understand that language.

• When a client and server are communicating, they are in effect having a conversation using these two languages

• The two languages and the rules of the conversation, taken together, are called the protocol

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

15

Tasks to perform to develop client-server applications

1. Design the primary work to be performed by both client and server

2. Design how the work will be distributed

3. Design the details of the set of messages that will be set

1. These can be nicely modeled with current tools.

4. Design the mechanism for 1. Initializing

2. Handling connections

3. Sending and receiving messages

4. Terminating

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

16

3.5 Technology Needed to Build Client-Server Systems (TCP/IP)

Internet Protocol (IP)

• Route messages from one computer to another

• Long messages are normally split up into small pieces (SDLC, packets, etc.)

Transmission Control Protocol (TCP)

• Handles connections between two computers

• Computers can then exchange many IP messages over a connection

• Assures that the messages have been satisfactorily received

A host has an IP address and a host name Several servers can run on the same host. Each server is identified by a port number (0 to 65535). To initiate communication with a server, a client must know

both the host name and the port number

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

17

How do we do all this in Java?

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

18

Establishing a connection in Java

The java.net package• Permits the creation of a TCP/IP connection between two

applications

Before a connection can be established, the server must start listening to one of the ports for a client hook up:It creates an instance of ServerSocket and listens!

ServerSocket serverSocket = newServerSocket(port);

For a client to connect to a server: the client executes: Socket clientSocket= new Socket(host, port);Identifies hostname or IP address and the port number of the server (who is hopefully listening).

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

19

Server must have following code embedded in a loop:

Server will wait – awaiting a connect request from a client.

• Socket clientSocket = serverSocket.accept();

•Once it gets a connect request from a client, the server will establish a socket to handle the new connections.

•Once both client and server have instances of Socket, we can now communicate in both directions.

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

20

Exchanging information in Java

• Each program uses an instance of — InputStream to receive messages from the other

program— OutputStream to send messages to the other program—These are found in package java.io

output = clientSocket.getOutputStream();

input = clientSocket.getInputStream();

One’s output stream is the other’s input stream.

But the transmission is in bytes!

Java provides us facilities to convert raw bytes into other forms.

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

21

Sending and receiving messages

• without any filters (raw bytes)output.write(msg);msg = input.read();

• or using DataInputStream / DataOutputStream filtersoutput.writeDouble(msg);msg = input.readDouble();These filters allow transmission of Java’s primitive types, such as float, int, char, etc.

• or using ObjectInputStream / ObjectOutputStream filtersoutput.writeObject(msg);msg = input.readObject();Java allows the transmission of objects with these filters.

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

22

Sending Objects

Objects must implement the interface java.io.Serializable

Data in the instance variables must also be serializable.

If so, Java uses a process called serialization to convert objects in the ObjectOutputStream into binary streams for transmission and will convert binary streams back into objects on the other end via ObjectInputStream.

We also use serialization to save objects to binary files.

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

23

Wrappers

In order to use the object input and object output streams, one must ‘wrap’ the binary input/output streams, as in:

(Recall: output = clientSocket.getOutputStream();) (byte data)

output = new ObjectOutputStream (clientSocket.getOutputStream());

Now, can send object via:output.writeObject(msg); and in order to receive objects, you create an

object input stream:

input = new ObjectInputStream(clientSocket.getInputStream());

Then, the following statement must be executed in a loop:

msg = input.readObject();

The readObject method will wait until an object is received over the socket, or until an I/O error occurs. An I/O error would occur if the program at the other end of the connection is terminated.

© Lethbridge/Laganière 2001 Chap. 3: Basing Development on Reusable Technology

24

Now, we are ready to implement a client-server framework for the exchange of objects.