Module AM37

Class VirtualClient

java.lang.Object
java.lang.Thread
it.polimi.ingsw.network.server.VirtualClient
All Implemented Interfaces:
ServerMessageManagerVisitor, java.lang.Runnable

public class VirtualClient
extends java.lang.Thread
implements ServerMessageManagerVisitor
Server-side client communication and message handler

For each new user connecting to the server, a VirtualClient object is created; its purpose is to receive, send and route the messages from/to the socket connected to the client.
Note that connecting means establishing a connection with the server, even without choosing an username.

  • Nested Class Summary

    Nested classes/interfaces inherited from class java.lang.Thread

    java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
  • Field Summary

    Fields inherited from class java.lang.Thread

    MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
  • Constructor Summary

    Constructors 
    Constructor Description
    VirtualClient​(Server server, java.net.Socket clientConnection)
    Default constructor
  • Method Summary

    Modifier and Type Method Description
    void cannotHandleMessage​(Message message)
    Manages all non-lobby related messages (e.g.
    void closeConnection()
    Terminates the connection with the corresponding client
    void createLobby​(CreateLobbyRequest message)
    Manages the CreateLobbyRequest Tries to create a new lobby, based on the user input; if the parameters are correct (name not already taken, size between 2 and 3), the lobby is created and a confirmation LobbyCreatedEvent is sent to the lobby creator and all the users in the waiting room; otherwise, an error message is sent to the creator only.
    void disconnectFromServer()
    Disconnects this user from the server
    User getUser()
    user getter
    void joinLobby​(JoinLobbyRequest message)
    Manages the JoinLobbyRequest
    void login​(LoginRequest message)
    Manages the LoginRequest
    void notify​(Message message)
    Sends a Message to this user's client
    void notify​(java.lang.String string)
    Sends a string to this user's client
    void ping()
    Manages the ping mechanism manually, disconnecting the user if no pong message is received after a certain amount of time after the ping is sent
    void run()
    Receives messages and passes them to the parser

    Methods inherited from class java.lang.Thread

    activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield

    Methods inherited from class java.lang.Object

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

    • VirtualClient

      public VirtualClient​(Server server, java.net.Socket clientConnection)
      Default constructor

      Given a socket, the constructor tries to open i/o streams; if it fails, the connection is closed.

      Parameters:
      server - the server object
      clientConnection - the socket connection to/from the client
  • Method Details

    • run

      public void run()
      Receives messages and passes them to the parser
      Specified by:
      run in interface java.lang.Runnable
      Overrides:
      run in class java.lang.Thread
    • getUser

      public User getUser()
      user getter
      Returns:
      the User object associated to this
    • notify

      public void notify​(Message message)
      Sends a Message to this user's client
      Parameters:
      message - the Message to send
    • notify

      public void notify​(java.lang.String string)
      Sends a string to this user's client
      Parameters:
      string - the string to send
    • closeConnection

      public void closeConnection()
      Terminates the connection with the corresponding client
    • disconnectFromServer

      public void disconnectFromServer()
      Disconnects this user from the server
      See Also:
      Server.onDisconnect(User)
    • login

      public void login​(LoginRequest message)
      Manages the LoginRequest

      This method tries to add the user to the server; if the username is invalid or already taken, it sends a response asking for a new username; if the server waiting room is full, sends a message and disconnects the client;

      Specified by:
      login in interface ServerMessageManagerVisitor
      Parameters:
      message - the message to manage
    • joinLobby

      public void joinLobby​(JoinLobbyRequest message)
      Manages the JoinLobbyRequest
      Specified by:
      joinLobby in interface ServerMessageManagerVisitor
      Parameters:
      message - the message to manage
    • createLobby

      public void createLobby​(CreateLobbyRequest message)
      Manages the CreateLobbyRequest Tries to create a new lobby, based on the user input; if the parameters are correct (name not already taken, size between 2 and 3), the lobby is created and a confirmation LobbyCreatedEvent is sent to the lobby creator and all the users in the waiting room; otherwise, an error message is sent to the creator only.
      Specified by:
      createLobby in interface ServerMessageManagerVisitor
      Parameters:
      message - the message to manage
    • cannotHandleMessage

      public void cannotHandleMessage​(Message message)
      Manages all non-lobby related messages (e.g. game messages), redirecting them to the user's corresponding lobby, which will take care of it; if the user is not associated to any lobby, the message is ignored and the incident is reported in the server log
      Specified by:
      cannotHandleMessage in interface ServerMessageManagerVisitor
      Parameters:
      message - the message to handle
    • ping

      public void ping()
      Manages the ping mechanism manually, disconnecting the user if no pong message is received after a certain amount of time after the ping is sent