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
-
Field Summary
-
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 serverjava.util.Map<java.lang.String,Lobby>
getGameLobbies()
gameLobbies getterUser
getUser(java.lang.String username, Lobby lobby)
Finds an User, looking for its username in a lobbyjava.util.Map<User,Lobby>
getUsers()
users getterjava.util.List<User>
getUsersInRoom(Lobby lobby)
Provides a list of the users in a lobbyjava.util.List<User>
getUsersInWaitingRoom()
Provides a list of the users in a lobbystatic void
main(java.lang.String[] args)
Creates and runs the servervoid
moveToRoom(User user, Lobby lobby)
Moves an user to another lobbyvoid
moveToWaitingRoom(User user)
Moves an user to the waiting roomvoid
onDisconnect(User user)
Disconnects an user from the servervoid
removeRoom(Lobby lobby)
Removes a lobbyvoid
run()
Client greetervoid
sendMessageToWaitingRoom(Message message)
Sends a message to all the clients in the waiting roomvoid
startServer()
Opens the Socket connectionMethods 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
-
Constructor Details
-
Server
public Server(int port, int maxClients) throws java.io.IOExceptionDefault constructorCreates 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.IOExceptionCreates and runs the server- Parameters:
args
- command line arguments, see README.md- Throws:
java.io.IOException
- if an I/O error occurs
-
getGameLobbies
gameLobbies getter- Returns:
- the LobbyName-Lobby map
-
getUsers
users getter- Returns:
- the user-lobby map
-
getUser
Finds an User, looking for its username in a lobby- Parameters:
username
- the user's usernamelobby
- the lobby to search for the user- Returns:
- the User object corresponding to the up mentioned description
-
run
public void run()Client greeterEvery 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 interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
startServer
public void startServer()Opens the Socket connection -
addClient
Adds a new User to the serverUpon 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
Provides a list of the users in a lobby- Parameters:
lobby
- the lobby to check- Returns:
- the list of users in the lobby
-
sendMessageToWaitingRoom
Sends a message to all the clients in the waiting room- Parameters:
message
- the message to send
-
getUsersInWaitingRoom
Provides a list of the users in a lobby- Returns:
- the list of users in the waiting room
-
removeRoom
Removes a lobbyIf there are players in the lobby, those are moved to the waiting room
- Parameters:
lobby
- the lobby to delete
-
onDisconnect
Disconnects an user from the server- Parameters:
user
- the user to kick
-
moveToRoom
Moves an user to another lobby- Parameters:
user
- the user to movelobby
- the lobby to move the user to
-
moveToWaitingRoom
Moves an user to the waiting room- Parameters:
user
- the user to move
-