org.apache.mina.transport.socket.nio
Class NioDatagramConnector

java.lang.Object
  extended by org.apache.mina.core.service.AbstractIoService
      extended by org.apache.mina.core.service.AbstractIoConnector
          extended by org.apache.mina.core.polling.AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
              extended by org.apache.mina.transport.socket.nio.NioDatagramConnector
All Implemented Interfaces:
IoConnector, IoService, DatagramConnector

public final class NioDatagramConnector
extends AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
implements DatagramConnector

IoConnector for 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
 
Constructor Summary
NioDatagramConnector()
          Creates a new instance.
NioDatagramConnector(java.lang.Class<? extends IoProcessor<NioSession>> processorClass)
          Constructor for NioDatagramConnector with default configuration with default configuration which will use a built-in thread pool executor to manage the default number of processor instances.
NioDatagramConnector(java.lang.Class<? extends IoProcessor<NioSession>> processorClass, int processorCount)
          Constructor for NioDatagramConnector with default configuration which will use a built-in thread pool executor to manage the given number of processor instances.
NioDatagramConnector(int processorCount)
          Creates a new instance.
NioDatagramConnector(IoProcessor<NioSession> processor)
          Creates a new instance.
 
Method Summary
protected  java.util.Iterator<java.nio.channels.DatagramChannel> allHandles()
          Iterator for all the client sockets polled for connection.
protected  void close(java.nio.channels.DatagramChannel handle)
          Close a client socket.
protected  boolean connect(java.nio.channels.DatagramChannel handle, java.net.SocketAddress remoteAddress)
          Connect a newly created client socket handle to a remote SocketAddress.
protected  void destroy()
          Destroy the polling system, will be called when this IoConnector implementation will be disposed.
protected  boolean finishConnect(java.nio.channels.DatagramChannel handle)
          Finish the connection process of a client socket after it was marked as ready to process by the AbstractPollingIoConnector.select(int) call.
protected  AbstractPollingIoConnector.ConnectionRequest getConnectionRequest(java.nio.channels.DatagramChannel handle)
          get the AbstractPollingIoConnector.ConnectionRequest for a given client socket handle
 java.net.InetSocketAddress getDefaultRemoteAddress()
          Returns the default remote address to connect to when no argument is specified in IoConnector.connect() method.
 DatagramSessionConfig getSessionConfig()
          Returns the default configuration of the new IoSessions created by this service.
 TransportMetadata getTransportMetadata()
          Returns the TransportMetadata that this service runs on.
protected  void init()
          Initialize the polling system, will be called at construction time.
protected  java.nio.channels.DatagramChannel newHandle(java.net.SocketAddress localAddress)
          Create a new client socket handle from a local SocketAddress
protected  NioSession newSession(IoProcessor<NioSession> processor, java.nio.channels.DatagramChannel handle)
          Create a new IoSession from a connected socket client handle.
protected  void register(java.nio.channels.DatagramChannel handle, AbstractPollingIoConnector.ConnectionRequest request)
          Register a new client socket for connection, add it to connection polling
protected  int select(int timeout)
          Check for connected sockets, interrupt when at least a connection is processed (connected or failed to connect).
protected  java.util.Iterator<java.nio.channels.DatagramChannel> selectedHandles()
          Iterator for the set of client sockets found connected or failed to connect during the last #select() call.
 void setDefaultRemoteAddress(java.net.InetSocketAddress defaultRemoteAddress)
           
protected  void wakeup()
          Interrupt the #select() 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, setConnectTimeout, setConnectTimeoutCheckInterval, setConnectTimeoutMillis, 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, setConnectTimeout, setConnectTimeoutMillis, 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.


NioDatagramConnector

public NioDatagramConnector(IoProcessor<NioSession> processor)
Creates a new instance.


NioDatagramConnector

public NioDatagramConnector(java.lang.Class<? extends IoProcessor<NioSession>> processorClass,
                            int processorCount)
Constructor for NioDatagramConnector with 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.SimpleIoProcessorPool(Class, Executor, int)

NioDatagramConnector

public NioDatagramConnector(java.lang.Class<? extends IoProcessor<NioSession>> processorClass)
Constructor for NioDatagramConnector with 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.SimpleIoProcessorPool(Class, Executor, int), SimpleIoProcessorPool.DEFAULT_SIZE
Method Detail

getTransportMetadata

public TransportMetadata getTransportMetadata()
Description copied from interface: IoService
Returns the TransportMetadata that this service runs on.

Specified by:
getTransportMetadata in interface IoService

getSessionConfig

public DatagramSessionConfig getSessionConfig()
Description copied from class: AbstractIoService
Returns the default configuration of the new IoSessions created by this service.

Specified by:
getSessionConfig in interface IoService
Overrides:
getSessionConfig in class AbstractIoService

getDefaultRemoteAddress

public java.net.InetSocketAddress getDefaultRemoteAddress()
Description copied from class: AbstractIoConnector
Returns the default remote address to connect to when no argument is specified in IoConnector.connect() method.

Specified by:
getDefaultRemoteAddress in interface IoConnector
Specified by:
getDefaultRemoteAddress in interface DatagramConnector
Overrides:
getDefaultRemoteAddress in class AbstractIoConnector

