glassfrog.server
Class Server.ServerConnectionHandler

java.lang.Object
  extended by glassfrog.server.Server.ServerConnectionHandler
All Implemented Interfaces:
java.lang.Runnable
Enclosing class:
Server

public class Server.ServerConnectionHandler
extends java.lang.Object
implements java.lang.Runnable

A class used to handle incoming connections to the server. This class is used to parse the request arguments to the server such as the requests to add and kill rooms, info requests from rooms and other information regarding the state of the server

Author:
jdavidso

Field Summary
private  java.io.BufferedReader br
           
private  java.lang.Object lock
           
private  java.io.PrintWriter pw
           
private  java.net.Socket socket
           
private  int TIMEOUT
           
 
Constructor Summary
Server.ServerConnectionHandler(java.net.Socket socket)
          The ServerConnectionHandler takes the socket that the ServerSocket gets from an accepted connection.
 
Method Summary
private  void autoConnect(java.lang.String key)
          The auto connect routine for a key value and the online client
private  java.lang.String getStatus(java.lang.String roomName)
          Get the status for a given room name, or for all the rooms if ALL is specified as the argument.
private  java.lang.String getUsage(java.lang.String helpRequest)
           
private  void handleRequest()
          Handles any request made from a connection to the server
private  void kill(java.lang.String roomName)
          Shudown a specific room or all of the rooms if the ALL argument is passed in as the roomName
private  void killServer()
          Closes the current connection and sets the alive boolean to false telling the server to stop listening for incoming connections and to exit
private  java.lang.String list()
          Get a list of all the Running, Finished, Disconnected and Errored Rooms
private  void parseConfigFile(java.lang.String path, java.lang.String name, int seed)
          Parse the given config file.
 void run()
          A wrapper to handle the requests from the server so that this can be invoked in a thread
private  int startRoom(java.lang.String name, int hands, java.lang.String gamedefPath, int seed)
          Start up a new room
private  java.lang.String validateKey(java.lang.String key)
          Lookup a seed and username from a specific key value.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

pw

private java.io.PrintWriter pw

br

private java.io.BufferedReader br

socket

private java.net.Socket socket

TIMEOUT

private final int TIMEOUT
See Also:
Constant Field Values

lock

private final java.lang.Object lock
Constructor Detail

Server.ServerConnectionHandler

public Server.ServerConnectionHandler(java.net.Socket socket)
                               throws java.io.IOException
The ServerConnectionHandler takes the socket that the ServerSocket gets from an accepted connection. A PrintWriter and BufferedReader are then set up to get the incoming request and possibly return any information to the sender. After 10s of inactivity from a socket, the socket will timeout

Parameters:
socket - A Socket passed in from the server
Throws:
java.io.IOException - Any exceptions from the socket handleing
Method Detail

run

public void run()
A wrapper to handle the requests from the server so that this can be invoked in a thread

Specified by:
run in interface java.lang.Runnable

handleRequest

private void handleRequest()
                    throws java.io.IOException,
                           java.net.SocketTimeoutException,
                           java.lang.InterruptedException
Handles any request made from a connection to the server

Throws:
java.io.IOException
java.socket.SocketTimeoutException
java.net.SocketTimeoutException
java.lang.InterruptedException

getUsage

private java.lang.String getUsage(java.lang.String helpRequest)

autoConnect

private void autoConnect(java.lang.String key)
                  throws java.lang.NullPointerException,
                         javax.xml.parsers.ParserConfigurationException,
                         org.xml.sax.SAXParseException,
                         org.xml.sax.SAXException,
                         java.io.IOException,
                         java.lang.InterruptedException
The auto connect routine for a key value and the online client

Parameters:
key -
Throws:
java.lang.NullPointerException
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXParseException
org.xml.sax.SAXException
java.io.IOException
java.lang.InterruptedException

validateKey

private java.lang.String validateKey(java.lang.String key)
                              throws java.lang.NullPointerException,
                                     javax.xml.parsers.ParserConfigurationException,
                                     org.xml.sax.SAXParseException,
                                     org.xml.sax.SAXException,
                                     java.io.IOException
Lookup a seed and username from a specific key value. Used for matching users to logs and seeds to recreate play

Parameters:
key - a String used for the key value pair of the seed
Throws:
java.lang.NullPointerException
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXParseException
org.xml.sax.SAXException
java.io.IOException

getStatus

private java.lang.String getStatus(java.lang.String roomName)
Get the status for a given room name, or for all the rooms if ALL is specified as the argument. Rooms are delimited by ||

Parameters:
roomName - A name for the specific status
Returns:
The status of a given room or all the rooms

list

private java.lang.String list()
Get a list of all the Running, Finished, Disconnected and Errored Rooms

Returns:

parseConfigFile

private void parseConfigFile(java.lang.String path,
                             java.lang.String name,
                             int seed)
                      throws javax.xml.parsers.ParserConfigurationException,
                             org.xml.sax.SAXParseException,
                             org.xml.sax.SAXException,
                             java.io.IOException,
                             java.lang.InterruptedException
Parse the given config file. Config files can have a number of options and requests in them, see the sample CONFIG.SAMPLE for more information on specific config file options

Parameters:
path - A path to a given XML config file
name - A @String for the name of the room, blank to use name specified in the config file
seed - Specify the seed. Use -1 to grab from config file
Throws:
javax.xml.parsers.ParserConfigurationException
org.xml.sax.SAXParseException
org.xml.sax.SAXException
java.io.IOException
java.lang.InterruptedException

kill

private void kill(java.lang.String roomName)
           throws java.io.IOException
Shudown a specific room or all of the rooms if the ALL argument is passed in as the roomName

Parameters:
roomName - The name of the room to kill
Throws:
java.io.IOException

killServer

private void killServer()
                 throws java.io.IOException
Closes the current connection and sets the alive boolean to false telling the server to stop listening for incoming connections and to exit

Throws:
java.io.IOException

startRoom

private int startRoom(java.lang.String name,
                      int hands,
                      java.lang.String gamedefPath,
                      int seed)
               throws java.io.IOException,
                      java.lang.InterruptedException
Start up a new room

Parameters:
name - The name of the room to start
hands - number of hands to plat
gamedefPath - the path to the gamedef file
seed - the seed for the cards
Returns:
the port the room is on
Throws:
java.io.IOException
java.lang.InterruptedException