java.lang.Object
it.polimi.ingsw.network.client.NetworkHandler
- All Implemented Interfaces:
java.lang.Runnable
public class NetworkHandler
extends java.lang.Object
implements java.lang.Runnable
Client-side client communication and message handler
-
Constructor Summary
Constructors Constructor Description NetworkHandler(Client client)Default constructor -
Method Summary
Modifier and Type Method Description voidcloseConnection()Terminates the current socket connectionvoidlogin(java.lang.String username)Sends a login request to the servervoidping()Handles the client-server ping messages to check for connectivityvoidrun()Keeps listening on the network for messagesvoidsendMessage(Message message)Sends a message to the servervoidsendMessage(java.lang.String string)Sends a string to the server
-
Constructor Details
-
NetworkHandler
public NetworkHandler(Client client) throws java.io.IOException, java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutExceptionDefault constructorTries to open a socket channel with the server
- Parameters:
client- the client to connect- Throws:
java.io.IOException- if an I/O error occurs while opening the socket streamsjava.lang.InterruptedExceptionjava.util.concurrent.ExecutionExceptionjava.util.concurrent.TimeoutException
-
-
Method Details
-
run
public void run()Keeps listening on the network for messagesIn case of a ping message, see ping(); for any other message, the relative visitor method is called
- Specified by:
runin interfacejava.lang.Runnable- See Also:
ClientMessageManagerVisitor
-
login
public void login(java.lang.String username)Sends a login request to the server- Parameters:
username- the username to log the user in
-
closeConnection
public void closeConnection()Terminates the current socket connectionThis method causes also the input and output socket streams to be closed
-
sendMessage
Sends a message to the server- Parameters:
message- the Message to be sent- Throws:
java.io.IOException- if an I/O error occurs
-
sendMessage
public void sendMessage(java.lang.String string) throws java.io.IOExceptionSends a string to the server- Parameters:
string- the string to send- Throws:
java.io.IOException- if an I/O error occurs- See Also:
ping()
-
ping
public void ping()Handles the client-server ping messages to check for connectivityEvery 5 seconds, this methods schedules a closeConnection() to be run; when a ping message is received, the timer is defused and the thread is put to sleep for a second.
At every call, a ping message is also sent to the server
-