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 org.jboss.netty.channel.ChannelEvent;
19  import org.jboss.netty.channel.ChannelHandlerContext;
20  import org.jboss.netty.channel.ChannelStateEvent;
21  import org.jboss.netty.channel.ExceptionEvent;
22  import org.jboss.netty.channel.MessageEvent;
23  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
24  
25  import java.math.BigInteger;
26  import java.util.Formatter;
27  
28  /**
29   * Handler for a server-side channel.  This handler maintains stateful
30   * information which is specific to a certain channel using member variables.
31   * Therefore, an instance of this handler can cover only one channel.  You have
32   * to create a new handler instance whenever you create a new channel and insert
33   * this handler  to avoid a race condition.
34   */
35  public class FactorialServerHandler extends SimpleChannelUpstreamHandler {
36  
37      // Stateful properties.
38      private int lastMultiplier = 1;
39      private BigInteger factorial = new BigInteger(new byte[] { 1 });
40  
41      @Override
42      public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
43          if (e instanceof ChannelStateEvent) {
44              System.err.println(e);
45          }
46          super.handleUpstream(ctx, e);
47      }
48  
49      @Override
50      public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) {
51          // Calculate the cumulative factorial and send it to the client.
52          BigInteger number;
53          if (e.getMessage() instanceof BigInteger) {
54              number = (BigInteger) e.getMessage();
55          } else {
56              number = new BigInteger(e.getMessage().toString());
57          }
58          lastMultiplier = number.intValue();
59          factorial = factorial.multiply(number);
60          e.getChannel().write(factorial);
61      }
62  
63      @Override
64      public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
65          System.err.println(new Formatter().format("Factorial of %,d is: %,d", lastMultiplier, factorial));
66      }
67  
68      @Override
69      public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
70          e.getCause().printStackTrace();
71          e.getChannel().close();
72      }
73  }