Class SwitchableRxSocket<M>
- Type Parameters:
M- The transport message type.
- All Implemented Interfaces:
Closeable,AutoCloseable,RxSocket<M>
-
Field Summary
Fields inherited from class org.forgerock.opendj.io.rx.TransformedRxSocket
socketFields inherited from interface org.forgerock.opendj.io.RxSocket
LOCAL_CLOSE, REMOTE_CLOSE -
Constructor Summary
ConstructorsConstructorDescriptionSwitchableRxSocket(RxSocket<M> socket) Returns a new switchable socket delegating to the provided downstream socket. -
Method Summary
Modifier and TypeMethodDescriptionReturns the active socket.Returns the active socket if it has the provided class ornullotherwise.Returns a "cold"Singlewhich will read the next message from the network each time it is subscribed.voidResumes reading without replacing the active socket.voidresumeReadsAfterSwitchingSocket(RxSocket<M> newSocket) Replaces the active socket with a new socket and then resumes reading.voidSets the active socket.voidPrevents further attempts to read from the socket until eitherresumeReadsAfterSwitchingSocket(org.forgerock.opendj.io.RxSocket<M>)orresumeReads()are invoked.Returns a "cold"Completablewhich will write the provided message to the network each time it is subscribed.Methods inherited from class org.forgerock.opendj.io.rx.TransformedRxSocket
closeWithReason, getLocalAddress, getRemoteAddress, toString
-
Constructor Details
-
SwitchableRxSocket
Returns a new switchable socket delegating to the provided downstream socket.- Parameters:
socket- The downstream socket.
-
-
Method Details
-
suspendReads
public void suspendReads()Prevents further attempts to read from the socket until eitherresumeReadsAfterSwitchingSocket(org.forgerock.opendj.io.RxSocket<M>)orresumeReads()are invoked. It is safe to replace the socket while reads are suspended. This method must be invoked by the reader thread. -
resumeReadsAfterSwitchingSocket
Replaces the active socket with a new socket and then resumes reading. This method is typically invoked by a writer thread after it has successfully negotiated a new network layer and only aftersuspendReads()was previously invoked by the reader thread.- Parameters:
newSocket- The socket which will become the active socket.
-
resumeReads
public void resumeReads()Resumes reading without replacing the active socket. This method is typically invoked by a writer thread after it has after it has unsuccessfully negotiated a new network layer and only aftersuspendReads()was previously invoked by the reader thread. -
read
Description copied from interface:RxSocketReturns a "cold"Singlewhich will read the next message from the network each time it is subscribed. Read operations cannot be cancelled: cancelling/disposing a subscription has no effect, and will not close the underlying socket.Socket implementations must buffer incoming messages until they are read. Note that invoking this method multiple times returns the same
Singleinstance. The returnedSinglewill not allow multiple concurrent subscriptions as this will result in non-deterministic behavior. Attempts to have multiple concurrent subscriptions will result in anIllegalStateException.Subscribers will be notified when the socket is closed through
SingleObserver.onError(Throwable)with the correspondingIOExceptionproviding the reason for the disconnection. In particular, if the socket is closed locally viaRxSocket.close()then it will be notified withRxSocket.LOCAL_CLOSE, if it is closed locally viaRxSocket.closeWithReason(java.lang.Throwable)then it will be notified using the provided exception and finally, if it is closed remotely by the peer, then it will be notified usingRxSocket.REMOTE_CLOSE. Low-level network transports should return aSocketTimeoutExceptionfor communicating any network timeouts. Application layers may return exceptions which are more closely aligned to their APIs.- Specified by:
readin interfaceRxSocket<M>- Specified by:
readin classTransformedRxSocket<M,M> - Parameters:
bufferSupplier- An optional buffer supplier which may be used by the transport to reduce copying between transport layers. Usenullwhen the transport does not support buffer suppliers or if the transport should allocate its own buffer. Note that the buffer supplier may be called multiple times in order to complete a single read.- Returns:
- A "cold"
Singlewhich will be signalled once the read operation completes or fails.
-
write
Description copied from interface:RxSocketReturns a "cold"Completablewhich will write the provided message to the network each time it is subscribed. Write operations cannot be cancelled reliably, as a consequence cancelling/disposing a subscription has no effect.Subscribers will be notified when the socket is closed through
CompletableObserver.onError(Throwable)with the correspondingIOExceptionproviding the reason for the disconnection. In particular, if the socket is closed locally viaRxSocket.close()then it will be notified withRxSocket.LOCAL_CLOSE, if it is closed locally viaRxSocket.closeWithReason(java.lang.Throwable)then it will be notified using the provided exception and finally, if it is closed remotely by the peer, then it will be notified usingRxSocket.REMOTE_CLOSE. Low-level network transports should return aSocketTimeoutExceptionfor communicating any network timeouts. Application layers may return exceptions which are more closely aligned to their APIs.This method is thread-safe, atomic and ordered with respect to other write attempts. In particular, consecutive writes will be performed in the order in which they are subscribed.
- Specified by:
writein interfaceRxSocket<M>- Specified by:
writein classTransformedRxSocket<M,M> - Parameters:
message- The message to be written to the network.- Returns:
- A "cold"
Completablewhich will be signalled once the write operation completes or fails.
-
getSocket
Returns the active socket.- Returns:
- The active socket.
-
getSocket
Returns the active socket if it has the provided class ornullotherwise.- Type Parameters:
S- The expected class of the active socket.- Parameters:
clazz- The expected class of the active socket.- Returns:
- The active socket or
null.
-
setSocket
Sets the active socket. This method may be used in order to directly replace the active socket without any coordination with the reader. It is typically safe to do this on the client side once the final protocol handshake (e.g. StartTLS response) is received.- Parameters:
newSocket- The new active socket.
-