setDefaultRemoteAddress

public void setDefaultRemoteAddress(java.net.InetSocketAddress defaultRemoteAddress)
Specified by:
setDefaultRemoteAddress in interface DatagramConnector

init

protected void init()
             throws java.lang.Exception
Description copied from class: AbstractPollingIoConnector
Initialize the polling system, will be called at construction time.

Specified by:
init in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Throws:
java.lang.Exception - any exception thrown by the underlying system calls

newHandle

protected java.nio.channels.DatagramChannel newHandle(java.net.SocketAddress localAddress)
                                               throws java.lang.Exception
Description copied from class: AbstractPollingIoConnector
Create a new client socket handle from a local SocketAddress

Specified by:
newHandle in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Parameters:
localAddress - the socket address for binding the new client socket
Returns:
a new client socket handle
Throws:
java.lang.Exception - any exception thrown by the underlying systems calls

connect

protected boolean connect(java.nio.channels.DatagramChannel handle,
                          java.net.SocketAddress remoteAddress)
                   throws java.lang.Exception
Description copied from class: AbstractPollingIoConnector
Connect a newly created client socket handle to a remote SocketAddress. This operation is non-blocking, so at end of the call the socket can be still in connection process.

Specified by:
connect in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Parameters:
handle - the client socket handle
remoteAddress - the remote address where to connect
Returns:
true if a connection was established, false if this client socket is in non-blocking mode and the connection operation is in progress
Throws:
java.lang.Exception

newSession

protected NioSession newSession(IoProcessor<NioSession> processor,
                                java.nio.channels.DatagramChannel handle)
Description copied from class: AbstractPollingIoConnector
Create a new IoSession from a connected socket client handle. Will assign the created IoSession to the given IoProcessor for managing future I/O events.

Specified by:
newSession in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Parameters:
processor - the processor in charge of this session
handle - the newly connected client socket handle
Returns:
a new IoSession

close

protected void close(java.nio.channels.DatagramChannel handle)
              throws java.lang.Exception
Description copied from class: AbstractPollingIoConnector
Close a client socket.

Specified by:
close in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Parameters:
handle - the client socket
Throws:
java.lang.Exception - any exception thrown by the underlying systems calls

allHandles

protected java.util.Iterator<java.nio.channels.DatagramChannel> allHandles()
Description copied from class: AbstractPollingIoConnector
Iterator for all the client sockets polled for connection.

Specified by:
allHandles in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Returns:
the list of client sockets currently polled for connection

getConnectionRequest

protected AbstractPollingIoConnector.ConnectionRequest getConnectionRequest(java.nio.channels.DatagramChannel handle)
Description copied from class: AbstractPollingIoConnector
get the AbstractPollingIoConnector.ConnectionRequest for a given client socket handle

Specified by:
getConnectionRequest in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Parameters:
handle - the socket client handle
Returns:
the connection request if the socket is connecting otherwise null

destroy

protected void destroy()
                throws java.lang.Exception
Description copied from class: AbstractPollingIoConnector
Destroy the polling system, will be called when this IoConnector implementation will be disposed.

Specified by:
destroy in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Throws:
java.lang.Exception - any exception thrown by the underlying systems calls

finishConnect

protected boolean finishConnect(java.nio.channels.DatagramChannel handle)
                         throws java.lang.Exception
Description copied from class: AbstractPollingIoConnector
Finish the connection process of a client socket after it was marked as ready to process by the AbstractPollingIoConnector.select(int) call. The socket will be connected or reported as connection failed.

Specified by:
finishConnect in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Parameters:
handle - the client socket handle to finsh to connect
Returns:
true if the socket is connected
Throws:
java.lang.Exception - any exception thrown by the underlying systems calls

register

protected void register(java.nio.channels.DatagramChannel handle,
                        AbstractPollingIoConnector.ConnectionRequest request)
                 throws java.lang.Exception
Description copied from class: AbstractPollingIoConnector
Register a new client socket for connection, add it to connection polling

Specified by:
register in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Parameters:
handle - client socket handle
request - the associated AbstractPollingIoConnector.ConnectionRequest
Throws:
java.lang.Exception - any exception thrown by the underlying systems calls

select

protected int select(int timeout)
              throws java.lang.Exception
Description copied from class: AbstractPollingIoConnector
Check 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 by AbstractPollingIoConnector.selectedHandles()

Specified by:
select in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Returns:
The number of socket having received some data
Throws:
java.lang.Exception - any exception thrown by the underlying systems calls

selectedHandles

protected java.util.Iterator<java.nio.channels.DatagramChannel> selectedHandles()
Description copied from class: AbstractPollingIoConnector
Iterator for the set of client sockets found connected or failed to connect during the last #select() call.

Specified by:
selectedHandles in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>
Returns:
the list of client socket handles to process

wakeup

protected void wakeup()
Description copied from class: AbstractPollingIoConnector
Interrupt the #select() method. Used when the poll set need to be modified.

Specified by:
wakeup in class AbstractPollingIoConnector<NioSession,java.nio.channels.DatagramChannel>