org.jboss.netty.bootstrap
Class ConnectionlessBootstrap

java.lang.Object
  extended by org.jboss.netty.bootstrap.Bootstrap
      extended by org.jboss.netty.bootstrap.ConnectionlessBootstrap
All Implemented Interfaces:
ExternalResourceReleasable

public class ConnectionlessBootstrap
extends Bootstrap

A helper class which creates a new server-side Channel for a connectionless transport.

Only for connectionless transports

This bootstrap is for connectionless transports only such as UDP/IP. Use ServerBootstrap instead for connection oriented transports. Do not use this helper if you are using a connection oriented transport such as TCP/IP and local transport which accepts an incoming connection and lets the accepted child channels handle received messages.

Configuring channels

Options are used to configure a channel:
 ConnectionlessBootstrap b = ...;

 // Options for a new channel
 b.setOption("localAddress", new InetSocketAddress(8080));
 b.setOption("tcpNoDelay", true);
 b.setOption("receiveBufferSize", 1048576);
 
For the detailed list of available options, please refer to ChannelConfig and its sub-types

Configuring a channel pipeline

Every channel has its own ChannelPipeline and you can configure it in two ways.

The first approach is to use the default pipeline and let the bootstrap to shallow-copy the default pipeline for each new channel:

 ConnectionlessBootstrap b = ...;
 ChannelPipeline p = b.getPipeline();

 // Add handlers to the pipeline.
 p.addLast("encoder", new EncodingHandler());
 p.addLast("decoder", new DecodingHandler());
 p.addLast("logic",   new LogicHandler());
 
Please note 'shallow-copy' here means that the added ChannelHandlers are not cloned but only their references are added to the new pipeline. Therefore, you have to choose the second approach if you are going to open more than one Channel whose ChannelPipeline contains any ChannelHandler whose ChannelPipelineCoverage is "one".

The second approach is to specify a ChannelPipelineFactory by yourself and have full control over how a new pipeline is created. This approach is more complex than the first approach while it is much more flexible:

 ConnectionlessBootstrap b = ...;
 b.setPipelineFactory(new MyPipelineFactory());

 public class MyPipelineFactory implements ChannelPipelineFactory {
   // Create a new pipeline for a new channel and configure it here ...
 }
 

Applying different settings for different Channels

ConnectionlessBootstrap is just a helper class. It neither allocates nor manages any resources. What manages the resources is the ChannelFactory implementation you specified in the constructor of ConnectionlessBootstrap. Therefore, it is OK to create as many ConnectionlessBootstrap instances as you want to apply different settings for different Channels.

Version:
$Rev: 1714 $, $Date: 2009-09-04 13:21:56 +0900 (금, 04 9 2009) $
Author:
The Netty Project (netty-dev@lists.jboss.org), Trustin Lee (tlee@redhat.com)

Constructor Summary
ConnectionlessBootstrap()
          Creates a new instance with no ChannelFactory set.
ConnectionlessBootstrap(ChannelFactory channelFactory)
          Creates a new instance with the specified initial ChannelFactory.
 
Method Summary
 Channel bind()
          Creates a new channel which is bound to the local address which was specified in the current "localAddress" option.
 Channel bind(SocketAddress localAddress)
          Creates a new channel which is bound to the specified local address.
 ChannelFuture connect()
          Creates a new connected channel with the current "remoteAddress" and "localAddress" option.
 ChannelFuture connect(SocketAddress remoteAddress)
          Creates a new connected channel with the specified "remoteAddress" and the current "localAddress" option.
 ChannelFuture connect(SocketAddress remoteAddress, SocketAddress localAddress)
          Creates a new connected channel with the specified "remoteAddress" and the specified "localAddress".
 
Methods inherited from class org.jboss.netty.bootstrap.Bootstrap
getFactory, getOption, getOptions, getPipeline, getPipelineAsMap, getPipelineFactory, releaseExternalResources, setFactory, setOption, setOptions, setPipeline, setPipelineAsMap, setPipelineFactory
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ConnectionlessBootstrap

public ConnectionlessBootstrap()
Creates a new instance with no ChannelFactory set. Bootstrap.setFactory(ChannelFactory) must be called before any I/O operation is requested.


ConnectionlessBootstrap

public ConnectionlessBootstrap(ChannelFactory channelFactory)
Creates a new instance with the specified initial ChannelFactory.

Method Detail

bind

public Channel bind()
Creates a new channel which is bound to the local address which was specified in the current "localAddress" option. This method is similar to the following code:
 ServerBootstrap b = ...;
 b.connect(b.getOption("localAddress"));
 

Returns:
a new bound channel which accepts incoming connections
Throws:
IllegalStateException - if "localAddress" option was not set
ClassCastException - if "localAddress" option's value is neither a SocketAddress nor null
ChannelException - if failed to create a new channel and bind it to the local address

bind

public Channel bind(SocketAddress localAddress)
Creates a new channel which is bound to the specified local address.

Returns:
a new bound channel which accepts incoming connections
Throws:
ChannelException - if failed to create a new channel and bind it to the local address

connect

public ChannelFuture connect()
Creates a new connected channel with the current "remoteAddress" and "localAddress" option. If the "localAddress" option is not set, the local address of a new channel is determined automatically. This method is similar to the following code:
 ConnectionlessBootstrap b = ...;
 b.connect(b.getOption("remoteAddress"), b.getOption("localAddress"));
 

Returns:
a future object which notifies when the creation of the connected channel succeeds or fails
Throws:
IllegalStateException - if "remoteAddress" option was not set
ClassCastException - if "remoteAddress" or "localAddress" option's value is neither a SocketAddress nor null
ChannelPipelineException - if this bootstrap's pipelineFactory failed to create a new ChannelPipeline

connect

public ChannelFuture connect(SocketAddress remoteAddress)
Creates a new connected channel with the specified "remoteAddress" and the current "localAddress" option. If the "localAddress" option is not set, the local address of a new channel is determined automatically. This method is identical with the following code:
 ClientBootstrap b = ...;
 b.connect(remoteAddress, b.getOption("localAddress"));
 

Returns:
a future object which notifies when the creation of the connected channel succeeds or fails
Throws:
ClassCastException - if "localAddress" option's value is neither a SocketAddress nor null
ChannelPipelineException - if this bootstrap's pipelineFactory failed to create a new ChannelPipeline

connect

public ChannelFuture connect(SocketAddress remoteAddress,
                             SocketAddress localAddress)
Creates a new connected channel with the specified "remoteAddress" and the specified "localAddress". If the specified local address is null, the local address of a new channel is determined automatically.

Returns:
a future object which notifies when the creation of the connected channel succeeds or fails
Throws:
ChannelPipelineException - if this bootstrap's pipelineFactory failed to create a new ChannelPipeline


Copyright © 2008-2009 JBoss, by Red Hat. All Rights Reserved.