de.cgarbs.net
Class LoginServer

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--de.cgarbs.net.LoginServer

public class LoginServer
extends java.lang.Thread

A multi-client TCP server with username/password access control. This server binds to a port and waits for connections. It then spawns LoginServerThreads. In order to create your own server you have to extend the LoginServerThread class to your needs.

This class is licensed under the GNU GENERAL PUBLIC LICENSE.

Version:
0.0.2
Author:
Christian Garbs
See Also:
LoginServerThread

Field Summary
static int ALLOW
          This doubleLoginPolicy allows a user to be connected to this LoginServer several times at once.
static int DENY
          This doubleLoginPolicy denies any additional connections to a particular user when he is already logged on at this LoginServer.
static int KICK
          This doubleLoginPolicy will close the first (=old) connection when a user logs on twice at this LoginServer.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
LoginServer(java.lang.Class threadClass, java.io.OutputStream log, java.util.Hashtable passwd, int port, java.lang.String serverName)
          constructor with no connection limit, allowing multiple logons with the same username (backwards compatible with version 0.0.1)
LoginServer(java.lang.Class threadClass, java.io.OutputStream log, java.util.Hashtable passwd, int port, java.lang.String serverName, int doubleLoginPolicy)
          constructor with no connection limit
LoginServer(java.lang.Class threadClass, java.io.OutputStream log, java.util.Hashtable passwd, int port, java.lang.String serverName, int doubleLoginPolicy, int maxConnections)
          standard constructor
 
Method Summary
 void accept()
          Tells the server to accept client connections.
 int connections()
          Returns the number of active connections (=number of LoginServerThreads).
 java.lang.String getServerName()
          Returns the name of this server.
 boolean isOpen()
          Tells whether client connections will be accepted.
 boolean isStarted()
          Tells whether the server started and listening on the given port.
 void kick()
          Closes all active client connections (kills all LoginServerThreads).
 boolean registerThread(LoginServerThread thread)
          The LoginServerThreads must register here after their creation.
 void reject()
          Tells the server to reject client connections.
 void run()
          Listens on the server socket.
 void setPort(int port)
          Changes the server port.
 void setSoTimeout(int socketTimeout)
          Changes the socket timeout.
 void shutdown()
          Stops the server and closes the server socket.
 void startup()
          Opens the server socket to allow clients to connect.
 boolean unregisterThread(LoginServerThread thread)
          The LoginServerThreads must unregister here before they exit.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, 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

ALLOW

public static final int ALLOW
This doubleLoginPolicy allows a user to be connected to this LoginServer several times at once.

KICK

public static final int KICK
This doubleLoginPolicy will close the first (=old) connection when a user logs on twice at this LoginServer.

DENY

public static final int DENY
This doubleLoginPolicy denies any additional connections to a particular user when he is already logged on at this LoginServer.
Constructor Detail

LoginServer

public LoginServer(java.lang.Class threadClass,
                   java.io.OutputStream log,
                   java.util.Hashtable passwd,
                   int port,
                   java.lang.String serverName,
                   int doubleLoginPolicy,
                   int maxConnections)
standard constructor
Parameters:
threadClass - The class of LoginServerThreads that are spawned to serve the clients.
log - Log messages go here.
passwd - Valid username/password pairs (key = username, value = password).
port - The TCP port the server listens on.
serverName - The name of this server.
doubleLoginPolicy - What to do when a user logs on twice (can be ALLOW, KICK or DENY).
maxConnections - The maximum number of simultaneous connections allowed to this server.

LoginServer

public LoginServer(java.lang.Class threadClass,
                   java.io.OutputStream log,
                   java.util.Hashtable passwd,
                   int port,
                   java.lang.String serverName,
                   int doubleLoginPolicy)
constructor with no connection limit
Parameters:
threadClass - The class of LoginServerThreads that are spawned to serve the clients.
log - Log messages go here.
passwd - Valid username/password pairs (key = username, value = password).
port - The TCP port the server listens on.
serverName - The name of this server.
doubleLoginPolicy - The maximum number of simultaneous connections allowed to this server.

LoginServer

public LoginServer(java.lang.Class threadClass,
                   java.io.OutputStream log,
                   java.util.Hashtable passwd,
                   int port,
                   java.lang.String serverName)
constructor with no connection limit, allowing multiple logons with the same username (backwards compatible with version 0.0.1)
Parameters:
threadClass - The class of LoginServerThreads that are spawned to serve the clients.
log - Log messages go here.
passwd - Valid username/password pairs (key = username, value = password).
port - The TCP port the server listens on.
serverName - The name of this server.
Method Detail

setSoTimeout

public void setSoTimeout(int socketTimeout)
Changes the socket timeout. After every timeout, the server will check if shutdown() has been called. The new timeout comes into effect the next time the server socket is opened (startup() after shutdown()).
Parameters:
socketTimeout - Timeout in milliseconds (default=1000msec).

setPort

public void setPort(int port)
Changes the server port. When the server is listening, it will be stopped and restarted on the new port. Active connections will not be affected.
Parameters:
port - new port to listen to

accept

public void accept()
Tells the server to accept client connections.

reject

public void reject()
Tells the server to reject client connections.

kick

public void kick()
Closes all active client connections (kills all LoginServerThreads).

isStarted

public boolean isStarted()
Tells whether the server started and listening on the given port.
Returns:
true - server is listening

isOpen

public boolean isOpen()
Tells whether client connections will be accepted.
Returns:
true - connections will be accepted

shutdown

public void shutdown()
Stops the server and closes the server socket. The actual close of the socket may take up to socketTimeout milliseconds.

startup

public void startup()
Opens the server socket to allow clients to connect.

registerThread

public boolean registerThread(LoginServerThread thread)
The LoginServerThreads must register here after their creation.
Parameters:
thread - the LoginServerThread that did the request
Returns:
always true

connections

public int connections()
Returns the number of active connections (=number of LoginServerThreads).
Returns:
Number of active connections.

unregisterThread

public boolean unregisterThread(LoginServerThread thread)
The LoginServerThreads must unregister here before they exit.
Parameters:
thread - the LoginServerThread that did the request

run

public void run()
Listens on the server socket. This should not be called from outside.
Overrides:
run in class java.lang.Thread

getServerName

public java.lang.String getServerName()
Returns the name of this server. Allows a LoginServerThread to find out which server has spawned it.
Returns:
name of this server