Class NetAcctClient

java.lang.Object
  extended byNetAcctClient
All Implemented Interfaces:
AcctMgrSpec

public class NetAcctClient
extends Object
implements AcctMgrSpec

This class is the client to the networked account server (NetAcctServer). It implements the AcctMgrSpec interface so that it can act as a drop-in AcctMgrSpec, and connects to the account server to retrieve its information. TODO: Change protocol to be like sunday protocol. TODO: Add support for purge db/geteverything.

Version:
$Revision: #22 $
Author:
Kevin Thompson [antiduh@csh.rit.edu], Michael J. Douglas [nodata@csh.rit.edu], DJ Edwards [dazig@csh.rit.edu], Marius Grigoriu [marius@csh.rit.edu]
See Also:
Networked Account Manager Protocol

Nested Class Summary
private  class NetAcctClient.ConnectionMgr
          Basically, this class keeps track of the connection to the NetAcctServer and trys to ensure that it is always connected.
 
Field Summary
private  ConfigMgr configMgr
          Where we get our runtime configuration management data from.
private  Socket connection
          The connection to the server.
private  NetAcctClient.ConnectionMgr connMgr
          The class responsible for maintaining the connection between this class and the server.
private  BufferedReader in
          The BufferedReader that wraps around the input stream from the socket.
private  DrinkLoggerSpec log
          Where we send all of our log messages.
private  PrintWriter out
          The PrintWriter that wraps around the output stream for the socket.
private  String passFile
          The file that stores the password that the CLIENT uses to authenticate to the server.
private  boolean performAuth
          Whether or not to actually care about authentication.
private  String remoteHost
          The IP/hostname of the host we are connected to.
private  int remotePort
          The port we are connected to.
 
Constructor Summary
NetAcctClient(DrinkLoggerSpec inLog, ConfigMgr inConfigMgr)
          Construct a new NetAcctClient to connect to the specified NetAcctServer on the specified port.
 
Method Summary
 boolean addUser(String username, String password)
          Adds a user to the database of known users.
 boolean addUser(String username, String password, int balance, boolean adminStatus)
          Adds a user to the database of known users using the supplied values to initialize the account.
private  boolean authenticate()
          A simple helper method to perform the initial authentication.
 boolean decreaseUserBalance(String username, int amount)
          Decrements a users balance by the specified amount.
 Vector dump()
          TODO: Implement this....
 int getUserBalance(String username)
          Retrieves the credit balance on a users account.
 boolean increaseUserBalance(String username, int amount)
          Increases a users balance.
 boolean isSystemReady()
          Verifies that the connection to the server is ready and working.
 boolean isUserAdmin(String username)
          Checks to see if a user is an admin.
 boolean isValidUser(String username)
          Determines if a user is known to the system.
 boolean isValidUser(String username, String password)
          Authenticates a user.
 void purgeDatabase()
          TODO: Implement this...
 String queryServer(String message)
          This method sends the server the message and waits for a response.
 String queryServer(String header, String message)
          Sends a message to the server and returns the response.
 boolean removeUser(String username)
          Removes a user from the database of known users.
 boolean setUserAdmin(String username, boolean admin)
          Changes the user's admin status.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

remotePort

private int remotePort
The port we are connected to.


remoteHost

private String remoteHost
The IP/hostname of the host we are connected to.


log

private DrinkLoggerSpec log
Where we send all of our log messages.


passFile

private String passFile
The file that stores the password that the CLIENT uses to authenticate to the server. This is NOT for user authentication.


performAuth

private boolean performAuth
Whether or not to actually care about authentication. If this is false, then anybody can get on the system.


connection

private Socket connection
The connection to the server.


out

private PrintWriter out
The PrintWriter that wraps around the output stream for the socket.


in

private BufferedReader in
The BufferedReader that wraps around the input stream from the socket.


connMgr

private NetAcctClient.ConnectionMgr connMgr
The class responsible for maintaining the connection between this class and the server.


configMgr

private ConfigMgr configMgr
Where we get our runtime configuration management data from.

Constructor Detail

NetAcctClient

public NetAcctClient(DrinkLoggerSpec inLog,
                     ConfigMgr inConfigMgr)
Construct a new NetAcctClient to connect to the specified NetAcctServer on the specified port. If the client is not able to the remote server, then it will try to reconnect a series of times. If it is unable to reconnect, then the system is shut down. Note that the client must authenticate with the NetAcctServer. Each client has its own username/password pair, and that pair is tied to the client's IP address.

Parameters:
inLog - The DrinkLoggerSpec to print messages to. //@param inHost The server to connect to. //@param inPort The port on the server to connect to. //@param inPassFile The file that contains this NetAcctClient's password file.
inConfigMgr - Where we get our runtime configuration data.
Method Detail

authenticate

