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