Java Code Examples for org.apache.mina.core.future.WriteFuture#await()

The following examples show how to use org.apache.mina.core.future.WriteFuture#await() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MINASession.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public IMessage send(Object message, long timeout) throws InterruptedException {
    if(!isConnected()) {
        throw new SendMessageFailedException("Session is not connected: " + this);
    }

    WriteFuture future = session.write(prepareMessage(message));

    if(future.await(timeout)) {
        if(!future.isDone()) {
            throw new SendMessageFailedException("Send operation is not done. Session: " + this, future.getException());
        }

        if(!future.isWritten()) {
            throw new SendMessageFailedException("Write operation is not done. Session: " + this, future.getException());
        }
    } else {
        throw new SendMessageFailedException("Send operation is not completed. Session: " + this, future.getException());
    }

    if(future.getException() != null) {
        throw new SendMessageFailedException("Message send failed. Session: " + this, future.getException());
    }

    return message instanceof IMessage ? (IMessage)message : null;
}
 
Example 2
Source File: MINAServerSession.java    From sailfish-core with Apache License 2.0 6 votes vote down vote up
public IMessage send(Object message, long timeout) throws InterruptedException {
    List<WriteFuture> futures = new ArrayList<>();
    Set<String> errorSending = new HashSet<>();

    for (IoSession session : server.sessions.keySet()) {
        futures.add(session.write(message));
    }

    long waitUntil = System.currentTimeMillis() + timeout;
    for (WriteFuture future : futures) {
        future.await(waitUntil - System.currentTimeMillis());
        if (!future.isDone() || !future.isWritten()) {
            errorSending.add(future.getSession().toString());
        }
    }

    if (!errorSending.isEmpty()) {
        throw new SendMessageFailedException(String.format("Message wasn't send during %d milliseconds. %s", timeout, String.join(System.lineSeparator(), errorSending)));
    }

    return message instanceof IMessage ? (IMessage) message : null;
}
 
Example 3
Source File: TesterSink.java    From streamsx.topology with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean processBatch(Queue<BatchedTuple> batch) throws Exception {
    List<WriteFuture> futures = new ArrayList<>(batch.size());
    for (BatchedTuple bt : batch) {
        int portIndex = bt.getStream().getPortNumber();
        TCPTestClient client = clients[portIndex];

        BinaryEncoding be = encoders[portIndex];
        byte[] tupleData = new byte[(int) be.getEncodedSize(bt.getTuple())];
        be.encodeTuple(bt.getTuple(), ByteBuffer.wrap(tupleData));
        TestTuple tt = new TestTuple(portIndex, tupleData);
        futures.add(client.writeTuple(tt));
    }
    for (WriteFuture future : futures) {
        future.await();
    }
    return false;
}
 
Example 4
Source File: MessageClientTest.java    From gameserver with Apache License 2.0 5 votes vote down vote up
/**
 * Deprecated test case since the client pool is used.
 * @throws Exception
 */
public void testConnectTimeoutToServer() throws Exception {
	server.stopServer();
	MessageClient client = new MessageClient(host, port);
	assertTrue(!client.connectToServer());
	SessionRawMessage msg = createSessionMessage();
	server.startServer(host, port);
	WriteFuture future = client.sendMessageToServer(msg);
	future.await();
	assertNotNull(future);
}