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 static java.util.Calendar.*;
19  
20  import java.util.Calendar;
21  import java.util.TimeZone;
22  import java.util.logging.Level;
23  import java.util.logging.Logger;
24  
25  import org.jboss.netty.channel.ChannelEvent;
26  import org.jboss.netty.channel.ChannelHandlerContext;
27  import org.jboss.netty.channel.ChannelStateEvent;
28  import org.jboss.netty.channel.ExceptionEvent;
29  import org.jboss.netty.channel.MessageEvent;
30  import org.jboss.netty.channel.SimpleChannelUpstreamHandler;
31  import org.jboss.netty.example.localtime.LocalTimeProtocol.Continent;
32  import org.jboss.netty.example.localtime.LocalTimeProtocol.DayOfWeek;
33  import org.jboss.netty.example.localtime.LocalTimeProtocol.LocalTime;
34  import org.jboss.netty.example.localtime.LocalTimeProtocol.LocalTimes;
35  import org.jboss.netty.example.localtime.LocalTimeProtocol.Location;
36  import org.jboss.netty.example.localtime.LocalTimeProtocol.Locations;
37  
38  /**
39   * @author <a href="http://www.jboss.org/netty/">The Netty Project</a>
40   * @author <a href="http://gleamynode.net/">Trustin Lee</a>
41   *
42   * @version $Rev: 2121 $, $Date: 2010-02-02 09:38:07 +0900 (Tue, 02 Feb 2010) $
43   */
44  public class LocalTimeServerHandler extends SimpleChannelUpstreamHandler {
45  
46      private static final Logger logger = Logger.getLogger(
47              LocalTimeServerHandler.class.getName());
48  
49      @Override
50      public void handleUpstream(
51              ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
52          if (e instanceof ChannelStateEvent) {
53              logger.info(e.toString());
54          }
55          super.handleUpstream(ctx, e);
56      }
57  
58      @Override
59      public void messageReceived(
60              ChannelHandlerContext ctx, MessageEvent e) {
61  
62          Locations locations = (Locations) e.getMessage();
63          long currentTime = System.currentTimeMillis();
64  
65          LocalTimes.Builder builder = LocalTimes.newBuilder();
66          for (Location l: locations.getLocationList()) {
67              TimeZone tz = TimeZone.getTimeZone(
68                      toString(l.getContinent()) + '/' + l.getCity());
69              Calendar calendar = Calendar.getInstance(tz);
70              calendar.setTimeInMillis(currentTime);
71  
72              builder.addLocalTime(LocalTime.newBuilder().
73                      setYear(calendar.get(YEAR)).
74                      setMonth(calendar.get(MONTH) + 1).
75                      setDayOfMonth(calendar.get(DAY_OF_MONTH)).
76                      setDayOfWeek(DayOfWeek.valueOf(calendar.get(DAY_OF_WEEK))).
77                      setHour(calendar.get(HOUR_OF_DAY)).
78                      setMinute(calendar.get(MINUTE)).
79                      setSecond(calendar.get(SECOND)).build());
80          }
81  
82          e.getChannel().write(builder.build());
83      }
84  
85      @Override
86      public void exceptionCaught(
87              ChannelHandlerContext ctx, ExceptionEvent e) {
88          logger.log(
89                  Level.WARNING,
90                  "Unexpected exception from downstream.",
91                  e.getCause());
92          e.getChannel().close();
93      }
94  
95      private static String toString(Continent c) {
96          return "" + c.name().charAt(0) + c.name().toLowerCase().substring(1);
97      }
98  }