Class ClientServer.ClientServerThread

java.lang.Object
  extended byjava.lang.Thread
      extended byClientServer.ClientServerThread
All Implemented Interfaces:
Runnable
Enclosing class:
ClientServer

public class ClientServer.ClientServerThread
extends Thread

Handles new client connections.


Nested Class Summary
 class ClientServer.ClientServerThread.TelnetInputReader
          This class handles the input gathering and processing functions as well as the required echoing.
 
Field Summary
private  Socket client
           
private  ClientServer cserv
           
private  BufferedInputStream in
           
private  PrintWriter out
           
(package private)  String password
           
(package private)  String username
           
(package private)  boolean validUser
           
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ClientServer.ClientServerThread(Socket _client, ClientServer _cserv)
          Constructs the thread.
 
Method Summary
private  int dropDrink(String username, int slot, int delay)
          Attempt to drop a drink, checking both user balance and slot quantity first.
private  void negotiate(PrintWriter out)
          Performs the telnet negotiation.
private  void printOptions()
          Prints the options the user has.
private  void printSlots()
          Print the contents of the drink machine and then the users balance.
 void run()
          This is the meat of the client.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, countStackFrames, currentThread, destroy, dumpStack, enumerate, getContextClassLoader, getName, getPriority, getThreadGroup, holdsLock, 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

client

private Socket client

cserv

private ClientServer cserv

out

private PrintWriter out

in

private BufferedInputStream in

username

String username

password

String password

validUser

boolean validUser
Constructor Detail

ClientServer.ClientServerThread

public ClientServer.ClientServerThread(Socket _client,
                                       ClientServer _cserv)
Constructs the thread.

Parameters:
_client - The socket to the client
_cserv - The ClientServer we interact with to inc/dec the number of connected clients
Method Detail

run

public void run()
This is the meat of the client. The socket is connected to a TelnetPrintWriter, a BufferedInputStream and an TelnetInputReader. The client is then negotiated using negotiate() and the session begins.


negotiate

private void negotiate(PrintWriter out)
                throws IOException
Performs the telnet negotiation. Basically, we just want to make sure that we get to do the echoing.

Parameters:
out - The PrintWriter that we write to.
Throws:
IOException

dropDrink

private int dropDrink(String username,
                      int slot,
                      int delay)
               throws IOException
Attempt to drop a drink, checking both user balance and slot quantity first. Note, this method does not authenticate the user, that should be checked outside of this method.

Parameters:
username - The username to charge for the drink
slot - The slot to drop
delay - The delay (in seconds) to wait before dropping
Returns:
0 if the drop is successful. 1 if The slot is empty. 2 if the user is poor. 3 if the slot number is incorrect. 4 if there is an unspecified failure.
Throws:
IOException

printOptions

private void printOptions()
Prints the options the user has. The options available depend on whether or not the user is an admin.


printSlots

private void printSlots()
Print the contents of the drink machine and then the users balance.