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 java.net.InetSocketAddress;
19
20 import org.jboss.netty.bootstrap.ConnectionlessBootstrap;
21 import org.jboss.netty.channel.ChannelPipeline;
22 import org.jboss.netty.channel.ChannelPipelineFactory;
23 import org.jboss.netty.channel.Channels;
24 import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory;
25 import org.jboss.netty.channel.socket.DatagramChannelFactory;
26 import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
27 import org.jboss.netty.handler.codec.string.StringDecoder;
28 import org.jboss.netty.handler.codec.string.StringEncoder;
29 import org.jboss.netty.util.CharsetUtil;
30
31 /**
32 * A UDP server that responds to the QOTM (quote of the moment) request to a
33 * {@link QuoteOfTheMomentClient}.
34 *
35 * Inspired by <a href="http://goo.gl/BsXVR">the official Java tutorial</a>.
36 */
37 public class QuoteOfTheMomentServer {
38
39 private final int port;
40
41 public QuoteOfTheMomentServer(int port) {
42 this.port = port;
43 }
44
45 public void run() {
46 DatagramChannelFactory f = new NioDatagramChannelFactory();
47 ConnectionlessBootstrap b = new ConnectionlessBootstrap(f);
48
49 // Configure the pipeline factory.
50 b.setPipelineFactory(new ChannelPipelineFactory() {
51 public ChannelPipeline getPipeline() throws Exception {
52 return Channels.pipeline(
53 new StringEncoder(CharsetUtil.ISO_8859_1),
54 new StringDecoder(CharsetUtil.ISO_8859_1),
55 new QuoteOfTheMomentServerHandler());
56 }
57 });
58
59 // Enable broadcast
60 b.setOption("broadcast", "false");
61
62 // Allow packets as large as up to 1024 bytes (default is 768).
63 // You could increase or decrease this value to avoid truncated packets
64 // or to improve memory footprint respectively.
65 //
66 // Please also note that a large UDP packet might be truncated or
67 // dropped by your router no matter how you configured this option.
68 // In UDP, a packet is truncated or dropped if it is larger than a
69 // certain size, depending on router configuration. IPv4 routers
70 // truncate and IPv6 routers drop a large packet. That's why it is
71 // safe to send small packets in UDP.
72 b.setOption(
73 "receiveBufferSizePredictorFactory",
74 new FixedReceiveBufferSizePredictorFactory(1024));
75
76 // Bind to the port and start the service.
77 b.bind(new InetSocketAddress(port));
78 }
79
80 public static void main(String[] args) throws Exception {
81 int port;
82 if (args.length > 0) {
83 port = Integer.parseInt(args[0]);
84 } else {
85 port = 8080;
86 }
87 new QuoteOfTheMomentServer(port).run();
88 }
89 }