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:
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. AnExecutor
will be used for running client accepting and anAbstractPollingIoProcessor
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.- 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 int
backlog
Define the number of socket that can wait to be accepted.protected boolean
reuseAddress
-
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 protected
AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass)
Constructor forAbstractPollingIoAcceptor
.protected
AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass, int processorCount)
Constructor forAbstractPollingIoAcceptor
.protected
AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Class<? extends IoProcessor<S>> processorClass, int processorCount, SelectorProvider selectorProvider)
Constructor forAbstractPollingIoAcceptor
.protected
AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, Executor executor, IoProcessor<S> processor)
Constructor forAbstractPollingIoAcceptor
.protected
AbstractPollingIoAcceptor(IoSessionConfig sessionConfig, IoProcessor<S> processor)
Constructor forAbstractPollingIoAcceptor
.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract S
accept(IoProcessor<S> processor, H handle)
Accept a client connection for a server socket and return a newIoSession
associated with the givenIoProcessor
protected Set<SocketAddress>
bindInternal(List<? extends SocketAddress> localAddresses)
Starts the acceptor, and register the given addressesprotected void
bindRequestAdded()
Invoked when a bind request has been registered for processing.protected abstract void
close(H handle)
Close a server socket.protected abstract void
destroy()
Destroy the polling system, will be called when thisIoAcceptor
implementation will be disposed.protected void
dispose0()
Implement this method to release any acquired resources.int
getBacklog()
SocketSessionConfig
getSessionConfig()
protected void
handleUnbound(Collection<AbstractIoAcceptor.AcceptorOperationFuture> unboundFutures)
Processes the futures for executed unbindings, marking all futures as done.protected boolean
hasUnbindings()
Tells whether there are pending unbindings.protected abstract void
init()
Initialize the polling system, will be called at construction time.protected abstract void
init(SelectorProvider selectorProvider)
Initialize the polling system, will be called at construction time.boolean
isReuseAddress()
protected abstract SocketAddress
localAddress(H handle)
Get the local address associated with a given server socketIoSession
newSession(SocketAddress remoteAddress, SocketAddress localAddress)
(Optional) Returns anIoSession
that is bound to the specifiedlocalAddress
and the specifiedremoteAddress
which reuses the local address that is already bound by this service.protected abstract H
open(SocketAddress localAddress)
Open a server socket for a given local address.protected void
processHandles(Iterator<H> handles)
Handles new incoming connections by accepting the connections and creating new sessions for them.protected abstract int
select()
Check for acceptable connections, interrupt when at least a server is ready for accepting.protected abstract Iterator<H>
selectedHandles()
void
setBacklog(int backlog)
Sets the Backlog parametervoid
setReuseAddress(boolean reuseAddress)
Set the Reuse Address flagprotected void
unbind0(List<? extends SocketAddress> localAddresses)
Implement this method to perform the actual unbind operation.protected abstract void
wakeup()
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 ofIoProcessor
which will be instantiated in aSimpleIoProcessorPool
for better scaling in multiprocessor systems. The default pool size will be used.- Parameters:
sessionConfig
- the default configuration for the managedIoSession
processorClass
- aClass
ofIoProcessor
for the associatedIoSession
type.- 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 ofIoProcessor
which will be instantiated in aSimpleIoProcessorPool
for using multiple thread for better scaling in multiprocessor systems.- Parameters:
sessionConfig
- the default configuration for the managedIoSession
processorClass
- aClass
ofIoProcessor
for the associatedIoSession
type.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 ofIoProcessor
which will be instantiated in aSimpleIoProcessorPool
for using multiple thread for better scaling in multiprocessor systems.- Parameters:
sessionConfig
- the default configuration for the managedIoSession
processorClass
- aClass
ofIoProcessor
for the associatedIoSession
type.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 defaultExecutor
will be created usingExecutors.newCachedThreadPool()
.- Parameters:
sessionConfig
- the default configuration for the managedIoSession
processor
- theIoProcessor
for processing theIoSession
of this transport, triggering events to the boundIoHandler
and 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 anExecutor
for handling I/O events. If a nullExecutor
is provided, a default one will be created usingExecutors.newCachedThreadPool()
.- Parameters:
sessionConfig
- the default configuration for the managedIoSession
executor
- theExecutor
used for handling asynchronous execution of I/O events. Can benull
.processor
- theIoProcessor
for processing theIoSession
of this transport, triggering events to the boundIoHandler
and processing the chains ofIoFilter
- See Also:
AbstractIoService(IoSessionConfig, Executor)
-
-
Method Detail
-
init
protected abstract void init() throws Exception
Initialize 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 Exception
Destroy the polling system, will be called when thisIoAcceptor
implementation will be disposed.- Throws:
Exception
- any exception thrown by the underlying systems calls
-
select
protected abstract int select() throws Exception
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
protected abstract Iterator<H> selectedHandles()
Iterator
for 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
SocketAddress
associated 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 newIoSession
associated with the givenIoProcessor
- Parameters:
processor
- theIoProcessor
to associate with theIoSession
handle
- 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 Exception
Implement this method to release any acquired resources. This method is invoked only once byAbstractIoService.dispose()
.- Specified by:
dispose0
in 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:
bindInternal
in classAbstractIoAcceptor
- Parameters:
localAddresses
- The address to bind to- Returns:
- the
Set
of 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:
unbind0
in classAbstractIoAcceptor
- Parameters:
localAddresses
- The address to unbind from- Throws:
Exception
- If the unbind failed
-
processHandles
protected void processHandles(Iterator<H> handles) throws Exception
Handles new incoming connections by accepting the connections and creating new sessions for them.- Parameters:
handles
- the connection handles to accept and create new sessions for- Throws:
Exception
- on errors
-
hasUnbindings
protected boolean hasUnbindings()
Tells whether there are pending unbindings.- Returns:
true
if there are any unbindings pending;false
otherwise
-
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
public final IoSession newSession(SocketAddress remoteAddress, SocketAddress localAddress)
(Optional) Returns anIoSession
that is bound to the specifiedlocalAddress
and the specifiedremoteAddress
which reuses the local address that is already bound by this service.This operation is optional. Please throw
UnsupportedOperationException
if 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
IoSession
s created by this service.
-
-