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.
develop
Nelson R. Perez 2019-07-12 17:51:50 -05:00
parent 7a5a975e4b
commit 606f7c183e
1 changed files with 15 additions and 13 deletions

View File

@ -226,15 +226,18 @@ public class NetworkService extends Service {
.readTimeout(5, TimeUnit.SECONDS) .readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS) .writeTimeout(5, TimeUnit.SECONDS)
.build(); .build();
mSelectedNode = nodeProvider.getBestNode();
if(mSelectedNode != null){ synchronized (mWebSocketListener){
Log.d(TAG,"Trying to connect to: "+ mSelectedNode.getUrl()); mSelectedNode = nodeProvider.getBestNode();
Request request = new Request.Builder().url(mSelectedNode.getUrl()).build(); if(mSelectedNode != null){
mWebSocket = client.newWebSocket(request, mWebSocketListener); Log.d(TAG,"Trying to connect to: "+ mSelectedNode.getUrl());
}else{ Request request = new Request.Builder().url(mSelectedNode.getUrl()).build();
Log.d(TAG,"Could not find best node, reescheduling"); mWebSocket = client.newWebSocket(request, mWebSocketListener);
// If no node could be found yet, schedule a new attempt in DEFAULT_INITIAL_DELAY ms }else{
mHandler.postDelayed(mConnectAttempt, DEFAULT_INITIAL_DELAY); 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() { private WebSocketListener mWebSocketListener = new WebSocketListener() {
@Override @Override
public void onOpen(WebSocket webSocket, Response response) { public synchronized void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response); super.onOpen(webSocket, response);
// Marking the selected node as connected // Marking the selected node as connected
@ -441,7 +444,7 @@ public class NetworkService extends Service {
} }
@Override @Override
public void onMessage(WebSocket webSocket, String text) { public synchronized void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text); super.onMessage(webSocket, text);
Log.v(TAG,"<- "+text); Log.v(TAG,"<- "+text);
JsonRpcNotification notification = gson.fromJson(text, JsonRpcNotification.class); 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 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. * @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); Log.d(TAG,"handleWebSocketDisconnection. try reconnection: " + tryReconnection + ", penalizeNode: " + penalizeNode);
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE)); RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE));
isLoggedIn = false; isLoggedIn = false;
// Clearing previous request id to class mappings // Clearing previous request id to class mappings