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.DatagramChannel;
24  import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory;
25  import org.jboss.netty.handler.codec.string.StringDecoder;
26  import org.jboss.netty.handler.codec.string.StringEncoder;
27  import org.jboss.netty.util.CharsetUtil;
28  
29  import java.net.InetSocketAddress;
30  import java.util.concurrent.Executors;
31  
32  /**
33   * A UDP broadcast client that asks for a quote of the moment (QOTM) to {@link QuoteOfTheMomentServer}.
34   *
35   * Inspired by <a href="http://docs.oracle.com/javase/tutorial/networking/datagrams/clientServer.html">the official
36   * Java tutorial</a>.
37   */
38  public final class QuoteOfTheMomentClient {
39  
40      static final int PORT = Integer.parseInt(System.getProperty("port", "7686"));
41  
42      public static void main(String[] args) throws Exception {
43          ConnectionlessBootstrap b = new ConnectionlessBootstrap(
44                  new NioDatagramChannelFactory(Executors.newCachedThreadPool()));
45  
46          try {
47              // Configure the pipeline factory.
48              b.setPipelineFactory(new ChannelPipelineFactory() {
49                  public ChannelPipeline getPipeline() {
50                      return Channels.pipeline(
51                              new StringEncoder(CharsetUtil.ISO_8859_1),
52                              new StringDecoder(CharsetUtil.ISO_8859_1),
53                              new QuoteOfTheMomentClientHandler());
54                  }
55              });
56  
57              // Enable broadcast
58              b.setOption("broadcast", "true");
59  
60              // Allow packets as large as up to 1024 bytes (default is 768).
61              // You could increase or decrease this value to avoid truncated packets
62              // or to improve memory footprint respectively.
63              //
64              // Please also note that a large UDP packet might be truncated or
65              // dropped by your router no matter how you configured this option.
66              // In UDP, a packet is truncated or dropped if it is larger than a
67              // certain size, depending on router configuration.  IPv4 routers
68              // truncate and IPv6 routers drop a large packet.  That's why it is
69              // safe to send small packets in UDP.
70              b.setOption(
71                      "receiveBufferSizePredictorFactory",
72                      new FixedReceiveBufferSizePredictorFactory(1024));
73  
74              DatagramChannel c = (DatagramChannel) b.bind(new InetSocketAddress(0));
75  
76              // Broadcast the QOTM request to port 8080.
77              c.write("QOTM?", new InetSocketAddress("255.255.255.255", PORT));
78  
79              // QuoteOfTheMomentClientHandler will close the DatagramChannel when a
80              // response is received.  If the channel is not closed within 5 seconds,
81              // print an error message and quit.
82              if (!c.getCloseFuture().await(5000)) {
83                  System.err.println("QOTM request timed out.");
84                  c.close().sync();
85              }
86          } finally {
87              b.releaseExternalResources();
88          }
89      }
90  }