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
38
39
40
41
42 public class UptimeClient {
43
44
45 static final int RECONNECT_DELAY = 5;
46
47
48 private static final int READ_TIMEOUT = 10;
49
50 public static void main(String[] args) throws Exception {
51
52 if (args.length != 2) {
53 System.err.println(
54 "Usage: " + UptimeClient.class.getSimpleName() +
55 " <host> <port>");
56 return;
57 }
58
59
60 String host = args[0];
61 int port = Integer.parseInt(args[1]);
62
63
64 final Timer timer = new HashedWheelTimer();
65
66
67 final ClientBootstrap bootstrap = new ClientBootstrap(
68 new NioClientSocketChannelFactory(
69 Executors.newCachedThreadPool(),
70 Executors.newCachedThreadPool()));
71
72
73 bootstrap.setPipelineFactory(new ChannelPipelineFactory() {
74
75 private final ChannelHandler timeoutHandler =
76 new ReadTimeoutHandler(timer, READ_TIMEOUT);
77 private final ChannelHandler uptimeHandler =
78 new UptimeClientHandler(bootstrap, timer);
79
80 public ChannelPipeline getPipeline() throws Exception {
81 return Channels.pipeline(
82 timeoutHandler, uptimeHandler);
83 }
84 });
85
86 bootstrap.setOption(
87 "remoteAddress", new InetSocketAddress(host, port));
88
89
90
91 bootstrap.connect();
92 }
93 }