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.
This commit is contained in:
Nelson R. Perez 2019-07-12 17:51:50 -05:00
parent 7a5a975e4b
commit 606f7c183e

View file

@ -226,6 +226,8 @@ public class NetworkService extends Service {
.readTimeout(5, TimeUnit.SECONDS)
.writeTimeout(5, TimeUnit.SECONDS)
.build();
synchronized (mWebSocketListener){
mSelectedNode = nodeProvider.getBestNode();
if(mSelectedNode != null){
Log.d(TAG,"Trying to connect to: "+ mSelectedNode.getUrl());
@ -237,6 +239,7 @@ public class NetworkService extends Service {
mHandler.postDelayed(mConnectAttempt, DEFAULT_INITIAL_DELAY);
}
}
}
public long sendMessage(String message){
if(mWebSocket != null){
@ -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