- Storing connectivity status at the FullNode class

- Flipping this flag accordingly at the NetworkService whenever we connect/disconnect from a specific node
- Notifying the NodeLatencyVerifier of this change
This commit is contained in:
Nelson R. Perez 2018-09-26 17:17:31 -05:00
parent 3bdfb777bf
commit d109d86bf3
3 changed files with 51 additions and 4 deletions

View file

@ -136,6 +136,9 @@ public class NetworkService extends Service {
// Counter used to trigger the connection only after we've received enough node latency updates
private long latencyUpdateCounter;
// Property used to keep track of the currently active node
private FullNode mSelectedNode;
private Gson gson = new GsonBuilder()
.registerTypeAdapter(Transaction.class, new Transaction.TransactionDeserializer())
.registerTypeAdapter(TransferOperation.class, new TransferOperation.TransferDeserializer())
@ -165,9 +168,9 @@ public class NetworkService extends Service {
*/
public void connect(){
OkHttpClient client = new OkHttpClient();
FullNode fullNode = nodeProvider.getBestNode();
Log.v(TAG,"connect.url: "+fullNode.getUrl());
Request request = new Request.Builder().url(fullNode.getUrl()).build();
mSelectedNode = nodeProvider.getBestNode();
Log.v(TAG,"connect.url: "+ mSelectedNode.getUrl());
Request request = new Request.Builder().url(mSelectedNode.getUrl()).build();
mWebSocket = client.newWebSocket(request, mWebSocketListener);
}
@ -254,7 +257,6 @@ public class NetworkService extends Service {
// Feeding all node information to the NodeProvider instance
for(String nodeUrl : nodeUrls){
Log.d(TAG, "NodeUrl: " + nodeUrl);
nodeProvider.addNode(new FullNode(nodeUrl));
}
@ -325,6 +327,12 @@ public class NetworkService extends Service {
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
// Marking the selected node as connected
mSelectedNode.setConnected(true);
// Updating the selected node's 'connected' status on the NodeLatencyVerifier instance
nodeLatencyVerifier.updateActiveNodeInformation(mSelectedNode);
// Notifying all listeners about the new connection status
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.CONNECTED, ApiAccess.API_NONE));
@ -555,6 +563,16 @@ public class NetworkService extends Service {
RxBus.getBusInstance().send(new ConnectionStatusUpdate(ConnectionStatusUpdate.DISCONNECTED, ApiAccess.API_NONE));
isLoggedIn = false;
// Marking the selected node as not connected
mSelectedNode.setConnected(false);
// Updating the selected node's 'connected' status on the NodeLatencyVerifier instance
nodeLatencyVerifier.updateActiveNodeInformation(mSelectedNode);
// We have currently no selected node
mSelectedNode = null;
}
@Override
@ -590,6 +608,14 @@ public class NetworkService extends Service {
}
}, DEFAULT_RETRY_DELAY);
}
// Marking the selected node as not connected
mSelectedNode.setConnected(false);
// Updating the selected node's 'connected' status on the NodeLatencyVerifier instance
nodeLatencyVerifier.updateActiveNodeInformation(mSelectedNode);
// We have currently no selected node
mSelectedNode = null;
}
};

View file

@ -9,6 +9,7 @@ public class FullNode implements Comparable {
private String mUrl;
private ExponentialMovingAverage latency;
private boolean isConnected;
private FullNode(){}
@ -49,6 +50,14 @@ public class FullNode implements Comparable {
return latency.getAverage();
}
public boolean isConnected() {
return isConnected;
}
public void setConnected(boolean connected) {
isConnected = connected;
}
/**
* Method that updates the latency average with a new value.
* @param latency Most recent latency sample to be added to the exponential average

View file

@ -162,4 +162,16 @@ public class NodeLatencyVerifier {
webSocket.close(NetworkService.NORMAL_CLOSURE_STATUS, null);
}
};
/**
* Updates the 'isConnected' attribute of a specific node.
* @param fullNode The node we want to update.
*/
public void updateActiveNodeInformation(FullNode fullNode){
for(FullNode node : mNodeList){
if(node.equals(fullNode)){
node.setConnected(fullNode.isConnected());
}
}
}
}