Class SocketAcceptor

java.lang.Object
org.simpleframework.transport.connect.SocketAcceptor
All Implemented Interfaces:
Runnable, Operation

class SocketAcceptor extends Object implements Operation
The SocketAcceptor object is used to accept incoming TCP connections from a specified socket address. This is used by the Connection object as a background process to accept the connections and hand them to a socket connector.

This is capable of processing SSL connections created by the internal server socket. All SSL connections are forced to finish the SSL handshake before being dispatched to the server. This ensures that there are no problems with reading the request.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final SocketAddress
    This is the local address to bind the listen socket to.
    private final TraceAnalyzer
    This is the tracing analyzer used to trace accepted sockets.
    private final SSLContext
    If provided the SSL context is used to create SSL engines.
    private final ServerSocketChannel
    This is the server socket channel used to accept connections.
    private final SocketProcessor
    The handler that manages the incoming TCP connections.
    private final ServerSocket
    This is the server socket to bind the socket address to.
    private final Trace
    This is used to collect trace events with the acceptor.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the SocketAcceptor object.
    SocketAcceptor(SocketAddress address, SocketProcessor processor, TraceAnalyzer analyzer, SSLContext context)
    Constructor for the SocketAcceptor object.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    The main processing done by this object is done using a thread calling the run method.
    void
    This is used to configure the server socket for non-blocking mode.
    void
    This is used to cancel the operation if the reactor decides to reject it for some reason.
    void
    This is used to close the server socket channel so that the port that it is bound to is released.
    private void
    This method is used to configure the accepted channel.
    This is used to acquire the local socket address that this is listening to.
    This is the SelectableChannel which is used to determine if the operation should be executed.
    This is used to acquire the trace object that is associated with the operation.
    private void
    This is used to throttle the acceptor when there is an error such as exhaustion of file descriptors.
    private void
    process(SocketChannel channel, Trace trace)
    This method is used to dispatch the socket for processing.
    private void
    process(SocketChannel channel, Trace trace, SSLEngine engine)
    This method is used to dispatch the socket for processing.
    void
    run()
    This is used to accept a new TCP connections.

    Methods inherited from class java.lang.Object

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

    • listener

      private final ServerSocketChannel listener
      This is the server socket channel used to accept connections.
    • processor

      private final SocketProcessor processor
      The handler that manages the incoming TCP connections.
    • socket

      private final ServerSocket socket
      This is the server socket to bind the socket address to.
    • context

      private final SSLContext context
      If provided the SSL context is used to create SSL engines.
    • analyzer

      private final TraceAnalyzer analyzer
      This is the tracing analyzer used to trace accepted sockets.
    • address

      private final SocketAddress address
      This is the local address to bind the listen socket to.
    • trace

      private final Trace trace
      This is used to collect trace events with the acceptor.
  • Constructor Details

    • SocketAcceptor

      public SocketAcceptor(SocketAddress address, SocketProcessor processor, TraceAnalyzer analyzer) throws IOException
      Constructor for the SocketAcceptor object. This accepts new TCP connections from the specified server socket. Each of the connections that is accepted is configured for performance for the application.
      Parameters:
      address - this is the address to accept connections from
      processor - this is used to initiate the HTTP processing
      analyzer - this is the tracing analyzer to be used
      Throws:
      IOException
    • SocketAcceptor

      public SocketAcceptor(SocketAddress address, SocketProcessor processor, TraceAnalyzer analyzer, SSLContext context) throws IOException
      Constructor for the SocketAcceptor object. This accepts new TCP connections from the specified server socket. Each of the connections that is accepted is configured for performance for the applications.
      Parameters:
      address - this is the address to accept connections from
      processor - this is used to initiate the HTTP processing
      analyzer - this is the tracing analyzer to be used
      context - this is the SSL context used for secure HTTPS
      Throws:
      IOException
  • Method Details

    • getAddress

      public SocketAddress getAddress()
      This is used to acquire the local socket address that this is listening to. This required in case the socket address that is specified is an emphemeral address, that is an address that is assigned dynamically when a port of 0 is specified.
      Returns:
      this returns the address for the listening address
    • getTrace

      public Trace getTrace()
      This is used to acquire the trace object that is associated with the operation. A trace object is used to collection details on what operations are being performed. For instance it may contain information relating to I/O events or errors.
      Specified by:
      getTrace in interface Operation
      Returns:
      this returns the trace associated with this operation
    • getChannel

      public SelectableChannel getChannel()
      This is the SelectableChannel which is used to determine if the operation should be executed. If the channel is ready for a given I/O event it can be run. For instance if the operation is used to perform some form of read operation it can be executed when ready to read data from the channel.
      Specified by:
      getChannel in interface Operation
      Returns:
      this returns the channel used to govern execution
    • bind

      public void bind() throws IOException
      This is used to configure the server socket for non-blocking mode. It will also bind the server socket to the socket port specified in the SocketAddress object. Once done the acceptor is ready to accept newly arriving connections.
      Parameters:
      address - this is the server socket address to bind to
      Throws:
      IOException
    • run

      public void run()
      This is used to accept a new TCP connections. When the socket is ready to accept a connection this method is invoked. It will then create a HTTP pipeline object using the accepted socket and if provided with an SSLContext it will also provide an SSLEngine which is handed to the processor to handle the HTTP requests.
      Specified by:
      run in interface Runnable
    • pause

      private void pause()
      This is used to throttle the acceptor when there is an error such as exhaustion of file descriptors. This will prevent the CPU from being hogged by the acceptor on such occasions. If the thread can not be put to sleep then this will freeze.
    • cancel

      public void cancel()
      This is used to cancel the operation if the reactor decides to reject it for some reason. Typically this method will never be invoked as this operation never times out. However, should the reactor cancel the operation this will close the socket.
      Specified by:
      cancel in interface Operation
    • accept

      private void accept() throws IOException
      The main processing done by this object is done using a thread calling the run method. Here the TCP connections are accepted from the ServerSocketChannel which creates the socket objects. Each socket is then encapsulated in to a pipeline and dispatched to the processor for processing.
      Throws:
      IOException - if there is a problem accepting the socket
    • configure

      private void configure(SocketChannel channel) throws IOException
      This method is used to configure the accepted channel. This will disable Nagles algorithm to improve the performance of the channel, also this will ensure the accepted channel disables blocking to ensure that it works within the processor object.
      Parameters:
      channel - this is the channel that is to be configured
      Throws:
      IOException
    • process

      private void process(SocketChannel channel, Trace trace) throws IOException
      This method is used to dispatch the socket for processing. The socket will be configured and connected to the client, this will hand processing to the Server which will create the pipeline instance used to wrap the socket object.
      Parameters:
      channel - this is the connected socket to be processed
      trace - this is the trace to associate with the socket
      Throws:
      IOException
    • process

      private void process(SocketChannel channel, Trace trace, SSLEngine engine) throws IOException
      This method is used to dispatch the socket for processing. The socket will be configured and connected to the client, this will hand processing to the Server which will create the pipeline instance used to wrap the socket object.
      Parameters:
      channel - this is the connected socket to be processed
      trace - this is the trace to associate with the socket
      engine - this is the SSL engine used for secure HTTPS
      Throws:
      IOException
    • close

      public void close() throws IOException
      This is used to close the server socket channel so that the port that it is bound to is released. This allows the acceptor to close off the interface to the server. Ensuring the socket is closed allows it to be recreated at a later point.
      Throws:
      IOException - thrown if the socket can not be closed