]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/client/WebSocketPing.java
Make tree view more robust
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / client / WebSocketPing.java
1 package org.argeo.cms.client;
2
3 import java.math.RoundingMode;
4 import java.net.URI;
5 import java.net.http.HttpClient;
6 import java.net.http.WebSocket;
7 import java.nio.ByteBuffer;
8 import java.text.DecimalFormat;
9 import java.util.UUID;
10 import java.util.concurrent.CompletableFuture;
11 import java.util.concurrent.CompletionStage;
12 import java.util.concurrent.ExecutionException;
13
14 /** Tests connectivity to the web socket server. */
15 public class WebSocketPing implements Runnable {
16 private final static int PING_FRAME_SIZE = 125;
17 private final static DecimalFormat decimalFormat = new DecimalFormat("0.0");
18 static {
19 decimalFormat.setRoundingMode(RoundingMode.HALF_UP);
20 }
21
22 private final URI uri;
23 private final UUID uuid;
24
25 private WebSocket webSocket;
26
27 public WebSocketPing(URI uri) {
28 this.uri = uri;
29 this.uuid = UUID.randomUUID();
30 }
31
32 @Override
33 public void run() {
34 try {
35 WebSocket.Listener listener = new WebSocket.Listener() {
36
37 @Override
38 public CompletionStage<?> onPong(WebSocket webSocket, ByteBuffer message) {
39 long msb = message.getLong();
40 long lsb = message.getLong();
41 long end = System.nanoTime();
42 if (msb != uuid.getMostSignificantBits() || lsb != uuid.getLeastSignificantBits())
43 return null; // ignore
44 long begin = message.getLong();
45 double durationNs = end - begin;
46 double durationMs = durationNs / 1000000;
47 int size = message.remaining() + (3 * Long.BYTES);
48 System.out.println(
49 size + " bytes from " + uri + ": time=" + decimalFormat.format(durationMs) + " ms");
50 return null;
51 }
52
53 };
54
55 HttpClient client = HttpClient.newHttpClient();
56 CompletableFuture<WebSocket> ws = client.newWebSocketBuilder().buildAsync(uri, listener);
57 webSocket = ws.get();
58 webSocket.request(Long.MAX_VALUE);
59
60 Runtime.getRuntime().addShutdownHook(new Thread(() -> webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "")));
61
62 while (!webSocket.isInputClosed()) {
63 long begin = System.nanoTime();
64 ByteBuffer buffer = ByteBuffer.allocate(PING_FRAME_SIZE);
65 buffer.putLong(uuid.getMostSignificantBits());
66 buffer.putLong(uuid.getLeastSignificantBits());
67 buffer.putLong(begin);
68 buffer.flip();
69 webSocket.sendPing(buffer);
70 Thread.sleep(1000);
71 }
72 } catch (InterruptedException e) {
73 if (webSocket != null)
74 webSocket.sendClose(WebSocket.NORMAL_CLOSURE, "");
75 } catch (ExecutionException e) {
76 throw new RuntimeException("Cannot ping " + uri, e.getCause());
77 }
78 }
79
80 // public static void main(String[] args) throws Exception {
81 // if (args.length == 0) {
82 // System.err.println("usage: java " + WsPing.class.getName() + " <url>");
83 // System.exit(1);
84 // return;
85 // }
86 // URI uri = URI.create(args[0]);
87 // new WsPing(uri).run();
88 // }
89
90 }