simpleNetworking
Class Server

java.lang.Object
  extended by simpleNetworking.Server

public class Server
extends Object

 A simple Server that you can use in your programs.
 
 It has methods for:
   -- reading from each of the Clients attached to this Server
   -- writing to each of the Clients attached to this Server
   -- closing this Server's resources
 
 It does a minimal-security validation protocol with each Client
 who seeks to connect (the Client must use the Client's side of that protocol).
 
 You can construct this Server directly, but it is probably easier for you to:
   1. Construct a new MultiServer.  It constructs a Server for you.
   2. Use the MultiServer's getServer(int n) method to get the Server
      after that Server has been connected to n validated Clients.
 For example, if you want a Server that communicates with 2 Clients, use:
     MultiServer multiServer = new MultiServer();
     Server server = multiServer.getServer(2);
        ... code that uses
        ...      server.readLine
        ...      server.writeLine
        ...      server.close
        ... as you see fit.
 

Author:
David Mutchler, based on the Java Tutorials on networking. May, 2009.

Field Summary
(package private) static String PASSWORD
          Password shared by the Server and all Clients in this minimal-security protocol.
 
Constructor Summary
Server()
          Prepares to communicate with Clients, with no Clients accepted so far.
Server(Socket socketToCommunicateWithClient)
          Prepares to communicate with Clients, with the given Client as the sole Client accepted so far (assuming that the given Client passes the validation protocol).
 
Method Summary
 void addClient(Socket socketToCommunicateWithClient)
          Adds the given Client to the list of clients with which this Server can communicate, if the Client passes the validation protocol.
 void close()
          For every Client currently associated with this Server, closes the resources associated with communicating with that Client.
 void close(int client)
          Closes the resources associated with communicating to the given Client.
 int numberOfValidatedClients()
          Returns the number of validated Clients currently associated with this Server.
 String readLine()
          Convenience method intended for when there is a single Client.
 String readLine(int client)
          Returns the next line available from this Server's nth Client, where n is the given parameter, stripping the terminating newline.
 String readLineIfReady()
          Convenience method intended for when there is a single Client.
 String readLineIfReady(int client)
          Returns the next line available from this Server's nth Client, where n is the given parameter, stripping the terminating newline.
protected  boolean validate(ReaderWriter connectionToClient)
          To validate a proposed Client: -- The Client should send the (announced) password to this Server.
 void writeLine(String stringToWrite)
          Convenience method intended for when there is a single Client.
 void writeLine(String stringToWrite, int client)
          Sends the given String, appending a newline to it, to this Server's nth Client, where n is the given parameter.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PASSWORD

static final String PASSWORD
Password shared by the Server and all Clients in this minimal-security protocol. Change the password if you wish.

See Also:
Constant Field Values
Constructor Detail

Server

public Server()
Prepares to communicate with Clients, with no Clients accepted so far.


Server

public Server(Socket socketToCommunicateWithClient)
       throws SignatureException,
              IOException
Prepares to communicate with Clients, with the given Client as the sole Client accepted so far (assuming that the given Client passes the validation protocol).

Parameters:
socketToCommunicateWithClient - Socket to the sole Client with whom this Server can communicate so far (assuming that the Client passes the validation protocol).
Throws:
IOException - if unable to construct a Reader/Writer to the Client.
SignatureException - if unable to validate the given Client
Method Detail

addClient

public void addClient(Socket socketToCommunicateWithClient)
               throws SignatureException,
                      IOException
Adds the given Client to the list of clients with which this Server can communicate, if the Client passes the validation protocol.

Parameters:
socketToCommunicateWithClient - Socket to the Client to be added to the list of clients with whom this Server can communicate, if the Client passes the validation protocol.
Throws:
IOException - if unable to construct a Reader/Writer to the Client.
SignatureException - if unable to validate the given Client

readLine

public String readLine(int client)
                throws IOException,
                       IndexOutOfBoundsException
Returns the next line available from this Server's nth Client, where n is the given parameter, stripping the terminating newline. Blocks (waits) if no line is available yet.

Parameters:
client - Index in the array of Clients for this Server, indicating which Client from which to read.
Returns:
the next line available from the nth Client, but with the terminating newline stripped.
Throws:
IOException - if an IO error occurs while reading.
IndexOutOfBoundsException - if the given index of the Client is out of bounds.

readLine

public String readLine()
                throws IOException
Convenience method intended for when there is a single Client.

Returns:
the next line available from the first (and presumably only) Client, but with the terminating newline stripped.
Throws:
IOException - if an IO error occurs while reading.

readLineIfReady

public String readLineIfReady(int client)
                       throws IOException,
                              IndexOutOfBoundsException
Returns the next line available from this Server's nth Client, where n is the given parameter, stripping the terminating newline. Returns immediately (returning null) if no line is available yet.

Parameters:
client - Index in the array of Clients for this Server, indicating which Client from which to read.
Returns:
the next line available from the nth Client, but with the terminating newline stripped, or null if no line is available yet.
Throws:
IOException - if an IO error occurs while reading.
IndexOutOfBoundsException - if the given index of the Client is out of bounds.

readLineIfReady

public String readLineIfReady()
                       throws IOException
Convenience method intended for when there is a single Client.

Returns:
the next line available from the first (and presumably only) Client, but with the terminating newline stripped, or null if no line is available yet.
Throws:
IOException - if an IO error occurs while reading.

writeLine

public void writeLine(String stringToWrite,
                      int client)
               throws IOException,
                      IndexOutOfBoundsException
Sends the given String, appending a newline to it, to this Server's nth Client, where n is the given parameter.

Parameters:
stringToWrite - String to send (with a newline appended) to this Server's nth Client.
client - Index in the array of Clients for this Server, indicating which Client to which to write.
Throws:
IOException - if an error occurs while writing.
IndexOutOfBoundsException - if the given index of the Client is out of bounds.

writeLine

public void writeLine(String stringToWrite)
               throws IOException
Convenience method intended for when there is a single Client.

Parameters:
stringToWrite - String to send (with a newline appended) to this Server's first (and presumably only) Client.
Throws:
IOException - if an IO error occurs while reading.

validate

protected boolean validate(ReaderWriter connectionToClient)
To validate a proposed Client: -- The Client should send the (announced) password to this Server. -- This Server receives the password and checks whether it is OK. -- If it is not OK, this Server returns that the proposed connection is not validated and this Server does no further communication with the proposed Client. -- This Server echoes back the sent password. This method is the Server side of the above. Subclasses can override this, simply returning 'true' for no validation or doing their own validation protocol with their Clients.

Parameters:
connectionToClient - ReaderWriter to Client to use for validation.
Returns:
true if the Client is valid according to this validation protocol.

close

public void close(int client)
Closes the resources associated with communicating to the given Client.

Parameters:
client - Index in the array of Clients for this Server, indicating which Client to close resources for communicating.

close

public void close()
For every Client currently associated with this Server, closes the resources associated with communicating with that Client.


numberOfValidatedClients

public int numberOfValidatedClients()
Returns the number of validated Clients currently associated with this Server. Blocks (waits) if a validation is in progress.

Returns:
the number of validated Clients currently associated with this Server.