Class NioDatagramAcceptor
java.lang.Object
org.apache.mina.core.service.AbstractIoService
org.apache.mina.core.service.AbstractIoAcceptor
org.apache.mina.transport.socket.nio.NioDatagramAcceptor
- All Implemented Interfaces:
IoAcceptor, IoProcessor<NioSession>, IoService, DatagramAcceptor
public final class NioDatagramAcceptor
extends AbstractIoAcceptor
implements DatagramAcceptor, IoProcessor<NioSession>
IoAcceptor for datagram transport (UDP/IP).-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classThis private class is used to accept incoming connection from clients.Nested classes/interfaces inherited from class AbstractIoAcceptor
AbstractIoAcceptor.AcceptorOperationFutureNested classes/interfaces inherited from class AbstractIoService
AbstractIoService.ServiceOperationFuture -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate NioDatagramAcceptor.AcceptorThe thread responsible of accepting incoming requestsprivate final Map<SocketAddress, DatagramChannel> private final Queue<AbstractIoAcceptor.AcceptorOperationFuture> private static final IoSessionRecyclerA session recycler that is used to retrieve an existing session, unless it's too old.private final AbstractIoService.ServiceOperationFutureprivate final Queue<NioSession> private longprivate final SemaphoreA lock used to protect the selector to be waked up before it's createdprivate final Queue<AbstractIoAcceptor.AcceptorOperationFuture> A queue used to store the list of pending Bindsprivate static final longA timeout used for the select, as we need to get out to deal with idle sessionsprivate booleanprivate SelectorThe Selector used by this acceptorprivate IoSessionRecyclerFields inherited from class AbstractIoAcceptor
bindLockFields inherited from class AbstractIoService
disposalLock, LOGGER, sessionConfig -
Constructor Summary
ConstructorsModifierConstructorDescriptionCreates a new instance.NioDatagramAcceptor(Executor executor) Creates a new instance.privateNioDatagramAcceptor(IoSessionConfig sessionConfig, Executor executor) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(NioSession session) Adds the specifiedsessionto the I/O processor so that the I/O processor starts to perform any I/O operations related with thesession.protected final Set<SocketAddress> bindInternal(List<? extends SocketAddress> localAddresses) Starts the acceptor, and register the given addressesprotected voidclose(DatagramChannel handle) protected voiddestroy()protected voiddispose0()Implement this method to release any acquired resources.voidflush(NioSession session) Flushes the internal write request queue of the specifiedsession.private booleanflush(NioSession session, long currentTime) private voidflushSessions(long currentTime) Returns the default local address to bind when no argument is specified inIoAcceptor.bind()method.Returns the local address which is bound currently.final IoSessionRecyclerprotected voidinit()protected booleanisReadable(DatagramChannel handle) protected booleanisWritable(DatagramChannel handle) protected SocketAddresslocalAddress(DatagramChannel handle) final 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 NioSessionnewSession(IoProcessor<NioSession> processor, DatagramChannel handle, SocketAddress remoteAddress) private IoSessionnewSessionWithoutLock(SocketAddress remoteAddress, SocketAddress localAddress) private voidnotifyIdleSessions(long currentTime) protected DatagramChannelopen(SocketAddress localAddress) private voidprocessReadySessions(Set<SelectionKey> handles) private voidreadHandle(DatagramChannel handle) protected SocketAddressreceive(DatagramChannel handle, IoBuffer buffer) private intvoidremove(NioSession session) Removes and closes the specifiedsessionfrom the I/O processor so that the I/O processor closes the connection associated with thesessionand releases any other related resources.private booleanscheduleFlush(NioSession session) protected intselect()protected intselect(long timeout) protected Set<SelectionKey> protected intsend(NioSession session, IoBuffer buffer, SocketAddress remoteAddress) voidsetDefaultLocalAddress(InetSocketAddress localAddress) Sets the default local InetSocketAddress to bind when no argument is specified inIoAcceptor.bind()method.protected voidsetInterestedInWrite(NioSession session, boolean isInterested) final voidsetSessionRecycler(IoSessionRecycler sessionRecycler) Sets theIoSessionRecyclerfor this service.private voidStarts the inner Acceptor thread.protected final voidunbind0(List<? extends SocketAddress> localAddresses) Implement this method to perform the actual unbind operation.private intvoidupdateTrafficControl(NioSession session) Controls the traffic of the specifiedsessiondepending of theIoSession.isReadSuspended()andIoSession.isWriteSuspended()flagsprotected voidwakeup()voidwrite(NioSession session, WriteRequest writeRequest) Writes the WriteRequest for the specifiedsession.Methods inherited from class AbstractIoAcceptor
bind, bind, bind, bind, bind, getDefaultLocalAddresses, 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 IoAcceptor
bind, bind, bind, bind, bind, getDefaultLocalAddresses, getLocalAddresses, isCloseOnDeactivation, setCloseOnDeactivation, setDefaultLocalAddress, setDefaultLocalAddresses, setDefaultLocalAddresses, setDefaultLocalAddresses, unbind, unbind, unbind, unbindMethods inherited from interface IoProcessor
dispose, isDisposed, isDisposingMethods inherited from interface IoService
addListener, broadcast, dispose, dispose, getActivationTime, getFilterChain, getFilterChainBuilder, getHandler, getManagedSessionCount, getManagedSessions, getScheduledWriteBytes, getScheduledWriteMessages, getSessionDataStructureFactory, getStatistics, isActive, isDisposed, isDisposing, removeListener, setFilterChainBuilder, setHandler, setSessionDataStructureFactory
-
Field Details
-
DEFAULT_RECYCLER
A session recycler that is used to retrieve an existing session, unless it's too old. -
SELECT_TIMEOUT
private static final long SELECT_TIMEOUTA timeout used for the select, as we need to get out to deal with idle sessions- See Also:
-
lock
A lock used to protect the selector to be waked up before it's created -
registerQueue
A queue used to store the list of pending Binds -
cancelQueue
-
flushingSessions
-
boundHandles
-
sessionRecycler
-
disposalFuture
-
selectable
private volatile boolean selectable -
acceptor
The thread responsible of accepting incoming requests -
lastIdleCheckTime
private long lastIdleCheckTime -
selector
The Selector used by this acceptor
-
-
Constructor Details
-
NioDatagramAcceptor
public NioDatagramAcceptor()Creates a new instance. -
NioDatagramAcceptor
Creates a new instance.- Parameters:
executor- The executor to use
-
NioDatagramAcceptor
Creates a new instance.
-
-
Method Details
-
registerHandles
private int registerHandles() -
processReadySessions
-
scheduleFlush
-
readHandle
- Throws:
Exception
-
newSessionWithoutLock
private IoSession newSessionWithoutLock(SocketAddress remoteAddress, SocketAddress localAddress) throws Exception - Throws:
Exception
-
flushSessions
private void flushSessions(long currentTime) -
flush
- Throws:
Exception
-
unregisterHandles
private int unregisterHandles() -
notifyIdleSessions
private void notifyIdleSessions(long currentTime) -
startupAcceptor
Starts the inner Acceptor thread.- Throws:
InterruptedException
-
init
-
add
Adds the specifiedsessionto the I/O processor so that the I/O processor starts to perform any I/O operations related with thesession.- Specified by:
addin interfaceIoProcessor<NioSession>- Parameters:
session- The added session
-
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
-
close
- Throws:
Exception
-
destroy
-
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
-
flush
Flushes the internal write request queue of the specifiedsession.- Specified by:
flushin interfaceIoProcessor<NioSession>- Parameters:
session- The session we want the message to be written
-
getDefaultLocalAddress
Description copied from class:AbstractIoAcceptorReturns the default local address to bind when no argument is specified inIoAcceptor.bind()method. Please note that the default will not be used if any local address is specified. If more than one address are set, only one of them will be returned, but it's not necessarily the firstly specified address inIoAcceptor.setDefaultLocalAddresses(List).- Specified by:
getDefaultLocalAddressin interfaceDatagramAcceptor- Specified by:
getDefaultLocalAddressin interfaceIoAcceptor- Overrides:
getDefaultLocalAddressin classAbstractIoAcceptor- Returns:
- The default bound LocalAddress
-
getLocalAddress
Description copied from class:AbstractIoAcceptorReturns the local address which is bound currently. If more than one address are bound, only one of them will be returned, but it's not necessarily the firstly bound address.- Specified by:
getLocalAddressin interfaceDatagramAcceptor- Specified by:
getLocalAddressin interfaceIoAcceptor- Overrides:
getLocalAddressin classAbstractIoAcceptor- Returns:
- The bound LocalAddress
-
getSessionConfig
- Specified by:
getSessionConfigin interfaceDatagramAcceptor- Specified by:
getSessionConfigin interfaceIoService- Returns:
- the default configuration of the new
IoSessions created by this service.
-
getSessionRecycler
- Specified by:
getSessionRecyclerin interfaceDatagramAcceptor- Returns:
- the
IoSessionRecyclerfor this service.
-
getTransportMetadata
- Specified by:
getTransportMetadatain interfaceIoService- Returns:
- the
TransportMetadatathat this service runs on.
-
isReadable
-
isWritable
-
localAddress
- Throws:
Exception
-
newSession
protected NioSession newSession(IoProcessor<NioSession> processor, DatagramChannel handle, SocketAddress remoteAddress) -
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.- Specified by:
newSessionin interfaceIoAcceptor- 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
-
open
- Throws:
Exception
-
receive
- Throws:
Exception
-
remove
Removes and closes the specifiedsessionfrom the I/O processor so that the I/O processor closes the connection associated with thesessionand releases any other related resources.- Specified by:
removein interfaceIoProcessor<NioSession>- Parameters:
session- The session to be removed
-
select
-
select
-
selectedHandles
-
send
protected int send(NioSession session, IoBuffer buffer, SocketAddress remoteAddress) throws Exception - Throws:
Exception
-
setDefaultLocalAddress
Description copied from interface:DatagramAcceptorSets the default local InetSocketAddress to bind when no argument is specified inIoAcceptor.bind()method. Please note that the default will not be used if any local InetSocketAddress is specified. This method overrides theIoAcceptor.setDefaultLocalAddress(java.net.SocketAddress)method.- Specified by:
setDefaultLocalAddressin interfaceDatagramAcceptor- Parameters:
localAddress- The local address
-
setInterestedInWrite
- Throws:
Exception
-
setSessionRecycler
Description copied from interface:DatagramAcceptorSets theIoSessionRecyclerfor this service.- Specified by:
setSessionRecyclerin interfaceDatagramAcceptor- Parameters:
sessionRecycler-nullto use the default recycler
-
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
-
updateTrafficControl
Controls the traffic of the specifiedsessiondepending of theIoSession.isReadSuspended()andIoSession.isWriteSuspended()flags- Specified by:
updateTrafficControlin interfaceIoProcessor<NioSession>- Parameters:
session- The session to be updated
-
wakeup
protected void wakeup() -
write
Writes the WriteRequest for the specifiedsession.- Specified by:
writein interfaceIoProcessor<NioSession>- Parameters:
session- The session we want the message to be writtenwriteRequest- the WriteRequest to write
-