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).- Author:
- Apache MINA Project
-
-
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 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 Constructor Description NioDatagramAcceptor()
Creates a new instance.NioDatagramAcceptor(Executor executor)
Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(NioSession session)
Adds the specifiedsession
to the I/O processor so that the I/O processor starts to perform any I/O operations related with thesession
.protected Set<SocketAddress>
bindInternal(List<? extends SocketAddress> localAddresses)
Starts the acceptor, and register the given addressesprotected void
close(DatagramChannel handle)
protected void
destroy()
protected void
dispose0()
Implement this method to release any acquired resources.void
flush(NioSession session)
Flushes the internal write request queue of the specifiedsession
.InetSocketAddress
getDefaultLocalAddress()
Returns the default local address to bind when no argument is specified inIoAcceptor.bind()
method.InetSocketAddress
getLocalAddress()
Returns the local address which is bound currently.DatagramSessionConfig
getSessionConfig()
IoSessionRecycler
getSessionRecycler()
TransportMetadata
getTransportMetadata()
protected void
init()
protected boolean
isReadable(DatagramChannel handle)
protected boolean
isWritable(DatagramChannel handle)
protected SocketAddress
localAddress(DatagramChannel handle)
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.protected NioSession
newSession(IoProcessor<NioSession> processor, DatagramChannel handle, SocketAddress remoteAddress)
protected DatagramChannel
open(SocketAddress localAddress)
protected SocketAddress
receive(DatagramChannel handle, IoBuffer buffer)
void
remove(NioSession session)
Removes and closes the specifiedsession
from the I/O processor so that the I/O processor closes the connection associated with thesession
and releases any other related resources.protected int
select()
protected int
select(long timeout)
protected Set<SelectionKey>
selectedHandles()
protected int
send(NioSession session, IoBuffer buffer, SocketAddress remoteAddress)
void
setDefaultLocalAddress(InetSocketAddress localAddress)
Sets the default local InetSocketAddress to bind when no argument is specified inIoAcceptor.bind()
method.protected void
setInterestedInWrite(NioSession session, boolean isInterested)
void
setSessionRecycler(IoSessionRecycler sessionRecycler)
Sets theIoSessionRecycler
for this service.protected void
unbind0(List<? extends SocketAddress> localAddresses)
Implement this method to perform the actual unbind operation.void
updateTrafficControl(NioSession session)
Controls the traffic of the specifiedsession
depending of theIoSession.isReadSuspended()
andIoSession.isWriteSuspended()
flagsprotected void
wakeup()
void
write(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
-
-
-
-
Constructor Detail
-
NioDatagramAcceptor
public NioDatagramAcceptor()
Creates a new instance.
-
NioDatagramAcceptor
public NioDatagramAcceptor(Executor executor)
Creates a new instance.- Parameters:
executor
- The executor to use
-
-
Method Detail
-
add
public void add(NioSession session)
Adds the specifiedsession
to the I/O processor so that the I/O processor starts to perform any I/O operations related with thesession
.- Specified by:
add
in 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:
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
-
close
protected void close(DatagramChannel handle) throws Exception
- Throws:
Exception
-
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
-
flush
public void flush(NioSession session)
Flushes the internal write request queue of the specifiedsession
.- Specified by:
flush
in interfaceIoProcessor<NioSession>
- Parameters:
session
- The session we want the message to be written
-
getDefaultLocalAddress
public InetSocketAddress getDefaultLocalAddress()
Description copied from class:AbstractIoAcceptor
Returns 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:
getDefaultLocalAddress
in interfaceDatagramAcceptor
- Specified by:
getDefaultLocalAddress
in interfaceIoAcceptor
- Overrides:
getDefaultLocalAddress
in classAbstractIoAcceptor
- Returns:
- The default bound LocalAddress
-
getLocalAddress
public InetSocketAddress getLocalAddress()
Description copied from class:AbstractIoAcceptor
Returns 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:
getLocalAddress
in interfaceDatagramAcceptor
- Specified by:
getLocalAddress
in interfaceIoAcceptor
- Overrides:
getLocalAddress
in classAbstractIoAcceptor
- Returns:
- The bound LocalAddress
-
getSessionConfig
public DatagramSessionConfig getSessionConfig()
- Specified by:
getSessionConfig
in interfaceDatagramAcceptor
- Specified by:
getSessionConfig
in interfaceIoService
- Returns:
- the default Datagram configuration of the new
IoSession
s created by this service.
-
getSessionRecycler
public final IoSessionRecycler getSessionRecycler()
- Specified by:
getSessionRecycler
in interfaceDatagramAcceptor
- Returns:
- the
IoSessionRecycler
for this service.
-
getTransportMetadata
public TransportMetadata getTransportMetadata()
- Specified by:
getTransportMetadata
in interfaceIoService
- Returns:
- the
TransportMetadata
that this service runs on.
-
isReadable
protected boolean isReadable(DatagramChannel handle)
-
isWritable
protected boolean isWritable(DatagramChannel handle)
-
localAddress
protected SocketAddress localAddress(DatagramChannel handle) throws Exception
- Throws:
Exception
-
newSession
protected NioSession newSession(IoProcessor<NioSession> processor, DatagramChannel handle, SocketAddress remoteAddress)
-
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.- Specified by:
newSession
in 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 DatagramChannel open(SocketAddress localAddress) throws Exception
- Throws:
Exception
-
receive
protected SocketAddress receive(DatagramChannel handle, IoBuffer buffer) throws Exception
- Throws:
Exception
-
remove
public void remove(NioSession session)
Removes and closes the specifiedsession
from the I/O processor so that the I/O processor closes the connection associated with thesession
and releases any other related resources.- Specified by:
remove
in interfaceIoProcessor<NioSession>
- Parameters:
session
- The session to be removed
-
selectedHandles
protected Set<SelectionKey> selectedHandles()
-
send
protected int send(NioSession session, IoBuffer buffer, SocketAddress remoteAddress) throws Exception
- Throws:
Exception
-
setDefaultLocalAddress
public void setDefaultLocalAddress(InetSocketAddress localAddress)
Description copied from interface:DatagramAcceptor
Sets 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:
setDefaultLocalAddress
in interfaceDatagramAcceptor
- Parameters:
localAddress
- The local address
-
setInterestedInWrite
protected void setInterestedInWrite(NioSession session, boolean isInterested) throws Exception
- Throws:
Exception
-
setSessionRecycler
public final void setSessionRecycler(IoSessionRecycler sessionRecycler)
Description copied from interface:DatagramAcceptor
Sets theIoSessionRecycler
for this service.- Specified by:
setSessionRecycler
in interfaceDatagramAcceptor
- Parameters:
sessionRecycler
-null
to use the default recycler
-
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
-
updateTrafficControl
public void updateTrafficControl(NioSession session)
Controls the traffic of the specifiedsession
depending of theIoSession.isReadSuspended()
andIoSession.isWriteSuspended()
flags- Specified by:
updateTrafficControl
in 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:
write
in interfaceIoProcessor<NioSession>
- Parameters:
session
- The session we want the message to be writtenwriteRequest
- the WriteRequest to write
-
-