@ Override public void connect ( ) throws BitfinexClientException { logger . debug ( "connect() called" ) ; connectionStateChange ( BitfinexConnectionStateEnum . CONNECTION_INIT ) ; try { sequenceNumberAuditor . reset ( ) ; final CountDownLatch connectionReadyLatch = new CountDownLatch ( 4 ) ; final Closeable authSuccessEventCallback = callbackRegistry . onAuthenticationSuccessEvent ( c -> { permissions = c . getPermissions ( ) ; authenticated = true ; connectionReadyLatch . countDown ( ) ; } ) ; final Closeable authFailedCallback = callbackRegistry . onAuthenticationFailedEvent ( c -> { permissions = c . getPermissions ( ) ; authenticated = false ; while ( connectionReadyLatch . getCount ( ) != 0 ) { connectionReadyLatch . countDown ( ) ; } } ) ; final Closeable positionInitCallback = callbackRegistry . onMyPositionEvent ( ( a , p ) -> connectionReadyLatch . countDown ( ) ) ; final Closeable walletsInitCallback = callbackRegistry . onMyWalletEvent ( ( a , w ) -> connectionReadyLatch . countDown ( ) ) ; final Closeable orderInitCallback = callbackRegistry . onMySubmittedOrderEvent ( ( a , o ) -> connectionReadyLatch . countDown ( ) ) ; setupDefaultAccountInfoHandler ( ) ; websocketEndpoint = new WebsocketClientEndpoint ( new URI ( BITFINEX_URI ) , this :: websocketCallback , r -> callbackRegistry . acceptConnectionStateChange ( BitfinexConnectionStateEnum . DISCONNECTION_BY_REMOTE ) , t -> callbackRegistry . acceptConnectionStateChange ( BitfinexConnectionStateEnum . DISCONNECTION_BY_REMOTE ) ) ; websocketEndpoint . connect ( ) ; updateConnectionHeartbeat ( ) ; connectionFeatureManager . applyConnectionFeatures ( ) ; if ( configuration . isAuthenticationEnabled ( ) ) { authenticateAndWait ( connectionReadyLatch ) ; } authSuccessEventCallback . close ( ) ; authFailedCallback . close ( ) ; positionInitCallback . close ( ) ; walletsInitCallback . close ( ) ; orderInitCallback . close ( ) ; if ( configuration . isHeartbeatThreadActive ( ) ) { heartbeatThread = new Thread ( new HeartbeatThread ( this , websocketEndpoint , lastHeartbeat :: get ) ) ; heartbeatThread . start ( ) ; } connectionStateChange ( BitfinexConnectionStateEnum . CONNECTION_SUCCESS ) ; } catch ( final Exception e ) { connectionStateChange ( BitfinexConnectionStateEnum . CONNECTION_FAILED ) ; throw new BitfinexClientException ( e ) ; } }
Open the connection