Class SugarServer
java.lang.Object
it.polimi.ingsw.communication.tcp_server.TcpServer
it.polimi.ingsw.communication.sugar_framework.SugarServer
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
GameServer
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidThe server calls this function before starting his blocking accept-connection loopprivate voidIdentifies expired heartbeatsCreates a new roomvoidcreateRoom(UUID roomId) Creates a new room with the specified uuidvoiddeleteRoom(UUID roomId) Deletes the room with the specified id (hall cannot be deleted), and puts all the peers in that room back into the hallprotected voiddisconnectPeer(Peer peer) Disconnects the peer from the servergetPeerFromClient(Socket client) (package private) voidheartbeatHandler(SugarMessage message) Removes the message with that id from the heartbeatsToWait listvoidmulticastToRoom(UUID roomId, SugarMessage message) Sends the message to any peer in the specified room (if it exists)protected final voidThe server calls this function any time a new client connects.protected final voidonDisconnect(Socket client) The server calls this function any time a client disconnects Subclasses that override this function MUST call super().protected final voidThe server calls this function any time it receives a message from the client.protected abstract voidonPeerConnect(Peer peer) The server calls this function any time a peer connects to the serverprotected abstract voidonPeerDisconnect(Peer peer) The server calls this function any time a peer disconnects from the serverprotected abstract voidonPeerMessage(Peer peer, SugarMessage message) The server calls this function any time it receives a message from a peer.voidsend(SugarMessage msg, Peer peer) Delivers the message to the peerprivate voidSends heartbeat messages to all peersprivate voidSend the UPI initial message to the peerprivate voidThis function is responsible for the heartbeat systemMethods inherited from class it.polimi.ingsw.communication.tcp_server.TcpServer
disconnectClient, log, run, send, setLogHeader, setLogStream
-
Field Details
-
port
private static final int port- See Also:
-
hallId
-
hall
-
rooms
-
heartbeatsToWait
-
-
Constructor Details
-
SugarServer
- Throws:
IOException
-
-
Method Details
-
beforeStart
protected void beforeStart()Description copied from class:TcpServerThe server calls this function before starting his blocking accept-connection loop- Overrides:
beforeStartin classTcpServer
-
onConnect
Description copied from class:TcpServerThe server calls this function any time a new client connects. Subclasses that override this function MUST call super(). -
onPeerConnect
The server calls this function any time a peer connects to the server- Parameters:
peer- the peer that has connected
-
onDisconnect
Description copied from class:TcpServerThe server calls this function any time a client disconnects Subclasses that override this function MUST call super().- Overrides:
onDisconnectin classTcpServer- Parameters:
client- the socket communicating with the client
-
onPeerDisconnect
The server calls this function any time a peer disconnects from the server- Parameters:
peer- the peer that disconnected from the server
-
onMessage
Description copied from class:TcpServerThe server calls this function any time it receives a message from the client. Subclasses that override this function MUST call super(). -
onPeerMessage
The server calls this function any time it receives a message from a peer.- Parameters:
peer- the peer who sent the messagemessage- the message received from the peer
-
heartbeatHandler
Removes the message with that id from the heartbeatsToWait list- Parameters:
message- the message received
-
startHeartbeatDaemon
private void startHeartbeatDaemon()This function is responsible for the heartbeat system -
sendHeartBeats
private void sendHeartBeats()Sends heartbeat messages to all peers -
checkHeartBeatExpiration
private void checkHeartBeatExpiration()Identifies expired heartbeats -
disconnectPeer
Disconnects the peer from the server- Parameters:
peer- any connected peer
-
createRoom
Creates a new room- Returns:
- the UUID of the new room
-
createRoom
Creates a new room with the specified uuid- Parameters:
roomId- the uuid of the room
-
getRoom
-
multicastToRoom
public void multicastToRoom(UUID roomId, SugarMessage message) throws IOException, RoomNotFoundException Sends the message to any peer in the specified room (if it exists)- Parameters:
roomId- uuid of the roommessage- message to send- Throws:
IOExceptionRoomNotFoundException
-
deleteRoom
Deletes the room with the specified id (hall cannot be deleted), and puts all the peers in that room back into the hall- Parameters:
roomId- the id of the room to be deleted
-
send
Delivers the message to the peer- Parameters:
msg- any Sugar messagepeer- any peer- Throws:
IOException- if the message could not be sent through the socket
-
sendUPI
Send the UPI initial message to the peer- Parameters:
peer- any peer- Throws:
IOException- if the message could not be sent through the socket
-
getPeerFromClient
- Parameters:
client- any connected client- Returns:
- the peer associated to that client
-