Package org.apache.mina.core.polling
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:
H- The type of IoHandlerS- The type of IoSession
- All Implemented Interfaces:
IoAcceptor,IoService
- Direct Known Subclasses:
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. AnExecutorwill be used for running client accepting and anAbstractPollingIoProcessorwill 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.- Author:
- Apache MINA Project
- See Also:
for a example of implementation
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.mina.core.service.AbstractIoAcceptor
AbstractIoAcceptor.AcceptorOperationFuture
-
Nested classes/interfaces inherited from class org.apache.mina.core.service.AbstractIoService
AbstractIoService.ServiceOperationFuture
-
-
Field Summary
Fields Modifier and Type Field Description protected intbacklogDefine the number of socket that can wait to be accepted.protected booleanreuseAddress-
Fields inherited from class org.apache.mina.core.service.AbstractIoAcceptor
bindLock
-
Fields inherited from class org.apache.mina.core.service.AbstractIoService
disposalLock, LOGGER, sessionConfig
-
-
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractPollingIoAcceptor(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.protectedAbstractPollingIoAcceptor(IoSessionConfig sessionConfig, IoProcessor<S> processor)Constructor forAbstractPollingIoAcceptor.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract Saccept(IoProcessor<S> processor, H handle)Accept a client connection for a server socket and return a newIoSessionassociated with the givenIoProcessorprotected Set<SocketAddress>bindInternal(List<? extends SocketAddress> localAddresses)Starts the acceptor, and register the given addressesprotected abstract voidclose(H handle)Close 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.intgetBacklog()SocketSessionConfiggetSessionConfig()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.booleanisReuseAddress()protected abstract SocketAddresslocalAddress(H handle)Get the local address associated with a given server socketIoSessionnewSession(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 abstract intselect()Check for acceptable connections, interrupt when at least a server is ready for accepting.protected abstract Iterator<H>selectedHandles()voidsetBacklog(int backlog)Sets the Backlog parametervoidsetReuseAddress(boolean reuseAddress)Set the Reuse Address flagprotected voidunbind0(List<? extends SocketAddress> localAddresses)Implement this method to perform the actual unbind operation.protected abstract voidwakeup()Interrupt theselect()method.-
Methods inherited from class org.apache.mina.core.service.AbstractIoAcceptor
bind, bind, bind, bind, bind, getDefaultLocalAddress, getDefaultLocalAddresses, getLocalAddress, getLocalAddresses, isCloseOnDeactivation, setCloseOnDeactivation, setDefaultLocalAddress, setDefaultLocalAddresses, setDefaultLocalAddresses, setDefaultLocalAddresses, toString, unbind, unbind, unbind, unbind
-
Methods inherited from class org.apache.mina.core.service.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, 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.IoService
addListener, broadcast, dispose, dispose, getActivationTime, getFilterChain, getFilterChainBuilder, getHandler, getManagedSessionCount, getManagedSessions, getScheduledWriteBytes, getScheduledWriteMessages, getSessionDataStructureFactory, getStatistics, getTransportMetadata, isActive, isDisposed, isDisposing, removeListener, setFilterChainBuilder, setHandler, setSessionDataStructureFactory
-
-
-
-
Constructor Detail
-
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:
SimpleIoProcessorPool
-
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:
SimpleIoProcessorPool
-
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:
SimpleIoProcessorPool
-
AbstractPollingIoAcceptor
protected AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, IoProcessor<S> processor)
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:
AbstractIoService
-
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:
AbstractIoService(IoSessionConfig, Executor)
-
-
Method Detail
-
init
protected abstract void init() throws ExceptionInitialize the polling system, will be called at construction time.- Throws:
Exception- any exception thrown by the underlying system calls
-
init
protected abstract void init(SelectorProvider selectorProvider) throws Exception
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
protected abstract void destroy() throws ExceptionDestroy the polling system, will be called when thisIoAcceptorimplementation will be disposed.- Throws:
Exception- any exception thrown by the underlying systems calls
-
select
protected abstract int select() throws ExceptionCheck 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
protected abstract Iterator<H> selectedHandles()
Iteratorfor the set of server sockets found with acceptable incoming connections during the lastselect()call.- Returns:
- the list of server handles ready
-
open
protected abstract H open(SocketAddress localAddress) throws Exception
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
protected abstract SocketAddress localAddress(H handle) throws Exception
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
protected abstract S accept(IoProcessor<S> processor, H handle) throws Exception
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
protected abstract void close(H handle) throws Exception
Close a server socket.- Parameters:
handle- the server socket- Throws:
Exception- any exception thrown by the underlying systems calls
-
dispose0
protected void dispose0() throws ExceptionImplement 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
-
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
-
unbind0
protected final void unbind0(List<? extends SocketAddress> localAddresses) throws Exception
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
-
newSession
public final IoSession newSession(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.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
public SocketSessionConfig getSessionConfig()
- Returns:
- the default configuration of the new
IoSessions created by this service.
-
-