private boolean authenticate()
A simple helper method to perform the initial authentication. Returns false if the authentication failed. If it does fail, we must close all the streams. TODO: Better error handling here. TODO: Check to make sure everthing is in order before passing on. The password file is set up so that the first line contains the username, and the second line contains the password. having it on a seperate line makes parsing simpler.


queryServer

public String queryServer(String message)
This method sends the server the message and waits for a response. When a response is received, it's is stripped and the data is returned in the form of a String.
Messages are in the form "(action)-(data)".
If there is an error while sending the message, "false" is returned.

Parameters:
message - The message to send to the server
Returns:
The response from the server

queryServer

public String queryServer(String header,
                          String message)
Sends a message to the server and returns the response. This is a wrapper for queryServer(String).

Parameters:
header - The header or action to be sent to the server.
message - The message or data to be sent to the server.
Returns:
The response from the server.
See Also:
queryServer(String)

getUserBalance

public int getUserBalance(String username)
Retrieves the credit balance on a users account.

Specified by:
getUserBalance in interface AcctMgrSpec
Parameters:
username - The username of the user to retrieve the balance for.
Returns:
The users balance

isUserAdmin

public boolean isUserAdmin(String username)
Checks to see if a user is an admin.

Specified by:
isUserAdmin in interface AcctMgrSpec
Parameters:
username - The username of the account to query.
Returns:
True if the user is an admin. False otherwise.

isValidUser

public boolean isValidUser(String username)
Determines if a user is known to the system.

Specified by:
isValidUser in interface AcctMgrSpec
Parameters:
username - The username of the account to query
Returns:
True if the user is known to the system, false otherwise.

isValidUser

public boolean isValidUser(String username,
                           String password)
Authenticates a user.

Specified by:
isValidUser in interface AcctMgrSpec
Parameters:
username - The username of the user.
password - The user's password.
Returns:
True if the username and password are valid.

isSystemReady

public boolean isSystemReady()
Verifies that the connection to the server is ready and working. The method makes no attempts to reconnect if the connection is not good; This is handled by ConnectionMgr.

Specified by:
isSystemReady in interface AcctMgrSpec
Returns:
True if the connection is good, false otherwise.

dump

public Vector dump()
TODO: Implement this....

Specified by:
dump in interface AcctMgrSpec

purgeDatabase

public void purgeDatabase()
TODO: Implement this...

Specified by:
purgeDatabase in interface AcctMgrSpec

decreaseUserBalance

public boolean decreaseUserBalance(String username,
                                   int amount)
Decrements a users balance by the specified amount.

Specified by:
decreaseUserBalance in interface AcctMgrSpec
Parameters:
username - The username of the account to decrement.
amount - The amount to decrement the users balance.
Returns:
The boolean value of the return from queryServer(String,String).
See Also:
AcctMgrSpec.decreaseUserBalance(String, int), LocalAcctMgr.decreaseUserBalance(String, int), queryServer(String,String), increaseUserBalance(String, int)

increaseUserBalance

public boolean increaseUserBalance(String username,
                                   int amount)
Increases a users balance.

Specified by:
increaseUserBalance in interface AcctMgrSpec
Parameters:
username - The username of the account to credit.
amount - The amount to increment the users account.
Returns:
The boolean value of the return from queryServer(String, String)
See Also:
AcctMgrSpec.increaseUserBalance(String, int), LocalAcctMgr.increaseUserBalance(String, int), queryServer(String,String), decreaseUserBalance(String, int)

addUser

public boolean addUser(String username,
                       String password)
Adds a user to the database of known users.

Specified by:
addUser in interface AcctMgrSpec
Parameters:
username - The username of the user to add
password - The password of the user.
Returns:
The boolean-value of the return from queryServer( String, String)
See Also:
AcctMgrSpec.addUser(String,String), queryServer( String, String )

addUser

public boolean addUser(String username,
                       String password,
                       int balance,
                       boolean adminStatus)
Adds a user to the database of known users using the supplied values to initialize the account. TODO: Implement this once switchover to smtp-like protocol is done.

Specified by:
addUser in interface AcctMgrSpec
Parameters:
username - The username of the account to add.
password - The password for the new account.
balance - The accounts initial balance.
adminStatus - Whether or not the new user is to be an admin.
Returns:
true if the operation succeeds, false otherwise. This should fail if the user already exists.

removeUser

public boolean removeUser(String username)
Removes a user from the database of known users.

Specified by:
removeUser in interface AcctMgrSpec
Parameters:
username - The username of the user to remove.
Returns:
The boolean-value of the return from queryServer(String,String).

setUserAdmin

public boolean setUserAdmin(String username,
                            boolean admin)
Changes the user's admin status.

Specified by:
setUserAdmin in interface AcctMgrSpec
Parameters:
username - The user to edit
admin - The new admin status of the user
Returns:
True if the operation was successful.