2222import java .util .List ;
2323import java .util .Map ;
2424import java .util .concurrent .*;
25+ import java .util .concurrent .atomic .AtomicReference ;
2526
2627/**
2728 * @author Gianlu
@@ -34,7 +35,7 @@ public class DealerClient implements Closeable {
3435 private final Map <String , RequestListener > reqListeners = new HashMap <>();
3536 private final Map <MessageListener , List <String >> msgListeners = new HashMap <>();
3637 private final ScheduledExecutorService scheduler = Executors .newSingleThreadScheduledExecutor (new NameThreadFactory ((r ) -> "dealer-scheduler-" + r .hashCode ()));
37- private ConnectionHolder conn ;
38+ private final AtomicReference < ConnectionHolder > conn = new AtomicReference <>() ;
3839 private ScheduledFuture lastScheduledReconnection ;
3940
4041 public DealerClient (@ NotNull Session session ) {
@@ -46,9 +47,9 @@ public DealerClient(@NotNull Session session) {
4647 * Creates a new WebSocket client. <b>Intended for internal use only!</b>
4748 */
4849 public void connect () throws IOException , MercuryClient .MercuryException {
49- conn = new ConnectionHolder (session , new Request .Builder ()
50+ conn . set ( new ConnectionHolder (session , new Request .Builder ()
5051 .url (String .format ("wss://%s/?access_token=%s" , ApResolver .getRandomDealer (), session .tokens ().get ("playlist-read" )))
51- .build ());
52+ .build ())) ;
5253 }
5354
5455 private void waitForListeners () {
@@ -82,7 +83,7 @@ private void handleRequest(@NotNull JsonObject obj) {
8283 interesting = true ;
8384 looper .submit (() -> {
8485 RequestResult result = listener .onRequest (mid , pid , sender , command );
85- conn .sendReply (key , result );
86+ conn .get (). sendReply (key , result );
8687 LOGGER .debug (String .format ("Handled request. {key: %s, result: %s}" , key , result ));
8788 });
8889 }
@@ -166,9 +167,9 @@ public void removeRequestListener(@NotNull RequestListener listener) {
166167
167168 @ Override
168169 public void close () {
169- if (conn != null ) {
170- ConnectionHolder tmp = conn ; // Do not trigger connectionInvalided()
171- conn = null ;
170+ if (conn . get () != null ) {
171+ ConnectionHolder tmp = conn . get () ; // Do not trigger connectionInvalided()
172+ conn . set ( null ) ;
172173 tmp .close ();
173174 }
174175
@@ -188,7 +189,7 @@ private void connectionInvalided() {
188189 if (lastScheduledReconnection != null && !lastScheduledReconnection .isDone ())
189190 throw new IllegalStateException ();
190191
191- conn = null ;
192+ conn . set ( null ) ;
192193
193194 LOGGER .trace ("Scheduled reconnection attempt in 10 seconds..." );
194195 lastScheduledReconnection = scheduler .schedule (() -> {
@@ -277,10 +278,10 @@ public void close() {
277278 lastScheduledPing = null ;
278279 }
279280
280- if (conn == ConnectionHolder .this )
281+ if (conn . get () == ConnectionHolder .this )
281282 connectionInvalided ();
282283 else
283- LOGGER .debug (String .format ("Did not dispatch connection invalidated: %s != %s" , conn , ConnectionHolder .this ));
284+ LOGGER .debug (String .format ("Did not dispatch connection invalidated: %s != %s" , conn . get () , ConnectionHolder .this ));
284285 }
285286
286287 private class WebSocketListenerImpl extends WebSocketListener {
0 commit comments