java.lang.Object
it.polimi.ingsw.communication.tcp_server.TcpServer
it.polimi.ingsw.communication.sugar_framework.SugarServer
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
GameServer

public abstract class SugarServer extends TcpServer
  • Field Details

  • Constructor Details

  • Method Details

    • beforeStart

      protected void beforeStart()
      Description copied from class: TcpServer
      The server calls this function before starting his blocking accept-connection loop
      Overrides:
      beforeStart in class TcpServer
    • onConnect

      protected final void onConnect(Socket client)
      Description copied from class: TcpServer
      The server calls this function any time a new client connects. Subclasses that override this function MUST call super().
      Overrides:
      onConnect in class TcpServer
      Parameters:
      client - the socket communicating with the client
    • onPeerConnect

      protected abstract void onPeerConnect(Peer peer)
      The server calls this function any time a peer connects to the server
      Parameters:
      peer - the peer that has connected
    • onDisconnect

      protected final void onDisconnect(Socket client)
      Description copied from class: TcpServer
      The server calls this function any time a client disconnects Subclasses that override this function MUST call super().
      Overrides:
      onDisconnect in class TcpServer
      Parameters:
      client - the socket communicating with the client
    • onPeerDisconnect

      protected abstract void onPeerDisconnect(Peer peer)
      The server calls this function any time a peer disconnects from the server
      Parameters:
      peer - the peer that disconnected from the server
    • onMessage

      protected final void onMessage(Socket client, String message)
      Description copied from class: TcpServer
      The server calls this function any time it receives a message from the client. Subclasses that override this function MUST call super().
      Overrides:
      onMessage in class TcpServer
      Parameters:
      client - the socket communicating with the client
      message - the message received from the client
    • onPeerMessage

      protected abstract void onPeerMessage(Peer peer, SugarMessage message)
      The server calls this function any time it receives a message from a peer.
      Parameters:
      peer - the peer who sent the message
      message - the message received from the peer
    • heartbeatHandler

      void heartbeatHandler(SugarMessage message)
      Removes the message with that id from the heartbeatsToWait list
      Parameters:
      message - the message received
    • startHeartbeatDaemon

      private void startHeartbeatDaemon()
      This function is responsible for the heartbeat system
    • sendHeartBeats

      private void sendHeartBeats()
      Sends heartbeat messages to all peers
    • checkHeartBeatExpiration

      private void checkHeartBeatExpiration()
      Identifies expired heartbeats
    • disconnectPeer

      protected void disconnectPeer(Peer peer)
      Disconnects the peer from the server
      Parameters:
      peer - any connected peer
    • createRoom

      public UUID createRoom()
      Creates a new room
      Returns:
      the UUID of the new room
    • createRoom

      public void createRoom(UUID roomId)
      Creates a new room with the specified uuid
      Parameters:
      roomId - the uuid of the room
    • getRoom

      public Room getRoom(UUID roomId)
    • multicastToRoom

      public void multicastToRoom(UUID roomId, SugarMessage message) throws IOException, RoomNotFoundException
      Sends the message to any peer in the specified room (if it exists)
      Parameters:
      roomId - uuid of the room
      message - message to send
      Throws:
      IOException
      RoomNotFoundException
    • deleteRoom

      public void deleteRoom(UUID roomId)
      Deletes the room with the specified id (hall cannot be deleted), and puts all the peers in that room back into the hall
      Parameters:
      roomId - the id of the room to be deleted
    • send

      public void send(SugarMessage msg, Peer peer) throws IOException
      Delivers the message to the peer
      Parameters:
      msg - any Sugar message
      peer - any peer
      Throws:
      IOException - if the message could not be sent through the socket
    • sendUPI

      private void sendUPI(Peer peer) throws IOException
      Send the UPI initial message to the peer
      Parameters:
      peer - any peer
      Throws:
      IOException - if the message could not be sent through the socket
    • getPeerFromClient

      private Optional<Peer> getPeerFromClient(Socket client)
      Parameters:
      client - any connected client
      Returns:
      the peer associated to that client