Class AbstractPollingIoAcceptor<S extends AbstractIoSession, H>
java.lang.Object
org.apache.mina.core.service.AbstractIoService
org.apache.mina.core.service.AbstractIoAcceptor
org.apache.mina.core.polling.AbstractPollingIoAcceptor<S,H>
- Type Parameters:
S- The type of IoSessionH- The type of IoHandler
- All Implemented Interfaces:
IoAcceptor, IoService
- Direct Known Subclasses:
AprSocketAcceptor, NioSocketAcceptor
public abstract class AbstractPollingIoAcceptor<S extends AbstractIoSession, H>
extends AbstractIoAcceptor
A base class for implementing 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,
accepting and disposing the server sockets. An
Executor will be used
for running client accepting and an AbstractPollingIoProcessor will
be used for processing client I/O operations like reading, writing and
closing.
All the low level methods for binding, accepting, closing need to be provided
by the subclassing implementation.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classThis class is called by the startupAcceptor() method and is placed into a NamePreservingRunnable class.Nested classes/interfaces inherited from class AbstractIoAcceptor
AbstractIoAcceptor.AcceptorOperationFutureNested classes/interfaces inherited from class AbstractIoService
AbstractIoService.ServiceOperationFuture -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate AtomicReference<AbstractPollingIoAcceptor<S, H>.Acceptor> The thread responsible of accepting incoming requestsprotected intDefine the number of socket that can wait to be accepted.private final Map<SocketAddress, H> private final Queue<AbstractIoAcceptor.AcceptorOperationFuture> private final booleanprivate final AbstractIoService.ServiceOperationFutureprivate final SemaphoreA lock used to protect the selector to be waked up before it's createdprivate final IoProcessor<S> private final Queue<AbstractIoAcceptor.AcceptorOperationFuture> protected booleanprivate booleanA flag set when the acceptor has been created and initializedFields inherited from class AbstractIoAcceptor
bindLockFields inherited from class AbstractIoService
disposalLock, LOGGER, sessionConfig -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass) Constructor forAbstractPollingIoAcceptor.protectedAbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass, int processorCount) Constructor forAbstractPollingIoAcceptor.protectedAbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass, int processorCount, SelectorProvider selectorProvider) Constructor forAbstractPollingIoAcceptor.protectedAbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor) Constructor forAbstractPollingIoAcceptor.privateAbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor, boolean createdProcessor, SelectorProvider selectorProvider) Constructor forAbstractPollingIoAcceptor.protectedAbstractPollingIoAcceptor(IoSessionConfig sessionConfig, IoProcessor<S> processor) Constructor forAbstractPollingIoAcceptor. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract Saccept(IoProcessor<S> processor, H handle) Accept a client connection for a server socket and return a newIoSessionassociated with the givenIoProcessorprotected final Set<SocketAddress> bindInternal(List<? extends SocketAddress> localAddresses) Starts the acceptor, and register the given addressesprotected voidInvoked when a bind request has been registered for processing.protected abstract voidClose a server socket.protected abstract voiddestroy()Destroy the polling system, will be called when thisIoAcceptorimplementation will be disposed.protected voiddispose0()Implement this method to release any acquired resources.intprotected voidhandleUnbound(Collection<AbstractIoAcceptor.AcceptorOperationFuture> unboundFutures) Processes the futures for executed unbindings, marking all futures as done.protected booleanTells whether there are pending unbindings.protected abstract voidinit()Initialize the polling system, will be called at construction time.protected abstract voidinit(SelectorProvider selectorProvider) Initialize the polling system, will be called at construction time.booleanprotected abstract SocketAddresslocalAddress(H handle) Get the local address associated with a given server socketfinal IoSessionnewSession(SocketAddress remoteAddress, SocketAddress localAddress) (Optional) Returns anIoSessionthat is bound to the specifiedlocalAddressand the specifiedremoteAddresswhich reuses the local address that is already bound by this service.protected abstract Hopen(SocketAddress localAddress) Open a server socket for a given local address.protected voidprocessHandles(Iterator<H> handles) Handles new incoming connections by accepting the connections and creating new sessions for them.protected abstract intselect()Check for acceptable connections, interrupt when at least a server is ready for accepting.voidsetBacklog(int backlog) Sets the Backlog parametervoidsetReuseAddress(boolean reuseAddress) Set the Reuse Address flagprivate voidThis method is called by the doBind() and doUnbind() methods.protected final voidunbind0(List<? extends SocketAddress> localAddresses) Implement this method to perform the actual unbind operation.protected abstract voidwakeup()Interrupt theselect()method.Methods inherited from class AbstractIoAcceptor
bind, bind, bind, bind, bind, getDefaultLocalAddress, getDefaultLocalAddresses, getLocalAddress, getLocalAddresses, isCloseOnDeactivation, setCloseOnDeactivation, setDefaultLocalAddress, setDefaultLocalAddresses, setDefaultLocalAddresses, setDefaultLocalAddresses, toString, unbind, unbind, unbind, unbindMethods inherited from class AbstractIoService
addListener, broadcast, dispose, dispose, executeWorker, executeWorker, finishSessionInitialization0, 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, getSessionDataStructureFactory, getStatistics, getTransportMetadata, isActive, isDisposed, isDisposing, removeListener, setFilterChainBuilder, setHandler, setSessionDataStructureFactory
-
Field Details
-
lock
A lock used to protect the selector to be waked up before it's created -
processor
-
createdProcessor
private final boolean createdProcessor -
registerQueue
-
cancelQueue
-
boundHandles
-
disposalFuture
-
selectable
private volatile boolean selectableA flag set when the acceptor has been created and initialized -
acceptorRef
private AtomicReference<AbstractPollingIoAcceptor<S extends AbstractIoSession, H>.Acceptor> acceptorRefThe thread responsible of accepting incoming requests -
reuseAddress
protected boolean reuseAddress -
backlog
protected int backlogDefine the number of socket that can wait to be accepted. Default to 50 (as in the SocketServer default).
-
-
Constructor Details
-
AbstractPollingIoAcceptor
protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass) Constructor forAbstractPollingIoAcceptor. 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:
-
AbstractPollingIoAcceptor
protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass, int processorCount) Constructor forAbstractPollingIoAcceptor. 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:
-
AbstractPollingIoAcceptor
protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass, int processorCount, SelectorProvider selectorProvider) Constructor forAbstractPollingIoAcceptor. 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 poolselectorProvider- The SelectorProvider to use- See Also:
-
AbstractPollingIoAcceptor
Constructor forAbstractPollingIoAcceptor. 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:
-
AbstractPollingIoAcceptor
protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor) Constructor forAbstractPollingIoAcceptor. You need to provide a default session configuration and anExecutorfor handling I/O events. If a 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:
-
AbstractPollingIoAcceptor
private AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor, boolean createdProcessor, SelectorProvider selectorProvider) Constructor forAbstractPollingIoAcceptor. You need to provide a default session configuration and anExecutorfor handling I/O events. If a 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
-
init
Initialize the polling system, will be called at construction time.- Parameters:
selectorProvider- The Selector Provider that will be used by this polling acceptor- Throws:
Exception- any exception thrown by the underlying system calls
-
destroy
Destroy the polling system, will be called when thisIoAcceptorimplementation will be disposed.- Throws:
Exception- any exception thrown by the underlying systems calls
-
select
Check for acceptable connections, interrupt when at least a server is ready for accepting. All the ready server socket descriptors need to be returned byselectedHandles()- Returns:
- The number of sockets having got incoming client
- Throws:
Exception- any exception thrown by the underlying systems calls
-
wakeup
protected abstract void wakeup()Interrupt theselect()method. Used when the poll set need to be modified. -
selectedHandles
-
open
Open a server socket for a given local address.- Parameters:
localAddress- the associated local address- Returns:
- the opened server socket
- Throws:
Exception- any exception thrown by the underlying systems calls
-
localAddress
Get the local address associated with a given server socket- Parameters:
handle- the server socket- Returns:
- the local
SocketAddressassociated with this handle - Throws:
Exception- any exception thrown by the underlying systems calls
-
accept
Accept a client connection for a server socket and return a newIoSessionassociated with the givenIoProcessor- Parameters:
processor- theIoProcessorto associate with theIoSessionhandle- the server handle- Returns:
- the created
IoSession - Throws:
Exception- any exception thrown by the underlying systems calls
-
close
-
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
-
bindRequestAdded
protected void bindRequestAdded()Invoked when a bind request has been registered for processing. The default implementation does nothing. -
bindInternal
protected final Set<SocketAddress> bindInternal(List<? extends SocketAddress> localAddresses) throws Exception Starts the acceptor, and register the given addresses- Specified by:
bindInternalin classAbstractIoAcceptor- Parameters:
localAddresses- The address to bind to- Returns:
- the
Setof the local addresses which is bound actually - Throws:
Exception- If the bind failed
-
startupAcceptor
This method is called by the doBind() and doUnbind() methods. If the acceptor is null, the acceptor object will be created and kicked off by the executor. If the acceptor object is null, probably already created and this class is now working, then nothing will happen and the method will just return.- Throws:
InterruptedException
-
unbind0
Implement this method to perform the actual unbind operation.- Specified by:
unbind0in classAbstractIoAcceptor- Parameters:
localAddresses- The address to unbind from- Throws:
Exception- If the unbind failed
-
processHandles
-
hasUnbindings
protected boolean hasUnbindings()Tells whether there are pending unbindings.- Returns:
trueif there are any unbindings pending;falseotherwise
-
handleUnbound
protected void handleUnbound(Collection<AbstractIoAcceptor.AcceptorOperationFuture> unboundFutures) throws Exception Processes the futures for executed unbindings, marking all futures as done.- Parameters:
unboundFutures- describing the unbindings- Throws:
Exception- on errors
-
newSession
(Optional) Returns anIoSessionthat is bound to the specifiedlocalAddressand the specifiedremoteAddresswhich reuses the local address that is already bound by this service.This operation is optional. Please throw
UnsupportedOperationExceptionif the transport type doesn't support this operation. This operation is usually implemented for connectionless transport types.- Parameters:
remoteAddress- The remote address bound to the servicelocalAddress- The local address the session will be bound to- Returns:
- The session bound to the the given localAddress and remote address
-
getBacklog
public int getBacklog()- Returns:
- the backLog
-
setBacklog
public void setBacklog(int backlog) Sets the Backlog parameter- Parameters:
backlog- the backlog variable
-
isReuseAddress
public boolean isReuseAddress()- Returns:
- the flag that sets the reuseAddress information
-
setReuseAddress
public void setReuseAddress(boolean reuseAddress) Set the Reuse Address flag- Parameters:
reuseAddress- The flag to set
-
getSessionConfig
- Returns:
- the default configuration of the new
IoSessions created by this service.
-