edu.umk.mat.merdacz.JGServer
Class ServerThread

java.lang.Object
  extended byjava.lang.Thread
      extended byedu.umk.mat.merdacz.JGServer.ServerThread
All Implemented Interfaces:
java.lang.Runnable

public class ServerThread
extends java.lang.Thread

Main per connection while-loop

Author:
Marcin Daczkowski

Field Summary
private  Room actualRoom
          The user can be only in one room at most and this is this room.
private  ServerCfg cfg
          Reference to ServerCfg object which is needed due to checking if Debug flag is set.
private  DbWrapper db
          db stuff
private  boolean depsInRoom
          Indcates wheter player is in a room
private  boolean depsLoggedIn
          Indicates whether player has already logged in.
private  boolean depsSetGame
          Indicates whether setgame request has been made We needs this because some requests depends on the fact that a game has been chosen
private  Game game
          Shortcut to games.getGameByName(...)
private  Games games
          Shortcut to server.getGames();
private  boolean gameUnsetted
           
private  boolean leftRoom
          Indicates whether we has just left the actualRoom We need this because of how message parsing is done.
private  Player player
          Player object is instantinated when login or register request occurs and is succesfully done
private  boolean removedRoom
          Indicates whether the room has been removed We need this because of how message parsing is done.
private  Rooms rooms
          Shortcut to game.getRooms()
private  Server server
          Reference to the Server object from which this instance of ServerThread had been created.
private  java.net.Socket socket
          The socket for this concrete connection.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
ServerThread(Server server, java.net.Socket socket)
           
 
Method Summary
private  java.lang.String broadcastChatMsg(java.lang.String from, java.lang.String msg)
          Special type of broadcastInRoom for chat purposes only
private  java.lang.String broadcastInGame(java.lang.String type, java.lang.String name)
          Send message to all but not "name" player in whole game
private  java.lang.String broadcastInRoom(java.lang.String type, java.lang.String content)
          Sends messgae to all but not "name" player in actual room
private  java.lang.String getPlayersInGame(java.lang.String id)
           
private  java.lang.String getPlayersInRoom(java.lang.String id, org.jdom.Element name)
           
private  java.lang.String getRooms(java.lang.String id)
           
private  java.lang.String getStats(java.lang.String id)
          Get statistic - simple XPath query on NXD Not implemented yet Should allow to pass XPath/XQuery queries??
private  java.lang.String grantInvitation(java.lang.String id, org.jdom.Element playerName, org.jdom.Element roomName)
           
private  java.lang.String joinRoom(java.lang.String id, org.jdom.Element name)
           
private  java.lang.String leaveRoom(java.lang.String id)
          Leaves actual room.
private  java.lang.String login(java.lang.String id, org.jdom.Element name, org.jdom.Element password)
           
private  java.lang.String logout(java.lang.String id)
          Logout doesn't mean destroying connection.
private  java.lang.String newRoom(java.lang.String id, org.jdom.Element name, org.jdom.Element type)
           
private  void parseActions(java.lang.String xmlToSend)
          Parse xml's produced by parseRequest and take proper actions
private  java.lang.String parseRequest(java.lang.String xmlin)
           
private  java.lang.String register(java.lang.String id, org.jdom.Element name, org.jdom.Element password, org.jdom.Element passwordAgain, org.jdom.Element avatar)
           
private  java.lang.String removeRoom(java.lang.String id, org.jdom.Element name)
           
private  java.lang.String reply(java.lang.String id, java.lang.String type, java.lang.String content)
          Generates reply with given content
private  java.lang.String replyFailed(java.lang.String id, java.lang.String type, java.lang.String reason)
          Generates Failed reply for given request type, with given explaination
private  java.lang.String replyOK(java.lang.String id, java.lang.String type)
          Generates OK reply for given request type
 void run()
          Main ServerThread while-loop
private  java.lang.String sendInvitation(java.lang.String roomName, java.lang.String to)
           
