1 /* 2 * Copyright 2009 Red Hat, Inc. 3 * 4 * Red Hat licenses this file to you under the Apache License, version 2.0 5 * (the "License"); you may not use this file except in compliance with the 6 * 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 import java.util.concurrent.Executors; 20 21 import org.jboss.netty.bootstrap.ConnectionlessBootstrap; 22 import org.jboss.netty.channel.ChannelPipeline; 23 import org.jboss.netty.channel.ChannelPipelineFactory; 24 import org.jboss.netty.channel.Channels; 25 import org.jboss.netty.channel.FixedReceiveBufferSizePredictorFactory; 26 import org.jboss.netty.channel.socket.DatagramChannelFactory; 27 import org.jboss.netty.channel.socket.nio.NioDatagramChannelFactory; 28 import org.jboss.netty.handler.codec.string.StringDecoder; 29 import org.jboss.netty.handler.codec.string.StringEncoder; 30 import org.jboss.netty.util.CharsetUtil; 31 32 /** 33 * A UDP server that responds to the QOTM (quote of the moment) request to a 34 * {@link QuoteOfTheMomentClient}. 35 * 36 * Inspired by <a href="http://java.sun.com/docs/books/tutorial/networking/datagrams/clientServer.html">the official Java tutorial</a>. 37 * 38 * @author <a href="http://www.jboss.org/netty/">The Netty Project</a> 39 * @author <a href="http://gleamynode.net/">Trustin Lee</a> 40 * @version $Rev: 2104 $, $Date: 2010-01-28 16:50:28 +0900 (Thu, 28 Jan 2010) $ 41 */ 42 public class QuoteOfTheMomentServer { 43 44 public static void main(String[] args) throws Exception { 45 DatagramChannelFactory f = 46 new NioDatagramChannelFactory(Executors.newCachedThreadPool()); 47 48 ConnectionlessBootstrap b = new ConnectionlessBootstrap(f); 49 50 // Configure the pipeline factory. 51 b.setPipelineFactory(new ChannelPipelineFactory() { 52 public ChannelPipeline getPipeline() throws Exception { 53 return Channels.pipeline( 54 new StringEncoder(CharsetUtil.ISO_8859_1), 55 new StringDecoder(CharsetUtil.ISO_8859_1), 56 new QuoteOfTheMomentServerHandler()); 57 } 58 }); 59 60 // Enable broadcast 61 b.setOption("broadcast", "false"); 62 63 // Allow packets as large as up to 1024 bytes (default is 768). 64 // You could increase or decrease this value to avoid truncated packets 65 // or to improve memory footprint respectively. 66 // 67 // Please also note that a large UDP packet might be truncated or 68 // dropped by your router no matter how you configured this option. 69 // In UDP, a packet is truncated or dropped if it is larger than a 70 // certain size, depending on router configuration. IPv4 routers 71 // truncate and IPv6 routers drop a large packet. That's why it is 72 // safe to send small packets in UDP. 73 b.setOption( 74 "receiveBufferSizePredictorFactory", 75 new FixedReceiveBufferSizePredictorFactory(1024)); 76 77 // Bind to the port and start the service. 78 b.bind(new InetSocketAddress(8080)); 79 } 80 }