com.eriksilkensen.net
Class Server

java.lang.Object
  extended by com.eriksilkensen.net.Server

public class Server
extends java.lang.Object

Default Server implementation. It is multi-threaded and can accept as many connections as one might want.

Each time a new Client wants to connect, a Client Handler takes care of everything. When a Server receieves a message from someone, it relays it on to all of the Clients currently connected and residing in the clientOutputStreams Map. When a Client quits, it sends out a QUIT message, with its name appended on to the end of it. This is the key to look up in the outputStreams Map, and then the corresponding PrintWriter is removed from the Map.

The Server always holds a String reference to the name of the current host, and a Queue of all of the other Clients connected to the Server in the order that they were connected. If it is detected that the Host has disconnected from the Server, the queue of Clients is polled to get the next one in line.

Clients should make use of the Server's static message constants when they want to perform a certain task. The ezpectation is that messages are sent across the network by first placing one of the Server's constants at the front of the message, and following it directly with any other information needed to be passed along.

For example, Clients should disconnect from the Server by sending a QUIT_MESSAGE followed by their name:
String message = Server.QUIT_MESSAGE + getName()

When messages sent across the network need to be processed, it is best to check for a Server constant by calling
if ( message.startsWith(Server.XXXX_MESSAGE) ) { . . . }
and to note that Server constants can be split off of a message by searching for the delimiters: String chat = message.substring( message.indexOf(Server.DELIMITER) + 1 )

Since:
1.0.2
Version:
04/20/07
Author:
Erik Silkensen
See Also:
Client

Nested Class Summary
 class Server.ClientHandler
          Inner Class to handle the Clients.
 
Field Summary
static java.lang.String CHAT_MESSAGE
          Identifies a "chat" message which should be used to notify the Server that a Client is sending a chat-room-type message over the network.
static java.lang.String DEFF_MESSAGE
          Identifies a "default" message which cannot be specified further using the other constants.
static java.lang.String DELIMITER
          Identifies the delimeter that is used in Server messages
static java.lang.String HOST_MESSAGE
          Identifies a "host" message which should be used to notify the Server that a Client is becoming the host.
static java.lang.String NAME_MESSAGE
          Identifies a "name" message which should be used to notify the Server that a Client's real name is being sent across the network.
static java.lang.String QUIT_MESSAGE
          Identifies a "quit" message which should be used to notify the Server that a Client is quitting.
static java.lang.String UPDT_MESSAGE
          Identifies a "update" message which should be used to notify all Clients connected to the Server of all the other currently connected Clients.
 
Constructor Summary
Server()
           
 
Method Summary
 void createView()
           
 void go(int port)
          Called when the Server starts running, initializes the outputStreams Map, creates the ServerSocket on whichever port is passed in at main.
static void main(java.lang.String[] args)
          The Server should be run as a main class, on the hosting terminal.
 void removeClient(java.lang.String name)
          Removes a Client with the given name from the Map of outputStreams.
 void tellEveryone(java.lang.String message)
          Sends out a message to all of the Client's PrintWriters in the outputStreams Map.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DELIMITER

public static final java.lang.String DELIMITER
Identifies the delimeter that is used in Server messages

See Also:
Constant Field Values

DEFF_MESSAGE

public static final java.lang.String DEFF_MESSAGE
Identifies a "default" message which cannot be specified further using the other constants.

See Also:
Constant Field Values

QUIT_MESSAGE

public static final java.lang.String QUIT_MESSAGE
Identifies a "quit" message which should be used to notify the Server that a Client is quitting.

See Also:
Constant Field Values

HOST_MESSAGE

public static final java.lang.String HOST_MESSAGE
Identifies a "host" message which should be used to notify the Server that a Client is becoming the host.

See Also:
Constant Field Values

CHAT_MESSAGE

public static final java.lang.String CHAT_MESSAGE
Identifies a "chat" message which should be used to notify the Server that a Client is sending a chat-room-type message over the network.

See Also:
Constant Field Values

NAME_MESSAGE

public static final java.lang.String NAME_MESSAGE
Identifies a "name" message which should be used to notify the Server that a Client's real name is being sent across the network.

See Also:
Constant Field Values

UPDT_MESSAGE

public static final java.lang.String UPDT_MESSAGE
Identifies a "update" message which should be used to notify all Clients connected to the Server of all the other currently connected Clients.

See Also:
Constant Field Values
Constructor Detail

Server

public Server()
Method Detail

main

public static void main(java.lang.String[] args)
The Server should be run as a main class, on the hosting terminal. It shouldn't take up much memory at all, as it just waits for Clients to connect.


go

public void go(int port)
Called when the Server starts running, initializes the outputStreams Map, creates the ServerSocket on whichever port is passed in at main.


tellEveryone

public void tellEveryone(java.lang.String message)
Sends out a message to all of the Client's PrintWriters in the outputStreams Map.

Parameters:
message - the message to send

removeClient

public void removeClient(java.lang.String name)
Removes a Client with the given name from the Map of outputStreams.

Parameters:
name - the name of the Client to remove

createView

public void createView()