Class SocketAcceptor
- java.lang.Object
-
- org.simpleframework.transport.connect.SocketAcceptor
-
- All Implemented Interfaces:
java.lang.Runnable
,Operation
class SocketAcceptor extends java.lang.Object implements Operation
TheSocketAcceptor
object is used to accept incoming TCP connections from a specified socket address. This is used by theConnection
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:
SocketConnection
-
-
Field Summary
Fields Modifier and Type Field Description private java.net.SocketAddress
address
This is the local address to bind the listen socket to.private TraceAnalyzer
analyzer
This is the tracing analyzer used to trace accepted sockets.private javax.net.ssl.SSLContext
context
If provided the SSL context is used to create SSL engines.private java.nio.channels.ServerSocketChannel
listener
This is the server socket channel used to accept connections.private SocketProcessor
processor
The handler that manages the incoming TCP connections.private java.net.ServerSocket
socket
This is the server socket to bind the socket address to.private Trace
trace
This is used to collect trace events with the acceptor.
-
Constructor Summary
Constructors Constructor Description SocketAcceptor(java.net.SocketAddress address, SocketProcessor processor, TraceAnalyzer analyzer)
Constructor for theSocketAcceptor
object.SocketAcceptor(java.net.SocketAddress address, SocketProcessor processor, TraceAnalyzer analyzer, javax.net.ssl.SSLContext context)
Constructor for theSocketAcceptor
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
accept()
The main processing done by this object is done using a thread calling therun
method.void
bind()
This is used to configure the server socket for non-blocking mode.void
cancel()
This is used to cancel the operation if the reactor decides to reject it for some reason.void
close()
This is used to close the server socket channel so that the port that it is bound to is released.private void
configure(java.nio.channels.SocketChannel channel)
This method is used to configure the accepted channel.java.net.SocketAddress
getAddress()
This is used to acquire the local socket address that this is listening to.java.nio.channels.SelectableChannel
getChannel()
This is theSelectableChannel
which is used to determine if the operation should be executed.Trace
getTrace()
This is used to acquire the trace object that is associated with the operation.private void
pause()
This is used to throttle the acceptor when there is an error such as exhaustion of file descriptors.private void
process(java.nio.channels.SocketChannel channel, Trace trace)
This method is used to dispatch the socket for processing.private void
process(java.nio.channels.SocketChannel channel, Trace trace, javax.net.ssl.SSLEngine engine)
This method is used to dispatch the socket for processing.void
run()
This is used to accept a new TCP connections.
-
-
-
Field Detail
-
listener
private final java.nio.channels.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 java.net.ServerSocket socket
This is the server socket to bind the socket address to.
-
context
private final javax.net.ssl.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 java.net.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 Detail
-
SocketAcceptor
public SocketAcceptor(java.net.SocketAddress address, SocketProcessor processor, TraceAnalyzer analyzer) throws java.io.IOException
Constructor for theSocketAcceptor
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 fromprocessor
- this is used to initiate the HTTP processinganalyzer
- this is the tracing analyzer to be used- Throws:
java.io.IOException
-
SocketAcceptor
public SocketAcceptor(java.net.SocketAddress address, SocketProcessor processor, TraceAnalyzer analyzer, javax.net.ssl.SSLContext context) throws java.io.IOException
Constructor for theSocketAcceptor
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 fromprocessor
- this is used to initiate the HTTP processinganalyzer
- this is the tracing analyzer to be usedcontext
- this is the SSL context used for secure HTTPS- Throws:
java.io.IOException
-
-
Method Detail
-
getAddress
public java.net.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.
-
getChannel
public java.nio.channels.SelectableChannel getChannel()
This is theSelectableChannel
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 interfaceOperation
- Returns:
- this returns the channel used to govern execution
-
bind
public void bind() throws java.io.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 theSocketAddress
object. Once done the acceptor is ready to accept newly arriving connections.- Parameters:
address
- this is the server socket address to bind to- Throws:
java.io.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 anSSLContext
it will also provide anSSLEngine
which is handed to the processor to handle the HTTP requests.- Specified by:
run
in interfacejava.lang.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.
-
accept
private void accept() throws java.io.IOException
The main processing done by this object is done using a thread calling therun
method. Here the TCP connections are accepted from theServerSocketChannel
which creates the socket objects. Each socket is then encapsulated in to a pipeline and dispatched to the processor for processing.- Throws:
java.io.IOException
- if there is a problem accepting the socket
-
configure
private void configure(java.nio.channels.SocketChannel channel) throws java.io.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:
java.io.IOException
-
process
private void process(java.nio.channels.SocketChannel channel, Trace trace) throws java.io.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 theServer
which will create the pipeline instance used to wrap the socket object.- Parameters:
channel
- this is the connected socket to be processedtrace
- this is the trace to associate with the socket- Throws:
java.io.IOException
-
process
private void process(java.nio.channels.SocketChannel channel, Trace trace, javax.net.ssl.SSLEngine engine) throws java.io.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 theServer
which will create the pipeline instance used to wrap the socket object.- Parameters:
channel
- this is the connected socket to be processedtrace
- this is the trace to associate with the socketengine
- this is the SSL engine used for secure HTTPS- Throws:
java.io.IOException
-
close
public void close() throws java.io.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:
java.io.IOException
- thrown if the socket can not be closed
-
-