Keep the app connected to the best node.

- Added a recurrent method that verifies every minute that the app is connected to the best node, and if that is not the case then it requests a reconnection.
master
Severiano Jaramillo 2019-08-29 11:06:17 -05:00
parent 855f47e793
commit e2637ddfa4
1 changed files with 44 additions and 12 deletions

View File

@ -58,6 +58,12 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
companion object {
private const val TAG = "ConnectedActivity"
// Delay between best node connection verifications
private const val NODE_CHECK_DELAY = 60000L // 60 seconds
// Worst acceptable position of the node the app is currently connected to
private const val BEST_NODE_THRESHOLD = 1
private const val RESPONSE_GET_FULL_ACCOUNTS = 1
private const val RESPONSE_GET_ACCOUNTS = 2
private const val RESPONSE_GET_ACCOUNT_BALANCES = 3
@ -399,6 +405,31 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
}
}
/**
* Task used to verify that the app is currently connected to one of the best nodes,
* and ask for a reconnection if it is not the case.
*/
private val verifyConnectionToSuitableNodeTask = object : Runnable {
override fun run() {
Log.d(TAG, "Verifying app is connected to one of the best nodes")
mNetworkService?.nodes?.let { nodes ->
for ((counter, node) in nodes.withIndex()) {
if (counter >= BEST_NODE_THRESHOLD) {
// Forcing reconnection to a better node
mNetworkService?.reconnectNode()
break
}
if (node.isConnected) {
// App is connected to one of the best nodes
break
}
}
}
mHandler.postDelayed(this, NODE_CHECK_DELAY)
}
}
/**
* Task used to obtain the missing UserAccounts from Graphenej's NetworkService.
*/
@ -477,6 +508,19 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
override fun onServiceDisconnected(name: ComponentName?) { }
override fun onResume() {
super.onResume()
val intent = Intent(this, NetworkService::class.java)
if (bindService(intent, this, Context.BIND_AUTO_CREATE)) {
mShouldUnbindNetwork = true
} else {
Log.e(TAG, "Binding to the network service failed.")
}
mHandler.postDelayed(mCheckMissingPaymentsTask, Constants.MISSING_PAYMENT_CHECK_PERIOD)
mHandler.postDelayed(verifyConnectionToSuitableNodeTask, NODE_CHECK_DELAY)
}
override fun onPause() {
super.onPause()
mNetworkService?.nodeLatencyVerifier?.nodeList?.let { nodes ->
@ -495,18 +539,6 @@ abstract class ConnectedActivity : AppCompatActivity(), ServiceConnection {
mHandler.removeCallbacks(mRequestBlockMissingTimeTask)
}
override fun onResume() {
super.onResume()
val intent = Intent(this, NetworkService::class.java)
if (bindService(intent, this, Context.BIND_AUTO_CREATE)) {
mShouldUnbindNetwork = true
} else {
Log.e(TAG, "Binding to the network service failed.")
}
mHandler.postDelayed(mCheckMissingPaymentsTask, Constants.MISSING_PAYMENT_CHECK_PERIOD)
}
override fun onDestroy() {
super.onDestroy()
if(!mCompositeDisposable.isDisposed) mCompositeDisposable.dispose()