Module it.polimi.ingsw.am13
Class NetworkHandlerSocket
java.lang.Object
it.polimi.ingsw.am13.client.network.socket.NetworkHandlerSocket
- All Implemented Interfaces:
NetworkHandler
This is the socket implementation of
NetworkHandler
.
For each method, it creates the corresponding MsgCommand
and then sends it to the server.-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate PlayerLobby
It is the player corresponding to this instance of the class (the latest one, in the sense that is updated if a player leaves a room and then creates, joins or reconnects to another)private final ObjectOutputStream
The output stream in which the messages are writtenprivate Thread
Thread sending periodically pings to server -
Constructor Summary
ConstructorsConstructorDescriptionNetworkHandlerSocket
(Socket socket, View view) Initialize the output stream and start the server response handler -
Method Summary
Modifier and TypeMethodDescriptionvoid
Choose the personal objective cardvoid
createRoom
(String chosenNickname, Token token, int players) Create a room with the passed parametersprivate void
Flush and reset the network output streamvoid
getRooms()
Get the existing roomsvoid
Make the player join the specified roomvoid
Make the player leave the room he is currently part ofvoid
pickCard
(CardPlayableIF card) Pick the passed cardprivate void
ping()
Ping the servervoid
playCard
(CardPlayableIF card, Coordinates coords, Side side) Play a card according to the parametersvoid
playStarter
(Side side) Play the starter cardvoid
Reconnect the passed player to an existing game he was previously part ofvoid
sendChatMessage
(List<PlayerLobby> receivers, String text) Send a chat message to the receivers (either a single player, or all the players)private void
sendMessage
(MsgCommand messageCommand) It sends the message to the server, by writing it to out, and then flushing and resetting out.void
Starts the thread sending pings to servervoid
stopPing()
Stops the thread sending pings to server
-
Field Details
-
out
The output stream in which the messages are written -
latestPlayer
It is the player corresponding to this instance of the class (the latest one, in the sense that is updated if a player leaves a room and then creates, joins or reconnects to another) -
pingThread
Thread sending periodically pings to server
-
-
Constructor Details
-
NetworkHandlerSocket
Initialize the output stream and start the server response handler- Parameters:
socket
- the socket which is used to communicate with the serverview
- the user interface
-
-
Method Details
-
flushReset
Flush and reset the network output stream- Throws:
IOException
- if an IO exception has occured
-
sendMessage
It sends the message to the server, by writing it to out, and then flushing and resetting out.- Parameters:
messageCommand
- the message that has 0to be sent to the server
-
getPlayer
- Specified by:
getPlayer
in interfaceNetworkHandler
- Returns:
- The current valid player this class is associated to. Null if no valid player is currently associated.
-
getRooms
public void getRooms()Get the existing rooms- Specified by:
getRooms
in interfaceNetworkHandler
-
createRoom
Create a room with the passed parameters- Specified by:
createRoom
in interfaceNetworkHandler
- Parameters:
chosenNickname
- of the player who is creating the roomtoken
- of the player who is creating the roomplayers
- of the room that is being created
-
joinRoom
Make the player join the specified room- Specified by:
joinRoom
in interfaceNetworkHandler
- Parameters:
chosenNickname
- the nickname the client wants to join the room withtoken
- chosen by the player who wants to join the roomgameId
- of the game that the client wants to join
-
reconnect
Reconnect the passed player to an existing game he was previously part of- Specified by:
reconnect
in interfaceNetworkHandler
- Parameters:
nickname
- of the player who wants to reconnecttoken
- of the player who wants to reconnect
-
leaveRoom
public void leaveRoom()Make the player leave the room he is currently part of- Specified by:
leaveRoom
in interfaceNetworkHandler
-
playStarter
Play the starter card- Specified by:
playStarter
in interfaceNetworkHandler
- Parameters:
side
- on which the starter card should be played
-
choosePersonalObjective
Choose the personal objective card- Specified by:
choosePersonalObjective
in interfaceNetworkHandler
- Parameters:
card
- the chosen personal objective
-
playCard
Play a card according to the parameters- Specified by:
playCard
in interfaceNetworkHandler
- Parameters:
card
- the player wants to playcoords
- where the card should be playedside
- on which the card should be played
-
pickCard
Pick the passed card- Specified by:
pickCard
in interfaceNetworkHandler
- Parameters:
card
- the player wants to pick
-
ping
private void ping()Ping the server -
startPing
public void startPing()Starts the thread sending pings to server- Specified by:
startPing
in interfaceNetworkHandler
-
stopPing
public void stopPing()Stops the thread sending pings to server- Specified by:
stopPing
in interfaceNetworkHandler
-
sendChatMessage
Send a chat message to the receivers (either a single player, or all the players)- Specified by:
sendChatMessage
in interfaceNetworkHandler
- Parameters:
receivers
- of the chat messagetext
- of the chat message
-