1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.example.uptime;
17
18 import java.net.InetSocketAddress;
19 import java.util.concurrent.Executors;
20
21 import org.jboss.netty.bootstrap.ClientBootstrap;
22 import org.jboss.netty.channel.ChannelHandler;
23 import org.jboss.netty.channel.ChannelPipeline;
24 import org.jboss.netty.channel.ChannelPipelineFactory;
25 import org.jboss.netty.channel.Channels;
26 import org.jboss.netty.channel.socket.nio.NioClientSocketChannelFactory;
27 import org.jboss.netty.handler.timeout.ReadTimeoutHandler;
28 import org.jboss.netty.util.HashedWheelTimer;
29 import org.jboss.netty.util.Timer;
30
31
32
33
34
35
36
37 public class UptimeClient {
38
39
40 static final int RECONNECT_DELAY = 5;
41
42
43 private static final int READ_TIMEOUT = 10;
44
45 private final String host;
46 private final int port;
47
48 public UptimeClient(String host, int port) {
49 this.host = host;
50 this.port = port;
51 }
52
53 public void run() {
54
55 final Timer timer = new HashedWheelTimer();
56
57
58 final ClientBootstrap bootstrap = new ClientBootstrap(
59 new NioClientSocketChannelFactory(
60 Executors.newCachedThreadPool(),
61 Executors.newCachedThreadPool()));
62
63
64 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
65
66 private final ChannelHandler timeoutHandler =
67 new ReadTimeoutHandler(timer, READ_TIMEOUT);
68 private final ChannelHandler uptimeHandler =
69 new UptimeClientHandler(bootstrap, timer);
70
71 public ChannelPipeline getPipeline() throws Exception {
72 return Channels.pipeline(
73 timeoutHandler, uptimeHandler);
74 }
75 });
76
77 bootstrap.setOption(
78 "remoteAddress", new InetSocketAddress(host, port));
79
80
81
82 bootstrap.connect();
83 }
84
85 public static void main(String[] args) throws Exception {
86
87 if (args.length != 2) {
88 System.err.println(
89 "Usage: " + UptimeClient.class.getSimpleName() +
90 " <host> <port>");
91 return;
92 }
93
94
95 String host = args[0];
96 int port = Integer.parseInt(args[1]);
97
98 new UptimeClient(host, port).run();
99 }
100 }