1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
42
43
44
45
46 public class LocalTimeClientHandler extends SimpleChannelUpstreamHandler {
47
48 private static final Logger logger = Logger.getLogger(
49 LocalTimeClientHandler.class.getName());
50
51
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 }