Skip to content
Mirko Mantovani edited this page Jul 11, 2017 · 5 revisions

Server implementation

Singleton pattern with private constructor and getInstance() method to prevent any other instantiation of the server. When created the Server starts the listening on both Socket port and RMI.

  • Observer pattern to let the listeners notify the Server that a new client has connected passing the abstract Runnable object ClientHandler (implemented by concrete socket and RMI handlers) to the server.
  • A ClientHandler is a Thread that is gonna handle a single Client for the entire game, filtering and allowing messages from the client and sending them over the right Network Interface.
  • When reaching the NetworkConstants.MINPLAYERS a timer starts, and if the timer expires or the number of clients connected reaches NetworkConstants.MAXPLAYERS the match starts.
  • ExecutorService Thread Pool used to Run MatchHandlers and a HashMap to store futures return by the executes, they’ll be needed to be able to stop the execution when the Match is finished.

MatchHandler – the Controller - MVC

  • The MVC controller of the game, given a set of ClientHandlers that it’s going to map to players, it acts based on users inputs notified by the ServerCommandHandler, the MatchHandler decides what to send to whom for every phase of the game.
  • It modifies and calls methods of model, contained in a single object called Match, where all the game-needed data are ‘stored’.
  • A timer Thread dictates the round time every player has to decide the action to perform, if the timer expires the player is automatically disconnected from the game and the other players are notified.
  • The player can however reconnect and go on playing whenever he wants.
  • Model-view interactions: the model notifies the view that the state has changed (e.g. PlayerStatusChangeCommand, OpponentStatusChangeCommand

Authentication - Reconnection

  • A mandatory authentication is required to start playing.
  • This is needed for both saving player stats and let users reconnect into the game demonstrating they were actually playing in that game.
  • A standard String hashCode() was used to make the authentication possible without having to save sensitive information.
  • A Thread saves updates the file containing Users information (played – won - lost matches, gameplay time) saving the on a File.
Clone this wiki locally