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
-
Field Summary
-
Constructor Summary
Constructors Constructor Description VirtualClient(Server server, java.net.Socket clientConnection)Default constructor -
Method Summary
Modifier and Type Method Description voidcannotHandleMessage(Message message)Manages all non-lobby related messages (e.g.voidcloseConnection()Terminates the connection with the corresponding clientvoidcreateLobby(CreateLobbyRequest message)Manages theCreateLobbyRequestTries 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.voiddisconnectFromServer()Disconnects this user from the serverUsergetUser()user gettervoidjoinLobby(JoinLobbyRequest message)Manages theJoinLobbyRequestvoidlogin(LoginRequest message)Manages the LoginRequestvoidnotify(Message message)Sends aMessageto this user's clientvoidnotify(java.lang.String string)Sends a string to this user's clientvoidping()Manages the ping mechanism manually, disconnecting the user if no pong message is received after a certain amount of time after the ping is sentvoidrun()Receives messages and passes them to the parserMethods 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, yieldMethods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface it.polimi.ingsw.controller.ServerMessageManagerVisitor
addWorkerOnBoard, buildableCells, chooseGod, chooseInitialGods, chooseStartingPlayer, endTurn, managePlayerBuild, managePlayerMove, onMatchReloadResponse, selectCellToBuild, selectWorker, walkableCells
-
Constructor Details
-
VirtualClient
Default constructorGiven a socket, the constructor tries to open i/o streams; if it fails, the connection is closed.
- Parameters:
server- the server objectclientConnection- the socket connection to/from the client
-
-
Method Details
-
run
public void run()Receives messages and passes them to the parser- Specified by:
runin interfacejava.lang.Runnable- Overrides:
runin classjava.lang.Thread
-
getUser
user getter- Returns:
- the User object associated to this
-
notify
Sends aMessageto 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
Manages the LoginRequestThis 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:
loginin interfaceServerMessageManagerVisitor- Parameters:
message- the message to manage
-
joinLobby
Manages theJoinLobbyRequest- Specified by:
joinLobbyin interfaceServerMessageManagerVisitor- Parameters:
message- the message to manage
-
createLobby
Manages theCreateLobbyRequestTries 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:
createLobbyin interfaceServerMessageManagerVisitor- Parameters:
message- the message to manage
-
cannotHandleMessage
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:
cannotHandleMessagein interfaceServerMessageManagerVisitor- 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
-