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>
IoAcceptorfor datagram transport (UDP/IP).
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classNioDatagramAcceptor.AcceptorThis private class is used to accept incoming connection from clients.-
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 private NioDatagramAcceptor.AcceptoracceptorThe thread responsible of accepting incoming requestsprivate java.util.Map<java.net.SocketAddress,java.nio.channels.DatagramChannel>boundHandlesprivate java.util.Queue<AbstractIoAcceptor.AcceptorOperationFuture>cancelQueueprivate static IoSessionRecyclerDEFAULT_RECYCLERA session recycler that is used to retrieve an existing session, unless it's too old.private AbstractIoService.ServiceOperationFuturedisposalFutureprivate java.util.Queue<NioSession>flushingSessionsprivate longlastIdleCheckTimeprivate java.util.concurrent.SemaphorelockA lock used to protect the selector to be waked up before it's createdprivate java.util.Queue<AbstractIoAcceptor.AcceptorOperationFuture>registerQueueA queue used to store the list of pending Bindsprivate static longSELECT_TIMEOUTA timeout used for the select, as we need to get out to deal with idle sessionsprivate booleanselectableprivate java.nio.channels.SelectorselectorThe Selector used by this acceptorprivate IoSessionRecyclersessionRecycler-
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 NioDatagramAcceptor()Creates a new instance.NioDatagramAcceptor(java.util.concurrent.Executor executor)Creates a new instance.privateNioDatagramAcceptor(IoSessionConfig sessionConfig, java.util.concurrent.Executor executor)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(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 java.util.Set<java.net.SocketAddress>bindInternal(java.util.List<? extends java.net.SocketAddress> localAddresses)Starts the acceptor, and register the given addressesprotected voidclose(java.nio.channels.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)java.net.InetSocketAddressgetDefaultLocalAddress()Returns the default local address to bind when no argument is specified inIoAcceptor.bind()method.java.net.InetSocketAddressgetLocalAddress()Returns the local address which is bound currently.DatagramSessionConfiggetSessionConfig()IoSessionRecyclergetSessionRecycler()TransportMetadatagetTransportMetadata()protected voidinit()protected booleanisReadable(java.nio.channels.DatagramChannel handle)protected booleanisWritable(java.nio.channels.DatagramChannel handle)protected java.net.SocketAddresslocalAddress(java.nio.channels.DatagramChannel handle)IoSessionnewSession(java.net.SocketAddress remoteAddress, java.net.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, java.nio.channels.DatagramChannel handle, java.net.SocketAddress remoteAddress)private IoSessionnewSessionWithoutLock(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress)private voidnotifyIdleSessions(long currentTime)protected java.nio.channels.DatagramChannelopen(java.net.SocketAddress localAddress)private voidprocessReadySessions(java.util.Set<java.nio.channels.SelectionKey> handles)private voidreadHandle(java.nio.channels.DatagramChannel handle)protected java.net.SocketAddressreceive(java.nio.channels.DatagramChannel handle, IoBuffer buffer)private intregisterHandles()voidremove(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 java.util.Set<java.nio.channels.SelectionKey>selectedHandles()protected intsend(NioSession session, IoBuffer buffer, java.net.SocketAddress remoteAddress)voidsetDefaultLocalAddress(java.net.InetSocketAddress localAddress)Sets the default local InetSocketAddress to bind when no argument is specified inIoAcceptor.bind()method.protected voidsetInterestedInWrite(NioSession session, boolean isInterested)voidsetSessionRecycler(IoSessionRecycler sessionRecycler)Sets theIoSessionRecyclerfor this service.private voidstartupAcceptor()Starts the inner Acceptor thread.protected voidunbind0(java.util.List<? extends java.net.SocketAddress> localAddresses)Implement this method to perform the actual unbind operation.private intunregisterHandles()voidupdateTrafficControl(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 org.apache.mina.core.service.AbstractIoAcceptor
bind, bind, bind, bind, bind, getDefaultLocalAddresses, 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.IoAcceptor
bind, bind, bind, bind, bind, getDefaultLocalAddresses, getLocalAddresses, isCloseOnDeactivation, setCloseOnDeactivation, setDefaultLocalAddress, setDefaultLocalAddresses, setDefaultLocalAddresses, setDefaultLocalAddresses, unbind, unbind, unbind, unbind
-
Methods inherited from interface org.apache.mina.core.service.IoProcessor
dispose, isDisposed, isDisposing
-
Methods inherited from interface org.apache.mina.core.service.IoService
addListener, broadcast, dispose, dispose, getActivationTime, getFilterChain, getFilterChainBuilder, getHandler, getManagedSessionCount, getManagedSessions, getScheduledWriteBytes, getScheduledWriteMessages, getSessionDataStructureFactory, getStatistics, isActive, isDisposed, isDisposing, removeListener, setFilterChainBuilder, setHandler, setSessionDataStructureFactory
-
-
-
-
Field Detail
-
DEFAULT_RECYCLER
private static final IoSessionRecycler 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_TIMEOUT
A timeout used for the select, as we need to get out to deal with idle sessions- See Also:
- Constant Field Values
-
lock
private final java.util.concurrent.Semaphore lock
A lock used to protect the selector to be waked up before it's created
-
registerQueue
private final java.util.Queue<AbstractIoAcceptor.AcceptorOperationFuture> registerQueue
A queue used to store the list of pending Binds
-
cancelQueue
private final java.util.Queue<AbstractIoAcceptor.AcceptorOperationFuture> cancelQueue
-
flushingSessions
private final java.util.Queue<NioSession> flushingSessions
-
boundHandles
private final java.util.Map<java.net.SocketAddress,java.nio.channels.DatagramChannel> boundHandles
-
sessionRecycler
private IoSessionRecycler sessionRecycler
-
disposalFuture
private final AbstractIoService.ServiceOperationFuture disposalFuture
-
selectable
private volatile boolean selectable
-
acceptor
private NioDatagramAcceptor.Acceptor acceptor
The thread responsible of accepting incoming requests
-
lastIdleCheckTime
private long lastIdleCheckTime
-
selector
private volatile java.nio.channels.Selector selector
The Selector used by this acceptor
-
-
Constructor Detail
-
NioDatagramAcceptor
public NioDatagramAcceptor()
Creates a new instance.
-
NioDatagramAcceptor
public NioDatagramAcceptor(java.util.concurrent.Executor executor)
Creates a new instance.- Parameters:
executor- The executor to use
-
NioDatagramAcceptor
private NioDatagramAcceptor(IoSessionConfig sessionConfig, java.util.concurrent.Executor executor)
Creates a new instance.
-
-
Method Detail
-
registerHandles
private int registerHandles()
-
processReadySessions
private void processReadySessions(java.util.Set<java.nio.channels.SelectionKey> handles)
-
scheduleFlush
private boolean scheduleFlush(NioSession session)
-
readHandle
private void readHandle(java.nio.channels.DatagramChannel handle) throws java.lang.Exception- Throws:
java.lang.Exception
-
newSessionWithoutLock
private IoSession newSessionWithoutLock(java.net.SocketAddress remoteAddress, java.net.SocketAddress localAddress) throws java.lang.Exception
- Throws:
java.lang.Exception
-
flushSessions
private void flushSessions(long currentTime)
-
flush
private boolean flush(NioSession session, long currentTime) throws java.lang.Exception
- Throws:
java.lang.Exception
-
unregisterHandles
private int unregisterHandles()
-
notifyIdleSessions
private void notifyIdleSessions(long currentTime)
-
startupAcceptor
private void startupAcceptor() throws java.lang.InterruptedExceptionStarts the inner Acceptor thread.- Throws:
java.lang.InterruptedException
-
init
protected void init() throws java.lang.Exception- Throws:
java.lang.Exception
-
add
public void add(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.- Specified by:
addin interfaceIoProcessor<NioSession>- Parameters:
session- The added session
-
bindInternal
protected final java.util.Set<java.net.SocketAddress> bindInternal(java.util.List<? extends java.net.SocketAddress> localAddresses) throws java.lang.ExceptionStarts 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:
java.lang.Exception- If the bind failed
-
close
protected void close(java.nio.channels.DatagramChannel handle) throws java.lang.Exception- Throws:
java.lang.Exception
-
destroy
protected void destroy() throws java.lang.Exception- Throws:
java.lang.Exception
-
dispose0
protected void dispose0() throws java.lang.ExceptionImplement this method to release any acquired resources. This method is invoked only once byAbstractIoService.dispose().- Specified by:
dispose0in classAbstractIoService- Throws:
java.lang.Exception- If the dispose failed
-
flush
public void flush(NioSession session)
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
public java.net.InetSocketAddress 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
public java.net.InetSocketAddress 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
public DatagramSessionConfig getSessionConfig()
- Specified by:
getSessionConfigin interfaceDatagramAcceptor- Specified by:
getSessionConfigin interfaceIoService- Returns:
- the default Datagram configuration of the new
IoSessions created by this service.
-
getSessionRecycler
public final IoSessionRecycler getSessionRecycler()
- Specified by:
getSessionRecyclerin interfaceDatagramAcceptor- Returns:
- the
IoSessionRecyclerfor this service.
-
getTransportMetadata
public TransportMetadata getTransportMetadata()
- Specified by:
getTransportMetadatain interfaceIoService- Returns:
- the
TransportMetadatathat this service runs on.
-
isReadable
protected boolean isReadable(java.nio.channels.DatagramChannel handle)
-
isWritable
protected boolean isWritable(java.nio.channels.DatagramChannel handle)
-
localAddress
protected java.net.SocketAddress localAddress(java.nio.channels.DatagramChannel handle) throws java.lang.Exception- Throws:
java.lang.Exception
-
newSession
protected NioSession newSession(IoProcessor<NioSession> processor, java.nio.channels.DatagramChannel handle, java.net.SocketAddress remoteAddress)
-
newSession
public final IoSession newSession(java.net.SocketAddress remoteAddress, java.net.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.- 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
protected java.nio.channels.DatagramChannel open(java.net.SocketAddress localAddress) throws java.lang.Exception- Throws:
java.lang.Exception
-
receive
protected java.net.SocketAddress receive(java.nio.channels.DatagramChannel handle, IoBuffer buffer) throws java.lang.Exception- Throws:
java.lang.Exception
-
remove
public void remove(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.- Specified by:
removein interfaceIoProcessor<NioSession>- Parameters:
session- The session to be removed
-
select
protected int select() throws java.lang.Exception- Throws:
java.lang.Exception
-
select
protected int select(long timeout) throws java.lang.Exception- Throws:
java.lang.Exception
-
selectedHandles
protected java.util.Set<java.nio.channels.SelectionKey> selectedHandles()
-
send
protected int send(NioSession session, IoBuffer buffer, java.net.SocketAddress remoteAddress) throws java.lang.Exception
- Throws:
java.lang.Exception
-
setDefaultLocalAddress
public void setDefaultLocalAddress(java.net.InetSocketAddress localAddress)
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
protected void setInterestedInWrite(NioSession session, boolean isInterested) throws java.lang.Exception
- Throws:
java.lang.Exception
-
setSessionRecycler
public final void setSessionRecycler(IoSessionRecycler sessionRecycler)
Description copied from interface:DatagramAcceptorSets theIoSessionRecyclerfor this service.- Specified by:
setSessionRecyclerin interfaceDatagramAcceptor- Parameters:
sessionRecycler-nullto use the default recycler
-
unbind0
protected final void unbind0(java.util.List<? extends java.net.SocketAddress> localAddresses) throws java.lang.ExceptionImplement this method to perform the actual unbind operation.- Specified by:
unbind0in classAbstractIoAcceptor- Parameters:
localAddresses- The address to unbind from- Throws:
java.lang.Exception- If the unbind failed
-
updateTrafficControl
public void updateTrafficControl(NioSession session)
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
public void write(NioSession session, WriteRequest writeRequest)
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
-
-