Class NioDatagramConnector
- java.lang.Object
-
- org.apache.mina.core.service.AbstractIoService
-
- org.apache.mina.core.service.AbstractIoConnector
-
- org.apache.mina.core.polling.AbstractPollingIoConnector<NioSession,DatagramChannel>
-
- org.apache.mina.transport.socket.nio.NioDatagramConnector
-
- All Implemented Interfaces:
IoConnector,IoService,DatagramConnector
public final class NioDatagramConnector extends AbstractPollingIoConnector<NioSession,DatagramChannel> implements DatagramConnector
IoConnectorfor datagram transport (UDP/IP).- Author:
- Apache MINA Project
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class org.apache.mina.core.polling.AbstractPollingIoConnector
AbstractPollingIoConnector.ConnectionRequest
-
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.AbstractIoService
disposalLock, LOGGER, sessionConfig
-
-
Constructor Summary
Constructors Constructor Description NioDatagramConnector()Creates a new instance.NioDatagramConnector(int processorCount)Creates a new instance.NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass)Constructor forNioDatagramConnectorwith default configuration with default configuration which will use a built-in thread pool executor to manage the default number of processor instances.NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass, int processorCount)Constructor forNioDatagramConnectorwith default configuration which will use a built-in thread pool executor to manage the given number of processor instances.NioDatagramConnector(IoProcessor<NioSession> processor)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Iterator<DatagramChannel>allHandles()Iteratorfor all the client sockets polled for connection.protected voidclose(DatagramChannel handle)Close a client socket.protected booleanconnect(DatagramChannel handle, SocketAddress remoteAddress)Connect a newly created client socket handle to a remoteSocketAddress.protected voiddestroy()Destroy the polling system, will be called when thisIoConnectorimplementation will be disposed.protected booleanfinishConnect(DatagramChannel handle)Finish the connection process of a client socket after it was marked as ready to process by theAbstractPollingIoConnector.select(int)call.protected AbstractPollingIoConnector.ConnectionRequestgetConnectionRequest(DatagramChannel handle)get theAbstractPollingIoConnector.ConnectionRequestfor a given client socket handleInetSocketAddressgetDefaultRemoteAddress()DatagramSessionConfiggetSessionConfig()TransportMetadatagetTransportMetadata()protected voidinit()Initialize the polling system, will be called at construction time.protected DatagramChannelnewHandle(SocketAddress localAddress)Create a new client socket handle from a localSocketAddressprotected NioSessionnewSession(IoProcessor<NioSession> processor, DatagramChannel handle)Create a newIoSessionfrom a connected socket client handle.protected voidregister(DatagramChannel handle, AbstractPollingIoConnector.ConnectionRequest request)Register a new client socket for connection, add it to connection pollingprotected intselect(int timeout)Check for connected sockets, interrupt when at least a connection is processed (connected or failed to connect).protected Iterator<DatagramChannel>selectedHandles()Iteratorfor the set of client sockets found connected or failed to connect during the lastAbstractPollingIoConnector.select(int)call.voidsetDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress)Sets the default remote InetSocketAddress to connect to when no argument is specified inIoConnector.connect()method.protected voidwakeup()Interrupt theAbstractPollingIoConnector.select(int)method.-
Methods inherited from class org.apache.mina.core.polling.AbstractPollingIoConnector
connect0, dispose0
-
Methods inherited from class org.apache.mina.core.service.AbstractIoConnector
connect, connect, connect, connect, connect, connect, finishSessionInitialization0, getConnectTimeout, getConnectTimeoutCheckInterval, getConnectTimeoutMillis, getDefaultLocalAddress, setConnectTimeout, setConnectTimeoutCheckInterval, setConnectTimeoutMillis, setDefaultLocalAddress, setDefaultRemoteAddress, toString
-
Methods inherited from class org.apache.mina.core.service.AbstractIoService
addListener, broadcast, dispose, dispose, executeWorker, executeWorker, 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.IoConnector
connect, connect, connect, connect, connect, connect, getConnectTimeout, getConnectTimeoutMillis, getDefaultLocalAddress, setConnectTimeout, setConnectTimeoutMillis, setDefaultLocalAddress, setDefaultRemoteAddress
-
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
-
NioDatagramConnector
public NioDatagramConnector()
Creates a new instance.
-
NioDatagramConnector
public NioDatagramConnector(int processorCount)
Creates a new instance.- Parameters:
processorCount- The number of IoProcessor instance to create
-
NioDatagramConnector
public NioDatagramConnector(IoProcessor<NioSession> processor)
Creates a new instance.- Parameters:
processor- The IoProcessor instance to use
-
NioDatagramConnector
public NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass, int processorCount)
Constructor forNioDatagramConnectorwith default configuration which will use a built-in thread pool executor to manage the given number of processor instances. The processor class must have a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a no-arg constructor.- Parameters:
processorClass- the processor class.processorCount- the number of processors to instantiate.- Since:
- 2.0.0-M4
- See Also:
SimpleIoProcessorPool(Class, Executor, int, java.nio.channels.spi.SelectorProvider)
-
NioDatagramConnector
public NioDatagramConnector(Class<? extends IoProcessor<NioSession>> processorClass)
Constructor forNioDatagramConnectorwith default configuration with default configuration which will use a built-in thread pool executor to manage the default number of processor instances. The processor class must have a constructor that accepts ExecutorService or Executor as its single argument, or, failing that, a no-arg constructor. The default number of instances is equal to the number of processor cores in the system, plus one.- Parameters:
processorClass- the processor class.- Since:
- 2.0.0-M4
- See Also:
SimpleIoProcessorPool(Class, Executor, int, java.nio.channels.spi.SelectorProvider)
-
-
Method Detail
-
getTransportMetadata
public TransportMetadata getTransportMetadata()
- Specified by:
getTransportMetadatain interfaceIoService- Returns:
- the
TransportMetadatathat this service runs on.
-
getSessionConfig
public DatagramSessionConfig getSessionConfig()
- Specified by:
getSessionConfigin interfaceDatagramConnector- Specified by:
getSessionConfigin interfaceIoService- Returns:
- the default configuration of the new FatagramSessions created by this connect service.
-
getDefaultRemoteAddress
public InetSocketAddress getDefaultRemoteAddress()
- Specified by:
getDefaultRemoteAddressin interfaceDatagramConnector- Specified by:
getDefaultRemoteAddressin interfaceIoConnector- Overrides:
getDefaultRemoteAddressin classAbstractIoConnector- Returns:
- the default remote address to connect to when no argument
is specified in
IoConnector.connect()method.
-
setDefaultRemoteAddress
public void setDefaultRemoteAddress(InetSocketAddress defaultRemoteAddress)
Sets the default remote InetSocketAddress to connect to when no argument is specified inIoConnector.connect()method. This method overrides theIoConnector.setDefaultRemoteAddress(java.net.SocketAddress)method.- Specified by:
setDefaultRemoteAddressin interfaceDatagramConnector- Parameters:
defaultRemoteAddress- The remote address to set
-
init
protected void init() throws ExceptionInitialize the polling system, will be called at construction time.- Specified by:
initin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Throws:
Exception- any exception thrown by the underlying system calls
-
newHandle
protected DatagramChannel newHandle(SocketAddress localAddress) throws Exception
Create a new client socket handle from a localSocketAddress- Specified by:
newHandlein classAbstractPollingIoConnector<NioSession,DatagramChannel>- Parameters:
localAddress- the socket address for binding the new client socket- Returns:
- a new client socket handle
- Throws:
Exception- any exception thrown by the underlying systems calls
-
connect
protected boolean connect(DatagramChannel handle, SocketAddress remoteAddress) throws Exception
Connect a newly created client socket handle to a remoteSocketAddress. This operation is non-blocking, so at end of the call the socket can be still in connection process.- Specified by:
connectin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Parameters:
handle- the client socket handleremoteAddress- the remote address where to connect- Returns:
trueif a connection was established,falseif this client socket is in non-blocking mode and the connection operation is in progress- Throws:
Exception- If the connect failed
-
newSession
protected NioSession newSession(IoProcessor<NioSession> processor, DatagramChannel handle)
Create a newIoSessionfrom a connected socket client handle. Will assign the createdIoSessionto the givenIoProcessorfor managing future I/O events.- Specified by:
newSessionin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Parameters:
processor- the processor in charge of this sessionhandle- the newly connected client socket handle- Returns:
- a new
IoSession
-
close
protected void close(DatagramChannel handle) throws Exception
Close a client socket.- Specified by:
closein classAbstractPollingIoConnector<NioSession,DatagramChannel>- Parameters:
handle- the client socket- Throws:
Exception- any exception thrown by the underlying systems calls
-
allHandles
protected Iterator<DatagramChannel> allHandles()
Iteratorfor all the client sockets polled for connection.- Specified by:
allHandlesin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Returns:
- the list of client sockets currently polled for connection
-
getConnectionRequest
protected AbstractPollingIoConnector.ConnectionRequest getConnectionRequest(DatagramChannel handle)
get theAbstractPollingIoConnector.ConnectionRequestfor a given client socket handle- Specified by:
getConnectionRequestin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Parameters:
handle- the socket client handle- Returns:
- the connection request if the socket is connecting otherwise
null
-
destroy
protected void destroy() throws ExceptionDestroy the polling system, will be called when thisIoConnectorimplementation will be disposed.- Specified by:
destroyin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Throws:
Exception- any exception thrown by the underlying systems calls
-
finishConnect
protected boolean finishConnect(DatagramChannel handle) throws Exception
Finish the connection process of a client socket after it was marked as ready to process by theAbstractPollingIoConnector.select(int)call. The socket will be connected or reported as connection failed.- Specified by:
finishConnectin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Parameters:
handle- the client socket handle to finish to connect- Returns:
- true if the socket is connected
- Throws:
Exception- any exception thrown by the underlying systems calls
-
register
protected void register(DatagramChannel handle, AbstractPollingIoConnector.ConnectionRequest request) throws Exception
Register a new client socket for connection, add it to connection polling- Specified by:
registerin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Parameters:
handle- client socket handlerequest- the associatedAbstractPollingIoConnector.ConnectionRequest- Throws:
Exception- any exception thrown by the underlying systems calls
-
select
protected int select(int timeout) throws ExceptionCheck for connected sockets, interrupt when at least a connection is processed (connected or failed to connect). All the client socket descriptors processed need to be returned byAbstractPollingIoConnector.selectedHandles()- Specified by:
selectin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Parameters:
timeout- The timeout for the select() method- Returns:
- The number of socket having received some data
- Throws:
Exception- any exception thrown by the underlying systems calls
-
selectedHandles
protected Iterator<DatagramChannel> selectedHandles()
Iteratorfor the set of client sockets found connected or failed to connect during the lastAbstractPollingIoConnector.select(int)call.- Specified by:
selectedHandlesin classAbstractPollingIoConnector<NioSession,DatagramChannel>- Returns:
- the list of client socket handles to process
-
wakeup
protected void wakeup()
Interrupt theAbstractPollingIoConnector.select(int)method. Used when the poll set need to be modified.- Specified by:
wakeupin classAbstractPollingIoConnector<NioSession,DatagramChannel>
-
-