Class AbstractPollingIoConnector<S extends AbstractIoSession, H>
java.lang.Object
org.apache.mina.core.service.AbstractIoService
org.apache.mina.core.service.AbstractIoConnector
org.apache.mina.core.polling.AbstractPollingIoConnector<S,H>
- Type Parameters:
S- The type of IoSessionH- The type of IoHandler
- All Implemented Interfaces:
IoConnector, IoService
- Direct Known Subclasses:
AprSocketConnector, NioDatagramConnector, NioSocketConnector
public abstract class AbstractPollingIoConnector<S extends AbstractIoSession, H>
extends AbstractIoConnector
A base class for implementing client transport using a polling strategy. The
underlying sockets will be checked in an active loop and woke up when an
socket needed to be processed. This class handle the logic behind binding,
connecting and disposing the client sockets. A
Executor will be used
for running client connection, and an AbstractPollingIoProcessor will
be used for processing connected client I/O operations like reading, writing
and closing.
All the low level methods for binding, connecting, closing need to be
provided by the subclassing implementation.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classA ConnectionRequest's Ioutureprivate classNested classes/interfaces inherited from class AbstractIoService
AbstractIoService.ServiceOperationFuture -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Queue<AbstractPollingIoConnector<S, H>.ConnectionRequest> private final AtomicReference<AbstractPollingIoConnector<S, H>.Connector> The connector threadprivate final Queue<AbstractPollingIoConnector<S, H>.ConnectionRequest> private final booleanprivate final AbstractIoService.ServiceOperationFutureprivate final IoProcessor<S> private booleanFields inherited from class AbstractIoService
disposalLock, LOGGER, sessionConfig -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractPollingIoConnector(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass) Constructor forAbstractPollingIoConnector.protectedAbstractPollingIoConnector(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass, int processorCount) Constructor forAbstractPollingIoConnector.protectedAbstractPollingIoConnector(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor) Constructor forAbstractPollingIoConnector.privateAbstractPollingIoConnector(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor, boolean createdProcessor) Constructor forAbstractPollingIoAcceptor.protectedAbstractPollingIoConnector(IoSessionConfig sessionConfig, IoProcessor<S> processor) Constructor forAbstractPollingIoConnector. -
Method Summary
Modifier and TypeMethodDescriptionIteratorfor all the client sockets polled for connection.protected abstract voidClose a client socket.protected abstract booleanconnect(H handle, SocketAddress remoteAddress) Connect a newly created client socket handle to a remoteSocketAddress.protected final ConnectFutureconnect0(SocketAddress remoteAddress, SocketAddress localAddress, IoSessionInitializer<? extends ConnectFuture> sessionInitializer) Implement this method to perform the actual connect operation.protected abstract voiddestroy()Destroy the polling system, will be called when thisIoConnectorimplementation will be disposed.protected final voiddispose0()Implement this method to release any acquired resources.protected abstract booleanfinishConnect(H handle) Finish the connection process of a client socket after it was marked as ready to process by theselect(int)call.protected abstract AbstractPollingIoConnector<S,H>.ConnectionRequest getConnectionRequest(H handle) get theAbstractPollingIoConnector<S extends AbstractIoSession, H>.ConnectionRequestfor a given client socket handleprotected abstract voidinit()Initialize the polling system, will be called at construction time.protected abstract HnewHandle(SocketAddress localAddress) Create a new client socket handle from a localSocketAddressprotected abstract SnewSession(IoProcessor<S> processor, H handle) Create a newIoSessionfrom a connected socket client handle.protected abstract voidregister(H handle, AbstractPollingIoConnector<S, H>.ConnectionRequest request) Register a new client socket for connection, add it to connection pollingprotected abstract intselect(int timeout) Check for connected sockets, interrupt when at least a connection is processed (connected or failed to connect).Iteratorfor the set of client sockets found connected or failed to connect during the lastselect(int)call.private voidprotected abstract voidwakeup()Interrupt theselect(int)method.Methods inherited from class AbstractIoConnector
connect, connect, connect, connect, connect, connect, finishSessionInitialization0, getConnectTimeout, getConnectTimeoutCheckInterval, getConnectTimeoutMillis, getDefaultLocalAddress, getDefaultRemoteAddress, setConnectTimeout, setConnectTimeoutCheckInterval, setConnectTimeoutMillis, setDefaultLocalAddress, setDefaultRemoteAddress, toStringMethods inherited from class 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, setSessionDataStructureFactoryMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface IoService
addListener, broadcast, dispose, dispose, getActivationTime, getFilterChain, getFilterChainBuilder, getHandler, getManagedSessionCount, getManagedSessions, getScheduledWriteBytes, getScheduledWriteMessages, getSessionConfig, getSessionDataStructureFactory, getStatistics, getTransportMetadata, isActive, isDisposed, isDisposing, removeListener, setFilterChainBuilder, setHandler, setSessionDataStructureFactory
-
Field Details
-
connectQueue
private final Queue<AbstractPollingIoConnector<S extends AbstractIoSession, H>.ConnectionRequest> connectQueue -
cancelQueue
private final Queue<AbstractPollingIoConnector<S extends AbstractIoSession, H>.ConnectionRequest> cancelQueue -
processor
-
createdProcessor
private final boolean createdProcessor -
disposalFuture
-
selectable
private volatile boolean selectable -
connectorRef
private final AtomicReference<AbstractPollingIoConnector<S extends AbstractIoSession, H>.Connector> connectorRefThe connector thread
-
-
Constructor Details
-
AbstractPollingIoConnector
protected AbstractPollingIoConnector(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass) Constructor forAbstractPollingIoConnector. You need to provide a default session configuration, a class ofIoProcessorwhich will be instantiated in aSimpleIoProcessorPoolfor better scaling in multiprocessor systems. The default pool size will be used.- Parameters:
sessionConfig- the default configuration for the managedIoSessionprocessorClass- aClassofIoProcessorfor the associatedIoSessiontype.- See Also:
-
AbstractPollingIoConnector
protected AbstractPollingIoConnector(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass, int processorCount) Constructor forAbstractPollingIoConnector. You need to provide a default session configuration, a class ofIoProcessorwhich will be instantiated in aSimpleIoProcessorPoolfor using multiple thread for better scaling in multiprocessor systems.- Parameters:
sessionConfig- the default configuration for the managedIoSessionprocessorClass- aClassofIoProcessorfor the associatedIoSessiontype.processorCount- the amount of processor to instantiate for the pool- See Also:
-
AbstractPollingIoConnector
Constructor forAbstractPollingIoConnector. You need to provide a default session configuration, a defaultExecutorwill be created usingExecutors.newCachedThreadPool().- Parameters:
sessionConfig- the default configuration for the managedIoSessionprocessor- theIoProcessorfor processing theIoSessionof this transport, triggering events to the boundIoHandlerand processing the chains ofIoFilter- See Also:
-
AbstractPollingIoConnector
protected AbstractPollingIoConnector(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor) Constructor forAbstractPollingIoConnector. You need to provide a default session configuration and anExecutorfor handling I/O events. If nullExecutoris provided, a default one will be created usingExecutors.newCachedThreadPool().- Parameters:
sessionConfig- the default configuration for the managedIoSessionexecutor- theExecutorused for handling asynchronous execution of I/O events. Can benull.processor- theIoProcessorfor processing theIoSessionof this transport, triggering events to the boundIoHandlerand processing the chains ofIoFilter- See Also:
-
AbstractPollingIoConnector
private AbstractPollingIoConnector(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor, boolean createdProcessor) Constructor forAbstractPollingIoAcceptor. You need to provide a default session configuration and anExecutorfor handling I/O events. If nullExecutoris provided, a default one will be created usingExecutors.newCachedThreadPool().- Parameters:
sessionConfig- the default configuration for the managedIoSessionexecutor- theExecutorused for handling asynchronous execution of I/O events. Can benull.processor- theIoProcessorfor processing theIoSessionof this transport, triggering events to the boundIoHandlerand processing the chains ofIoFiltercreatedProcessor- tagging the processor as automatically created, so it will be automatically disposed- See Also:
-
-
Method Details
-
init
-
destroy
Destroy the polling system, will be called when thisIoConnectorimplementation will be disposed.- Throws:
Exception- any exception thrown by the underlying systems calls
-
newHandle
Create a new client socket handle from a localSocketAddress- 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
-
connect
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.- Parameters:
handle- the client socket handleremoteAddress- the remote address where to connect- Returns:
trueif a connection was established,falseif this client socket is in non-blocking mode and the connection operation is in progress- Throws:
Exception- If the connect failed
-
finishConnect
Finish the connection process of a client socket after it was marked as ready to process by theselect(int)call. The socket will be connected or reported as connection failed.- 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
-
newSession
Create a newIoSessionfrom a connected socket client handle. Will assign the createdIoSessionto the givenIoProcessorfor managing future I/O events. -
close
-
wakeup
protected abstract void wakeup()Interrupt theselect(int)method. Used when the poll set need to be modified. -
select
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 byselectedHandles()- 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
Iteratorfor the set of client sockets found connected or failed to connect during the lastselect(int)call.- Returns:
- the list of client socket handles to process
-
allHandles
-
register
protected abstract void register(H handle, AbstractPollingIoConnector<S, H>.ConnectionRequest request) throws ExceptionRegister a new client socket for connection, add it to connection polling- Parameters:
handle- client socket handlerequest- the associatedAbstractPollingIoConnector<S extends AbstractIoSession, H>.ConnectionRequest- Throws:
Exception- any exception thrown by the underlying systems calls
-
getConnectionRequest
get theAbstractPollingIoConnector<S extends AbstractIoSession, H>.ConnectionRequestfor a given client socket handle- Parameters:
handle- the socket client handle- Returns:
- the connection request if the socket is connecting otherwise
null
-
dispose0
Implement this method to release any acquired resources. This method is invoked only once byAbstractIoService.dispose().- Specified by:
dispose0in classAbstractIoService- Throws:
Exception- If the dispose failed
-
connect0
protected final ConnectFuture connect0(SocketAddress remoteAddress, SocketAddress localAddress, IoSessionInitializer<? extends ConnectFuture> sessionInitializer) Implement this method to perform the actual connect operation.- Specified by:
connect0in classAbstractIoConnector- Parameters:
remoteAddress- The remote address to connect fromlocalAddress-nullif no local address is specifiedsessionInitializer- The IoSessionInitializer to use when the connection s successful- Returns:
- The ConnectFuture associated with this asynchronous operation
-
startupWorker
private void startupWorker()
-