private  java.lang.String setGame(java.lang.String id, org.jdom.Element name)
           
 
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

server

private Server server
Reference to the Server object from which this instance of ServerThread had been created. Need this because of serverCfg (we need it for checking whether debug flag is set) and for invoking closing connection routine


socket

private java.net.Socket socket
The socket for this concrete connection. TODO (major) do it with secure sockets layer


games

private Games games
Shortcut to server.getGames();


game

private Game game
Shortcut to games.getGameByName(...)


rooms

private Rooms rooms
Shortcut to game.getRooms()


actualRoom

private Room actualRoom
The user can be only in one room at most and this is this room. Redundance to Player's private room field. We use it just to have shortcut.


player

private Player player
Player object is instantinated when login or register request occurs and is succesfully done


cfg

private ServerCfg cfg
Reference to ServerCfg object which is needed due to checking if Debug flag is set.


depsSetGame

private boolean depsSetGame
Indicates whether setgame request has been made We needs this because some requests depends on the fact that a game has been chosen


depsLoggedIn

private boolean depsLoggedIn
Indicates whether player has already logged in. We needs this because some requests depends on the fact that player has logged in


depsInRoom

private boolean depsInRoom
Indcates wheter player is in a room


removedRoom

private boolean removedRoom
Indicates whether the room has been removed We need this because of how message parsing is done.


leftRoom

private boolean leftRoom
Indicates whether we has just left the actualRoom We need this because of how message parsing is done.


gameUnsetted

private boolean gameUnsetted

db

private DbWrapper db
db stuff

Constructor Detail

ServerThread

public ServerThread(Server server,
                    java.net.Socket socket)
Parameters:
server - Reference to main server thread from which this one has been created.
socket - Socket which is used for communication with the player.
Method Detail

run

public void run()
Main ServerThread while-loop


parseRequest

private java.lang.String parseRequest(java.lang.String xmlin)
                               throws org.jdom.JDOMException,
                                      java.io.IOException,
                                      DbWrapperException
Parameters:
xmlin - XML message from player
Returns:
XML's (one per line) such like reply and broadcasts (in room & in game)
Throws:
org.jdom.JDOMException
java.io.IOException
DbWrapperException

parseActions

private void parseActions(java.lang.String xmlToSend)
                   throws org.jdom.JDOMException,
                          java.io.IOException
Parse xml's produced by parseRequest and take proper actions

Parameters:
xmlToSend -
Throws:
java.io.IOException
org.jdom.JDOMException

setGame

private java.lang.String setGame(java.lang.String id,
                                 org.jdom.Element name)
                          throws DbWrapperException
Parameters:
id - Idicates for which request reply will be produced
name - node which contains name of gamelet to load
Returns:
XML messages to send (on per line)
Throws:
DbWrapperException

login

private java.lang.String login(java.lang.String id,
                               org.jdom.Element name,
                               org.jdom.Element password)
                        throws DbWrapperException
Parameters:
id - Idicates for which request reply will be produced
name - node which contains login name passed in request
password - node which contains passoword passed in request
Returns:
XML messages to send (one per line)
Throws:
DbWrapperException

logout

private java.lang.String logout(java.lang.String id)
Logout doesn't mean destroying connection. For example player can have two account's and want to switch between them while the client is running

Parameters:
id - Idicates for which request reply will be produced
Returns:
XML messages to send (on per line)

register

private java.lang.String register(java.lang.String id,
                                  org.jdom.Element name,
                                  org.jdom.Element password,
                                  org.jdom.Element passwordAgain,
                                  org.jdom.Element avatar)
                           throws DbWrapperException
Parameters:
id - Idicates for which request reply will be produced
name - node which contains login name passed in request
password - node which contains password passed in request
passwordAgain - node which contains passwordAgain in request
avatar - node which contains avatar which is uuencoded image
Returns:
XML messages to send (on per line)
Throws:
DbWrapperException

getRooms

