Holger's
Java API

com.antelmann.game
Class GameServer

java.lang.Object
  extended by java.lang.Thread
      extended by com.antelmann.game.GameServer
All Implemented Interfaces:
ConnectionDispatcher, NetConnectionHandler, Runnable

public class GameServer
extends Thread
implements ConnectionDispatcher, NetConnectionHandler

A GameServer hosts a GamePlay object that clients can play remotely by connecting to it and following this server's protocol. JPlayerClient is known to work with this server. Every GamePlay role is represented by a different NetConnection running in a different thread. The GameServer hosts a single game and then closes all connections once the game is over (i.e. when there are no more legal moves available); if you want to play another game, you need a new GameServer. The GameServer listens to new connections only if there are still game roles left that are not taken by a connection. The GameServer feature reconnects; i.e. if a client disconnects, the server is restarted to allow a new client to take that role - until all game roles are connected again.

Author:
Holger Antelmann
See Also:
GamePlay, JPlayerClient

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static Handshake HANDSHAKE
          this Handshake must be used when creating connections to this server
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
GameServer(int serverPort, GamePlay game, int timeout, Logger logger)
           
 
Method Summary
 void connectionLost(NetConnection con)
          called when a connection caused an IOException during reading or writing; the connection may not have been closed at this point.
 SocketConnection createConnection(Socket socket)
          produces a NetConnection based on the given socket.
 NetConnectionHandler createHandler(NetConnection con)
          produces a NetConnectionHandler which will be started in a new Thread (created by the NetConnectionServer's ThreadFactory) to handle the given NetConnection and listens to messages.
 Handshake getHandshake()
          returns a signature object used to identify the desired connection type handled by this ConnectionDispatcher object
 void handleMessage(Object message, NetConnection con)
          called when a message is received by the given NetConnection object
static void main(String[] args)
          starts a GameServer after some GUI selections
 void run()
           
 void shutdown()
           
static void startServerWithGUI(Logger logger)
          a conveninent way to start a GameServer with a little GUI;
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, 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

HANDSHAKE

public static final Handshake HANDSHAKE
this Handshake must be used when creating connections to this server

Constructor Detail

GameServer

public GameServer(int serverPort,
                  GamePlay game,
                  int timeout,
                  Logger logger)
           throws IOException
Throws:
IOException
Method Detail

main

public static void main(String[] args)
starts a GameServer after some GUI selections


startServerWithGUI

public static void startServerWithGUI(Logger logger)
                               throws IOException
a conveninent way to start a GameServer with a little GUI;

Throws:
IOException

createConnection

public SocketConnection createConnection(Socket socket)
                                  throws IOException
Description copied from interface: ConnectionDispatcher
produces a NetConnection based on the given socket.

Specified by:
createConnection in interface ConnectionDispatcher
Throws:
IOException
See Also:
SocketConnection.createServerConnection(Socket, Handshake)

connectionLost

public void connectionLost(NetConnection con)
Description copied from interface: NetConnectionHandler
called when a connection caused an IOException during reading or writing; the connection may not have been closed at this point.
Usually, it is a good idea to close the connection here explicitly.

Specified by:
connectionLost in interface NetConnectionHandler

getHandshake

public Handshake getHandshake()
Description copied from interface: ConnectionDispatcher
returns a signature object used to identify the desired connection type handled by this ConnectionDispatcher object

Specified by:
getHandshake in interface ConnectionDispatcher

handleMessage

public void handleMessage(Object message,
                          NetConnection con)
Description copied from interface: NetConnectionHandler
called when a message is received by the given NetConnection object

Specified by:
handleMessage in interface NetConnectionHandler

shutdown

public void shutdown()

run

public void run()
Specified by:
run in interface Runnable
Overrides:
run in class Thread

createHandler

public NetConnectionHandler createHandler(NetConnection con)
Description copied from interface: ConnectionDispatcher
produces a NetConnectionHandler which will be started in a new Thread (created by the NetConnectionServer's ThreadFactory) to handle the given NetConnection and listens to messages. Typically, the returned object could be a MessageDelegator.

Specified by:
createHandler in interface ConnectionDispatcher
See Also:
MessageDelegator


(c) 2001-2006 Holger Antelmann - all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads