Search in sources :

Example 1 with WebSocketMessage

use of org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage in project Signal-Android by WhisperSystems.

the class WebSocketConnection method onMessage.

@Override
public synchronized void onMessage(WebSocket webSocket, ByteString payload) {
    try {
        WebSocketMessage message = WebSocketMessage.parseFrom(payload.toByteArray());
        if (message.getType().getNumber() == WebSocketMessage.Type.REQUEST_VALUE) {
            incomingRequests.add(message.getRequest());
        } else if (message.getType().getNumber() == WebSocketMessage.Type.RESPONSE_VALUE) {
            OutgoingRequest listener = outgoingRequests.remove(message.getResponse().getId());
            if (listener != null) {
                listener.onSuccess(new WebsocketResponse(message.getResponse().getStatus(), new String(message.getResponse().getBody().toByteArray()), message.getResponse().getHeadersList(), !credentialsProvider.isPresent()));
                if (message.getResponse().getStatus() >= 400) {
                    healthMonitor.onMessageError(message.getResponse().getStatus(), credentialsProvider.isPresent());
                }
            } else if (keepAlives.remove(message.getResponse().getId())) {
                healthMonitor.onKeepAliveResponse(message.getResponse().getId(), credentialsProvider.isPresent());
            }
        }
        notifyAll();
    } catch (InvalidProtocolBufferException e) {
        warn(e);
    }
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) WebSocketMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage) ByteString(okio.ByteString)

Example 2 with WebSocketMessage

use of org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage in project Signal-Android by WhisperSystems.

the class WebSocketConnection method sendRequest.

public synchronized Single<WebsocketResponse> sendRequest(WebSocketRequestMessage request) throws IOException {
    if (client == null) {
        throw new IOException("No connection!");
    }
    WebSocketMessage message = WebSocketMessage.newBuilder().setType(WebSocketMessage.Type.REQUEST).setRequest(request).build();
    SingleSubject<WebsocketResponse> single = SingleSubject.create();
    outgoingRequests.put(request.getId(), new OutgoingRequest(single));
    if (!client.send(ByteString.of(message.toByteArray()))) {
        throw new IOException("Write failed!");
    }
    return single.subscribeOn(Schedulers.io()).observeOn(Schedulers.io()).timeout(10, TimeUnit.SECONDS, Schedulers.io());
}
Also used : IOException(java.io.IOException) WebSocketMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage)

Example 3 with WebSocketMessage

use of org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage in project libsignal-service-java by signalapp.

the class WebSocketConnection method sendRequest.

public synchronized Future<Pair<Integer, String>> sendRequest(WebSocketRequestMessage request) throws IOException {
    if (client == null || !connected)
        throw new IOException("No connection!");
    WebSocketMessage message = WebSocketMessage.newBuilder().setType(WebSocketMessage.Type.REQUEST).setRequest(request).build();
    SettableFuture<Pair<Integer, String>> future = new SettableFuture<>();
    outgoingRequests.put(request.getId(), future);
    if (!client.send(ByteString.of(message.toByteArray()))) {
        throw new IOException("Write failed!");
    }
    return future;
}
Also used : SettableFuture(org.whispersystems.signalservice.internal.util.concurrent.SettableFuture) IOException(java.io.IOException) WebSocketMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage) Pair(org.whispersystems.libsignal.util.Pair)

Example 4 with WebSocketMessage

use of org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage in project libsignal-service-java by signalapp.

the class WebSocketConnection method onMessage.

@Override
public synchronized void onMessage(WebSocket webSocket, ByteString payload) {
    Log.w(TAG, "WSC onMessage()");
    try {
        WebSocketMessage message = WebSocketMessage.parseFrom(payload.toByteArray());
        Log.w(TAG, "Message Type: " + message.getType().getNumber());
        if (message.getType().getNumber() == WebSocketMessage.Type.REQUEST_VALUE) {
            incomingRequests.add(message.getRequest());
        } else if (message.getType().getNumber() == WebSocketMessage.Type.RESPONSE_VALUE) {
            SettableFuture<Pair<Integer, String>> listener = outgoingRequests.get(message.getResponse().getId());
            if (listener != null)
                listener.set(new Pair<>(message.getResponse().getStatus(), new String(message.getResponse().getBody().toByteArray())));
        }
        notifyAll();
    } catch (InvalidProtocolBufferException e) {
        Log.w(TAG, e);
    }
}
Also used : SettableFuture(org.whispersystems.signalservice.internal.util.concurrent.SettableFuture) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) WebSocketMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage) ByteString(okio.ByteString)

Aggregations

WebSocketMessage (org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketMessage)4 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 IOException (java.io.IOException)2 ByteString (okio.ByteString)2 SettableFuture (org.whispersystems.signalservice.internal.util.concurrent.SettableFuture)2 Pair (org.whispersystems.libsignal.util.Pair)1