Search in sources :

Example 1 with WriteFuture

use of org.apache.mina.core.future.WriteFuture in project directory-ldap-api by apache.

the class LdapNetworkConnection method writeRequest.

/**
 * a reusable code block to be used in various bind methods
 */
private void writeRequest(Request request) throws LdapException {
    // Send the request to the server
    WriteFuture writeFuture = ldapSession.write(request);
    long localTimeout = timeout;
    while (localTimeout > 0) {
        // Wait only 100 ms
        boolean done = writeFuture.awaitUninterruptibly(100);
        if (done) {
            return;
        }
        // Wait for the message to be sent to the server
        if (!ldapSession.isConnected()) {
            // We didn't received anything : this is an error
            if (LOG.isErrorEnabled()) {
                LOG.error(I18n.err(I18n.ERR_03207_SOMETHING_WRONG_HAPPENED));
            }
            Exception exception = (Exception) ldapSession.removeAttribute(EXCEPTION_KEY);
            if (exception != null) {
                if (exception instanceof LdapException) {
                    throw (LdapException) exception;
                } else {
                    throw new InvalidConnectionException(exception.getMessage(), exception);
                }
            }
            throw new InvalidConnectionException("Error while sending some message : the session has been closed");
        }
        localTimeout -= 100;
    }
    if (LOG.isErrorEnabled()) {
        LOG.error(I18n.err(I18n.ERR_03208_TIMEOUT));
    }
    throw new LdapException(TIME_OUT_ERROR);
}
Also used : InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) WriteFuture(org.apache.mina.core.future.WriteFuture) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnresolvedAddressException(java.nio.channels.UnresolvedAddressException) ConnectException(java.net.ConnectException) IOException(java.io.IOException) LdapInvalidDnException(org.apache.directory.api.ldap.model.exception.LdapInvalidDnException) InvalidConnectionException(org.apache.directory.ldap.client.api.exception.InvalidConnectionException) LdapOperationException(org.apache.directory.api.ldap.model.exception.LdapOperationException) LdapAuthenticationException(org.apache.directory.api.ldap.model.exception.LdapAuthenticationException) MessageEncoderException(org.apache.directory.api.ldap.codec.api.MessageEncoderException) CursorException(org.apache.directory.api.ldap.model.cursor.CursorException) DecoderException(org.apache.directory.api.asn1.DecoderException) LdapNoPermissionException(org.apache.directory.api.ldap.model.exception.LdapNoPermissionException) LdapOtherException(org.apache.directory.api.ldap.model.exception.LdapOtherException) ProtocolEncoderException(org.apache.mina.filter.codec.ProtocolEncoderException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException)

Example 2 with WriteFuture

use of org.apache.mina.core.future.WriteFuture in project camel by apache.

the class Mina2Helper method writeBody.

/**
     * Asynchronously writes the given body to MINA session. Will wait at most for
     * 10 seconds until the body has been written.
     *
     * @param session  the MINA session
     * @param body     the body to write (send)
     * @param exchange the exchange
     * @throws CamelExchangeException is thrown if the body could not be written for some reasons
     *                                (eg remote connection is closed etc.)
     */
public static void writeBody(IoSession session, Object body, Exchange exchange) throws CamelExchangeException {
    // the write operation is asynchronous. Use WriteFuture to wait until the session has been written
    WriteFuture future = session.write(body);
    // must use a timeout (we use 10s) as in some very high performance scenarios a write can cause 
    // thread hanging forever
    LOG.trace("Waiting for write to complete for body: {} using session: {}", body, session);
    if (!future.awaitUninterruptibly(10000L)) {
        String message = "Cannot write body: " + body.getClass().getCanonicalName() + " using session: " + session;
        if (future.getException() != null) {
            throw new CamelExchangeException(message, exchange, future.getException());
        } else {
            throw new CamelExchangeException(message, exchange);
        }
    }
}
Also used : CamelExchangeException(org.apache.camel.CamelExchangeException) WriteFuture(org.apache.mina.core.future.WriteFuture)

Example 3 with WriteFuture

use of org.apache.mina.core.future.WriteFuture in project directory-ldap-api by apache.

the class LdapNetworkConnection method unBind.

// ------------------------ The LDAP operations ------------------------//
// Unbind operations                                                   //
// ---------------------------------------------------------------------//
/**
 * {@inheritDoc}
 */
