class InternalStreamConnection extends java.lang.Object implements InternalConnection
Modifier and Type | Class and Description |
---|---|
private class |
InternalStreamConnection.ResponseBuffersCallback |
private class |
InternalStreamConnection.ResponseHeaderCallback |
private static class |
InternalStreamConnection.SendMessageRequest |
Modifier and Type | Field and Description |
---|---|
private InternalConnectionInitializer |
connectionInitializer |
private ConnectionDescription |
description |
private MongoException |
exceptionThatPrecededStreamClosing |
private java.util.concurrent.atomic.AtomicBoolean |
isClosed |
private boolean |
isReading |
private boolean |
isWriting |
(package private) static Logger |
LOGGER |
private java.util.Map<java.lang.Integer,ResponseBuffers> |
messages |
private java.util.concurrent.atomic.AtomicBoolean |
opened |
private java.util.concurrent.locks.Lock |
readerLock |
private java.util.concurrent.atomic.AtomicReference<java.util.concurrent.CountDownLatch> |
readingPhase |
private java.util.Map<java.lang.Integer,SingleResultCallback<ResponseBuffers>> |
readQueue |
private ServerId |
serverId |
private Stream |
stream |
private StreamFactory |
streamFactory |
private java.util.Deque<InternalStreamConnection.SendMessageRequest> |
writeQueue |
private java.util.concurrent.locks.Lock |
writerLock |
Constructor and Description |
---|
InternalStreamConnection(ServerId serverId,
StreamFactory streamFactory,
InternalConnectionInitializer connectionInitializer) |
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the connection.
|
private void |
executeCallbackAndReceiveResponse(SingleResultCallback<ResponseBuffers> callback,
ResponseBuffers result,
boolean mustRead) |
private void |
failAllQueuedReads(java.lang.Throwable t) |
ByteBuf |
getBuffer(int size)
Gets a buffer with the givens capacity.
|
ConnectionDescription |
getDescription()
Gets the description of this connection.
|
private ConnectionId |
getId() |
private ServerAddress |
getServerAddress() |
boolean |
isClosed()
Returns the closed state of the connection
|
void |
open()
Opens the connection so its ready for use
|
void |
openAsync(SingleResultCallback<java.lang.Void> callback)
Opens the connection so its ready for use
|
boolean |
opened()
Returns if the connection has been opened
|
private void |
readAsync(int numBytes,
SingleResultCallback<ByteBuf> callback) |
ResponseBuffers |
receiveMessage(int responseTo)
Receive a response to a sent message from the server.
|
void |
receiveMessageAsync(int responseTo,
SingleResultCallback<ResponseBuffers> callback)
Asynchronously receive a response to a sent message from the server.
|
private void |
receiveResponseAsync() |
private ResponseBuffers |
receiveResponseBuffers() |
void |
sendMessage(java.util.List<ByteBuf> byteBuffers,
int lastRequestId)
Send a message to the server.
|
void |
sendMessageAsync(java.util.List<ByteBuf> byteBuffers,
int lastRequestId,
SingleResultCallback<java.lang.Void> callback)
Asynchronously send a message to the server.
|
private MongoException |
translateReadException(java.lang.Throwable e) |
private MongoException |
translateWriteException(java.lang.Throwable e) |
private void |
writeAsync(InternalStreamConnection.SendMessageRequest request) |
private final ServerId serverId
private final StreamFactory streamFactory
private final InternalConnectionInitializer connectionInitializer
private final java.util.concurrent.locks.Lock writerLock
private final java.util.concurrent.locks.Lock readerLock
private final java.util.Deque<InternalStreamConnection.SendMessageRequest> writeQueue
private final java.util.Map<java.lang.Integer,SingleResultCallback<ResponseBuffers>> readQueue
private final java.util.Map<java.lang.Integer,ResponseBuffers> messages
private boolean isWriting
private boolean isReading
private final java.util.concurrent.atomic.AtomicReference<java.util.concurrent.CountDownLatch> readingPhase
private volatile MongoException exceptionThatPrecededStreamClosing
private volatile ConnectionDescription description
private volatile Stream stream
private final java.util.concurrent.atomic.AtomicBoolean isClosed
private final java.util.concurrent.atomic.AtomicBoolean opened
static final Logger LOGGER
InternalStreamConnection(ServerId serverId, StreamFactory streamFactory, InternalConnectionInitializer connectionInitializer)
public ConnectionDescription getDescription()
InternalConnection
getDescription
in interface InternalConnection
public void open()
InternalConnection
open
in interface InternalConnection
public void openAsync(SingleResultCallback<java.lang.Void> callback)
InternalConnection
openAsync
in interface InternalConnection
callback
- the callback to be called once the connection has been openedpublic void close()
InternalConnection
close
in interface InternalConnection
public boolean opened()
InternalConnection
opened
in interface InternalConnection
public boolean isClosed()
InternalConnection
isClosed
in interface InternalConnection
public void sendMessage(java.util.List<ByteBuf> byteBuffers, int lastRequestId)
InternalConnection
sendMessage
in interface InternalConnection
byteBuffers
- the list of byte buffers to send.lastRequestId
- the request id of the last message in byteBufferspublic ResponseBuffers receiveMessage(int responseTo)
InternalConnection
receiveMessage
in interface InternalConnection
responseTo
- the request id that this message is a response topublic void sendMessageAsync(java.util.List<ByteBuf> byteBuffers, int lastRequestId, SingleResultCallback<java.lang.Void> callback)
InternalConnection
sendMessageAsync
in interface InternalConnection
byteBuffers
- the list of byte buffers to sendlastRequestId
- the request id of the last message in byteBufferscallback
- the callback to invoke on completionprivate void writeAsync(InternalStreamConnection.SendMessageRequest request)
public void receiveMessageAsync(int responseTo, SingleResultCallback<ResponseBuffers> callback)
InternalConnection
receiveMessageAsync
in interface InternalConnection
responseTo
- the request id that this message is a response tocallback
- the callback to invoke on completionprivate void executeCallbackAndReceiveResponse(SingleResultCallback<ResponseBuffers> callback, ResponseBuffers result, boolean mustRead)
private ConnectionId getId()
private ServerAddress getServerAddress()
private void receiveResponseAsync()
private void readAsync(int numBytes, SingleResultCallback<ByteBuf> callback)
private MongoException translateWriteException(java.lang.Throwable e)
private MongoException translateReadException(java.lang.Throwable e)
private ResponseBuffers receiveResponseBuffers() throws java.io.IOException
java.io.IOException
public ByteBuf getBuffer(int size)
BufferProvider
getBuffer
in interface BufferProvider
size
- the size required for the bufferprivate void failAllQueuedReads(java.lang.Throwable t)