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.concurrent.BlockingQueue;
20  import java.util.concurrent.LinkedBlockingQueue;
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  import org.jboss.netty.channel.Channel;
25  import org.jboss.netty.channel.ChannelEvent;
26  import org.jboss.netty.channel.ChannelFuture;
27  import org.jboss.netty.channel.ChannelFutureListener;
28  import org.jboss.netty.channel.ChannelHandlerContext;
29  import org.jboss.netty.channel.ChannelStateEvent;
30  import org.jboss.netty.channel.ExceptionEvent;
31  import org.jboss.netty.channel.MessageEvent;
32  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
33  
34  /**
35   * Handler for a client-side channel.  This handler maintains stateful
36   * information which is specific to a certain channel using member variables.
37   * Therefore, an instance of this handler can cover only one channel.  You have
38   * to create a new handler instance whenever you create a new channel and insert
39   * this handler to avoid a race condition.
40   *
41   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
42   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
43   *
44   * @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
45   */
46  public class FactorialClientHandler extends SimpleChannelUpstreamHandler {
47  
48      private static final Logger logger = Logger.getLogger(
49              FactorialClientHandler.class.getName());
50  
51      // Stateful properties
52      private int i = 1;
53      private int receivedMessages = 0;
54      private final int count;
55      final BlockingQueue<BigInteger> answer = new LinkedBlockingQueue<BigInteger>();
56  
57      public FactorialClientHandler(int count) {
58          this.count = count;
59      }
60  
61      public BigInteger getFactorial() {
62          boolean interrupted = false;
63          for (;;) {
64              try {
65                  BigInteger factorial = answer.take();
66                  if (interrupted) {
67                      Thread.currentThread().interrupt();
68                  }
69                  return factorial;
70              } catch (InterruptedException e) {
71                  interrupted = true;
72              }
73          }
74      }
75  
76      @Override
77      public void handleUpstream(
78              ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
79          if (e instanceof ChannelStateEvent) {
80              logger.info(e.toString());
81          }
82          super.handleUpstream(ctx, e);
83      }
84  
85      @Override
86      public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) {
87          sendNumbers(e);
88      }
89  
90      @Override
91      public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) {
92          sendNumbers(e);
93      }
94  
95      @Override
96      public void messageReceived(
97              ChannelHandlerContext ctx, final MessageEvent e) {
98          receivedMessages ++;
99          if (receivedMessages == count) {
100             // Offer the answer after closing the connection.
101             e.getChannel().close().addListener(new ChannelFutureListener() {
102                 public void operationComplete(ChannelFuture future) {
103                     boolean offered = answer.offer((BigInteger) e.getMessage());
104                     assert offered;
105                 }
106             });
107         }
108     }
109 
110     @Override
111     public void exceptionCaught(
112             ChannelHandlerContext ctx, ExceptionEvent e) {
113         logger.log(
114                 Level.WARNING,
115                 "Unexpected exception from downstream.",
116                 e.getCause());
117         e.getChannel().close();
118     }
119 
120     private void sendNumbers(ChannelStateEvent e) {
121         Channel channel = e.getChannel();
122         while (channel.isWritable()) {
123             if (i <= count) {
124                 channel.write(Integer.valueOf(i));
125                 i ++;
126             } else {
127                 break;
128             }
129         }
130     }
131 }