de.cgarbs.net
Class LoginServerThread

java.lang.Object
  |
  +--java.lang.Thread
        |
        +--de.cgarbs.net.LoginServerThread
Direct Known Subclasses:
EchoServerThread

public abstract class LoginServerThread
extends java.lang.Thread

This class handles a connection from a client to a LoginServer.

Communication to this server thread is done using plain text. You can use a telnet to chat with your server or write your own client. It shouldn't be hard to write an SMTP or HTTP server using this package.

This server thread reacts to commands. A command is a string that is ended by a space. Anything after the space up to the end of a line is considered data. For example the line SAY Hello Bob! contains the command SAY and the data Hello Bob!. All server threads must provide the commands USER, PASS, QUIT and HELP. You may define as many other commands as you like.

Before any user-defined commands can be executed, the client has to log in using the USER and PASS commands. After that, any user defined commands can be executed. The commands QUIT and HELP can be executed at any time.

In order to write your own LoginServerThread you must create your own class that extends this class. Overwrite all abstract methods with your own. Your class must also have a Constructor that calls the Constructor of this class (Any ideas of how to come around this?). The main functionality or your server will reside in the validCommand() and parseCommand() methods. The other methods are mainly used for session handling and user authentification.

This class is licensed under the GNU GENERAL PUBLIC LICENSE.

Version:
0.0.2
Author:
Christian Garbs
See Also:
LoginServer

Field Summary
protected  LoginServer myServer
          a reference to the LoginServer that has spawned this LoginServerThread
protected  java.io.PrintWriter out
          the output stream to the client
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
LoginServerThread(LoginServer myServer, java.io.PrintWriter logStream, java.util.Hashtable passwd, java.net.Socket socket)
          standard constructor
 
Method Summary
protected abstract  void doubleLogin()
          tells the client that this user is already logged on and the connection will be closed
 java.net.InetAddress getClientIP()
          returns the IP address of the client
 long getID()
          returns the ID of this LoginServerThread
 java.lang.String getUsername()
          returns the username of the user logged on
protected abstract  void gotUser()
          tells the client that a USER command was successful
protected abstract  void invalidCommand()
          tells the client that the given command was invalid
protected  void log(java.lang.String message)
          writes a message to the log stream
protected abstract  void login()
          tells the client that the login was successful
protected abstract  boolean loginFailed(int cnt)
          tells the client that the login was unsuccessful and decides if the connection shall be closed
protected abstract  void noPassGiven()
          tells the client that the PASS command must be followed by a password
protected abstract  void noUserGiven()
          tells the client that the USER command must be followed by a username
protected abstract  void parseCommand(java.lang.String command, java.lang.String data)
          This method handles non-standard client commands.
protected abstract  void prompt()
          tells the client that it can send a command
 void run()
          This method handles the communication with the client.
protected abstract  void sendPass()
          tells the client to send a PASS command
protected abstract  void sendUser()
          tells the client to send a USER command
protected abstract  void sessionEnd()
          tells the client that the connection will be closed
protected abstract  void sessionStart()
          tells the client that the connection is established
protected abstract  void showHelp()
          sends a list of supported commands to the client
 void shutdown()
          immediately closes connection to the client
protected abstract  boolean validCommand(java.lang.String command)
          checks whether a command sent from the client is allowed
 
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

out

protected java.io.PrintWriter out
the output stream to the client

myServer

protected LoginServer myServer
a reference to the LoginServer that has spawned this LoginServerThread
Constructor Detail

LoginServerThread

public LoginServerThread(LoginServer myServer,
                         java.io.PrintWriter logStream,
                         java.util.Hashtable passwd,
                         java.net.Socket socket)
standard constructor
Parameters:
myServer - reference to the LoginServer that spawned this LoginServerThread
logStream - log messages go here
passwd - valid username/password pairs (usernames=keys, passwords=values)
socket - the socket to the client
Method Detail

getID

public long getID()
returns the ID of this LoginServerThread
Returns:
the ID of this LoginServerThread

log

protected void log(java.lang.String message)
writes a message to the log stream
Parameters:
message - the message to write

getClientIP

public java.net.InetAddress getClientIP()
returns the IP address of the client
Returns:
the IP address of the client

shutdown

public void shutdown()
immediately closes connection to the client

showHelp

protected abstract void showHelp()
sends a list of supported commands to the client

getUsername

public java.lang.String getUsername()
returns the username of the user logged on
Returns:
name of user (null before successful login)

validCommand

protected abstract boolean validCommand(java.lang.String command)
checks whether a command sent from the client is allowed
Parameters:
command - command from client
Returns:
true - command is valid

doubleLogin

protected abstract void doubleLogin()
tells the client that this user is already logged on and the connection will be closed

sessionStart

protected abstract void sessionStart()
tells the client that the connection is established

sessionEnd

protected abstract void sessionEnd()
tells the client that the connection will be closed

parseCommand

protected abstract void parseCommand(java.lang.String command,
                                     java.lang.String data)
This method handles non-standard client commands. (It does the thing your server is designed for)
Parameters:
command - command from client (first string token)
data - data from client (all but first string token)

invalidCommand

protected abstract void invalidCommand()
tells the client that the given command was invalid

noUserGiven

protected abstract void noUserGiven()
tells the client that the USER command must be followed by a username

gotUser

protected abstract void gotUser()
tells the client that a USER command was successful

sendUser

protected abstract void sendUser()
tells the client to send a USER command

noPassGiven

protected abstract void noPassGiven()
tells the client that the PASS command must be followed by a password

login

protected abstract void login()
tells the client that the login was successful

loginFailed

protected abstract boolean loginFailed(int cnt)
tells the client that the login was unsuccessful and decides if the connection shall be closed
Parameters:
cnt - number of failed logins so far
Returns:
true - the connection to the client will be closed

sendPass

protected abstract void sendPass()
tells the client to send a PASS command

prompt

protected abstract void prompt()
tells the client that it can send a command

run

public void run()
This method handles the communication with the client.
Overrides:
run in class java.lang.Thread