|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.opamp.client.internal.connectivity.websocket; |
| 7 | + |
| 8 | +import java.util.Objects; |
| 9 | +import java.util.concurrent.atomic.AtomicReference; |
| 10 | +import javax.annotation.Nonnull; |
| 11 | +import javax.annotation.Nullable; |
| 12 | +import okhttp3.OkHttpClient; |
| 13 | +import okhttp3.Response; |
| 14 | +import okhttp3.WebSocketListener; |
| 15 | +import okio.ByteString; |
| 16 | + |
| 17 | +public class OkHttpWebSocket implements WebSocket { |
| 18 | + private final String url; |
| 19 | + private final OkHttpClient client; |
| 20 | + private final AtomicReference<Status> status = new AtomicReference<>(Status.NOT_RUNNING); |
| 21 | + private final AtomicReference<okhttp3.WebSocket> webSocket = new AtomicReference<>(); |
| 22 | + |
| 23 | + public static OkHttpWebSocket create(String url) { |
| 24 | + return create(url, new OkHttpClient()); |
| 25 | + } |
| 26 | + |
| 27 | + public static OkHttpWebSocket create(String url, OkHttpClient client) { |
| 28 | + return new OkHttpWebSocket(url, client); |
| 29 | + } |
| 30 | + |
| 31 | + private OkHttpWebSocket(String url, OkHttpClient client) { |
| 32 | + this.url = url; |
| 33 | + this.client = client; |
| 34 | + } |
| 35 | + |
| 36 | + @Override |
| 37 | + public void open(Listener listener) { |
| 38 | + if (status.compareAndSet(Status.NOT_RUNNING, Status.STARTING)) { |
| 39 | + okhttp3.Request request = new okhttp3.Request.Builder().url(url).build(); |
| 40 | + webSocket.set(client.newWebSocket(request, new ListenerAdapter(listener))); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Override |
| 45 | + public boolean send(byte[] request) { |
| 46 | + if (status.get() != Status.RUNNING) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + return getWebSocket().send(ByteString.of(request)); |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public void close(int code, @Nullable String reason) { |
| 54 | + if (status.compareAndSet(Status.RUNNING, Status.CLOSING)) { |
| 55 | + try { |
| 56 | + if (!getWebSocket().close(code, reason)) { |
| 57 | + status.set(Status.NOT_RUNNING); |
| 58 | + } |
| 59 | + } catch (IllegalArgumentException e) { |
| 60 | + status.set(Status.RUNNING); |
| 61 | + // Re-throwing as this error happens due to a caller error. |
| 62 | + throw e; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private okhttp3.WebSocket getWebSocket() { |
| 68 | + return Objects.requireNonNull(webSocket.get()); |
| 69 | + } |
| 70 | + |
| 71 | + private class ListenerAdapter extends WebSocketListener { |
| 72 | + private final Listener delegate; |
| 73 | + |
| 74 | + private ListenerAdapter(Listener delegate) { |
| 75 | + this.delegate = delegate; |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public void onOpen(@Nonnull okhttp3.WebSocket webSocket, @Nonnull Response response) { |
| 80 | + status.set(Status.RUNNING); |
| 81 | + delegate.onOpen(); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public void onClosing(@Nonnull okhttp3.WebSocket webSocket, int code, @Nonnull String reason) { |
| 86 | + status.set(Status.CLOSING); |
| 87 | + delegate.onClosing(); |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void onClosed(@Nonnull okhttp3.WebSocket webSocket, int code, @Nonnull String reason) { |
| 92 | + status.set(Status.NOT_RUNNING); |
| 93 | + delegate.onClosed(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public void onFailure( |
| 98 | + @Nonnull okhttp3.WebSocket webSocket, @Nonnull Throwable t, @Nullable Response response) { |
| 99 | + status.set(Status.NOT_RUNNING); |
| 100 | + delegate.onFailure(t); |
| 101 | + } |
| 102 | + |
| 103 | + @Override |
| 104 | + public void onMessage(@Nonnull okhttp3.WebSocket webSocket, @Nonnull ByteString bytes) { |
| 105 | + delegate.onMessage(bytes.toByteArray()); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + enum Status { |
| 110 | + NOT_RUNNING, |
| 111 | + STARTING, |
| 112 | + CLOSING, |
| 113 | + RUNNING |
| 114 | + } |
| 115 | +} |
0 commit comments