72
72
import static com .mongodb .assertions .Assertions .assertNotNull ;
73
73
import static com .mongodb .assertions .Assertions .isTrue ;
74
74
import static com .mongodb .assertions .Assertions .notNull ;
75
+ import static com .mongodb .internal .async .AsyncRunnable .beginAsync ;
75
76
import static com .mongodb .internal .async .ErrorHandlingResultCallback .errorHandlingCallback ;
76
77
import static com .mongodb .internal .connection .CommandHelper .HELLO ;
77
78
import static com .mongodb .internal .connection .CommandHelper .LEGACY_HELLO ;
@@ -205,15 +206,16 @@ public int getGeneration() {
205
206
206
207
@ Override
207
208
public void open () {
208
- isTrue ("Open already called" , stream == null );
209
- stream = streamFactory .create (getServerAddressWithResolver ());
210
209
try {
210
+ isTrue ("Open already called" , stream == null );
211
+ stream = streamFactory .create (getServerAddressWithResolver ());
211
212
stream .open ();
212
213
213
214
InternalConnectionInitializationDescription initializationDescription = connectionInitializer .startHandshake (this );
214
- initAfterHandshakeStart (initializationDescription );
215
215
216
+ initAfterHandshakeStart (initializationDescription );
216
217
initializationDescription = connectionInitializer .finishHandshake (this , initializationDescription );
218
+
217
219
initAfterHandshakeFinish (initializationDescription );
218
220
} catch (Throwable t ) {
219
221
close ();
@@ -227,45 +229,27 @@ public void open() {
227
229
228
230
@ Override
229
231
public void openAsync (final SingleResultCallback <Void > callback ) {
230
- isTrue ("Open already called" , stream == null , callback );
231
- try {
232
- stream = streamFactory .create (getServerAddressWithResolver ());
233
- stream .openAsync (new AsyncCompletionHandler <Void >() {
234
- @ Override
235
- public void completed (@ Nullable final Void aVoid ) {
236
- connectionInitializer .startHandshakeAsync (InternalStreamConnection .this ,
237
- (initialResult , initialException ) -> {
238
- if (initialException != null ) {
239
- close ();
240
- callback .onResult (null , initialException );
241
- } else {
242
- assertNotNull (initialResult );
243
- initAfterHandshakeStart (initialResult );
244
- connectionInitializer .finishHandshakeAsync (InternalStreamConnection .this ,
245
- initialResult , (completedResult , completedException ) -> {
246
- if (completedException != null ) {
247
- close ();
248
- callback .onResult (null , completedException );
249
- } else {
250
- assertNotNull (completedResult );
251
- initAfterHandshakeFinish (completedResult );
252
- callback .onResult (null , null );
253
- }
254
- });
255
- }
256
- });
257
- }
258
-
259
- @ Override
260
- public void failed (final Throwable t ) {
261
- close ();
262
- callback .onResult (null , t );
263
- }
264
- });
265
- } catch (Throwable t ) {
232
+ beginAsync ().thenRun (
233
+ beginAsync ().thenRun (c -> {
234
+ isTrue ("Open already called" , stream == null , callback );
235
+ stream = streamFactory .create (getServerAddressWithResolver ());
236
+ stream .openAsync (c );
237
+ }).<InternalConnectionInitializationDescription >thenSupply (c -> {
238
+ connectionInitializer .startHandshakeAsync (this , c );
239
+ }).<InternalConnectionInitializationDescription >thenApply ((initializationDescription , c ) -> {
240
+ initAfterHandshakeStart (assertNotNull (initializationDescription ));
241
+ connectionInitializer .finishHandshakeAsync (this , initializationDescription , c );
242
+ }).thenConsume ((initializationDescription , c ) -> {
243
+ initAfterHandshakeFinish (assertNotNull (initializationDescription ));
244
+ })
245
+ ).onErrorIf (t -> true , (t , c ) -> {
266
246
close ();
267
- callback .onResult (null , t );
268
- }
247
+ if (t instanceof MongoException ) {
248
+ throw (MongoException ) t ;
249
+ } else {
250
+ throw new MongoException (assertNotNull (t ).toString (), t );
251
+ }
252
+ }).finish (callback );
269
253
}
270
254
271
255
private ServerAddress getServerAddressWithResolver () {
@@ -336,7 +320,7 @@ private Compressor createCompressor(final MongoCompressor mongoCompressor) {
336
320
public void close () {
337
321
// All but the first call is a no-op
338
322
if (!isClosed .getAndSet (true ) && (stream != null )) {
339
- stream .close ();
323
+ stream .close ();
340
324
}
341
325
}
342
326
@@ -352,8 +336,9 @@ public boolean isClosed() {
352
336
353
337
@ Nullable
354
338
@ Override
355
- public <T > T sendAndReceive (final CommandMessage message , final Decoder <T > decoder , final SessionContext sessionContext ,
356
- final RequestContext requestContext , final OperationContext operationContext ) {
339
+ public <T > T sendAndReceive (final CommandMessage message ,
340
+ final Decoder <T > decoder , final SessionContext sessionContext ,
341
+ final RequestContext requestContext , final OperationContext operationContext ) {
357
342
CommandEventSender commandEventSender ;
358
343
359
344
try (ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput (this )) {
@@ -476,8 +461,10 @@ private <T> T receiveCommandMessageResponse(final Decoder<T> decoder,
476
461
}
477
462
478
463
@ Override
479
- public <T > void sendAndReceiveAsync (final CommandMessage message , final Decoder <T > decoder , final SessionContext sessionContext ,
480
- final RequestContext requestContext , final OperationContext operationContext , final SingleResultCallback <T > callback ) {
464
+ public <T > void sendAndReceiveAsync (final CommandMessage message ,
465
+ final Decoder <T > decoder , final SessionContext sessionContext ,
466
+ final RequestContext requestContext , final OperationContext operationContext ,
467
+ final SingleResultCallback <T > callback ) {
481
468
notNull ("stream is open" , stream , callback );
482
469
483
470
if (isClosed ()) {
@@ -575,11 +562,9 @@ private <T> T getCommandResult(final Decoder<T> decoder, final ResponseBuffers r
575
562
@ Override
576
563
public void sendMessage (final List <ByteBuf > byteBuffers , final int lastRequestId ) {
577
564
notNull ("stream is open" , stream );
578
-
579
565
if (isClosed ()) {
580
566
throw new MongoSocketClosedException ("Cannot write to a closed stream" , getServerAddress ());
581
567
}
582
-
583
568
try {
584
569
stream .write (byteBuffers );
585
570
} catch (Exception e ) {
@@ -609,14 +594,14 @@ private ResponseBuffers receiveMessageWithAdditionalTimeout(final int additional
609
594
610
595
@ Override
611
596
public void sendMessageAsync (final List <ByteBuf > byteBuffers , final int lastRequestId , final SingleResultCallback <Void > callback ) {
612
- notNull ("stream is open" , stream , callback );
613
-
614
- if (isClosed ()) {
615
- callback .onResult (null , new MongoSocketClosedException ("Can not read from a closed socket" , getServerAddress ()));
616
- return ;
617
- }
597
+ beginAsync ().thenRun (c -> {
598
+ notNull ("stream is open" , stream , callback );
599
+ if (isClosed ()) {
600
+ throw new MongoSocketClosedException ("Can not read from a closed socket" , getServerAddress ());
601
+ }
618
602
619
- writeAsync (byteBuffers , errorHandlingCallback (callback , LOGGER ));
603
+ writeAsync (byteBuffers , errorHandlingCallback (callback , LOGGER ));
604
+ }).finish (callback );
620
605
}
621
606
622
607
private void writeAsync (final List <ByteBuf > byteBuffers , final SingleResultCallback <Void > callback ) {
0 commit comments