Search in sources :

Example 1 with MongoSocketClosedException

use of com.mongodb.MongoSocketClosedException in project mongo-java-driver by mongodb.

the class InternalStreamConnection method receiveMessage.

@Override
public ResponseBuffers receiveMessage(final int responseTo) {
    notNull("stream is open", stream);
    if (isClosed()) {
        throw new MongoSocketClosedException("Cannot read from a closed stream", getServerAddress());
    }
    CountDownLatch localLatch = new CountDownLatch(1);
    readerLock.lock();
    try {
        ResponseBuffers responseBuffers = receiveResponseBuffers();
        messages.put(responseBuffers.getReplyHeader().getResponseTo(), responseBuffers);
        readingPhase.getAndSet(localLatch).countDown();
    } catch (Throwable t) {
        exceptionThatPrecededStreamClosing = translateReadException(t);
        close();
        readingPhase.getAndSet(localLatch).countDown();
    } finally {
        readerLock.unlock();
    }
    while (true) {
        if (isClosed()) {
            if (exceptionThatPrecededStreamClosing != null) {
                throw exceptionThatPrecededStreamClosing;
            } else {
                throw new MongoSocketClosedException("Socket has been closed", getServerAddress());
            }
        }
        ResponseBuffers myResponse = messages.remove(responseTo);
        if (myResponse != null) {
            connectionListener.messageReceived(new ConnectionMessageReceivedEvent(getId(), myResponse.getReplyHeader().getResponseTo(), myResponse.getReplyHeader().getMessageLength()));
            return myResponse;
        }
        try {
            localLatch.await();
        } catch (InterruptedException e) {
            throw new MongoInterruptedException("Interrupted while reading from stream", e);
        }
        localLatch = readingPhase.get();
    }
}
Also used : MongoInterruptedException(com.mongodb.MongoInterruptedException) ConnectionMessageReceivedEvent(com.mongodb.event.ConnectionMessageReceivedEvent) MongoSocketClosedException(com.mongodb.MongoSocketClosedException) CountDownLatch(java.util.concurrent.CountDownLatch) MongoInterruptedException(com.mongodb.MongoInterruptedException)

Example 2 with MongoSocketClosedException

use of com.mongodb.MongoSocketClosedException in project mongo-java-driver by mongodb.

the class InternalStreamConnection method sendMessage.

@Override
public void sendMessage(final List<ByteBuf> byteBuffers, final int lastRequestId) {
    notNull("stream is open", stream);
    if (isClosed()) {
        throw new MongoSocketClosedException("Cannot write to a closed stream", getServerAddress());
    }
    writerLock.lock();
    try {
        int messageSize = getMessageSize(byteBuffers);
        stream.write(byteBuffers);
        connectionListener.messagesSent(new ConnectionMessagesSentEvent(getId(), lastRequestId, messageSize));
    } catch (Exception e) {
        close();
        throw translateWriteException(e);
    } finally {
        writerLock.unlock();
    }
}
Also used : ConnectionMessagesSentEvent(com.mongodb.event.ConnectionMessagesSentEvent) MongoSocketClosedException(com.mongodb.MongoSocketClosedException) MongoInternalException(com.mongodb.MongoInternalException) MongoSocketReadException(com.mongodb.MongoSocketReadException) MongoSocketClosedException(com.mongodb.MongoSocketClosedException) MongoSocketWriteException(com.mongodb.MongoSocketWriteException) InterruptedIOException(java.io.InterruptedIOException) SocketTimeoutException(java.net.SocketTimeoutException) MongoInterruptedException(com.mongodb.MongoInterruptedException) MongoException(com.mongodb.MongoException) IOException(java.io.IOException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) MongoSocketReadTimeoutException(com.mongodb.MongoSocketReadTimeoutException)

Example 3 with MongoSocketClosedException

use of com.mongodb.MongoSocketClosedException in project mongo-java-driver by mongodb.

the class InternalStreamConnection method receiveMessageAsync.

@Override
public void receiveMessageAsync(final int responseTo, final SingleResultCallback<ResponseBuffers> callback) {
    isTrue("stream is open", stream != null, callback);
    if (isClosed()) {
        callback.onResult(null, new MongoSocketClosedException("Can not read from a closed socket", getServerAddress()));
        return;
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("Queuing read message: %s. ([%s])", responseTo, getId()));
    }
    ResponseBuffers response = null;
    readerLock.lock();
    boolean mustRead = false;
    try {
        response = messages.remove(responseTo);
        if (response == null) {
            readQueue.put(responseTo, callback);
        }
        if (!readQueue.isEmpty() && !isReading) {
            isReading = true;
            mustRead = true;
        }
    } finally {
        readerLock.unlock();
    }
    executeCallbackAndReceiveResponse(callback, response, mustRead);
}
Also used : MongoSocketClosedException(com.mongodb.MongoSocketClosedException)

Example 4 with MongoSocketClosedException

use of com.mongodb.MongoSocketClosedException in project mongo-java-driver by mongodb.

the class InternalStreamConnection method sendMessageAsync.

@Override
public void sendMessageAsync(final List<ByteBuf> byteBuffers, final int lastRequestId, final SingleResultCallback<Void> callback) {
    notNull("stream is open", stream, callback);
    if (isClosed()) {
        callback.onResult(null, new MongoSocketClosedException("Can not read from a closed socket", getServerAddress()));
        return;
    }
    if (LOGGER.isTraceEnabled()) {
        LOGGER.trace(format("Queuing send message: %s. ([%s])", lastRequestId, getId()));
    }
    SendMessageRequest sendMessageRequest = new SendMessageRequest(byteBuffers, lastRequestId, errorHandlingCallback(callback, LOGGER));
    boolean mustWrite = false;
    writerLock.lock();
    try {
        if (isWriting) {
            writeQueue.add(sendMessageRequest);
        } else {
            isWriting = true;
            mustWrite = true;
        }
    } finally {
        writerLock.unlock();
    }
    if (mustWrite) {
        writeAsync(sendMessageRequest);
    }
}
Also used : MongoSocketClosedException(com.mongodb.MongoSocketClosedException)

Aggregations

MongoSocketClosedException (com.mongodb.MongoSocketClosedException)4 MongoInterruptedException (com.mongodb.MongoInterruptedException)2 MongoException (com.mongodb.MongoException)1 MongoInternalException (com.mongodb.MongoInternalException)1 MongoSocketReadException (com.mongodb.MongoSocketReadException)1 MongoSocketReadTimeoutException (com.mongodb.MongoSocketReadTimeoutException)1 MongoSocketWriteException (com.mongodb.MongoSocketWriteException)1 ConnectionMessageReceivedEvent (com.mongodb.event.ConnectionMessageReceivedEvent)1 ConnectionMessagesSentEvent (com.mongodb.event.ConnectionMessagesSentEvent)1 IOException (java.io.IOException)1 InterruptedIOException (java.io.InterruptedIOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 ClosedByInterruptException (java.nio.channels.ClosedByInterruptException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1