private java.lang.String getRooms(java.lang.String id)
Parameters:
id - Idicates for which request reply will be produced
Returns:
XML messages to send (on per line)

joinRoom

private java.lang.String joinRoom(java.lang.String id,
                                  org.jdom.Element name)
Parameters:
id - Idicates for which request reply will be produced
name - node element which contains room name passed in request
Returns:
XML messages to send (on per line)

leaveRoom

private java.lang.String leaveRoom(java.lang.String id)
Leaves actual room.

Parameters:
id - Idicates for which request reply will be produced
Returns:
XML messages to send (on per line)

grantInvitation

private java.lang.String grantInvitation(java.lang.String id,
                                         org.jdom.Element playerName,
                                         org.jdom.Element roomName)
                                  throws DbWrapperException
Parameters:
playerName -
roomName -
Returns:
Throws:
DbWrapperException

getPlayersInRoom

private java.lang.String getPlayersInRoom(java.lang.String id,
                                          org.jdom.Element name)
Parameters:
id - Idicates for which request reply will be produced
name - Node which contains room name passed in request
Returns:
XML messages to send (on per line)

getPlayersInGame

private java.lang.String getPlayersInGame(java.lang.String id)
Parameters:
id - Idicates for which request reply will be produced
Returns:
XML messages to send (on per line)

newRoom

private java.lang.String newRoom(java.lang.String id,
                                 org.jdom.Element name,
                                 org.jdom.Element type)
                          throws DbWrapperException
Parameters:
id - Idicates for which request reply will be produced
name - Node which contains room name to create passed in request
type - Node which contains room type (public or private) passed in request
Returns:
XML messages to send (on per line)
Throws:
DbWrapperException

removeRoom

private java.lang.String removeRoom(java.lang.String id,
                                    org.jdom.Element name)
                             throws DbWrapperException
Parameters:
id - Idicates for which request reply will be produced
name - Node which contains room name to remove passed in request
Returns:
XML messages to send (on per line)
Throws:
DbWrapperException

getStats

private java.lang.String getStats(java.lang.String id)
Get statistic - simple XPath query on NXD Not implemented yet Should allow to pass XPath/XQuery queries?? or at least choose range of "places"

Parameters:
id - Idicates for which request reply will be produced *
Returns:
XML messages to send (on per line)

replyOK

private java.lang.String replyOK(java.lang.String id,
                                 java.lang.String type)
Generates OK reply for given request type

Parameters:
id - Idicates for which request reply will be produced
type - Type of request for which reply will be generated
Returns:
XML messages to send (on per line)

replyFailed

private java.lang.String replyFailed(java.lang.String id,
                                     java.lang.String type,
                                     java.lang.String reason)
Generates Failed reply for given request type, with given explaination

Parameters:
id - Idicates for which request reply will be produced
type - Type of request for which reply will be generated
reason - Reason why failure occured
Returns:
XML messages to send (on per line)

reply

private java.lang.String reply(java.lang.String id,
                               java.lang.String type,
                               java.lang.String content)
Generates reply with given content

Parameters:
id -
type -
content -
Returns:

broadcastInRoom

private java.lang.String broadcastInRoom(java.lang.String type,
                                         java.lang.String content)
Sends messgae to all but not "name" player in actual room

Parameters:
type - Which type of request caused this broadcast
Returns:
XML to broadcast

broadcastInGame

private java.lang.String broadcastInGame(java.lang.String type,
                                         java.lang.String name)
Send message to all but not "name" player in whole game

Parameters:
type - Which type of request caused this broadcast
name - What name node will contain
Returns:
XML to broadcast

broadcastChatMsg

private java.lang.String broadcastChatMsg(java.lang.String from,
                                          java.lang.String msg)
Special type of broadcastInRoom for chat purposes only

Parameters:
from - Name of user who sends this message
msg - Message to send
Returns:
XML to broadcast

sendInvitation

private java.lang.String sendInvitation(java.lang.String roomName,
                                        java.lang.String to)
Parameters:
roomName -
Returns: