Package network

Class NetworkService

java.lang.Object
network.NetworkService
All Implemented Interfaces:
INetwork

public class NetworkService
extends Object
implements INetwork
  • Field Details

  • Constructor Details

  • Method Details

    • myHost

      public Host myHost()
      Description copied from interface: INetwork
      Get information about the local node
      Specified by:
      myHost in interface INetwork
      Returns:
      An object with the IP and port of the local node
    • registerNodeListener

      public void registerNodeListener​(INodeListener listener)
      Description copied from interface: INetwork
      Registers the listener to receive updates when peer state changes
      Specified by:
      registerNodeListener in interface INetwork
      Parameters:
      listener - The listener to register.
    • registerConsumer

      public void registerConsumer​(short msgCode, IMessageConsumer consumer)
      Description copied from interface: INetwork
      Registers a new consumer to handle messages with a specific code
      Specified by:
      registerConsumer in interface INetwork
      Parameters:
      msgCode - The message to register
      consumer - The consumer to handle messages with the given code
    • registerSerializer

      public void registerSerializer​(short msgCode, ISerializer serializer)
      Description copied from interface: INetwork
      Registers the (de)serializer to be used when sending or receiving messages with a specific code
      Specified by:
      registerSerializer in interface INetwork
      Parameters:
      msgCode - The message code to register
      serializer - The object used to (de)serialize messages with the given code
    • addPeer

      public void addPeer​(Host peer)
      Description copied from interface: INetwork
      Adds a new peer connection to the network layer. This method immediately attempts to connect to the peer. This method needs to be called before attempting to send messages to the peer.
      Specified by:
      addPeer in interface INetwork
      Parameters:
      peer - The peer to create a connection to.
    • removePeer

      public void removePeer​(Host peerHost)
      Description copied from interface: INetwork
      Removes the peer from the network layer, waiting until any pending messages are sent (or the connection attempt fails) before closing.
      Specified by:
      removePeer in interface INetwork
      Parameters:
      peerHost - The peer to remove from the network layer
    • forceRemovePeer

      public void forceRemovePeer​(Host peerHost)
      Description copied from interface: INetwork
      Removes the peer from the network layer, which closes any open connections.
      Specified by:
      forceRemovePeer in interface INetwork
      Parameters:
      peerHost - The peer to remove from the network layer
    • isConnectionActive

      public boolean isConnectionActive​(Host peerHost)
      Description copied from interface: INetwork
      Checks if the is an open connection to a peer
      Specified by:
      isConnectionActive in interface INetwork
      Parameters:
      peerHost - The peer
      Returns:
      Whether there is an open connection to the peer.
    • sendMessage

      public void sendMessage​(short msgCode, Object payload, Host to, boolean newChannel)
      Description copied from interface: INetwork
      Sends a message to a peer, possibly using a dedicated channel
      Specified by:
      sendMessage in interface INetwork
      Parameters:
      msgCode - The code of the message to send
      payload - The message to send
      to - The peer to send the message to
      newChannel - Whether to create a dedicated channel to send the message or not
    • sendMessage

      public void sendMessage​(short msgCode, Object payload, Host to)
      Description copied from interface: INetwork
      Sends a message to a peer
      Specified by:
      sendMessage in interface INetwork
      Parameters:
      msgCode - The code of the message to send
      payload - The message to send
      to - The peer to send the message to
    • broadcastMessage

      public void broadcastMessage​(short msgCode, Object payload, Iterator<Host> targets)
      Description copied from interface: INetwork
      Send a message to multiple peers
      Specified by:
      broadcastMessage in interface INetwork
      Parameters:
      msgCode - The code of the message to send
      payload - The message to send
      targets - The list of peers to send the message to