soc.server.genericServer
Class Server

java.lang.Object
  extended byjava.lang.Thread
      extended bysoc.server.genericServer.Server
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Runnable, java.io.Serializable
Direct Known Subclasses:
SOCServer

public abstract class Server
extends java.lang.Thread
implements java.io.Serializable, java.lang.Cloneable

a general purpose server.

This is the real stuff. Server subclasses won't have to care about reading/writing on the net, data consistency among threads, etc.

Newly connecting clients arrive in run(), start a thread for the server side of their Connection or LocalStringConnection, and are integrated into server data via addConnection(StringConnection) called from that thread.

The first message over the connection should be from the server to the client, in newConnection1(StringConnection) or newConnection2(StringConnection).

Version:
1.5
Author:
Original author: Cristian Bogdan
Lots of mods by Robert S. Thomas and Jay Budzik
Local (StringConnection) network system by Jeremy D Monin
See Also:
Serialized Form

Nested Class Summary
(package private)  class Server.Command
           
protected  class Server.NetStringServerSocket
          Uses ServerSocket to implement StringServerSocket over a network.
(package private)  class Server.Treater
           
 
Field Summary
protected  java.util.Hashtable conns
          the named connections
protected  java.lang.Exception error
           
 java.util.Vector inQueue
          in process of connecting
protected  int numberOfConnections
          total number of connections made
protected  int port
           
(package private)  StringServerSocket ss
           
protected  java.lang.String strSocketName
           
protected  java.util.Vector unnamedConns
          the newly connected, unnamed connections.
(package private)  boolean up
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
Server(int port)
          start listening to the given port
Server(java.lang.String stringSocketName)
          start listening to the given local string port (practice game)
 
Method Summary
 void addConnection(StringConnection c)
          Add a connection to the system.
protected  void broadcast(java.lang.String m)
          Broadcast a SOCmessage to all connected clients, named and unnamed.
protected  int connectionCount()
           
protected  StringConnection getConnection(java.lang.Object connKey)
          Given a key data, return the connected client.
protected  java.util.Enumeration getConnections()
           
 boolean isUp()
           
protected  void leaveConnection(StringConnection c)
          placeholder for doing things when a connection is closed.
 void nameConnection(StringConnection c)
          Name a current connection to the system.
protected  boolean newConnection1(StringConnection c)
          placeholder for doing things when a new connection comes, part 1 - decide whether to accept.
protected  void newConnection2(StringConnection c)
          placeholder for doing things when a new connection comes, part 2 - has been accepted and added to a connection list.
abstract  void processCommand(java.lang.String str, StringConnection con)
          DOCUMENT ME!
 void removeConnection(StringConnection c)
          remove a connection from the system; synchronized on list of connections.
protected  void removeConnectionCleanup(StringConnection c)
          do cleanup after a remove connection
 void run()
          run method for Server
protected  void serverDown()
          placeholder for doing things when server gets down
 void stopServer()
          The server is being cleanly stopped, disconnect all the connections.
 void treat(java.lang.String s, StringConnection c)
          treat a request from the given connection
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setName, setPriority, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

ss

StringServerSocket ss

up

boolean up

error

protected java.lang.Exception error

port

protected int port

strSocketName

protected java.lang.String strSocketName

numberOfConnections

protected int numberOfConnections
total number of connections made


conns

protected java.util.Hashtable conns
the named connections


unnamedConns

protected java.util.Vector unnamedConns
the newly connected, unnamed connections. Adding/removing/naming connections synchronizes on this Vector.


inQueue

public java.util.Vector inQueue
in process of connecting

Constructor Detail

Server

public Server(int port)
start listening to the given port


Server

public Server(java.lang.String stringSocketName)
start listening to the given local string port (practice game)

Method Detail

getConnection

protected StringConnection getConnection(java.lang.Object connKey)
Given a key data, return the connected client.

Parameters:
connKey - Object key data, as in StringConnection.getData(); if null, returns null
Returns:
The connection with this key, or null if none

getConnections

protected java.util.Enumeration getConnections()

connectionCount

protected int connectionCount()

isUp

public boolean isUp()

run

public void run()
run method for Server

Specified by:
run in interface java.lang.Runnable

treat

public void treat(java.lang.String s,
                  StringConnection c)
treat a request from the given connection


processCommand

public abstract void processCommand(java.lang.String str,
                                    StringConnection con)
DOCUMENT ME!

Parameters:
str - DOCUMENT ME!
con - DOCUMENT ME!

serverDown

protected void serverDown()
placeholder for doing things when server gets down


newConnection1

protected boolean newConnection1(StringConnection c)
placeholder for doing things when a new connection comes, part 1 - decide whether to accept. If the connection is accepted, it's added to a list (unnamedConns or conns). Unless you override this method, always returns true.

Should send a message to the client in either newConnection1(StringConnection) or newConnection2(StringConnection).

Note that addConnection(StringConnection) won't close the channel or take other action to disconnect a rejected client.

SYNCHRONIZATION NOTE: During the call to newConnection1, the monitor lock of unnamedConns is held. Thus, defer as much as possible until newConnection2(StringConnection) (after the connection is accepted).

Parameters:
c - incoming connection to evaluate and act on
Returns:
true to accept and continue, false if you have rejected this connection; if false, addConnection will call StringConnection.disconnectSoft().
See Also:
addConnection(StringConnection), newConnection2(StringConnection), nameConnection(StringConnection)

newConnection2

protected void newConnection2(StringConnection c)
placeholder for doing things when a new connection comes, part 2 - has been accepted and added to a connection list. Unlike newConnection1(StringConnection), no connection-list locks are held when this method is called.


leaveConnection

protected void leaveConnection(StringConnection c)
placeholder for doing things when a connection is closed. called after connection is removed from conns collection, and after c.disconnect() has been called.


stopServer

public void stopServer()
The server is being cleanly stopped, disconnect all the connections. Calls serverDown() before disconnect; if your child class has more work to do (such as sending a final message to all clients, or disconnecting from a database), override serverDown() or stopServer(). Check isUp() before calling.


removeConnection

public void removeConnection(StringConnection c)
remove a connection from the system; synchronized on list of connections. The callback leaveConnection(StringConnection) will be called, after calling StringConnection.disconnect() on c.

Parameters:
c - Connection to remove; will call its disconnect() method and remove it from the server state.

removeConnectionCleanup

protected void removeConnectionCleanup(StringConnection c)
do cleanup after a remove connection


addConnection

public void addConnection(StringConnection c)
Add a connection to the system. c.connect() is called at the start of this method. Synchronized on unnamedConns, although named conns (getData not null) are added to conns, not unnamedConns.

App-specific work should be done by overriding newConnection1(StringConnection) and newConnection2(StringConnection).

Parameters:
c - Connecting client; its key data (StringConnection.getData()) must not be null.
See Also:
nameConnection(StringConnection), removeConnection(StringConnection)

nameConnection

public void nameConnection(StringConnection c)
                    throws java.lang.IllegalArgumentException
Name a current connection to the system. Synchronized on unnamedConns.

Parameters:
c - Connected client; its key data (StringConnection.getData()) must not be null.
Throws:
java.lang.IllegalArgumentException - If c isn't already connected, or If c.getData() returns null
See Also:
addConnection(StringConnection)

broadcast

protected void broadcast(java.lang.String m)
Broadcast a SOCmessage to all connected clients, named and unnamed.

Parameters:
m - SOCmessage string, generated by SOCMessage.toCmd()