1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.example.uptime;
17
18 import io.netty5.bootstrap.Bootstrap;
19 import io.netty5.channel.ChannelInitializer;
20 import io.netty5.channel.EventLoopGroup;
21 import io.netty5.channel.MultithreadEventLoopGroup;
22 import io.netty5.channel.nio.NioHandler;
23 import io.netty5.channel.socket.SocketChannel;
24 import io.netty5.channel.socket.nio.NioSocketChannel;
25 import io.netty5.handler.timeout.IdleStateHandler;
26
27
28
29
30
31
32
33 public final class UptimeClient {
34
35 static final String HOST = System.getProperty("host", "127.0.0.1");
36 static final int PORT = Integer.parseInt(System.getProperty("port", "8080"));
37
38 static final int RECONNECT_DELAY = Integer.parseInt(System.getProperty("reconnectDelay", "5"));
39
40 private static final int READ_TIMEOUT = Integer.parseInt(System.getProperty("readTimeout", "10"));
41
42 private static final UptimeClientHandler handler = new UptimeClientHandler();
43 private static final Bootstrap bs = new Bootstrap();
44
45 public static void main(String[] args) throws Exception {
46 EventLoopGroup group = new MultithreadEventLoopGroup(NioHandler.newFactory());
47 bs.group(group)
48 .channel(NioSocketChannel.class)
49 .remoteAddress(HOST, PORT)
50 .handler(new ChannelInitializer<SocketChannel>() {
51 @Override
52 protected void initChannel(SocketChannel ch) throws Exception {
53 ch.pipeline().addLast(new IdleStateHandler(READ_TIMEOUT, 0, 0), handler);
54 }
55 });
56 bs.connect();
57 }
58
59 static void connect() {
60 bs.connect().addListener(future -> {
61 if (future.cause() != null) {
62 handler.startTime = -1;
63 handler.println("Failed to connect: " + future.cause());
64 }
65 });
66 }
67 }