Title: The Object ClientServer Framework OCSF
1The Object Client-Server Framework (OCSF)
ConnectionToClient
AbstractServer
AbstractClient
openConnection
sendToClient
listen
sendToServer
stopListening
close
close
closeConnection
setInfo
sendToAllClients
connectionClosed
getClientConnections
getInfo
connectionException
clientConnected
clientDisconnected
connectionEstablished
clientException
handleMessageFromServer
serverStarted
serverStopped
listeningException
serverClosed
Software installed on Client host.
Software installed on Server host.
handleMessageFromClient
2The Methods Provided By The OCSF
- The OCSF provides three types of methods, which
are accessible by application developers - Service methods, which represent the API of the
OCSF - Controlling methods.
- Accessing methods.
- Slot methods, also called callback methods
- These must be implemented.
- These methods are declared abstract in the OCSF,
and, therefore, a concrete implementation is
enforced. - Hook methods , also called callback methods
- An application developer may choose optionally to
implement these methods. - These methods are declared with empty bodies in
the OCSF, and, therefore, the application
developer has the option to override them (or
not).
3The API of AbstractClient(Service Methods)
- Controlling methods
- openConnection
- Opens a connection with the server.
- Requires the host name and port number of the
server. - closeConnection
- Closes the socket as well as the I/O streams.
- sendToServer
- Sends the specified object to the server using
the output stream. - The specified object is typically a string
message for another client. - The message could be any type of object that
supports serialization.
4The API of AbstractClient(Service Methods)
- Accessing methods
- isConnected
- Returns (boolean) if the client is connected.
- getHost
- Returns the instance variable (String host) host
name of the server. - setHost
- Sets the instance variable (String host) the
host name of the server. - getPort
- Returns the instance variable (String port) the
port of the server. - setPort
- Sets the instance variable (String port) the
port of the server. - getInetAddress
- Returns the clients Internet address.
5The Slot Method of AbstractClient(A Callback
Method)
- handleMessageFromServer
- Must be implemented.
- At any time after a client has been connected to
a server, the server may send the client a
message asynchronously. - The OCSF receives this message from the
InputStream of the Socket, and then calls the
method handleMessageFromServer. - The Client app must provide a concrete
implementation, and do something useful, like
displaying the message.
Why is this called a call back?
Why does this method have to be implemented?
6The Hook Methods of AbstractClient(A Callback
Method)
- connectionEstablished
- Method may be overridden.
- After the openConnection method has been called
by the client app, the OCSF will call the
connectionEstablished method when a connection
has been established. - The OCSF defines this method with an empty body.
- An application developer can override this method
and do something interesting, such as let the
user know a connection has been established.
Why is this called a call back?
7The Hook Methods of AbstractClient(A Callback
Method)
- connectionClosed
- Method may be overridden.
- After the closeConnection method has been called
by the client app, the OCSF will call the
connectionEstablished method when a connection
has been closed. - The OCSF defines this method with an empty body.
- An application developer can override this method
and do something interesting, such as let the
user know a connection has been closed.
Why is this called a call back?
8The Hook Methods of AbstractClient(A Callback
Method)
- connectionException
- Method may be overridden.
- If the connection to the server is unexpectedly
interrupted for some reason, the OCSF catches the
exception and then calls the connectionException
method. - The OCSF defines this method with an empty body.
- An application developer can override this method
and do something interesting, such as let the
user know a problem has been encountered.
Why is this called a call back?
9Instance Variables of AbstractClient
- clientSocket
- A Socket which keeps all the information about
the connection to the server. - input, output
- Two streams, an ObjectOutputStream and an
ObjectInputStream. - clientReader
- A Thread that runs using AbstractClients run
method to listen and respond to server messages. - host, port
- The host name and port number of the server.
10Using AbstractClient
- An application developer, who uses the OCSF to
develop a client application, would use the OCSF
as follows - Create a subclass of AbstractClient.
- Implement handleMessageFromServer slot method.
- Write code that
- Creates an instance of the new subclass.
- Calls openConnection.
- Sends messages to the server using the
sendToServer service method. - Optionally Implement the hook methods
- connectionEstablished
- connectionClosed
- connectionException
11The Server Side AbstractServer
- Provides the basic services to create and open a
server socket. - Has a thread which listens for new connections.
- When a connection request from a client is
accepted, a new ConnectionToClient is created.
ConnectionToClient
AbstractServer
sendToClient
listen
stopListening
close
close
setInfo
sendToAllClients
getClientConnections
getInfo
clientConnected
clientDisconnected
clientException
serverStarted
serverStopped
listeningException
serverClosed
handleMessageFromClient
12The Server Side ConnectionToClient
- Provides the basic services to exchange data with
the connected client. - Manages the connection with the connected client.
- Runs as a thread.
ConnectionToClient
AbstractServer
sendToClient
listen
stopListening
close
close
setInfo
sendToAllClients
getClientConnections
getInfo
clientConnected
clientDisconnected
clientException
serverStarted
serverStopped
listeningException
serverClosed
handleMessageFromClient
13The API of AbstractServer(Service Methods)
- Controlling methods
- listen
- stopListening
- close
- sendToAllClients
- Accessing methods
- isListening
- getClientConnections
- getPort
- setPort
- setBacklog
14The API of AbstractServer(listen Method)
- Creates a serverSocket.
- Creates a thread in which the run method listens
for connection requests through the accept()
method of the serverSocket object. - When a connection is accepted, a new client
Socket is created, and a reference to this object
is passed to the constructor of
ConnectionToClient. - The ConnectionToClient object creates input and
output streams associated with this Socket
object, and then starts a thread to exchange data
and manage the connection with the connected
client.
15The Hook Methods of AbstractServer (A Callback
Method)
- Methods that may be overridden
- serverStarted
- clientConnected
- clientDisconnected
- clientException
- serverStopped
- listeningException
- serverClosed
16The Slot Method of AbstractServer (A Callback
Method)
- Method that must be implemented
- handleMessageFromClient
17The API of ConnectionToClient (Service Methods)
- Controlling methods
- sendToClient
- close
- Accessing methods
- getInetAddress
- setInfo
- getInfo
18Using AbstractServer and ConnectionToClient
- An application developer, who uses the OCSF to
develop a server application, would use the OCSF
as follows - Create a subclass of AbstractServer
- Implement the slot method handleMessageFromClient
- Write code that
- Creates an instance of the subclass of
AbstractServer - Calls the listen method
- Sends messages to clients, using
- the getClientConnections and sendToClient
service methods - or sendToAllClients
- Implement one or more of the other callback
methods
19Internals of AbstractServer and ConnectionToClient
- The setInfo and getInfo methods make use of a
Java class called HashMap. HashMap is an
associative array. Imagine you can index an
array with strings and you are not far off. E.x.
myHashMaprobots - Many methods in the server side are synchronized.
- The collection of instances of ConnectionToClient
is stored using a special class called
ThreadGroup. - The server must pause from listening every 500ms
to see if the stopListening method has been
called. - if not, then it resumes listening immediately.
20An Instant Messaging Application GUIChat
AbstractClient
Frame
AbstractServer
listen
SimpleClient
handleMessageFromServer connectionClosed connectio
nException
ClientFrame
close
open
EchoServer
send quit
handleMessageFromClient
ConnectionToClient
serverStarted
1
serverStopped
main