public abstract class AbstractSocketConnection extends Object implements Connection, Receiver
Modifier and Type | Field and Description |
---|---|
static int |
HEADER_LENGTH
The length of the message header in bytes.
|
protected BsonInputStream |
myBsonIn
The writer for BSON documents.
|
protected MongoClientConfiguration |
myConfig
The connections configuration.
|
protected PropertyChangeSupport |
myEventSupport
Support for emitting property change events.
|
protected Executor |
myExecutor
The executor for the responses.
|
protected InputStream |
myInput
The buffered input stream.
|
protected Log |
myLog
The logger for the connection.
|
protected AtomicBoolean |
myOpen
Holds if the connection is open.
|
protected BufferedOutputStream |
myOutput
The buffered output stream.
|
protected PendingMessageQueue |
myPendingQueue
The queue of messages sent but waiting for a reply.
|
protected Server |
myServer
The open socket.
|
protected AtomicBoolean |
myShutdown
Set to true when the connection should be gracefully closed.
|
protected Socket |
mySocket
The open socket.
|
COMMAND_COLLECTION, OPEN_PROP_NAME
Constructor and Description |
---|
AbstractSocketConnection(Server server,
MongoClientConfiguration config)
Creates a new AbstractSocketConnection.
|
Modifier and Type | Method and Description |
---|---|
void |
addPropertyChangeListener(PropertyChangeListener listener)
Adds a
PropertyChangeListener to this connection. |
protected Message |
doReceive()
Receives a single message from the connection.
|
protected void |
doReceiveOne()
Receives and process a single message.
|
protected void |
doSend(int messageId,
RandomAccessOutputStream message)
Sends a single message to the connection.
|
void |
flush() |
int |
getPendingCount()
Returns the number of messages that are pending responses from the
server.
|
String |
getServerName()
Returns the name of a server the connection is currently connected to.
|
boolean |
isAvailable()
Returns true if the connection is open and not shutting down, false
otherwise.
|
boolean |
isIdle()
Determines if the connection is idle.
|
boolean |
isOpen()
Determines if the connection is open.
|
boolean |
isShuttingDown()
Returns true if the connection is being gracefully closed, false
otherwise.
|
protected void |
markReaderNeedsToFlush()
Should be called when the send of a message happens on the receive
thread.
|
protected void |
raiseError(Throwable exception,
ReplyCallback replyCallback)
Updates to raise an error on the callback, if any.
|
void |
raiseErrors(MongoDbException exception)
Notifies the call backs for the pending and optionally the to be sent
messages that there has been an external, unrecoverable error.
|
protected int |
readIntSuppressTimeoutOnNonFirstByte()
Reads a little-endian 4 byte signed integer from the stream.
|
void |
removePropertyChangeListener(PropertyChangeListener listener)
Removes a
PropertyChangeListener from this connection. |
protected void |
reply(Reply reply,
PendingMessage pendingMessage)
Updates to set the reply for the callback, if any.
|
protected void |
send(PendingMessage pendingMessage,
RandomAccessOutputStream message)
Sends a single message.
|
void |
shutdown(boolean force)
Notifies the connection that once all outstanding requests have been sent
and all replies received the Connection should be closed.
|
protected void |
shutdown(MongoDbException error,
boolean receiveError)
Shutsdown the connection on an error.
|
abstract void |
start()
Starts the connection.
|
void |
stop()
Stops the socket connection by calling
shutdown(false) . |
String |
toString() |
void |
tryReceive()
Tries to receive a message from the server.
|
protected void |
updateSocketWithOptions(MongoClientConfiguration config)
Updates the socket with the configuration's socket options.
|
protected void |
validate(Message message1,
Message message2)
Ensures that the documents in the message do not exceed the maximum size
allowed by MongoDB.
|
void |
waitForClosed(int timeout,
TimeUnit timeoutUnits)
Waits for the connection to become idle.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
send, send
public static final int HEADER_LENGTH
protected final BsonInputStream myBsonIn
myInput
.protected final MongoClientConfiguration myConfig
protected final PropertyChangeSupport myEventSupport
protected final Executor myExecutor
protected final InputStream myInput
protected final Log myLog
protected final AtomicBoolean myOpen
protected final BufferedOutputStream myOutput
protected final PendingMessageQueue myPendingQueue
protected final Server myServer
protected final AtomicBoolean myShutdown
protected final Socket mySocket
public AbstractSocketConnection(Server server, MongoClientConfiguration config) throws SocketException, IOException
server
- The MongoDB server to connect to.config
- The configuration for the Connection to the MongoDB server.SocketException
- On a failure connecting to the MongoDB server.IOException
- On a failure to read or write data to the MongoDB server.public void addPropertyChangeListener(PropertyChangeListener listener)
PropertyChangeListener
to this connection. Events are
fired as the state of the connection changes.
Overridden to add the listener to this connection.
addPropertyChangeListener
in interface Connection
listener
- The listener for the change events.public void flush() throws IOException
flush
in interface Flushable
IOException
public int getPendingCount()
getPendingCount
in interface Connection
public String getServerName()
Overridden to returns the server's name.
getServerName
in interface Connection
public boolean isAvailable()
True if the connection is open and not shutting down.
isAvailable
in interface Connection
public boolean isIdle()
True if the send and pending queues are empty.
isIdle
in interface Connection
public boolean isOpen()
True if the connection has not been closed.
isOpen
in interface Connection
public boolean isShuttingDown()
isShuttingDown
in interface Connection
public void raiseErrors(MongoDbException exception)
Notifies the appropriate messages of the error.
raiseErrors
in interface Connection
exception
- The error condition.public void removePropertyChangeListener(PropertyChangeListener listener)
PropertyChangeListener
from this connection.
Overridden to remove the listener from this connection.
removePropertyChangeListener
in interface Connection
listener
- The listener for the change events.public void shutdown(boolean force)
Overridden to mark the socket as shutting down and tickles the sender to make sure that happens as soon as possible.
shutdown
in interface Connection
force
- If true then the connection can be immediately closed as the
caller knows there are no outstanding requests to the server.public abstract void start()
public void stop()
shutdown(false)
.public String toString()
Overridden to return the socket information.
public void tryReceive()
If there is a pending flush then flushes.
If there is any available data then does a single receive.
tryReceive
in interface Receiver
public void waitForClosed(int timeout, TimeUnit timeoutUnits)
Waits for the connections pending queues to empty.
waitForClosed
in interface Connection
timeout
- The amount of time to wait for the connection to become idle.timeoutUnits
- The units for the amount of time to wait for the connection to
become idle.protected Message doReceive() throws MongoDbException
Message
received.MongoDbException
- On an error receiving the message.protected void doReceiveOne()
protected void doSend(int messageId, RandomAccessOutputStream message) throws IOException
messageId
- The id to use for the message.message
- The message to send.IOException
- On a failure sending the message.protected void markReaderNeedsToFlush()
protected void raiseError(Throwable exception, ReplyCallback replyCallback)
exception
- The thrown exception.replyCallback
- The callback for the reply to the message.protected int readIntSuppressTimeoutOnNonFirstByte() throws EOFException, IOException
EOFException
- On insufficient data for the integer.IOException
- On a failure reading the integer.protected void reply(Reply reply, PendingMessage pendingMessage)
reply
- The reply.pendingMessage
- The pending message.protected final void send(PendingMessage pendingMessage, RandomAccessOutputStream message) throws InterruptedException, IOException
pendingMessage
- The message to be sent.message
- The message that has already been encoded/serialized. This may
be null
in which case the message is streamed to
the socket.InterruptedException
- If the thread is interrupted waiting for a message to send.IOException
- On a failure sending the message.protected void shutdown(MongoDbException error, boolean receiveError)
error
- The error causing the shutdown.receiveError
- If true then the socket experienced a receive error.protected void updateSocketWithOptions(MongoClientConfiguration config) throws SocketException
config
- The configuration to apply.SocketException
- On a failure setting the socket options.protected void validate(Message message1, Message message2) throws DocumentToLargeException, ServerVersionException
message1
- The message to be sent to the server.message2
- The second message to be sent to the server.DocumentToLargeException
- On a message being too large.ServerVersionException
- If one of the messages cannot be sent to the server version.Copyright © 2011–2014 Allanbank Consulting, Inc.. All rights reserved.