@Override
public void unBind() throws LdapException {
    // If the session has not been establish, or is closed, we get out immediately
    checkSession();
    // Creates the messageID and stores it into the
    // initial message and the transmitted message.
    int newId = messageId.incrementAndGet();
    // Create the UnbindRequest
    UnbindRequest unbindRequest = new UnbindRequestImpl();
    unbindRequest.setMessageId(newId);
    if (LOG.isDebugEnabled()) {
        LOG.debug(I18n.msg(I18n.MSG_03233_SENDING_UNBIND, unbindRequest));
    }
    // Send the request to the server
    // Use this for logging instead: WriteFuture unbindFuture = ldapSession.write( unbindRequest )
    WriteFuture unbindFuture = ldapSession.write(unbindRequest);
    unbindFuture.awaitUninterruptibly(timeout);
    authenticated.set(false);
    // Close all the Future for this session
    for (ResponseFuture<? extends Response> responseFuture : futureMap.values()) {
        responseFuture.cancel();
    }
    // clear the mappings
    clearMaps();
    // We now have to close the session
    try {
        close();
    } catch (IOException e) {
        LOG.error(e.getMessage());
        throw new LdapException(e.getMessage());
    }
    connected.set(false);
    // Last, not least, reset the MessageId value
    messageId.set(0);
    // And get out
    if (LOG.isDebugEnabled()) {
        LOG.debug(I18n.msg(I18n.MSG_03234_UNBINDSUCCESSFUL));
    }
}
Also used : UnbindRequestImpl(org.apache.directory.api.ldap.model.message.UnbindRequestImpl) WriteFuture(org.apache.mina.core.future.WriteFuture) IOException(java.io.IOException) LdapException(org.apache.directory.api.ldap.model.exception.LdapException) UnbindRequest(org.apache.directory.api.ldap.model.message.UnbindRequest)

Example 4 with WriteFuture

use of org.apache.mina.core.future.WriteFuture in project streamsx.topology by IBMStreams.

the class TesterSink method processBatch.

@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;
}
Also used : TCPTestClient(com.ibm.streamsx.topology.internal.tester.tcp.TCPTestClient) WriteFuture(org.apache.mina.core.future.WriteFuture) TestTuple(com.ibm.streamsx.topology.internal.tester.tcp.TestTuple) ArrayList(java.util.ArrayList) BinaryEncoding(com.ibm.streams.operator.encoding.BinaryEncoding)

Example 5 with WriteFuture

use of org.apache.mina.core.future.WriteFuture in project zm-mailbox by Zimbra.

the class NioOutputStream method writeToSession.

private synchronized void writeToSession(Object output) throws IOException {
    long writeBytes = session.getScheduledWriteBytes();
    WriteFuture future = session.write(output);
    if (writeBytes > maxScheduledBytes) {
        ZimbraLog.nio.debug("IOSession has %d scheduled write bytes; waiting for buffer to catch up", writeBytes);
        long start = System.currentTimeMillis();
        if (maxWritePause > 0) {
            boolean done = future.awaitUninterruptibly(maxWritePause);
            if (!done) {
                throw new IOException("Write stalled, client may have gone away");
            }
        } else {
            future.awaitUninterruptibly();
        }
        if (ZimbraLog.nio.isDebugEnabled()) {
            ZimbraLog.nio.debug("waited %d for %d scheduled bytes", (System.currentTimeMillis() - start), writeBytes);
            ZimbraLog.nio.debug("now have %d scheduled bytes, %d messages; %d written bytes %d messages", session.getScheduledWriteBytes(), session.getScheduledWriteMessages(), session.getWrittenBytes(), session.getWrittenMessages());
        }
    }
}
Also used : WriteFuture(org.apache.mina.core.future.WriteFuture) IOException(java.io.IOException)

Aggregations

WriteFuture (org.apache.mina.core.future.WriteFuture)6 IOException (java.io.IOException)3 LdapException (org.apache.directory.api.ldap.model.exception.LdapException)2 BinaryEncoding (com.ibm.streams.operator.encoding.BinaryEncoding)1 TCPTestClient (com.ibm.streamsx.topology.internal.tester.tcp.TCPTestClient)1 TestTuple (com.ibm.streamsx.topology.internal.tester.tcp.TestTuple)1 ConnectException (java.net.ConnectException)1 UnresolvedAddressException (java.nio.channels.UnresolvedAddressException)1 ArrayList (java.util.ArrayList)1 CamelExchangeException (org.apache.camel.CamelExchangeException)1 DecoderException (org.apache.directory.api.asn1.DecoderException)1 MessageEncoderException (org.apache.directory.api.ldap.codec.api.MessageEncoderException)1 CursorException (org.apache.directory.api.ldap.model.cursor.CursorException)1 LdapAuthenticationException (org.apache.directory.api.ldap.model.exception.LdapAuthenticationException)1 LdapInvalidDnException (org.apache.directory.api.ldap.model.exception.LdapInvalidDnException)1 LdapNoPermissionException (org.apache.directory.api.ldap.model.exception.LdapNoPermissionException)1 LdapOperationException (org.apache.directory.api.ldap.model.exception.LdapOperationException)1 LdapOtherException (org.apache.directory.api.ldap.model.exception.LdapOtherException)1 UnbindRequest (org.apache.directory.api.ldap.model.message.UnbindRequest)1 UnbindRequestImpl (org.apache.directory.api.ldap.model.message.UnbindRequestImpl)1