Preventing a potential problem at the NetworkServiceManager and introducing some getters

- Preventing accidentally calling the 'bindService' twice at the NetworkServiceManager
- Providing a getter for the node list at the NodeLatencyVerifier
- Created a getter for the NodeLatencyVerifier at the NetworkService
This commit is contained in:
Nelson R. Perez 2019-06-13 07:49:36 -05:00
parent b032db80bc
commit 562955586b
3 changed files with 18 additions and 4 deletions

View file

@ -230,9 +230,11 @@ public class NetworkService extends Service {
.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);
}
@ -797,4 +799,6 @@ public class NetworkService extends Service {
public PublishSubject<FullNode> getNodeLatencyObservable(){
return fullNodePublishSubject;
}
public NodeLatencyVerifier getNodeLatencyVerifier(){ return nodeLatencyVerifier; }
}

View file

@ -54,7 +54,8 @@ public class NetworkServiceManager implements Application.ActivityLifecycleCallb
private List<String> mCustomNodeUrls = new ArrayList<>();
private boolean mAutoConnect;
private boolean mVerifyLatency;
private double alpha;
// Flag used to make sure we only call 'bindService' once.
private boolean mStartingService;
/**
* Runnable used to schedule a service disconnection once the app is not visible to the user for
@ -85,7 +86,9 @@ public class NetworkServiceManager implements Application.ActivityLifecycleCallb
@Override
public void onActivityResumed(Activity activity) {
mHandler.removeCallbacks(mDisconnectRunnable);
if(mService == null){
if(mService == null && !mStartingService){
// Setting this flag to 'true' to avoid repeated calls to the bindService.
mStartingService = true;
// Creating a new Intent that will be used to start the NetworkService
Context context = mContextReference.get();
Intent intent = new Intent(context, NetworkService.class);
@ -143,12 +146,15 @@ public class NetworkServiceManager implements Application.ActivityLifecycleCallb
// We've bound to LocalService, cast the IBinder and get LocalService instance
NetworkService.LocalBinder binder = (NetworkService.LocalBinder) service;
mService = binder.getService();
// Setting the 'starting service' flag back to false
mStartingService = false;
passRequiredInfoToConfigureService();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {}
public void onServiceDisconnected(ComponentName componentName) {
mStartingService = false;
}
};
public String getUserName() {

View file

@ -203,4 +203,8 @@ public class NodeLatencyVerifier {
}
}
}
public List<FullNode> getNodeList(){
return mNodeList;
}
}