Search in sources :

Example 6 with SendResult

use of javax.websocket.SendResult in project atmosphere by Atmosphere.

the class JSR356WebSocketTest method test_semaphore_is_released_in_case_of_failing_write.

@Test(timeOut = 1000)
public void test_semaphore_is_released_in_case_of_failing_write() throws Exception {
    mockWriteResult(new SendResult(new RuntimeException("Fails")));
    webSocket.write("Hello");
    webSocket.write("Hello");
    verify(asyncRemoteEndpoint, times(2)).sendText(eq("Hello"), any(SendHandler.class));
}
Also used : SendHandler(javax.websocket.SendHandler) SendResult(javax.websocket.SendResult) Test(org.testng.annotations.Test)

Example 7 with SendResult

use of javax.websocket.SendResult in project undertow by undertow-io.

the class SendHandlerAdapter method complete.

@Override
public void complete(WebSocketChannel channel, Void context) {
    if (done) {
        return;
    }
    done = true;
    handler.onResult(new SendResult());
}
Also used : SendResult(javax.websocket.SendResult)

Example 8 with SendResult

use of javax.websocket.SendResult in project undertow by undertow-io.

the class JsrWebSocketServer07Test method testBinaryWithByteBufferByCompletion.

@org.junit.Test
public void testBinaryWithByteBufferByCompletion() throws Exception {
    final byte[] payload = "payload".getBytes();
    final AtomicReference<SendResult> sendResult = new AtomicReference<>();
    final AtomicBoolean connected = new AtomicBoolean(false);
    final FutureResult latch = new FutureResult();
    final FutureResult latch2 = new FutureResult();
    class TestEndPoint extends Endpoint {

        @Override
        public void onOpen(final Session session, EndpointConfig config) {
            connected.set(true);
            session.addMessageHandler(new MessageHandler.Whole<ByteBuffer>() {

                @Override
                public void onMessage(ByteBuffer message) {
                    ByteBuffer buf = ByteBuffer.allocate(message.remaining());
                    buf.put(message);
                    buf.flip();
                    session.getAsyncRemote().sendBinary(buf, new SendHandler() {

                        @Override
                        public void onResult(SendResult result) {
                            sendResult.set(result);
                            if (result.getException() != null) {
                                latch2.setException(new IOException(result.getException()));
                            } else {
                                latch2.setResult(null);
                            }
                        }
                    });
                }
            });
        }
    }
    ServerWebSocketContainer builder = new ServerWebSocketContainer(TestClassIntrospector.INSTANCE, DefaultServer.getWorker(), DefaultServer.getBufferPool(), Collections.EMPTY_LIST, false, false);
    builder.addEndpoint(ServerEndpointConfig.Builder.create(TestEndPoint.class, "/").configurator(new InstanceConfigurator(new TestEndPoint())).build());
    deployServlet(builder);
    WebSocketTestClient client = new WebSocketTestClient(getVersion(), new URI("ws://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostPort("default") + "/"));
    client.connect();
    client.send(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(payload)), new FrameChecker(BinaryWebSocketFrame.class, payload, latch));
    latch.getIoFuture().get();
    latch2.getIoFuture().get();
    SendResult result = sendResult.get();
    Assert.assertNotNull(result);
    Assert.assertNull(result.getException());
    client.destroy();
}
Also used : SendHandler(javax.websocket.SendHandler) MessageHandler(javax.websocket.MessageHandler) ServerWebSocketContainer(io.undertow.websockets.jsr.ServerWebSocketContainer) BinaryWebSocketFrame(io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame) AtomicReference(java.util.concurrent.atomic.AtomicReference) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) WebSocketTestClient(io.undertow.websockets.utils.WebSocketTestClient) FutureResult(org.xnio.FutureResult) Endpoint(javax.websocket.Endpoint) AnnotatedClientEndpoint(io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint) SendResult(javax.websocket.SendResult) FrameChecker(io.undertow.websockets.utils.FrameChecker) ServerEndpointConfig(javax.websocket.server.ServerEndpointConfig) EndpointConfig(javax.websocket.EndpointConfig) Session(javax.websocket.Session) UndertowSession(io.undertow.websockets.jsr.UndertowSession) Test(org.junit.Test)

Example 9 with SendResult

