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.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  public class FactorialServerHandler extends SimpleChannelUpstreamHandler {
38  
39      private static final Logger logger = Logger.getLogger(
40              FactorialServerHandler.class.getName());
41  
42      // Stateful properties.
43      private int lastMultiplier = 1;
44      private BigInteger factorial = new BigInteger(new byte[] { 1 });
45  
46      @Override
47      public void handleUpstream(
48              ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
49          if (e instanceof ChannelStateEvent) {
50              logger.info(e.toString());
51          }
52          super.handleUpstream(ctx, e);
53      }
54  
55      @Override
56      public void messageReceived(
57              ChannelHandlerContext ctx, MessageEvent e) {
58  
59          // Calculate the cumulative factorial and send it to the client.
60          BigInteger number;
61          if (e.getMessage() instanceof BigInteger) {
62              number = (BigInteger) e.getMessage();
63          } else {
64              number = new BigInteger(e.getMessage().toString());
65          }
66          lastMultiplier = number.intValue();
67          factorial = factorial.multiply(number);
68          e.getChannel().write(factorial);
69      }
70  
71      @Override
72      public void channelDisconnected(ChannelHandlerContext ctx,
73              ChannelStateEvent e) throws Exception {
74          logger.info(new Formatter().format(
75                  "Factorial of %,d is: %,d", lastMultiplier, factorial).toString());
76      }
77  
78      @Override
79      public void exceptionCaught(
80              ChannelHandlerContext ctx, ExceptionEvent e) {
81          logger.log(
82                  Level.WARNING,
83                  "Unexpected exception from downstream.",
84                  e.getCause());
85          e.getChannel().close();
86      }
87  }