LectureGame.Core
Class NetworkManager

java.lang.Object
  extended by LectureGame.Core.NetworkManager
All Implemented Interfaces:
java.lang.Runnable

public class NetworkManager
extends java.lang.Object
implements java.lang.Runnable

Created by IntelliJ IDEA. User: olekrst Date: 26.jan.2007 Time: 13:05:41 The nerver center of the server. This class handles the responses from both StudentClients and the Masterclient. Uses the DataBase LectureGame.DataBase package for database connectivity, and GameMode interface for selecting GameMode.

See Also:
GameMode, StudentClient, MasterClient, NetworkConnectionKeeper

Field Summary
private  NetworkConnectionKeeper connectionKeeper
           
private  java.util.Vector<StudentClient> connections
           
private  Question currentQuestion
           
private  DataBaseConnection db
           
private  GameMode gameMode
           
private  MasterClient master
           
private  int roundIndex
           
private  int[] roundList
           
private  boolean serverIsAlive
           
private  int serverState
           
private  java.net.ServerSocket socketListener
           
private  int totalNumberOfQuestions
           
private  int totalPossibleScore
           
 
Constructor Summary
NetworkManager()
          Constructor of the NetworkManager class.
 
Method Summary
private  void action()
          This method performs actions on the connected clients and on the GameMode according to the state its in.
 int checkAns(java.lang.String answer, StudentClient c)
          Checks the answer using handeling code in the class implementing the GameMode interface
private  boolean currentRoundIsLastRound()
          Performs check to see if the current round is the last round in a lecture
 java.lang.String generateANS(int roundScore, int totalScore)
          Generates a string to be sent to the clients
 int getCurrentRound()
          Returns which round is the current round
 int getTotalNumberOfQuestions()
          Method used for controlling how many questions a round contains
private  java.lang.String handleEndOfRound()
          Handles the end of a round, building the end of round string to be sendt to the MasterClient
 void listenForMasterClient()
          Starts a new MasterClient thread.
private  void makeClientsDie()
           
private  boolean nextRound()
          Sets the next round and loads the questions in it. or if the current round is the final one in the lecture then the serverstate END OF LECTURE is set.
private  void resetStatistics()
           
 void run()
          Contains main loop which queries the connections for input data according to the server state.
private  void setGameMode(int gamemode)
          This is where the GameMode is set according to the GameMode code registered in the database in the round table.
 void setLecture(int lectureID)
          Sets tje lecture id where the rounds are to be extracted from
 void setState(int state)
          Sets the server state.
 void stop()
          Stops the networkmanager and closes all connections.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

connectionKeeper

private NetworkConnectionKeeper connectionKeeper

serverState

private int serverState

currentQuestion

private Question currentQuestion

roundList

private int[] roundList

db

private DataBaseConnection db

roundIndex

private int roundIndex

totalNumberOfQuestions

private int totalNumberOfQuestions

totalPossibleScore

private int totalPossibleScore

gameMode

private GameMode gameMode

connections

private java.util.Vector<StudentClient> connections

master

private MasterClient master

socketListener

private java.net.ServerSocket socketListener

serverIsAlive

private boolean serverIsAlive
Constructor Detail

NetworkManager

public NetworkManager()
Constructor of the NetworkManager class.

Method Detail

listenForMasterClient

public void listenForMasterClient()
Starts a new MasterClient thread. Instansiated when a masterconnection is disconnected or when NetworkManager is instanciated

See Also:
MasterClient

run

public void run()
Contains main loop which queries the connections for input data according to the server state.

Specified by:
run in interface java.lang.Runnable

action

private void action()
This method performs actions on the connected clients and on the GameMode according to the state its in. possible states are:

- NO CONNECTIONS : no clients connected - CONNECTED : clients connected - PUSH QUESTIONS : question is to be pushed to the clients - WAITING REPLY : waiting for question answers from the clients - END OF ROUND : end of the round - END OF LECTURE : end of the lecture - LOAD NEW LECTURE : loading a new lecture - PING : ping, handles a ping session


resetStatistics

private void resetStatistics()

makeClientsDie

private void makeClientsDie()

setState

public void setState(int state)
Sets the server state. See LectureGame.Core.LG for the list of possible constants

Parameters:
state -
See Also:
LG

checkAns

public int checkAns(java.lang.String answer,
                    StudentClient c)
Checks the answer using handeling code in the class implementing the GameMode interface

Parameters:
answer - the answer from the client, a string representing an integer
c - the client which answered
Returns:
1 if correct, 0 if false

getTotalNumberOfQuestions

public int getTotalNumberOfQuestions()
Method used for controlling how many questions a round contains

Returns:
the total number of questions in a round

generateANS

public java.lang.String generateANS(int roundScore,
                                    int totalScore)
Generates a string to be sent to the clients

Parameters:
roundScore - score acheived in this round
totalScore - total score accumulated
Returns:
string formatted for sending

setGameMode

private void setGameMode(int gamemode)
                  throws IllegalGameModeException
This is where the GameMode is set according to the GameMode code registered in the database in the round table. Any additional game modes must be added here in a new 'else if'. Also the new GameMode code must be added to the LG constants class.

Parameters:
gamemode -
Throws:
IllegalGameModeException

getCurrentRound

public int getCurrentRound()
Returns which round is the current round

Returns:
current round

setLecture

public void setLecture(int lectureID)
Sets tje lecture id where the rounds are to be extracted from

Parameters:
lectureID - the lectureID to be used

stop

public void stop()
Stops the networkmanager and closes all connections.


nextRound

private boolean nextRound()
Sets the next round and loads the questions in it. or if the current round is the final one in the lecture then the serverstate END OF LECTURE is set.

Returns:
true if the method executed correctly, false if not

handleEndOfRound

private java.lang.String handleEndOfRound()
Handles the end of a round, building the end of round string to be sendt to the MasterClient

Returns:
string to be sendt to MasterClient

currentRoundIsLastRound

private boolean currentRoundIsLastRound()
Performs check to see if the current round is the last round in a lecture

Returns:
is the round the last round