Class HostServer

java.lang.Object
model.logic.server.MyServer
model.logic.server.HostServer
All Implemented Interfaces:
Serializable

public class HostServer extends MyServer implements Serializable
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    class 
    Manage each guest turn, update models and if player is found initiate clientHandler.
  • Field Summary

    Fields inherited from class model.logic.server.MyServer

    clientHandler, port, stop
  • Constructor Summary

    Constructors
    Constructor
    Description
    HostServer(int port, ClientHandler clientHandler)
    The HostServer function is a constructor for the HostServer class.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    broadcastUpdate(String messageForPlayers)
    The broadcastUpdate function is used to send a message to all players in the game.
    void
    The close function closes the server socket and all the client sockets.
    void
    The connectClients function is responsible for connecting clients to the server.
    boolean
    The isGameRunning function returns a boolean value that indicates whether the game is running or not.
    void
    The playGame function is the main function of the game.
    void
    removePlayer(int playerId)
    The removePlayer function removes a player from the game.
    void
    The resetTimerTask function is used to reset the timerTask variable.
    protected void
    The runServer function is the main function of the server.
    void
    The sendUpdatedModel function is used to send the updated model to all the clients.
    void
    The startGame function is called when the game is ready to start.
    void
    The stopGame function is called when the game ends.

    Methods inherited from class model.logic.server.MyServer

    start

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • HostServer

      public HostServer(int port, ClientHandler clientHandler)
      The HostServer function is a constructor for the HostServer class. It takes in two parameters: an integer representing the port number, and a ClientHandler object. The function initializes two HashMaps, one to store clients and their corresponding client handlers, and another to store clients' model receivers.
      Parameters:
      port - port Specify the port number that the server will be listening on
      clientHandler - clientHandler Create a new instance of the clientHandler class
  • Method Details

    • runServer

      protected void runServer() throws Exception
      The runServer function is the main function of the server. It creates a new MyServerSocket object, which is a subclass of ServerSocket that allows for multiple clients to connect at once. The runServer function then calls two other functions: connectClients and playGame. The first connects all clients to the server, while the second runs through each round of gameplay until one player wins or there's a tie.
      Overrides:
      runServer in class MyServer
      Throws:
      Exception - if there is an error while creating or closing the server socket
    • connectClients

      public void connectClients()
      The connectClients function is responsible for connecting clients to the server. It does this by accepting connections from clients and adding them to a list of connected players. The function also adds the player's name to a list of players in GameManager, which will be used later on when creating the game board.
    • sendUpdatedModel

      public void sendUpdatedModel()
      The sendUpdatedModel function is used to send the updated model to all the clients. It does this by creating a new thread that iterates through each client and sends them an object output stream containing the GameManager's current gameModel. This function is called whenever there are changes made to the gameModel, such as when a player moves or when a card is played.
    • resetTimerTask

      public void resetTimerTask()
      The resetTimerTask function is used to reset the timerTask variable. This function is called when a new game begins, and it resets the timerTask variable so that it can be used again.
    • playGame

      public void playGame()
      The playGame function is the main function of the game. It starts by checking if a game is running, and if not, it starts one. Then it enters a while loop that runs as long as there's an active game running. The while loop contains two try-catch blocks: One for accepting new players to join the server (if they're allowed), and one for receiving messages from clients (such as when they disconnect). If a player disconnects, their ID will be sent to this class through MySocket's getPlayerSocket().getInputStream(), which will then be read by Scanner scanner = new Scan
    • broadcastUpdate

      public void broadcastUpdate(String messageForPlayers)
      The broadcastUpdate function is used to send a message to all players in the game.
      Parameters:
      messageForPlayers - messageForPlayers Send a message to all the clients
    • removePlayer

      public void removePlayer(int playerId)
      The removePlayer function removes a player from the game.
      Parameters:
      playerId - playerId Find the player in the clients map
    • close

      public void close()
      The close function closes the server socket and all the client sockets. It also clears both clients hashmaps.
      Overrides:
      close in class MyServer
    • isGameRunning

      public boolean isGameRunning()
      The isGameRunning function returns a boolean value that indicates whether the game is running or not.
      Returns:
      A boolean value
    • startGame

      public void startGame()
      The startGame function is called when the game is ready to start. It sets the gameIsRunning variable to true, and then sends an updated model to all the players in this Game's playerList.
    • stopGame

      public void stopGame()
      The stopGame function is called when the game ends. It sets the gameIsRunning boolean to false, cancels any turnTimer that may be running, resets the timerTask and closes all of its sockets.