Module AM37

Class NetworkHandler

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
    void closeConnection()
    Terminates the current socket connection
    void login​(java.lang.String username)
    Sends a login request to the server
    void ping()
    Handles the client-server ping messages to check for connectivity
    void run()
    Keeps listening on the network for messages
    void sendMessage​(Message message)
    Sends a message to the server
    void sendMessage​(java.lang.String string)
    Sends a string to the server

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • NetworkHandler

      public NetworkHandler​(Client client) throws java.io.IOException, java.lang.InterruptedException, java.util.concurrent.ExecutionException, java.util.concurrent.TimeoutException
      Default constructor

      Tries 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 streams
      java.lang.InterruptedException
      java.util.concurrent.ExecutionException
      java.util.concurrent.TimeoutException
  • Method Details

    • run

      public void run()
      Keeps listening on the network for messages

      In case of a ping message, see ping(); for any other message, the relative visitor method is called

      Specified by:
      run in interface java.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 connection

      This method causes also the input and output socket streams to be closed

    • sendMessage

      public void sendMessage​(Message message) throws java.io.IOException
      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.IOException
      Sends 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 connectivity

      Every 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