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 void
cannotHandleMessage(Message message)
Manages all non-lobby related messages (e.g.void
closeConnection()
Terminates the connection with the corresponding clientvoid
createLobby(CreateLobbyRequest message)
Manages theCreateLobbyRequest
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 serverUser
getUser()
user gettervoid
joinLobby(JoinLobbyRequest message)
Manages theJoinLobbyRequest
void
login(LoginRequest message)
Manages the LoginRequestvoid
notify(Message message)
Sends aMessage
to this user's clientvoid
notify(java.lang.String string)
Sends a string to this user's clientvoid
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 sentvoid
run()
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, yield
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
Methods 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:
run
in interfacejava.lang.Runnable
- Overrides:
run
in classjava.lang.Thread
-
getUser
user getter- Returns:
- the User object associated to this
-
notify
Sends aMessage
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
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:
login
in interfaceServerMessageManagerVisitor
- Parameters:
message
- the message to manage
-
joinLobby
Manages theJoinLobbyRequest
- Specified by:
joinLobby
in interfaceServerMessageManagerVisitor
- Parameters:
message
- the message to manage
-
createLobby
Manages theCreateLobbyRequest
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 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:
cannotHandleMessage
in 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
-