Search in sources :

Example 1 with WebSocketResponseMessage

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

the class SignalWebSocket method readOrEmpty.

/**
 * <p>
 * A blocking call that reads a message off the pipe. When this call returns, the message has been
 * acknowledged and will not be retransmitted. This will return {@link Optional#absent()} when an
 * empty response is hit, which indicates the WebSocket is empty.
 * <p>
 * You can specify a {@link MessageReceivedCallback} that will be called before the received message is acknowledged.
 * This allows you to write the received message to durable storage before acknowledging receipt of it to the
 * server.
 * <p>
 * Important: The empty response will only be hit once for each connection. That means if you get
 * an empty response and call readOrEmpty() again on the same instance, you will not get an empty
 * response, and instead will block until you get an actual message. This will, however, reset if
 * connection breaks (if, for instance, you lose and regain network).
 *
 * @param timeout  The timeout to wait for.
 * @param callback A callback that will be called before the message receipt is acknowledged to the server.
 * @return The message read (same as the message sent through the callback).
 */
@SuppressWarnings("DuplicateThrows")
public Optional<SignalServiceEnvelope> readOrEmpty(long timeout, MessageReceivedCallback callback) throws TimeoutException, WebSocketUnavailableException, IOException {
    while (true) {
        WebSocketRequestMessage request = getWebSocket().readRequest(timeout);
        WebSocketResponseMessage response = createWebSocketResponse(request);
        try {
            if (isSignalServiceEnvelope(request)) {
                Optional<String> timestampHeader = findHeader(request);
                long timestamp = 0;
                if (timestampHeader.isPresent()) {
                    try {
                        timestamp = Long.parseLong(timestampHeader.get());
                    } catch (NumberFormatException e) {
                        Log.w(TAG, "Failed to parse " + SERVER_DELIVERED_TIMESTAMP_HEADER);
                    }
                }
                SignalServiceEnvelope envelope = new SignalServiceEnvelope(request.getBody().toByteArray(), timestamp);
                callback.onMessage(envelope);
                return Optional.of(envelope);
            } else if (isSocketEmptyRequest(request)) {
                return Optional.absent();
            }
        } finally {
            getWebSocket().sendResponse(response);
        }
    }
}
Also used : SignalServiceEnvelope(org.whispersystems.signalservice.api.messages.SignalServiceEnvelope) WebSocketRequestMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketRequestMessage) WebSocketResponseMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketResponseMessage)

Example 2 with WebSocketResponseMessage

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

the class SignalServiceMessagePipe method read.

/**
 * A blocking call that reads a message off the pipe (see {@link #read(long, java.util.concurrent.TimeUnit)}
 *
 * Unlike {@link #read(long, java.util.concurrent.TimeUnit)}, this method allows you
 * to specify a callback that will be called before the received message is acknowledged.
 * This allows you to write the received message to durable storage before acknowledging
 * receipt of it to the server.
 *
 * @param timeout The timeout to wait for.
 * @param unit The timeout time unit.
 * @param callback A callback that will be called before the message receipt is
 *                 acknowledged to the server.
 * @return The message read (same as the message sent through the callback).
 * @throws TimeoutException
 * @throws IOException
 * @throws InvalidVersionException
 */
public SignalServiceEnvelope read(long timeout, TimeUnit unit, MessagePipeCallback callback) throws TimeoutException, IOException, InvalidVersionException {
    if (!credentialsProvider.isPresent()) {
        throw new IllegalArgumentException("You can't read messages if you haven't specified credentials");
    }
    while (true) {
        WebSocketRequestMessage request = websocket.readRequest(unit.toMillis(timeout));
        WebSocketResponseMessage response = createWebSocketResponse(request);
        boolean signalKeyEncrypted = isSignalKeyEncrypted(request);
        try {
            if (isSignalServiceEnvelope(request)) {
                SignalServiceEnvelope envelope = new SignalServiceEnvelope(request.getBody().toByteArray(), credentialsProvider.get().getSignalingKey(), signalKeyEncrypted);
                callback.onMessage(envelope);
                return envelope;
            }
        } finally {
            websocket.sendResponse(response);
        }
    }
}
Also used : SignalServiceEnvelope(org.whispersystems.signalservice.api.messages.SignalServiceEnvelope) WebSocketRequestMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketRequestMessage) WebSocketResponseMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketResponseMessage)

Example 3 with WebSocketResponseMessage

use of org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketResponseMessage in project Signal-Android by signalapp.

the class SignalWebSocket method readOrEmpty.

/**
 * <p>
 * A blocking call that reads a message off the pipe. When this call returns, the message has been
 * acknowledged and will not be retransmitted. This will return {@link Optional#absent()} when an
 * empty response is hit, which indicates the WebSocket is empty.
 * <p>
 * You can specify a {@link MessageReceivedCallback} that will be called before the received message is acknowledged.
 * This allows you to write the received message to durable storage before acknowledging receipt of it to the
 * server.
 * <p>
 * Important: The empty response will only be hit once for each connection. That means if you get
 * an empty response and call readOrEmpty() again on the same instance, you will not get an empty
 * response, and instead will block until you get an actual message. This will, however, reset if
 * connection breaks (if, for instance, you lose and regain network).
 *
 * @param timeout  The timeout to wait for.
 * @param callback A callback that will be called before the message receipt is acknowledged to the server.
 * @return The message read (same as the message sent through the callback).
 */
@SuppressWarnings("DuplicateThrows")
public Optional<SignalServiceEnvelope> readOrEmpty(long timeout, MessageReceivedCallback callback) throws TimeoutException, WebSocketUnavailableException, IOException {
    while (true) {
        WebSocketRequestMessage request = getWebSocket().readRequest(timeout);
        WebSocketResponseMessage response = createWebSocketResponse(request);
        try {
            if (isSignalServiceEnvelope(request)) {
                Optional<String> timestampHeader = findHeader(request);
                long timestamp = 0;
                if (timestampHeader.isPresent()) {
                    try {
                        timestamp = Long.parseLong(timestampHeader.get());
                    } catch (NumberFormatException e) {
                        Log.w(TAG, "Failed to parse " + SERVER_DELIVERED_TIMESTAMP_HEADER);
                    }
                }
                SignalServiceEnvelope envelope = new SignalServiceEnvelope(request.getBody().toByteArray(), timestamp);
                callback.onMessage(envelope);
                return Optional.of(envelope);
            } else if (isSocketEmptyRequest(request)) {
                return Optional.absent();
            }
        } finally {
            getWebSocket().sendResponse(response);
        }
    }
}
Also used : SignalServiceEnvelope(org.whispersystems.signalservice.api.messages.SignalServiceEnvelope) WebSocketRequestMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketRequestMessage) WebSocketResponseMessage(org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketResponseMessage)

Aggregations

SignalServiceEnvelope (org.whispersystems.signalservice.api.messages.SignalServiceEnvelope)3 WebSocketRequestMessage (org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketRequestMessage)3 WebSocketResponseMessage (org.whispersystems.signalservice.internal.websocket.WebSocketProtos.WebSocketResponseMessage)3