Holger's
Java API

com.antelmann.net
Class ChatServer

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

public class ChatServer
extends Thread
implements ConnectionDispatcher, NetConnectionHandler

ChatServer implements a server that handles chat messages that are broadcasted among the active connections to the server.

Author:
Holger Antelmann
See Also:
JChat

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static Handshake HANDSHAKE
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ChatServer(int serverPort, 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.
 MessageDelegator createDispatcher(NetConnection con, NetConnectionHandler handler)
          responsible for creating the delegator based on the created connection and the associated handler
 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.
 NetConnection[] getConnections()
           
 Handshake getHandshake()
          returns a signature object used to identify the desired connection type handled by this ConnectionDispatcher object
 int getLocalPort()
          returns the port the server is listening on
 Logger getLogger()
           
 void handleMessage(Object message, NetConnection con)
          called when a message is received by the given NetConnection object
static void main(String[] args)
          starts a ChatServer after making some GUI selections
 void run()
          starts the underlying server
 void shutdown()
           
static void startServerWithGUI(Logger logger)
          uses the com.antelmann.net.chat.port property from Settings
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, 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
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

HANDSHAKE

public static final Handshake HANDSHAKE
Constructor Detail

ChatServer

public ChatServer(int serverPort,
                  Logger logger)
           throws IOException
Throws:
IOException
Method Detail

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

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

createDispatcher

public MessageDelegator createDispatcher(NetConnection con,
                                         NetConnectionHandler handler)
Description copied from interface: ConnectionDispatcher
responsible for creating the delegator based on the created connection and the associated handler

Specified by:
createDispatcher 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()
starts the underlying server

Specified by:
run in interface Runnable
Overrides:
run in class Thread

getLocalPort

public int getLocalPort()
returns the port the server is listening on


getConnections

public NetConnection[] getConnections()

getLogger

public Logger getLogger()

startServerWithGUI

public static void startServerWithGUI(Logger logger)
                               throws IOException
uses the com.antelmann.net.chat.port property from Settings

Throws:
IOException
See Also:
Settings

main

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



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