X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fclient%2FWebSocketPing.java;fp=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fclient%2FWebSocketPing.java;h=808c8de6897f4cfe6ecc02376725cd45c5fa87e7;hb=5b6b49fa655c7b3ae3dcc06d6c504e3d0225684f;hp=0000000000000000000000000000000000000000;hpb=fbdca9fcba285280f1d113671ef3ba7a670e45c7;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/client/WebSocketPing.java b/org.argeo.cms/src/org/argeo/cms/client/WebSocketPing.java new file mode 100644 index 000000000..808c8de68 --- /dev/null +++ b/org.argeo.cms/src/org/argeo/cms/client/WebSocketPing.java @@ -0,0 +1,90 @@ +package org.argeo.cms.client; + +import java.math.RoundingMode; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.WebSocket; +import java.nio.ByteBuffer; +import java.text.DecimalFormat; +import java.util.UUID; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; +import java.util.concurrent.ExecutionException; + +/** Tests connectivity to the web socket server. */ +public class WebSocketPing implements Runnable { + private final static int PING_FRAME_SIZE = 125; + private final static DecimalFormat decimalFormat = new DecimalFormat("0.0"); + static { + decimalFormat.setRoundingMode(RoundingMode.HALF_UP); + } + + private final URI uri; + private final UUID uuid; + + private WebSocket webSocket; + + public WebSocketPing(URI uri) { + this.uri = uri; + this.uuid = UUID.randomUUID(); + } + + @Override + public void run() { + try { + WebSocket.Listener listener = new WebSocket.Listener() { + + @Override + public CompletionStage onPong(WebSocket webSocket, ByteBuffer message) { + long msb = message.getLong(); + long lsb = message.getLong(); + long end = System.nanoTime(); + if (msb != uuid.getMostSignificantBits() || lsb != uuid.getLeastSignificantBits()) + return null; // ignore + long begin = message.getLong(); + double durationNs = end - begin; + double durationMs = durationNs / 1000000; + int size = message.remaining() + (3 * Long.BYTES); + System.out.println( + size + " bytes from " + uri + ": time=" + decimalFormat.format(durationMs) + " ms"); + return null; + } + + }; + + HttpClient client = HttpClient.newHttpClient(); + CompletableFuture ws = client.newWebSocketBuilder().buildAsync(uri, listener); + webSocket = ws.get(); + webSocket.request(Long.MAX_VALUE); + + Runtime.getRuntime().addShutdownHook(new Thread(() -> webSocket.sendClose(WebSocket.NORMAL_CLOSURE, ""))); + + while (!webSocket.isInputClosed()) { + long begin = System.nanoTime(); + ByteBuffer buffer = ByteBuffer.allocate(PING_FRAME_SIZE); + buffer.putLong(uuid.getMostSignificantBits()); + buffer.putLong(uuid.getLeastSignificantBits()); + buffer.putLong(begin); + buffer.flip(); + webSocket.sendPing(buffer); + Thread.sleep(1000); + } + } catch (InterruptedException e) { + if (webSocket != null) + webSocket.sendClose(WebSocket.NORMAL_CLOSURE, ""); + } catch (ExecutionException e) { + throw new RuntimeException("Cannot ping " + uri, e.getCause()); + } + } + +// public static void main(String[] args) throws Exception { +// if (args.length == 0) { +// System.err.println("usage: java " + WsPing.class.getName() + " "); +// System.exit(1); +// return; +// } +// URI uri = URI.create(args[0]); +// new WsPing(uri).run(); +// } + +}