Package cruise.umple
Class AbstractServer
java.lang.Object
cruise.umple.AbstractServer
- All Implemented Interfaces:
java.lang.Runnable
- Direct Known Subclasses:
PlaygroundMain
public abstract class AbstractServer
extends java.lang.Object
implements java.lang.Runnable
This file contains material derived from
"Object Oriented Software Engineering" by Lethbridge and Laganiere
and is issued under the open-source
license found at www.lloseng.com
This is a framework for a server that can read and write characters
-
Constructor Summary
Constructors Constructor Description AbstractServer(int port)
CONSTRUCTOR ****************************************************** Constructs a new server.AbstractServer(java.lang.Thread aConnectionListener, int aPort, java.lang.ThreadGroup aClientThreadGroup)
-
Method Summary
Modifier and Type Method Description protected void
clientConnected(ConnectionToClient client)
METHODS DESIGNED TO BE OVERRIDDEN BY CONCRETE SUBCLASSES --------- Hook method called each time a new client connection is accepted.protected void
clientDisconnected(ConnectionToClient client)
protected void
clientException(ConnectionToClient client, java.lang.Throwable exception)
void
close()
Closes the server socket and the connections with all clients.void
delete()
int
getBacklog()
The maximum queue length; i.e.java.lang.Thread[]
getClientConnections()
java.lang.ThreadGroup
getClientThreadGroup()
The thread group associated with client threads.java.lang.Thread
getConnectionListener()
The connection listener thread.int
getNumberOfClients()
Counts the number of clients currently connected.int
getPort()
The port numberboolean
getReadyToStop()
Indicates if the listening thread is ready to stop.java.net.ServerSocket
getServerSocket()
INSTANCE VARIABLES ********************************************* The server socket: listens for clients who want to connect.int
getTimeout()
The server timeout while for accepting connections.protected abstract void
handleMessageFromClient(java.lang.String msg, ConnectionToClient client)
Handles a command sent from one client to the server.boolean
isClosed()
Returns true if the server is closed.boolean
isListening()
ACCESSING METHODS ------------------------------------------------ Returns true if the server is ready to accept new clients.void
listen()
INSTANCE METHODS ************************************************* Begins the thread that waits for new clients.protected void
listeningException(java.lang.Throwable exception)
Hook method called when the server stops accepting connections because an exception has been raised.void
run()
RUN METHOD ------------------------------------------------------- Runs the listening thread that allows clients to connect.void
sendToAllClients(java.lang.String msg)
Sends a message to every client connected to the server.protected void
serverClosed()
Hook method called when the server is clased.protected void
serverStarted()
Hook method called when the server starts listening for connections.protected void
serverStopped()
Hook method called when the server stops accepting connections.boolean
setBacklog(int aBacklog)
boolean
setClientThreadGroup(java.lang.ThreadGroup aClientThreadGroup)
boolean
setConnectionListener(java.lang.Thread aConnectionListener)
boolean
setPort(int aPort)
boolean
setReadyToStop(boolean aReadyToStop)
boolean
setServerSocket(java.net.ServerSocket aServerSocket)
boolean
setTimeout(int aTimeout)
void
stopListening()
Causes the server to stop accepting new connections.java.lang.String
toString()
-
Constructor Details
-
AbstractServer
public AbstractServer(java.lang.Thread aConnectionListener, int aPort, java.lang.ThreadGroup aClientThreadGroup) -
AbstractServer
public AbstractServer(int port)CONSTRUCTOR ****************************************************** Constructs a new server.- Parameters:
port
- the port number on which to listen.
-
-
Method Details
-
setServerSocket
public boolean setServerSocket(java.net.ServerSocket aServerSocket) -
setConnectionListener
public boolean setConnectionListener(java.lang.Thread aConnectionListener) -
setPort
public boolean setPort(int aPort) -
setTimeout
public boolean setTimeout(int aTimeout) -
setBacklog
public boolean setBacklog(int aBacklog) -
setClientThreadGroup
public boolean setClientThreadGroup(java.lang.ThreadGroup aClientThreadGroup) -
setReadyToStop
public boolean setReadyToStop(boolean aReadyToStop) -
getServerSocket
public java.net.ServerSocket getServerSocket()INSTANCE VARIABLES ********************************************* The server socket: listens for clients who want to connect. -
getConnectionListener
public java.lang.Thread getConnectionListener()The connection listener thread. -
getPort
public int getPort()The port number -
getTimeout
public int getTimeout()The server timeout while for accepting connections. After timing out, the server will check to see if a command to stop the server has been issued; it not it will resume accepting connections. Set to half a second by default. -
getBacklog
public int getBacklog()The maximum queue length; i.e. the maximum number of clients that can be waiting to connect. Set to 10 by default. -
getClientThreadGroup
public java.lang.ThreadGroup getClientThreadGroup()The thread group associated with client threads. Each member of the thread group is aConnectionToClient
. -
getReadyToStop
public boolean getReadyToStop()Indicates if the listening thread is ready to stop. Set to false by default. -
delete
public void delete() -
listen
public void listen() throws java.io.IOExceptionINSTANCE METHODS ************************************************* Begins the thread that waits for new clients. If the server is already in listening mode, this call has no effect.- Throws:
java.io.IOException
- if an I/O error occurs when creating the server socket.
-
stopListening
public void stopListening()Causes the server to stop accepting new connections. -
close
public void close() throws java.io.IOExceptionCloses the server socket and the connections with all clients. Any exception thrown while closing a client is ignored. If one wishes to catch these exceptions, then clients should be individually closed before calling this method. The method also stops listening if this thread is running. If the server is already closed, this call has no effect.- Throws:
java.io.IOException
- if an I/O error occurs while closing the server socket.
-
sendToAllClients
public void sendToAllClients(java.lang.String msg)Sends a message to every client connected to the server. This is merely a utility; a subclass may want to do some checks before actually sending messages to all clients. This method can be overriden, but if so it should still perform the general function of sending to all clients, perhaps after some kind of filtering is done. Any exception thrown while sending the message to a particular client is ignored.- Parameters:
msg
- Object The message to be sent
-
isListening
public boolean isListening()ACCESSING METHODS ------------------------------------------------ Returns true if the server is ready to accept new clients.- Returns:
- true if the server is listening.
-
isClosed
public boolean isClosed()Returns true if the server is closed.- Returns:
- true if the server is closed.
- Since:
- version 2.2
-
getNumberOfClients
public int getNumberOfClients()Counts the number of clients currently connected.- Returns:
- the number of clients currently connected.
-
run
public void run()RUN METHOD ------------------------------------------------------- Runs the listening thread that allows clients to connect. Not to be called.- Specified by:
run
in interfacejava.lang.Runnable
-
clientConnected
METHODS DESIGNED TO BE OVERRIDDEN BY CONCRETE SUBCLASSES --------- Hook method called each time a new client connection is accepted. The default implementation does nothing. This method does not have to be synchronized since only one client can be accepted at a time.- Parameters:
client
- the connection connected to the client.
-
listeningException
protected void listeningException(java.lang.Throwable exception)Hook method called when the server stops accepting connections because an exception has been raised. The default implementation does nothing. This method may be overriden by subclasses.- Parameters:
exception
- the exception raised.
-
serverStarted
protected void serverStarted()Hook method called when the server starts listening for connections. The default implementation does nothing. The method may be overridden by subclasses. -
serverStopped
protected void serverStopped()Hook method called when the server stops accepting connections. The default implementation does nothing. This method may be overriden by subclasses. -
serverClosed
protected void serverClosed()Hook method called when the server is clased. The default implementation does nothing. This method may be overriden by subclasses. When the server is closed while still listening, serverStopped() will also be called. -
handleMessageFromClient
Handles a command sent from one client to the server. This MUST be implemented by subclasses, who should respond to messages. This method is called by a synchronized method so it is also implcitly synchronized.- Parameters:
msg
- the message sent.client
- the connection connected to the client that sent the message.
-
toString
public java.lang.String toString()- Overrides:
toString
in classjava.lang.Object
-
getClientConnections
public final java.lang.Thread[] getClientConnections() -
clientDisconnected
-
clientException
-