View Javadoc

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.factorial;
17  
18  import java.math.BigInteger;
19  import java.util.Formatter;
20  import java.util.logging.Level;
21  import java.util.logging.Logger;
22  
23  import org.jboss.netty.channel.ChannelEvent;
24  import org.jboss.netty.channel.ChannelHandlerContext;
25  import org.jboss.netty.channel.ChannelStateEvent;
26  import org.jboss.netty.channel.ExceptionEvent;
27  import org.jboss.netty.channel.MessageEvent;
28  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
29  
30  /**
31   * Handler for a server-side channel.  This handler maintains stateful
32   * information which is specific to a certain channel using member variables.
33   * Therefore, an instance of this handler can cover only one channel.  You have
34   * to create a new handler instance whenever you create a new channel and insert
35   * this handler  to avoid a race condition.
36   *
37   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
38   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
39   *
40   * @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
41   */
42  public class FactorialServerHandler extends SimpleChannelUpstreamHandler {
43  
44      private static final Logger logger = Logger.getLogger(
45              FactorialServerHandler.class.getName());
46  
47      // Stateful properties.
48      private int lastMultiplier = 1;
49      private BigInteger factorial = new BigInteger(new byte[] { 1 });
50  
51      @Override
52      public void handleUpstream(
53              ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
54          if (e instanceof ChannelStateEvent) {
55              logger.info(e.toString());
56          }
57          super.handleUpstream(ctx, e);
58      }
59  
60      @Override
61      public void messageReceived(
62              ChannelHandlerContext ctx, MessageEvent e) {
63  
64          // Calculate the cumulative factorial and send it to the client.
65          BigInteger number;
66          if (e.getMessage() instanceof BigInteger) {
67              number = (BigInteger) e.getMessage();
68          } else {
69              number = new BigInteger(e.getMessage().toString());
70          }
71          lastMultiplier = number.intValue();
72          factorial = factorial.multiply(number);
73          e.getChannel().write(factorial);
74      }
75  
76      @Override
77      public void channelDisconnected(ChannelHandlerContext ctx,
78              ChannelStateEvent e) throws Exception {
79          logger.info(new Formatter().format(
80                  "Factorial of %,d is: %,d", lastMultiplier, factorial).toString());
81      }
82  
83      @Override
84      public void exceptionCaught(
85              ChannelHandlerContext ctx, ExceptionEvent e) {
86          logger.log(
87                  Level.WARNING,
88                  "Unexpected exception from downstream.",
89                  e.getCause());
90          e.getChannel().close();
91      }
92  }