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.regex.Pattern;
38  
39  public class LocalTimeClientHandler extends SimpleChannelUpstreamHandler {
40  
41      private static final Pattern DELIM = Pattern.compile("/");
42  
43      // Stateful properties
44      private volatile Channel channel;
45      private final BlockingQueue<LocalTimes> answer = new LinkedBlockingQueue<LocalTimes>();
46  
47      public List<String> getLocalTimes(Collection<String> cities) {
48          Locations.Builder builder = Locations.newBuilder();
49  
50          for (String c: cities) {
51              String[] components = DELIM.split(c);
52              builder.addLocation(Location.newBuilder().
53                  setContinent(Continent.valueOf(components[0].toUpperCase())).
54                  setCity(components[1]).build());
55          }
56  
57          channel.write(builder.build());
58  
59          LocalTimes localTimes;
60          boolean interrupted = false;
61          for (;;) {
62              try {
63                  localTimes = answer.take();
64                  break;
65              } catch (InterruptedException e) {
66                  interrupted = true;
67              }
68          }
69  
70          if (interrupted) {
71              Thread.currentThread().interrupt();
72          }
73  
74          List<String> result = new ArrayList<String>();
75          for (LocalTime lt: localTimes.getLocalTimeList()) {
76              result.add(
77                      new Formatter().format(
78                              "%4d-%02d-%02d %02d:%02d:%02d %s",
79                              lt.getYear(),
80                              lt.getMonth(),
81                              lt.getDayOfMonth(),
82                              lt.getHour(),
83                              lt.getMinute(),
84                              lt.getSecond(),
85                              lt.getDayOfWeek().name()).toString());
86          }
87  
88          return result;
89      }
90  
91      @Override
92      public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
93          if (e instanceof ChannelStateEvent) {
94              System.err.println(e);
95          }
96          super.handleUpstream(ctx, e);
97      }
98  
99      @Override
100     public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception {
101         channel = e.getChannel();
102         super.channelOpen(ctx, e);
103     }
104 
105     @Override
106     public void messageReceived(ChannelHandlerContext ctx, final MessageEvent e) {
107         answer.add((LocalTimes) e.getMessage());
108     }
109 
110     @Override
111     public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
112         e.getCause().printStackTrace();
113         e.getChannel().close();
114     }
115 }