View Javadoc

1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  package org.jboss.netty.example.qotm;
17  
18  import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
19  import org.jboss.netty.channel.ChannelPipeline;
20  import org.jboss.netty.channel.ChannelPipelineFactory;
21  import org.jboss.netty.channel.Channels;
22  import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory;
23  import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
24  import org.jboss.netty.handler.codec.string.StringDecoder;
25  import org.jboss.netty.handler.codec.string.StringEncoder;
26  import org.jboss.netty.util.CharsetUtil;
27  
28  import java.net.InetSocketAddress;
29  
30  /**
31   * A UDP server that responds to the QOTM (quote of the moment) request to a {@link QuoteOfTheMomentClient}.
32   *
33   * Inspired by <a href="http://docs.oracle.com/javase/tutorial/networking/datagrams/clientServer.html">the official
34   * Java tutorial</a>.
35   */
36  public final class QuoteOfTheMomentServer {
37  
38      private static final int PORT = Integer.parseInt(System.getProperty("port", "7686"));
39  
40      public static void main(String[] args) throws Exception {
41          ConnectionlessBootstrap b = new ConnectionlessBootstrap(new NioDatagramChannelFactory());
42  
43          // Configure the pipeline factory.
44          b.setPipelineFactory(new ChannelPipelineFactory() {
45              public ChannelPipeline getPipeline() {
46                  return Channels.pipeline(
47                          new StringEncoder(CharsetUtil.ISO_8859_1),
48                          new StringDecoder(CharsetUtil.ISO_8859_1),
49                          new QuoteOfTheMomentServerHandler());
50              }
51          });
52  
53          // Enable broadcast
54          b.setOption("broadcast", "false");
55  
56          // Allow packets as large as up to 1024 bytes (default is 768).
57          // You could increase or decrease this value to avoid truncated packets
58          // or to improve memory footprint respectively.
59          //
60          // Please also note that a large UDP packet might be truncated or
61          // dropped by your router no matter how you configured this option.
62          // In UDP, a packet is truncated or dropped if it is larger than a
63          // certain size, depending on router configuration.  IPv4 routers
64          // truncate and IPv6 routers drop a large packet.  That's why it is
65          // safe to send small packets in UDP.
66          b.setOption(
67                  "receiveBufferSizePredictorFactory",
68                  new FixedReceiveBufferSizePredictorFactory(1024));
69  
70          // Bind to the port and start the service.
71          b.bind(new InetSocketAddress(PORT));
72      }
73  }