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    *   https://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 io.netty.example.worldclock;
17  
18  import io.netty.channel.Channel;
19  import io.netty.channel.ChannelHandlerContext;
20  import io.netty.channel.SimpleChannelInboundHandler;
21  import io.netty.example.worldclock.WorldClockProtocol.Continent;
22  import io.netty.example.worldclock.WorldClockProtocol.LocalTime;
23  import io.netty.example.worldclock.WorldClockProtocol.LocalTimes;
24  import io.netty.example.worldclock.WorldClockProtocol.Location;
25  import io.netty.example.worldclock.WorldClockProtocol.Locations;
26  
27  import java.util.ArrayList;
28  import java.util.Collection;
29  import java.util.Formatter;
30  import java.util.List;
31  import java.util.concurrent.BlockingQueue;
32  import java.util.concurrent.LinkedBlockingQueue;
33  import java.util.regex.Pattern;
34  
35  public class WorldClockClientHandler extends SimpleChannelInboundHandler<LocalTimes> {
36  
37      private static final Pattern DELIM = Pattern.compile("/");
38  
39      // Stateful properties
40      private volatile Channel channel;
41      private final BlockingQueue<LocalTimes> answer = new LinkedBlockingQueue<LocalTimes>();
42  
43      public WorldClockClientHandler() {
44          super(false);
45      }
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.writeAndFlush(builder.build());
58  
59          LocalTimes localTimes;
60          boolean interrupted = false;
61          for (;;) {
62              try {
63                  localTimes = answer.take();
64                  break;
65              } catch (InterruptedException ignore) {
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 channelRegistered(ChannelHandlerContext ctx) {
93          channel = ctx.channel();
94      }
95  
96      @Override
97      public void channelRead0(ChannelHandlerContext ctx, LocalTimes times) throws Exception {
98          answer.add(times);
99      }
100 
101     @Override
102     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
103         cause.printStackTrace();
104         ctx.close();
105     }
106 }