From 606f7c183e170285d45f6977faba435966dac4e9 Mon Sep 17 00:00:00 2001 From: "Nelson R. Perez" Date: Fri, 12 Jul 2019 17:51:50 -0500 Subject: [PATCH] Fixed a concurrency problem Introduced some synchronized methods and a block in order to prevent an aparent concurrency problem with the variable 'mSelectedNode' at the NetworkService class. --- .../graphenej/api/android/NetworkService.java | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java index dcc2bf3..32e6918 100644 --- a/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java +++ b/graphenej/src/main/java/cy/agorise/graphenej/api/android/NetworkService.java @@ -226,15 +226,18 @@ public class NetworkService extends Service { .readTimeout(5, TimeUnit.SECONDS) .writeTimeout(5, TimeUnit.SECONDS) .build(); - mSelectedNode = nodeProvider.getBestNode(); - if(mSelectedNode != null){ - Log.d(TAG,"Trying to connect to: "+ mSelectedNode.getUrl()); - Request request = new Request.Builder().url(mSelectedNode.getUrl()).build(); - mWebSocket = client.newWebSocket(request, mWebSocketListener); - }else{ - Log.d(TAG,"Could not find best node, reescheduling"); - // If no node could be found yet, schedule a new attempt in DEFAULT_INITIAL_DELAY ms - mHandler.postDelayed(mConnectAttempt, DEFAULT_INITIAL_DELAY); + + synchronized (mWebSocketListener){ + mSelectedNode = nodeProvider.getBestNode(); + if(mSelectedNode != null){ + Log.d(TAG,"Trying to connect to: "+ mSelectedNode.getUrl()); + Request request = new Request.Builder().url(mSelectedNode.getUrl()).build(); + mWebSocket = client.newWebSocket(request, mWebSocketListener); + }else{ + Log.d(TAG,"Could not find best node, reescheduling"); + // If no node could be found yet, schedule a new attempt in DEFAULT_INITIAL_DELAY ms + mHandler.postDelayed(mConnectAttempt, DEFAULT_INITIAL_DELAY); + } } } @@ -416,7 +419,7 @@ public class NetworkService extends Service { private WebSocketListener mWebSocketListener = new WebSocketListener() { @Override - public void onOpen(WebSocket webSocket, Response response) { + public synchronized void onOpen(WebSocket webSocket, Response response) { super.onOpen(webSocket, response); // Marking the selected node as connected @@ -441,7 +444,7 @@ public class NetworkService extends Service { } @Override - public void onMessage(WebSocket webSocket, String text) { + public synchronized void onMessage(WebSocket webSocket, String text) { super.onMessage(webSocket, text); Log.v(TAG,"<- "+text); JsonRpcNotification notification = gson.fromJson(text, JsonRpcNotification.class); @@ -698,10 +701,9 @@ public class NetworkService extends Service { * @param tryReconnection States if a reconnection to other node should be tried. * @param penalizeNode Whether or not to penalize the current node with a very high latency reading. */ - private void handleWebSocketDisconnection(boolean tryReconnection, boolean penalizeNode) { + private synchronized void handleWebSocketDisconnection(boolean tryReconnection, boolean penalizeNode) { Log.d(TAG,"handleWebSocketDisconnection. try reconnection: " + tryReconnection + ", penalizeNode: " + penalizeNode); RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE)); - isLoggedIn = false; // Clearing previous request id to class mappings