This commit is contained in:
Trustin Lee 2009-06-18 07:33:37 +00:00
parent 48634e3d30
commit 2b33c26e72
7 changed files with 64 additions and 38 deletions

View File

@ -29,7 +29,7 @@ import java.net.NetworkInterface;
import org.jboss.netty.channel.Channel;
/**
* A UDP datagram {@link Channel} which is created by {@link DatagramChannelFactory}.
* A UDP/IP {@link Channel} which is created by {@link DatagramChannelFactory}.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)

View File

@ -36,7 +36,7 @@ import org.jboss.netty.channel.ReceiveBufferSizePredictor;
import org.jboss.netty.util.internal.ConversionUtil;
/**
* The default {@link SocketChannelConfig} implementation.
* The default {@link DatagramChannelConfig} implementation.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)

View File

@ -24,12 +24,10 @@ package org.jboss.netty.channel.socket;
import java.net.InetSocketAddress;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ServerChannel;
/**
* A server-side TCP/IP socket {@link Channel} which accepts incoming TCP/IP
* connections.
* A TCP/IP {@link ServerChannel} which accepts incoming TCP/IP connections.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)

View File

@ -48,7 +48,7 @@ import org.jboss.netty.util.internal.LinkedTransferQueue;
import org.jboss.netty.util.internal.ThreadLocalBoolean;
/**
* NioDatagramChannel provides a connection less NIO UDP channel for Netty.
* Provides an NIO based {@link org.jboss.netty.channel.socket.DatagramChannel}.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)

View File

@ -22,36 +22,62 @@
*/
package org.jboss.netty.channel.socket.nio;
import java.nio.channels.Selector;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionException;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelPipeline;
import org.jboss.netty.channel.group.ChannelGroup;
import org.jboss.netty.channel.socket.DatagramChannel;
import org.jboss.netty.channel.socket.DatagramChannelFactory;
import org.jboss.netty.channel.socket.oio.OioDatagramChannelFactory;
import org.jboss.netty.util.internal.ExecutorUtil;
/**
* A {@link NioDatagramChannelFactory} creates a server-side NIO-based
* {@link DatagramChannel}. It utilizes the non-blocking I/O mode which
* A {@link NioDatagramChannelFactory} creates a NIO-based connectionless
* {@link DatagramChannel}. It utilizes the non-blocking I/O mode which
* was introduced with NIO to serve many number of concurrent connections
* efficiently.
*
* <h3>How threads work</h3>
* <p>
* There is only one type of thread in a {@link NioDatagramChannelFactory},
* as opposed to the {@link NioServerSocketChannelFactory} where there is one boss
* thread and a worker thread.
* The boss thread in {@link NioServerSocketChannelFactory} performs the accept of
* incoming connection and then passes the accepted Channel to on of the worker
* threads. As DatagramChannels can act as both server (listener) and client (sender)
* hence there is no concept of ServerSocketChannel and SocketChannel whic means that
* there nothing to accept accept. This is the reason that there is only worker theads.
* There is only one thread type in a {@link NioDatagramChannelFactory};
* worker threads.
*
* <h4>Worker threads</h4>
* <p>
* One {@link NioDatagramChannelFactory} can have one or more worker
* threads. A worker thread performs non-blocking read and write for one or
* more {@link Channel}s in a non-blocking mode.
* more {@link DatagramChannel}s in a non-blocking mode.
*
* <h3>Life cycle of threads and graceful shutdown</h3>
* <p>
* All worker threads are acquired from the {@link Executor} which was specified
* when a {@link NioDatagramChannelFactory} was created. Therefore, you should
* make sure the specified {@link Executor} is able to lend the sufficient
* number of threads. It is the best bet to specify
* {@linkplain Executors#newCachedThreadPool() a cached thread pool}.
* <p>
* All worker threads are acquired lazily, and then released when there's
* nothing left to process. All the related resources such as {@link Selector}
* are also released when the worker threads are released. Therefore, to shut
* down a service gracefully, you should do the following:
*
* <ol>
* <li>close all channels created by the factory usually using
* {@link ChannelGroup#close()}, and</li>
* <li>call {@link #releaseExternalResources()}.</li>
* </ol>
*
* Please make sure not to shut down the executor until all channels are
* closed. Otherwise, you will end up with a {@link RejectedExecutionException}
* and the related resources might not be released properly.
*
* <h3>Limitation</h3>
* <p>
* Multicast is not supported. Please use {@link OioDatagramChannelFactory}
* instead.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
@ -60,19 +86,18 @@ import org.jboss.netty.util.internal.ExecutorUtil;
* @version $Rev$, $Date$
*/
public class NioDatagramChannelFactory implements DatagramChannelFactory {
/**
*
*/
private final Executor workerExecutor;
/**
*
*/
private final Executor workerExecutor;
private final NioDatagramPipelineSink sink;
/**
* Creates a new instance. Calling this constructor is same with calling
* {@link #NioDatagramChannelFactory(Executor, int)} with the number of
* available processors in the machine. The number of available processors
* is obtained by {@link Runtime#availableProcessors()}.
*
* @param workerExecutor the {@link Executor} which will execute the I/O worker threads
* @param workerExecutor
* the {@link Executor} which will execute the I/O worker threads
*/
public NioDatagramChannelFactory(final Executor workerExecutor) {
this(workerExecutor, Runtime.getRuntime().availableProcessors());
@ -81,8 +106,10 @@ public class NioDatagramChannelFactory implements DatagramChannelFactory {
/**
* Creates a new instance.
*
* @param workerExecutor the {@link Executor} which will execute the I/O worker threads
* @param workerCount the maximum number of I/O worker threads
* @param workerExecutor
* the {@link Executor} which will execute the I/O worker threads
* @param workerCount
* the maximum number of I/O worker threads
*/
public NioDatagramChannelFactory(final Executor workerExecutor,
final int workerCount) {

View File

@ -39,9 +39,8 @@ import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.MessageEvent;
/**
* NioDatagramPipelineSink receives downstream events from a ChannelPipeline.
* <p/>
* A {@link NioDatagramPipelineSink} contains an array of {@link NioDatagramWorker}s
* Receives downstream events from a {@link ChannelPipeline}. It contains
* an array of I/O workers.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)

View File

@ -57,8 +57,8 @@ import org.jboss.netty.util.ThreadRenamingRunnable;
import org.jboss.netty.util.internal.LinkedTransferQueue;
/**
* NioDatagramWorker is responsible for registering channels with selector, and
* also manages the select process.
* A class responsible for registering channels with {@link Selector}.
* It also implements the {@link Selector} loop.
*
* @author The Netty Project (netty-dev@lists.jboss.org)
* @author Trustin Lee (tlee@redhat.com)
@ -84,7 +84,8 @@ class NioDatagramWorker implements Runnable {
private final int bossId;
/**
* Executor used to exeucte runnables such as {@link ChannelRegistionTask}.
* Executor used to execute {@link Runnable}s such as
* {@link ChannelRegistionTask}.
*/
private final Executor executor;
@ -108,7 +109,7 @@ class NioDatagramWorker implements Runnable {
* Boolean that controls determines if a blocked Selector.select should
* break out of its selection process. In our case we use a timeone for
* the select method and the select method will block for that time unless
* woken up.
* waken up.
*/
private final AtomicBoolean wakenUp = new AtomicBoolean();
@ -135,9 +136,10 @@ class NioDatagramWorker implements Runnable {
/**
* Sole constructor.
*
* @param bossId This id of the NioDatagramPipelineSink.
* @param id The id of this worker.
* @param executor Executor used to exeucte runnables such as {@link ChannelRegistionTask}.
* @param bossId This id of the NioDatagramPipelineSink
* @param id The id of this worker
* @param executor the {@link Executor} used to execute {@link Runnable}s
* such as {@link ChannelRegistionTask}
*/
NioDatagramWorker(final int bossId, final int id, final Executor executor) {
this.bossId = bossId;