1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
33
34
35
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
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
60 b.setOption("broadcast", "false");
61
62
63
64
65
66
67
68
69
70
71
72 b.setOption(
73 "receiveBufferSizePredictorFactory",
74 new FixedReceiveBufferSizePredictorFactory(1024));
75
76
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 }