Class NioSocketConnector
- java.lang.Object
-
- org.apache.mina.core.service.AbstractIoService
-
- org.apache.mina.core.service.AbstractIoConnector
-
- org.apache.mina.core.polling.AbstractPollingIoConnector<NioSession,SocketChannel>
-
- org.apache.mina.transport.socket.nio.NioSocketConnector
-
- All Implemented Interfaces:
IoConnector
,IoService
,SocketConnector
public final class NioSocketConnector extends AbstractPollingIoConnector<NioSession,SocketChannel> implements SocketConnector
IoConnector
for socket transport (TCP/IP).- Author:
- Apache MINA Project
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.mina.core.polling.AbstractPollingIoConnector
AbstractPollingIoConnector.ConnectionRequest
-
Nested classes/interfaces inherited from class org.apache.mina.core.service.AbstractIoService
AbstractIoService.ServiceOperationFuture
-
-
Field Summary
-
Fields inherited from class org.apache.mina.core.service.AbstractIoService
disposalLock, LOGGER, sessionConfig
-
-
Constructor Summary
Constructors Constructor Description NioSocketConnector()
Constructor forNioSocketConnector
with default configuration (multiple thread model).NioSocketConnector(int processorCount)
Constructor forNioSocketConnector
with default configuration, and given number ofNioProcessor
for multithreading I/O operationsNioSocketConnector(Class<? extends IoProcessor<NioSession>> processorClass)
Constructor forNioSocketConnector
with default configuration with default configuration which will use a built-in thread pool executor to manage the default number of processor instances.NioSocketConnector(Class<? extends IoProcessor<NioSession>> processorClass, int processorCount)
Constructor forNioSocketConnector
with default configuration which will use a built-in thread pool executor to manage the given number of processor instances.NioSocketConnector(Executor executor, IoProcessor<NioSession> processor)
Constructor forNioSocketConnector
with a givenExecutor
for handling connection events and a givenIoProcessor
for handling I/O events, useful for sharing the same processor and executor over multipleIoService
of the same type.NioSocketConnector(IoProcessor<NioSession> processor)
Constructor forNioSocketConnector
with default configuration but a specificIoProcessor
, useful for sharing the same processor over multipleIoService
of the same type.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Iterator<SocketChannel>
allHandles()
Iterator
for all the client sockets polled for connection.protected void
close(SocketChannel handle)
Close a client socket.protected boolean
connect(SocketChannel handle, SocketAddress remoteAddress)
Connect a newly created client socket handle to a remoteSocketAddress
.protected void
destroy()
Destroy the polling system, will be called when thisIoConnector
implementation will be disposed.protected boolean
finishConnect(SocketChannel handle)
Finish the connection process of a client socket after it was marked as ready to process by theAbstractPollingIoConnector.select(int)
call.protected AbstractPollingIoConnector.ConnectionRequest
getConnectionRequest(SocketChannel handle)
get theAbstractPollingIoConnector.ConnectionRequest
for a given client socket handleInetSocketAddress
getDefaultRemoteAddress()
SocketSessionConfig
getSessionConfig()
TransportMetadata
getTransportMetadata()
protected void
init()
Initialize the polling system, will be called at construction time.protected SocketChannel
newHandle(SocketAddress localAddress)
Create a new client socket handle from a localSocketAddress
protected NioSession
newSession(IoProcessor<NioSession> processor, SocketChannel handle)
Create a newIoSession
from a connected socket client handle.protected void
register(SocketChannel handle, AbstractPollingIoConnector.ConnectionRequest request)
Register a new client socket for connection, add it to connection pollingprotected int
select(int timeout)
Check for connected sockets, interrupt when at least a connection is processed (connected or failed to connect).protected Iterator<SocketChannel>
selectedHandles()
Iterator
for the set of client sockets found connected or failed to connect during the lastAbstractPollingIoConnector.select(int)
call.void
setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress)
Sets the default remote InetSocketAddress to connect to when no argument is specified inIoConnector.connect()
method.protected void
wakeup()
Interrupt theAbstractPollingIoConnector.select(int)
method.-
Methods inherited from class org.apache.mina.core.polling.AbstractPollingIoConnector
connect0, dispose0
-
Methods inherited from class org.apache.mina.core.service.AbstractIoConnector
connect, connect, connect, connect, connect, connect, finishSessionInitialization0, getConnectTimeout, getConnectTimeoutCheckInterval, getConnectTimeoutMillis, getDefaultLocalAddress, setConnectTimeout, setConnectTimeoutCheckInterval, setConnectTimeoutMillis, setDefaultLocalAddress, setDefaultRemoteAddress, toString
-
Methods inherited from class org.apache.mina.core.service.AbstractIoService
addListener, broadcast, dispose, dispose, executeWorker, executeWorker, getActivationTime, getFilterChain, getFilterChainBuilder, getHandler, getListeners, getManagedSessionCount, getManagedSessions, getScheduledWriteBytes, getScheduledWriteMessages, getSessionDataStructureFactory, getStatistics, initSession, isActive, isDisposed, isDisposing, removeListener, setFilterChainBuilder, setHandler, setSessionDataStructureFactory
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.mina.core.service.IoConnector
connect, connect, connect, connect, connect, connect, getConnectTimeout, getConnectTimeoutMillis, getDefaultLocalAddress, setConnectTimeout, setConnectTimeoutMillis, setDefaultLocalAddress, setDefaultRemoteAddress
-
Methods inherited from interface org.apache.mina.core.service.IoService
addListener, broadcast, dispose, dispose, getActivationTime, getFilterChain, getFilterChainBuilder, getHandler, getManagedSessionCount, getManagedSessions, getScheduledWriteBytes, getScheduledWriteMessages, getSessionDataStructureFactory, getStatistics, isActive, isDisposed, isDisposing, removeListener, setFilterChainBuilder, setHandler, setSessionDataStructureFactory
-
-
-
-
Constructor Detail
-
NioSocketConnector
public NioSocketConnector()
Constructor forNioSocketConnector
with default configuration (multiple thread model).
-
NioSocketConnector
public NioSocketConnector(int processorCount)
Constructor forNioSocketConnector
with default configuration, and given number ofNioProcessor
for multithreading I/O operations- Parameters:
processorCount
- the number of processor to create and place in aSimpleIoProcessorPool
-
NioSocketConnector
public NioSocketConnector(IoProcessor<NioSession> processor)
Constructor forNioSocketConnector
with default configuration but a specificIoProcessor
, useful for sharing the same processor over multipleIoService
of the same type.- Parameters:
processor
- the processor to use for managing I/O events
-
NioSocketConnector
public NioSocketConnector(Executor executor, IoProcessor<NioSession> processor)
Constructor forNioSocketConnector
with a givenExecutor
for handling connection events and a givenIoProcessor
for handling I/O events, useful for sharing the same processor and executor over multipleIoService
of the same type.- Parameters:
executor
- the executor for connectionprocessor
- the processor for I/O operations
-
NioSocketConnector
public NioSocketConnector(Class<? extends IoProcessor<NioSession>> processorClass, int processorCount)
Constructor forNioSocketConnector
with default configuration which will use a built-in thread pool executor to manage the given number of processor instances. The processor class must have a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a no-arg constructor.- Parameters:
processorClass
- the processor class.processorCount
- the number of processors to instantiate.- Since:
- 2.0.0-M4
- See Also:
SimpleIoProcessorPool(Class, Executor, int, java.nio.channels.spi.SelectorProvider)
-
NioSocketConnector
public NioSocketConnector(Class<? extends IoProcessor<NioSession>> processorClass)
Constructor forNioSocketConnector
with default configuration with default configuration which will use a built-in thread pool executor to manage the default number of processor instances. The processor class must have a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a no-arg constructor. The default number of instances is equal to the number of processor cores in the system, plus one.- Parameters:
processorClass
- the processor class.- Since:
- 2.0.0-M4
- See Also:
SimpleIoProcessorPool(Class, Executor, int, java.nio.channels.spi.SelectorProvider)
-
-
Method Detail
-
init
protected void init() throws Exception
Initialize the polling system, will be called at construction time.- Specified by:
init
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Throws:
Exception
- any exception thrown by the underlying system calls
-
destroy
protected void destroy() throws Exception
Destroy the polling system, will be called when thisIoConnector
implementation will be disposed.- Specified by:
destroy
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Throws:
Exception
- any exception thrown by the underlying systems calls
-
getTransportMetadata
public TransportMetadata getTransportMetadata()
- Specified by:
getTransportMetadata
in interfaceIoService
- Returns:
- the
TransportMetadata
that this service runs on.
-
getSessionConfig
public SocketSessionConfig getSessionConfig()
- Specified by:
getSessionConfig
in interfaceIoService
- Specified by:
getSessionConfig
in interfaceSocketConnector
- Returns:
- the default configuration of the new SocketSessions created by this connect service.
-
getDefaultRemoteAddress
public InetSocketAddress getDefaultRemoteAddress()
- Specified by:
getDefaultRemoteAddress
in interfaceIoConnector
- Specified by:
getDefaultRemoteAddress
in interfaceSocketConnector
- Overrides:
getDefaultRemoteAddress
in classAbstractIoConnector
- Returns:
- the default remote address to connect to when no argument
is specified in
IoConnector.connect()
method.
-
setDefaultRemoteAddress
public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress)
Sets the default remote InetSocketAddress to connect to when no argument is specified inIoConnector.connect()
method. This method overrides theIoConnector.setDefaultRemoteAddress(java.net.SocketAddress)
method.- Specified by:
setDefaultRemoteAddress
in interfaceSocketConnector
- Parameters:
defaultRemoteAddress
- The remote address to set
-
allHandles
protected Iterator<SocketChannel> allHandles()
Iterator
for all the client sockets polled for connection.- Specified by:
allHandles
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Returns:
- the list of client sockets currently polled for connection
-
connect
protected boolean connect(SocketChannel handle, SocketAddress remoteAddress) throws Exception
Connect a newly created client socket handle to a remoteSocketAddress
. This operation is non-blocking, so at end of the call the socket can be still in connection process.- Specified by:
connect
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Parameters:
handle
- the client socket handleremoteAddress
- the remote address where to connect- Returns:
true
if a connection was established,false
if this client socket is in non-blocking mode and the connection operation is in progress- Throws:
Exception
- If the connect failed
-
getConnectionRequest
protected AbstractPollingIoConnector.ConnectionRequest getConnectionRequest(SocketChannel handle)
get theAbstractPollingIoConnector.ConnectionRequest
for a given client socket handle- Specified by:
getConnectionRequest
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Parameters:
handle
- the socket client handle- Returns:
- the connection request if the socket is connecting otherwise
null
-
close
protected void close(SocketChannel handle) throws Exception
Close a client socket.- Specified by:
close
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Parameters:
handle
- the client socket- Throws:
Exception
- any exception thrown by the underlying systems calls
-
finishConnect
protected boolean finishConnect(SocketChannel handle) throws Exception
Finish the connection process of a client socket after it was marked as ready to process by theAbstractPollingIoConnector.select(int)
call. The socket will be connected or reported as connection failed.- Specified by:
finishConnect
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Parameters:
handle
- the client socket handle to finish to connect- Returns:
- true if the socket is connected
- Throws:
Exception
- any exception thrown by the underlying systems calls
-
newHandle
protected SocketChannel newHandle(SocketAddress localAddress) throws Exception
Create a new client socket handle from a localSocketAddress
- Specified by:
newHandle
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Parameters:
localAddress
- the socket address for binding the new client socket- Returns:
- a new client socket handle
- Throws:
Exception
- any exception thrown by the underlying systems calls
-
newSession
protected NioSession newSession(IoProcessor<NioSession> processor, SocketChannel handle)
Create a newIoSession
from a connected socket client handle. Will assign the createdIoSession
to the givenIoProcessor
for managing future I/O events.- Specified by:
newSession
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Parameters:
processor
- the processor in charge of this sessionhandle
- the newly connected client socket handle- Returns:
- a new
IoSession
-
register
protected void register(SocketChannel handle, AbstractPollingIoConnector.ConnectionRequest request) throws Exception
Register a new client socket for connection, add it to connection polling- Specified by:
register
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Parameters:
handle
- client socket handlerequest
- the associatedAbstractPollingIoConnector.ConnectionRequest
- Throws:
Exception
- any exception thrown by the underlying systems calls
-
select
protected int select(int timeout) throws Exception
Check for connected sockets, interrupt when at least a connection is processed (connected or failed to connect). All the client socket descriptors processed need to be returned byAbstractPollingIoConnector.selectedHandles()
- Specified by:
select
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Parameters:
timeout
- The timeout for the select() method- Returns:
- The number of socket having received some data
- Throws:
Exception
- any exception thrown by the underlying systems calls
-
selectedHandles
protected Iterator<SocketChannel> selectedHandles()
Iterator
for the set of client sockets found connected or failed to connect during the lastAbstractPollingIoConnector.select(int)
call.- Specified by:
selectedHandles
in classAbstractPollingIoConnector<NioSession,SocketChannel>
- Returns:
- the list of client socket handles to process
-
wakeup
protected void wakeup()
Interrupt theAbstractPollingIoConnector.select(int)
method. Used when the poll set need to be modified.- Specified by:
wakeup
in classAbstractPollingIoConnector<NioSession,SocketChannel>
-
-