Trying to fix a NullPointerException by checking the refeence and enclosing the whole chunk of code into a syncronized block

This commit is contained in:
Nelson R. Perez 2018-10-15 18:35:26 -05:00
parent d25a1f3fa0
commit 36420e7f56

View file

@ -35,8 +35,6 @@ public class NodeLatencyVerifier {
private HashMap<HttpUrl, FullNode> nodeURLMap = new HashMap<>(); private HashMap<HttpUrl, FullNode> nodeURLMap = new HashMap<>();
// private WebSocket webSocket;
// Map used to store the first timestamp required for a RTT (Round Trip Time) measurement. // Map used to store the first timestamp required for a RTT (Round Trip Time) measurement.
// If: // If:
// RTT = t2 - t1 // RTT = t2 - t1
@ -104,11 +102,10 @@ public class NodeLatencyVerifier {
} }
String normalURL = fullNode.getUrl().replace("wss://", "https://"); String normalURL = fullNode.getUrl().replace("wss://", "https://");
if(!nodeURLMap.containsKey(fullNode.getUrl().replace("wss://", "https://"))){
HttpUrl key = HttpUrl.parse(normalURL); HttpUrl key = HttpUrl.parse(normalURL);
if(!nodeURLMap.containsKey(key)){
nodeURLMap.put(key, fullNode); nodeURLMap.put(key, fullNode);
} }
client.newWebSocket(request, mWebSocketListener); client.newWebSocket(request, mWebSocketListener);
} }
mHandler.postDelayed(this, verificationPeriod); mHandler.postDelayed(this, verificationPeriod);
@ -135,10 +132,11 @@ public class NodeLatencyVerifier {
* Method used to handle the node's first response. The idea here is to obtain * Method used to handle the node's first response. The idea here is to obtain
* the RTT (Round Trip Time) measurement and publish it using the PublishSubject. * the RTT (Round Trip Time) measurement and publish it using the PublishSubject.
* *
* @param webSocket Websocket instance * @param webSocket WebSocket instance
* @param response Response instance * @param response Response instance
*/ */
private void handleResponse(WebSocket webSocket, Response response){ private void handleResponse(WebSocket webSocket, Response response){
synchronized (this){
// Obtaining the HttpUrl instance that was previously used as a key // Obtaining the HttpUrl instance that was previously used as a key
HttpUrl url = webSocket.request().url(); HttpUrl url = webSocket.request().url();
if(nodeURLMap.containsKey(url)){ if(nodeURLMap.containsKey(url)){
@ -154,9 +152,12 @@ public class NodeLatencyVerifier {
long before = timestamps.get(fullNode); long before = timestamps.get(fullNode);
delay = after - before; delay = after - before;
} }
if(fullNode != null){
fullNode.addLatencyValue(delay); fullNode.addLatencyValue(delay);
subject.onNext(fullNode); subject.onNext(fullNode);
}else{
Log.w(TAG,"Could not extract FullNode instance from the map");
}
}else{ }else{
// We cannot properly handle a response to a request whose // We cannot properly handle a response to a request whose
// URL was not registered at the nodeURLMap. This is because without this, // URL was not registered at the nodeURLMap. This is because without this,
@ -168,6 +169,7 @@ public class NodeLatencyVerifier {
} }
webSocket.close(NetworkService.NORMAL_CLOSURE_STATUS, null); webSocket.close(NetworkService.NORMAL_CLOSURE_STATUS, null);
} }
}
}; };
/** /**