Search in sources :

Example 1 with ExecMessage

use of com.marcnuri.yakc.api.ExecMessage in project yakc by manusa.

the class ExecOnSubscribe method subscribe.

@Override
public void subscribe(ObservableEmitter<ExecMessage> emitter) throws Exception {
    emitter.setDisposable(this);
    final CountDownLatch cdl = new CountDownLatch(1);
    final HttpUrl updatedUrl = request.url().newBuilder().addQueryParameter("stdout", "true").addQueryParameter("stderr", "true").build();
    final Request updatedRequest = request.newBuilder().url(updatedUrl).build();
    webSocket.set(kubernetesClient.getOkHttpClient().newWebSocket(updatedRequest, new WebSocketListener() {

        @Override
        public void onMessage(WebSocket webSocket, String text) {
            emitter.onNext(ExecMessage.builder().standardStream(StandardStream.STDOUT).message(text).build());
        }

        @Override
        public void onMessage(WebSocket webSocket, ByteString bytes) {
            emitter.onNext(ExecMessage.builder().standardStream(StandardStream.fromByte(bytes.getByte(0))).message(bytes.substring(1).utf8()).build());
        }

        @Override
        public void onClosing(WebSocket webSocket, int code, String reason) {
            dispose();
        }

        @Override
        public void onClosed(WebSocket webSocket, int code, String reason) {
            emitter.onComplete();
            close();
        }

        @Override
        public void onFailure(WebSocket webSocket, Throwable t, Response response) {
            final String message = Optional.ofNullable(response).map(Response::body).map(rb -> {
                try {
                    return rb.string();
                } catch (IOException ex) {
                    return null;
                }
            }).orElse(t.getMessage());
            emitter.onError(new KubernetesException(message, response));
            close();
        }

        private void close() {
            disposed.set(true);
            cdl.countDown();
        }
    }));
    cdl.await();
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) ObservableOnSubscribe(io.reactivex.ObservableOnSubscribe) ObservableEmitter(io.reactivex.ObservableEmitter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) KubernetesClient(com.marcnuri.yakc.KubernetesClient) WebSocket(okhttp3.WebSocket) WebSocketListener(okhttp3.WebSocketListener) IOException(java.io.IOException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Disposable(io.reactivex.disposables.Disposable) ByteString(okio.ByteString) Response(okhttp3.Response) Optional(java.util.Optional) ExecMessage(com.marcnuri.yakc.api.ExecMessage) KubernetesException(com.marcnuri.yakc.api.KubernetesException) HttpUrl(okhttp3.HttpUrl) StandardStream(com.marcnuri.yakc.api.ExecMessage.StandardStream) WebSocketListener(okhttp3.WebSocketListener) ByteString(okio.ByteString) Request(okhttp3.Request) ByteString(okio.ByteString) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) HttpUrl(okhttp3.HttpUrl) KubernetesException(com.marcnuri.yakc.api.KubernetesException) WebSocket(okhttp3.WebSocket)

Aggregations

KubernetesClient (com.marcnuri.yakc.KubernetesClient)1 ExecMessage (com.marcnuri.yakc.api.ExecMessage)1 StandardStream (com.marcnuri.yakc.api.ExecMessage.StandardStream)1 KubernetesException (com.marcnuri.yakc.api.KubernetesException)1 ObservableEmitter (io.reactivex.ObservableEmitter)1 ObservableOnSubscribe (io.reactivex.ObservableOnSubscribe)1 Disposable (io.reactivex.disposables.Disposable)1 IOException (java.io.IOException)1 Optional (java.util.Optional)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 HttpUrl (okhttp3.HttpUrl)1 Request (okhttp3.Request)1 Response (okhttp3.Response)1 WebSocket (okhttp3.WebSocket)1 WebSocketListener (okhttp3.WebSocketListener)1 ByteString (okio.ByteString)1