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.localtime;
    17
    18  import java.util.ArrayList;
    19  import java.util.Collection;
    20  import java.util.Formatter;
    21  import java.util.List;
    22  import java.util.concurrent.BlockingQueue;
    23  import java.util.concurrent.LinkedBlockingQueue;
    24  import java.util.logging.Level;
    25  import java.util.logging.Logger;
    26
    27  import org.jboss.netty.channel.Channel;
    28  import org.jboss.netty.channel.ChannelEvent;
    29  import org.jboss.netty.channel.ChannelHandlerContext;
    30  import org.jboss.netty.channel.ChannelStateEvent;
    31  import org.jboss.netty.channel.ExceptionEvent;
    32  import org.jboss.netty.channel.MessageEvent;
    33  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
    34  import org.jboss.netty.example.localtime.LocalTimeProtocol.Continent;
    35  import org.jboss.netty.example.localtime.LocalTimeProtocol.LocalTime;
    36  import org.jboss.netty.example.localtime.LocalTimeProtocol.LocalTimes;
    37  import org.jboss.netty.example.localtime.LocalTimeProtocol.Location;
    38  import org.jboss.netty.example.localtime.LocalTimeProtocol.Locations;
    39
    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: 2190 $, $Date: 2010-02-19 18:08:01 +0900 (Fri, 19 Feb 2010) $
    45   */
    46  public class LocalTimeClientHandler extends SimpleChannelUpstreamHandler {
    47
    48      private static final Logger logger = Logger.getLogger(
    49              LocalTimeClientHandler.class.getName());
    50
    51      // Stateful properties
    52      private volatile Channel channel;
    53      private final BlockingQueue<LocalTimes> answer = new LinkedBlockingQueue<LocalTimes>();
    54
    55      public List<String> getLocalTimes(Collection<String> cities) {
    56          Locations.Builder builder = Locations.newBuilder();
    57
    58          for (String c: cities) {
    59              String[] components = c.split("/");
    60              builder.addLocation(Location.newBuilder().
    61                  setContinent(Continent.valueOf(components[0].toUpperCase())).
    62                  setCity(components[1]).build());
    63          }
    64
    65          channel.write(builder.build());
    66
    67          LocalTimes localTimes;
    68          boolean interrupted = false;
    69          for (;;) {
    70              try {
    71                  localTimes = answer.take();
    72                  break;
    73              } catch (InterruptedException e) {
    74                  interrupted = true;
    75              }
    76          }
    77
    78          if (interrupted) {
    79              Thread.currentThread().interrupt();
    80          }
    81
    82          List<String> result = new ArrayList<String>();
    83          for (LocalTime lt: localTimes.getLocalTimeList()) {
    84              result.add(
    85                      new Formatter().format(
    86                              "%4d-%02d-%02d %02d:%02d:%02d %s",
    87                              lt.getYear(),
    88                              lt.getMonth(),
    89                              lt.getDayOfMonth(),
    90                              lt.getHour(),
    91                              lt.getMinute(),
    92                              lt.getSecond(),
    93                              lt.getDayOfWeek().name()).toString());
    94          }
    95
    96          return result;
    97      }
    98
    99      @Override
    100     public void handleUpstream(
    101             ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
    102         if (e instanceof ChannelStateEvent) {
    103             logger.info(e.toString());
    104         }
    105         super.handleUpstream(ctx, e);
    106     }
    107
    108     @Override
    109     public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
    110             throws Exception {
    111         channel = e.getChannel();
    112         super.channelOpen(ctx, e);
    113     }
    114
    115     @Override
    116     public void messageReceived(
    117             ChannelHandlerContext ctx, final MessageEvent e) {
    118         boolean offered = answer.offer((LocalTimes) e.getMessage());
    119         assert offered;
    120     }
    121
    122     @Override
    123     public void exceptionCaught(
    124             ChannelHandlerContext ctx, ExceptionEvent e) {
    125         logger.log(
    126                 Level.WARNING,
    127                 "Unexpected exception from downstream.",
    128                 e.getCause());
    129         e.getChannel().close();
    130     }
    131 }