Module AM37

Class Server

java.lang.Object
java.lang.Thread
it.polimi.ingsw.network.server.Server
All Implemented Interfaces:
java.lang.Runnable

public class Server
extends java.lang.Thread
Manages the client connections
  • 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
    Server​(int port, int maxClients)
    Default constructor
  • Method Summary

    Modifier and Type Method Description
    void addClient​(VirtualClient virtualClient)
    Adds a new User to the server
    java.util.Map<java.lang.String,​Lobby> getGameLobbies()
    gameLobbies getter
    User getUser​(java.lang.String username, Lobby lobby)
    Finds an User, looking for its username in a lobby
    java.util.Map<User,​Lobby> getUsers()
    users getter
    java.util.List<User> getUsersInRoom​(Lobby lobby)
    Provides a list of the users in a lobby
    java.util.List<User> getUsersInWaitingRoom()
    Provides a list of the users in a lobby
    static void main​(java.lang.String[] args)
    Creates and runs the server
    void moveToRoom​(User user, Lobby lobby)
    Moves an user to another lobby
    void moveToWaitingRoom​(User user)
    Moves an user to the waiting room
    void onDisconnect​(User user)
    Disconnects an user from the server
    void removeRoom​(Lobby lobby)
    Removes a lobby
    void run()
    Client greeter
    void sendMessageToWaitingRoom​(Message message)
    Sends a message to all the clients in the waiting room
    void startServer()
    Opens the Socket connection

    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

    • Server

      public Server​(int port, int maxClients) throws java.io.IOException
      Default constructor

      Creates a new server instance

      Parameters:
      port - the custom port (between 1024 and 65535)
      maxClients - the maximum number of clients allowed in the waiting room (at least 2)
      Throws:
      java.io.IOException - if an I/O error occurs while creating the log file
  • Method Details

    • main

      public static void main​(java.lang.String[] args) throws java.io.IOException
      Creates and runs the server
      Parameters:
      args - command line arguments, see README.md
      Throws:
      java.io.IOException - if an I/O error occurs
    • getGameLobbies

      public java.util.Map<java.lang.String,​Lobby> getGameLobbies()
      gameLobbies getter
      Returns:
      the LobbyName-Lobby map
    • getUsers

      public java.util.Map<User,​Lobby> getUsers()
      users getter
      Returns:
      the user-lobby map
    • getUser

      public User getUser​(java.lang.String username, Lobby lobby)
      Finds an User, looking for its username in a lobby
      Parameters:
      username - the user's username
      lobby - the lobby to search for the user
      Returns:
      the User object corresponding to the up mentioned description
    • run

      public void run()
      Client greeter

      Every time the server receives a Socket connection, this method creates and runs a new VirtualClient object, which handles all the messages.
      In the case of a Socket-thrown exception, the connection with the client is closed.

      Specified by:
      run in interface java.lang.Runnable
      Overrides:
      run in class java.lang.Thread
    • startServer

      public void startServer()
      Opens the Socket connection
    • addClient

      public void addClient​(VirtualClient virtualClient) throws RoomFullException
      Adds a new User to the server

      Upon receiving a login request (in the VirtualClient), this method checks if the username is valid (usernames have to be unique and non-reserved keywords, such as ReservedUsernames.BROADCAST) and if there is "enough space" on the server

      Parameters:
      virtualClient - the user to add
      Throws:
      RoomFullException - if the room the user is trying to join is full
    • getUsersInRoom

      public java.util.List<User> getUsersInRoom​(Lobby lobby)
      Provides a list of the users in a lobby
      Parameters:
      lobby - the lobby to check
      Returns:
      the list of users in the lobby
    • sendMessageToWaitingRoom

      public void sendMessageToWaitingRoom​(Message message)
      Sends a message to all the clients in the waiting room
      Parameters:
      message - the message to send
    • getUsersInWaitingRoom

      public java.util.List<User> getUsersInWaitingRoom()
      Provides a list of the users in a lobby
      Returns:
      the list of users in the waiting room
    • removeRoom

      public void removeRoom​(Lobby lobby)
      Removes a lobby

      If there are players in the lobby, those are moved to the waiting room

      Parameters:
      lobby - the lobby to delete
    • onDisconnect

      public void onDisconnect​(User user)
      Disconnects an user from the server
      Parameters:
      user - the user to kick
    • moveToRoom

      public void moveToRoom​(User user, Lobby lobby)
      Moves an user to another lobby
      Parameters:
      user - the user to move
      lobby - the lobby to move the user to
    • moveToWaitingRoom

      public void moveToWaitingRoom​(User user)
      Moves an user to the waiting room
      Parameters:
      user - the user to move