com.h3c.util.net.msg

## Class ConnectionBroker

- java.lang.Object
- - com.h3c.util.net.msg.ConnectionBroker

-     public class ConnectionBroker
      extends Object

  Broker for easily making network-spanning connections. Servers host and accept connections, while clients simply connect. Beyond that point the connection-to-connection setup is entirely symmetrical as either side can initiate a conversation on a particular topic, provided the far-side supports having conversations on the given topic.

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `ConnectionBroker.Transport` Enumeration indicating whether plain or ssl connection is used.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected int` | `backlog` Maximum socket queue backlog.
    `protected InetAddress` | `bindAddress` Interface address to bind the socket to; if null, bind to all.
    `protected int` | `port` Port on which the channel is bound.
    `protected ServerSocket` | `socket`
    `protected ServerSocketFactory` | `socketFactory` Factory to be used for creating the channel server socket.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `ConnectionBroker()` Creates a client-side-only connection broker using plain-text transport.
    `ConnectionBroker(int port)` Creates a server-side connection broker for hosting connections on the specified port.
    `ConnectionBroker(int port, InetAddress bindAddress)` Creates a server-side connection broker for hosting connections on the specified port and adapter.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `Connection` | `accept(String name, ConversationProvider resolver)` Host and accept a connection on the specified network port.
    `Connection` | `accept(String name, ConversationProvider resolver, ConnectionListener listener)` Host and accept a connection on the specified network port.
    `void` | `close()` Close the broker server socket.
    `Connection` | `connect(String name, String host, int port, ConnectionBroker.Transport transport, ConversationProvider resolver)` Make a connection to the given host and on the specified network port.
    `Connection` | `connect(String name, String host, int port, ConnectionBroker.Transport transport, ConversationProvider resolver, ConnectionListener listener)` Make a connection to the given host and on the specified network port.
    `Connection` | `connect(String name, String host, int port, ConversationProvider resolver)` Make a plain-text connection to the given host and on the specified network port.
    `protected void` | `finalize()`
    `int` | `getBacklog()` Get the maximum backlog.
    `InetAddress` | `getBindAddress()` Get the address on which this server-side broker socket is (to be) bound.
    `int` | `getPort()` Get the port to which this channel is currently bound.
    `ServerSocket` | `getSocket()` Get the server socket.
    `ServerSocketFactory` | `getSocketFactory()` Get the socket factory used to generate the broker server socket.
    `void` | `setBacklog(int backlog)` Set the maximum backlog.
    `void` | `setBindAddress(InetAddress bindAddress)` Set the address on which this server-side broker socket is to be bound.
    `void` | `setSocketFactory(ServerSocketFactory socketFactory)` Set the socket factory used to generate the broker server socket.

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

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

- - ### Field Detail

    - #### socketFactory

          protected ServerSocketFactory socketFactory

      Factory to be used for creating the channel server socket.

    - #### socket

          protected ServerSocket socket

    - #### port

          protected int port

      Port on which the channel is bound.

    - #### backlog

          protected int backlog

      Maximum socket queue backlog.

    - #### bindAddress

          protected InetAddress bindAddress

      Interface address to bind the socket to; if null, bind to all.

  - ### Constructor Detail

    - #### ConnectionBroker

          public ConnectionBroker()

      Creates a client-side-only connection broker using plain-text transport.

    - #### ConnectionBroker

          public ConnectionBroker(int port)

      Creates a server-side connection broker for hosting connections on the specified port.
      Parameters:
        `port` - tcp/ip port on which to listen for connections

    - #### ConnectionBroker

          public ConnectionBroker(int port,
                          InetAddress bindAddress)

      Creates a server-side connection broker for hosting connections on the specified port and adapter.
      Parameters:
        `port` - tcp/ip port on which to listen for connections
        `bindAddress` - adapter address

  - ### Method Detail

    - #### getSocket

          public ServerSocket getSocket()
                                 throws IOException

      Get the server socket. Applies only to the server-side connection broker. Creates and binds the sockets if one has not yet been created.
      Returns:
        the server socket or null for client-side connection broker.
      Throws:
        `IOException` - thrown if an error is encountered while creating a server socket

    - #### finalize

          protected void finalize()
                           throws Throwable

      Overrides:
        `finalize` in class `Object`
      Throws:
        `Throwable`

    - #### accept

          public Connection accept(String name,
                          ConversationProvider resolver,
                          ConnectionListener listener)
                            throws IOException

      Host and accept a connection on the specified network port. The calling application may resume accepting additional connections on the same port once it has obtained the connection. The returned connection will be named according to the supplied name.
      Parameters:
        `name` - name to give the accepted connection
        `resolver` - conversation provider to be assigned to the new connection
        `listener` - connection listener to be assigned to the newly created connection
      Returns:
        accepted connection properly initialized to read/write across the socket
      Throws:
        `IOException` - thrown if an error is encountered while accepting a connection

    - #### accept

          public Connection accept(String name,
                          ConversationProvider resolver)
                            throws IOException

      Host and accept a connection on the specified network port. The calling application may resume accepting additional connections on the same port once it has obtained the connection. The returned connection will be named according to the supplied name.
      Parameters:
        `name` - name to give the accepted connection
        `resolver` - conversation provider to be assigned to the new connection
      Returns:
        accepted connection properly initialized to read/write across the socket
      Throws:
        `IOException` - thrown if an error is encountered while accepting a connection

    - #### connect

          public Connection connect(String name,
                           String host,
                           int port,
                           ConversationProvider resolver)
                             throws IOException

      Make a plain-text connection to the given host and on the specified network port. The calling application may make additional connections on the same port once it has obtained the connection. The returned connection will be named according to the supplied name.
      Parameters:
        `name` - name to give the accepted connection
        `host` - IP or DNS name of the host to which the connect
        `port` - IP network port to connect to
        `resolver` - conversation provider to be assigned to the new connection
      Returns:
        accepted connection properly initialized to read/write across the socket
      Throws:
        `IOException` - thrown if an error is encountered during connecting

    - #### connect

          public Connection connect(String name,
                           String host,
                           int port,
                           ConnectionBroker.Transport transport,
                           ConversationProvider resolver,
                           ConnectionListener listener)
                             throws IOException

      Make a connection to the given host and on the specified network port. The calling application may make additional connections on the same port once it has obtained the connection. The returned connection will be named according to the supplied name.
      Parameters:
        `name` - name to give the accepted connection
        `host` - IP or DNS name of the host to which the connect
        `port` - IP network port to connect to
        `transport` - indicates whether to use a plain-text or SSL socket
        `resolver` - conversation provider to be assigned to the new connection
        `listener` - connection listener to be applied before the connection is started
      Returns:
        accepted connection properly initialized to read/write across the socket
      Throws:
        `IOException` - thrown if an error is encountered during connecting

    - #### connect

          public Connection connect(String name,
                           String host,
                           int port,
                           ConnectionBroker.Transport transport,
                           ConversationProvider resolver)
                             throws IOException

      Make a connection to the given host and on the specified network port. The calling application may make additional connections on the same port once it has obtained the connection. The returned connection will be named according to the supplied name.
      Parameters:
        `name` - name to give the accepted connection
        `host` - IP or DNS name of the host to which the connect
        `port` - IP network port to connect to
        `transport` - indicates whether to use a plain-text or SSL socket
        `resolver` - conversation provider to be assigned to the new connection
      Returns:
        accepted connection properly initialized to read/write across the socket
      Throws:
        `IOException` - thrown if an error is encountered during connecting

    - #### getPort

          public int getPort()

      Get the port to which this channel is currently bound.
      Returns:
        port number or 0 if not curently bound

    - #### getBacklog

          public int getBacklog()

      Get the maximum backlog.
      Returns:
        size of the backlog queue

    - #### setBacklog

          public void setBacklog(int backlog)

      Set the maximum backlog.
      Parameters:
        `backlog` - maximum size of the backlog queue

    - #### getBindAddress

          public InetAddress getBindAddress()

      Get the address on which this server-side broker socket is (to be) bound.
      Returns:
        adapter address on which the channel socket is (to be) bound

    - #### setBindAddress

          public void setBindAddress(InetAddress bindAddress)

      Set the address on which this server-side broker socket is to be bound.
      Parameters:
        `bindAddress` - adapter address on which to bind the channel socket

    - #### getSocketFactory

          public ServerSocketFactory getSocketFactory()

      Get the socket factory used to generate the broker server socket.
      Returns:
        server socket factory.

    - #### setSocketFactory

          public void setSocketFactory(ServerSocketFactory socketFactory)

      Set the socket factory used to generate the broker server socket.
      Parameters:
        `socketFactory` - server socket factory

    - #### close

          public void close()
                     throws IOException

      Close the broker server socket.
      Throws:
        `IOException` - thrown if an error is encountered during closing the connection socket

Copyright © 2014. All Rights Reserved.