use of javax.websocket.SendResult in project tomcat by apache.

the class WsRemoteEndpointImplClient method doWrite.

@Override
protected void doWrite(SendHandler handler, long blockingWriteTimeoutExpiry, ByteBuffer... data) {
    long timeout;
    for (ByteBuffer byteBuffer : data) {
        if (blockingWriteTimeoutExpiry == -1) {
            timeout = getSendTimeout();
            if (timeout < 1) {
                timeout = Long.MAX_VALUE;
            }
        } else {
            timeout = blockingWriteTimeoutExpiry - System.currentTimeMillis();
            if (timeout < 0) {
                SendResult sr = new SendResult(new IOException("Blocking write timeout"));
                handler.onResult(sr);
            }
        }
        try {
            channel.write(byteBuffer).get(timeout, TimeUnit.MILLISECONDS);
        } catch (InterruptedException | ExecutionException | TimeoutException e) {
            handler.onResult(new SendResult(e));
            return;
        }
    }
    handler.onResult(SENDRESULT_OK);
}
Also used : SendResult(javax.websocket.SendResult) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ByteBuffer(java.nio.ByteBuffer) TimeoutException(java.util.concurrent.TimeoutException)

Example 10 with SendResult

use of javax.websocket.SendResult in project tomcat by apache.

the class WsRemoteEndpointImplBase method startMessage.

void startMessage(byte opCode, ByteBuffer payload, boolean last, SendHandler handler) {
    wsSession.updateLastActive();
    List<MessagePart> messageParts = new ArrayList<>();
    messageParts.add(new MessagePart(last, 0, opCode, payload, intermediateMessageHandler, new EndMessageHandler(this, handler), -1));
    messageParts = transformation.sendMessagePart(messageParts);
    // trigger the supplied SendHandler
    if (messageParts.size() == 0) {
        handler.onResult(new SendResult());
        return;
    }
    MessagePart mp = messageParts.remove(0);
    boolean doWrite = false;
    synchronized (messagePartLock) {
        if (Constants.OPCODE_CLOSE == mp.getOpCode() && getBatchingAllowed()) {
            // Should not happen. To late to send batched messages now since
            // the session has been closed. Complain loudly.
            log.warn(sm.getString("wsRemoteEndpoint.flushOnCloseFailed"));
        }
        if (messagePartInProgress.tryAcquire()) {
            doWrite = true;
        } else {
            // When a control message is sent while another message is being
            // sent, the control message is queued. Chances are the
            // subsequent data message part will end up queued while the
            // control message is sent. The logic in this class (state
            // machine, EndMessageHandler, TextMessageSendHandler) ensures
            // that there will only ever be one data message part in the
            // queue. There could be multiple control messages in the queue.
            // Add it to the queue
            messagePartQueue.add(mp);
        }
        // Add any remaining messages to the queue
        messagePartQueue.addAll(messageParts);
    }
    if (doWrite) {
        // Actual write has to be outside sync block to avoid possible
        // deadlock between messagePartLock and writeLock in
        // o.a.coyote.http11.upgrade.AbstractServletOutputStream
        writeMessagePart(mp);
    }
}
Also used : SendResult(javax.websocket.SendResult) ArrayList(java.util.ArrayList)

Aggregations

SendResult (javax.websocket.SendResult)16 IOException (java.io.IOException)7 SendHandler (javax.websocket.SendHandler)7 ByteBuffer (java.nio.ByteBuffer)5 URI (java.net.URI)4 Session (javax.websocket.Session)4 Test (org.junit.Test)4 ServerWebSocketContainer (io.undertow.websockets.jsr.ServerWebSocketContainer)2 UndertowSession (io.undertow.websockets.jsr.UndertowSession)2 AnnotatedClientEndpoint (io.undertow.websockets.jsr.test.annotated.AnnotatedClientEndpoint)2 FrameChecker (io.undertow.websockets.utils.FrameChecker)2 WebSocketTestClient (io.undertow.websockets.utils.WebSocketTestClient)2 SocketTimeoutException (java.net.SocketTimeoutException)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 ClientEndpointConfig (javax.websocket.ClientEndpointConfig)2 EncodeException (javax.websocket.EncodeException)2 Encoder (javax.websocket.Encoder)2 Endpoint (javax.websocket.Endpoint)2