nxt.peer

## Class NetworkHandler

- java.lang.Object
- - nxt.peer.NetworkHandler

- All Implemented Interfaces:
    java.lang.Runnable

      public final class NetworkHandler
      extends java.lang.Object
      implements java.lang.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.

- - ### Method Summary

    All Methods Static Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `static int` | `broadcastMessage(NetworkMessage message)` Broadcast a message to all connected peers
    `static int` | `broadcastMessage(Peer sender, NetworkMessage message)` Broadcast a message to all connected peers
    `static int` | `getConnectionCount()` Get the connected peer count
    `static int` | `getDefaultPeerPort()` Get the default peer port
    `static int` | `getInboundCount()` Get the number of inbound connections
    `static int` | `getMaxInboundConnections()` Return the maximum number of inbound connections
    `static int` | `getMaxOutboundConnections()` Return the maximum number of outbound connections
    `static int` | `getOutboundCount()` Get the number of outbound connections
    `static void` | `init()` Initialize the network handler
    `static boolean` | `isNetworkStarted()` Check if the network has finished initialization
    `void` | `run()` Network listener
    `static void` | `shutdown()` Shutdown the network handler

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Method Detail

    - #### 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 `java.lang.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