simpleNetworking
Class MultiServer

java.lang.Object
  extended by simpleNetworking.MultiServer
All Implemented Interfaces:
Runnable

public class MultiServer
extends Object
implements Runnable

 A MultiServer first constructs a Server.
 
 Then the MultiServer, in its own Thread, repeatedly accepts connections
 to Clients and sends them to the Server (who can accept or reject them).
 
 After constructing a MultiServer, you can ask it to give you the Server
 that the MultiServer constructs via:
   -- getServer(N): Returns the Server only after it has N validated Clients
   -- getServer():  Returns the Server with whatever Clients it currently has,
                    and the MultiServer continues to add Clients to the Server
   -- getServerAndQuit():  Returns the Server with whatever Clients
                    it currently has, but now the MultiServer stops (so
                    no new Clients are added to the Server by the MultiServer).
 
 Or, you can stop the MultiServer at any point and receive the Server
 that the 

 NOTE:
   1. The MultServer must be started before any Clients attempt to connect.
   2. Before running this program, you may need to tell your Firewall
      not to block the port that this program uses (4444, chosen arbitrarily).
      In Windows, do so by:
         Control Panel ~ Windows Firewall ~ Exceptions tab ~ Add Port
      and enter the port number (4444) with any name you like. 

Author:
David Mutchler, using examples.example1_one_client from the Java Tutorials. May, 2009.

Field Summary
(package private) static int PORT
          Port to use for establishing connections to this server.
 
Constructor Summary
MultiServer()
           Constructs a ServerSocket that this MultiServer uses to accept multiple connections to Clients.
 
Method Summary
(package private)  void finishUpFailedConstruction(Throwable exception, String errorMessage)
          Something went wrong in constructing this MultiServer.
 Server getServer(int requiredNumberOfClients)
          Blocks (waits) until the Server created by this MultiServer has validated connections to at least the given number of Clients; then it returns that Server.
 void run()
          Repeatedly: 1.
 void stop()
          Stop this MultiThread (which is running in its own Thread) and close all its resources.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PORT

static final int PORT
Port to use for establishing connections to this server. Any open, unused port is fine, but ports less than 1024 are reserved.

See Also:
Constant Field Values
Constructor Detail

MultiServer

public MultiServer()
            throws SocketException
 Constructs a ServerSocket that this MultiServer uses to accept
 multiple connections to Clients.  If that ServerSocket is constructed
 successfully:
 
 1. Construct a Server that runs in its own Thread.
 
 2. In a new Thread, repeatedly:
    a. Accept a connection, getting a Socket for communicating with a Client.
    b. Ask the Server to add that Client.
       -- The Server can run a validation protocol on the proposed Client,
          accepting or rejecting the proposed Client as it chooses.
 

Throws:
SocketException - if the MultiServer cannot construct a ServerSocket that uses the default port to accept connections from Clients.
Method Detail

run

public void run()
Repeatedly: 1. Accept a connection, getting a Socket to a proposed Client. 2. Ask the Server to add that Client to the list of Clients with which it communicates. -- The Server can run a validation protocol on the proposed Client, accepting or rejecting the proposed Client as it chooses.

Specified by:
run in interface Runnable

getServer

public Server getServer(int requiredNumberOfClients)
Blocks (waits) until the Server created by this MultiServer has validated connections to at least the given number of Clients; then it returns that Server.

Parameters:
requiredNumberOfClients - Number of Clients required before this method returns.
Returns:
the Server created by this MultiServer, after that Server has validated connections to at least the given number of Clients.

stop

public void stop()
Stop this MultiThread (which is running in its own Thread) and close all its resources.


finishUpFailedConstruction

void finishUpFailedConstruction(Throwable exception,
                                String errorMessage)
                          throws SocketException
Something went wrong in constructing this MultiServer. Close the MultiServer, print an error message, and throw a SocketException.

Parameters:
exception - that was thrown by the failed construction of this Client.
errorMessage - to display in a message dialog.
Throws:
SocketException - so that the caller knows that construction failed.