java.lang.Object
|
+--java.lang.Thread
|
+--de.cgarbs.net.LoginServerThread
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.
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 |
protected java.io.PrintWriter out
protected LoginServer myServer
| Constructor Detail |
public LoginServerThread(LoginServer myServer,
java.io.PrintWriter logStream,
java.util.Hashtable passwd,
java.net.Socket socket)
myServer - reference to the LoginServer that spawned this
LoginServerThreadlogStream - log messages go herepasswd - valid username/password pairs (usernames=keys,
passwords=values)socket - the socket to the client| Method Detail |
public long getID()
protected void log(java.lang.String message)
message - the message to writepublic java.net.InetAddress getClientIP()
public void shutdown()
protected abstract void showHelp()
public java.lang.String getUsername()
protected abstract boolean validCommand(java.lang.String command)
command - command from clientprotected abstract void doubleLogin()
protected abstract void sessionStart()
protected abstract void sessionEnd()
protected abstract void parseCommand(java.lang.String command,
java.lang.String data)
command - command from client (first string token)data - data from client (all but first string token)protected abstract void invalidCommand()
protected abstract void noUserGiven()
protected abstract void gotUser()
protected abstract void sendUser()
protected abstract void noPassGiven()
protected abstract void login()
protected abstract boolean loginFailed(int cnt)
cnt - number of failed logins so farprotected abstract void sendPass()
protected abstract void prompt()
public void run()