Package nxt.peer

Class NetworkHandler

java.lang.Object
nxt.peer.NetworkHandler
All Implemented Interfaces:
Runnable

public final class NetworkHandler extends Object implements Runnable
The network handler creates outbound connections and adds them to the network selector. A new outbound connection will be created whenever the number of outbound connections is less than the maximum number of outbound connections. The network handler opens a local port and listens for incoming connections. When a connection is received, it creates a socket channel and accepts the connection as long as the maximum number of inbound connections has not been reached. The socket is then added to the network selector. When a message is received from a peer node, it is processed by a message handler executing on a separate thread. The message handler processes the message and then creates a response message to be returned to the originating node. The network handler terminates when its shutdown() method is called.
  • Field Details

    • TESTNET_PEER_PORT

      public static final int TESTNET_PEER_PORT
      Testnet peer port
    • peerReadTimeout

      public static final int peerReadTimeout
      Peer read timeout (seconds)
  • Method Details

    • init

      public static void init()
      Initialize the network handler
    • shutdown

      public static void shutdown()
      Shutdown the network handler
    • run

      public void run()
      Network listener
      Specified by:
      run in interface Runnable
    • isNetworkStarted

      public static boolean isNetworkStarted()
      Check if the network has finished initialization
      Returns:
      TRUE if the network is available
    • broadcastMessage

      public static int broadcastMessage(NetworkMessage message)
      Broadcast a message to all connected peers
      Parameters:
      message - Message to send
      Returns:
      number of peers to which message was sent
    • broadcastMessage

      public static int broadcastMessage(Peer sender, NetworkMessage message)
      Broadcast a message to all connected peers
      Parameters:
      sender - Message sender or null if our message
      message - Message to send
      Returns:
      number of peers to which message was sent
    • getDefaultPeerPort

      public static int getDefaultPeerPort()
      Get the default peer port
      Returns:
      Default peer port
    • getConnectionCount

      public static int getConnectionCount()
      Get the connected peer count
      Returns:
      Connected peer count
    • getInboundCount

      public static int getInboundCount()
      Get the number of inbound connections
      Returns:
      Number of inbound connections
    • getMaxInboundConnections

      public static int getMaxInboundConnections()
      Return the maximum number of inbound connections
      Returns:
      Number of inbound connections
    • getOutboundCount

      public static int getOutboundCount()
      Get the number of outbound connections
      Returns:
      Number of outbound connections
    • getMaxOutboundConnections

      public static int getMaxOutboundConnections()
      Return the maximum number of outbound connections
      Returns:
      Number of outbound connections