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();
}
}
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();
}
}
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);
}
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);
}
}
Aggregations