Class SocketServer

java.lang.Object
it.polimi.ingsw.Server.SocketServer

public class SocketServer extends Object
Socket Server, uses Status Messages to inform clients of game's evolution
  • Field Details

  • Constructor Details

    • SocketServer

      public SocketServer(Map<String,GameController> identifierToController, Map<GameController,Semaphore> controllerToSemaphore, Map<GameController,Object> controllerToCommuter, Map<GameController,Object> controllerToLobbyCommuter)
      SocketServer constructor, managed by the Server Manager by default
      Parameters:
      identifierToController - maps game's identifiers to GameControllers
      controllerToSemaphore - maps GameControllers to synchronization semaphores
      controllerToCommuter - maps GamesController to synchronization commuters
      controllerToLobbyCommuter - maps GameController to synchronization lobbyCommuters
  • Method Details

    • main

      public static void main(String[] args)
      Main method, welcoming Server management console
      Parameters:
      args - default arguments array
    • communication

      private static void communication(Socket socket) throws IOException
      this method manages communication phases between Server and Client (Server side)
      Parameters:
      socket - socket
      Throws:
      IOException - input/output exception
    • updateClientStatus

      private static void updateClientStatus(GameController gc, String nickname, PrintWriter out, com.google.gson.Gson gson) throws RemoteException
      this method sends Board, Shelf and cards at the end of every turn
      Parameters:
      gc - game controller
      nickname - player's nickname
      out - PrintWriter
      gson - Gson Builder
      Throws:
      RemoteException - related to RMI
    • makeMove

      private static void makeMove(GameController gc, PrintWriter out, com.google.gson.Gson gson, AtomicBoolean clientsTurn, ProducerConsumerLock lineLockers, AtomicReference<String> l, com.google.gson.Gson gson_move)
      this method manages the move phase
      Parameters:
      gc - game controller
      out - print writer
      gson - Gson Builder
      clientsTurn - true if it's client turn, false otherwise
      lineLockers - ProducerConsumerLock
      l - line read by the thread
      gson_move - Gson builder for move
    • admissionResult

      private static boolean admissionResult(GameController gc, String nickname, PrintWriter out, int numofPlayers, com.google.gson.Gson gson)
      this method manages admission phase
      Parameters:
      gc - game controller
      nickname - player's nickname
      out - printWriter
      numofPlayers - number of players
      gson - gson builder
      Returns:
      true if player has been accepted, false otherwise
    • requestNumOfPlayer

      private static int requestNumOfPlayer(BufferedReader in, PrintWriter out, com.google.gson.Gson gson) throws IOException
      this method receives the number of player by the first player
      Parameters:
      in - bufferedreader
      out - printwriter
      gson - gson builder
      Returns:
      int number of players chosen by the first player
      Throws:
      IOException - input/output exception
    • receiveIdentifier

      private static String receiveIdentifier(BufferedReader in, PrintWriter out, com.google.gson.Gson gson) throws IOException
      this method receives the identifier
      Parameters:
      in - buffered reader
      out - printwriter
      gson - gson builder
      Returns:
      game identifier
      Throws:
      IOException - input/output exception
    • readClientLine

      private static void readClientLine(BufferedReader in, PrintWriter out, AtomicBoolean clientsTurn, ProducerConsumerLock lineLockers, AtomicReference<String> l, AtomicReference<SocketKeepAlive> ka) throws IOException
      Socket thread listener
      Parameters:
      in - bufferedreader
      out - printwriter
      clientsTurn - true if it's clientsTurn, false otherwise
      lineLockers - ProducerConsumerLock
      l - line read by the thread
      Throws:
      IOException - input/output exception
    • requestNickname

      public static String requestNickname(BufferedReader in, PrintWriter out, String identifier, com.google.gson.Gson gson) throws IOException
      this method receives the nickname chosen by the player
      Parameters:
      in - bufferedReader
      out - printwriter
      identifier - identifier
      gson - gsonBuilder
      Returns:
      player's nickname
      Throws:
      IOException - input/output exception
    • endGameHandler

      public static void endGameHandler(GameController gc, boolean invokedbyRMI, boolean invokedAtKATimeout)
      Implements End Game strategy, informing all clients (Socket and RMI ones) to disconnect ad deallocates Model Objects from global structures
      Parameters:
      gc - Game's Controller
      invokedbyRMI - to avoid Server callbacks
      invokedAtKATimeout - selects the correct Status Message to send to the client