Skip to content

Commit

Permalink
Merge pull request #203 from mysteriumnetwork/reconnect-dead-session
Browse files Browse the repository at this point in the history
Reconnect on network change
  • Loading branch information
zolia authored Jul 10, 2020
2 parents fe023a1 + 4432c12 commit 1e4a491
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 deletions.
4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ dependencies {
androidTestImplementation 'com.android.support.test.espresso:espresso-contrib:3.0.2'
androidTestImplementation 'androidx.test:rules:1.3.0-alpha03'

implementation 'network.mysterium:mobile-node:0.35.1-1snapshot-20200629T1411-494e56a1'
implementation 'network.mysterium:mobile-node:0.36.0'
// Comment network.mysterium:mobile-node and replace with your local path to use local node build.
// implementation files('/Users/anjmao/go/src/github.com/mysteriumnetwork/node/build/package/Mysterium.aar')
//implementation files('/Users/zolia/golang/src/github.com/mysteriumnetwork/node/build/package/Mysterium.aar')
}

// Run this once to be able to run the application with BUCK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class MainActivity : AppCompatActivity() {
// start mobile node and initial data when network is available.
CoroutineScope(Dispatchers.Main).launch {
val coreService = deferredMysteriumCoreService.await()
coreService.startConnectivityChecker()
coreService.startConnectivityChecker(appContainer.nodeRepository)
coreService.networkConnState().observe(this@MainActivity, Observer {
CoroutineScope(Dispatchers.Main).launch { handleConnChange(it) }
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ import android.os.IBinder
import android.util.Log
import androidx.lifecycle.MutableLiveData
import kotlinx.coroutines.*
import mysterium.ConnectRequest
import mysterium.MobileNode
import mysterium.Mysterium
import network.mysterium.NotificationFactory
import network.mysterium.ui.ProposalViewItem
import network.mysterium.ui.ServiceType

class NetworkConnState {
var wifiConn = false
Expand Down Expand Up @@ -102,7 +102,7 @@ class MysteriumAndroidCoreService : VpnService() {
}
}

private fun startConnectivityChecker(ctx: Context, onChange: (netState: NetworkConnState) -> Unit) {
private fun startConnectivityChecker(ctx: Context, onChange: (netState: NetworkConnState) -> Unit, nodeRepository: NodeRepository) {
val connectivityManager = ctx.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val wifiManager = ctx.applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager

Expand Down Expand Up @@ -136,6 +136,16 @@ class MysteriumAndroidCoreService : VpnService() {
CoroutineScope(Dispatchers.Main).launch {
netConnState.value = newValue
}
if (activeProposal != null) {
val req = ConnectRequest()

req.identityAddress = nodeRepository.getIdentity().address
req.providerID = activeProposal!!.providerID
req.serviceType = activeProposal!!.serviceType.type
req.forceReconnect = true
Log.i(TAG, "Reconnecting identity ${req.identityAddress} to provider ${req.providerID} with service ${req.serviceType}")
nodeRepository.reconnect(req)
}
}
} catch (e: Exception) {
Log.e(TAG, "Failed to check network connection state", e)
Expand Down Expand Up @@ -172,9 +182,9 @@ class MysteriumAndroidCoreService : VpnService() {
return netConnState
}

override fun startConnectivityChecker() {
startConnectivityChecker(applicationContext) {
}
override fun startConnectivityChecker(nodeRepository: NodeRepository) {
startConnectivityChecker(applicationContext, {
}, nodeRepository)
}

override fun startNode(): MobileNode {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ interface MysteriumCoreService : IBinder {

fun stopNode()

fun startConnectivityChecker()
fun startConnectivityChecker(nodeRepository: NodeRepository)

fun networkConnState(): MutableLiveData<NetworkConnState>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ class NodeRepository(private val deferredNode: DeferredNode) {
}
}

// Reconnect to VPN service.
suspend fun reconnect(req: ConnectRequest) = withContext(Dispatchers.IO) {
val res = deferredNode.await().reconnect(req) ?: return@withContext

when(res.errorCode) {
"InvalidProposal" -> throw ConnectInvalidProposalException(res.errorMessage)
"InsufficientBalance" -> throw ConnectInsufficientBalanceException(res.errorMessage)
"Unknown" -> throw ConnectUnknownException(res.errorMessage)
}
}

// Disconnect from VPN service.
suspend fun disconnect() = withContext(Dispatchers.IO) {
deferredNode.await().disconnect()
Expand Down
7 changes: 5 additions & 2 deletions android/app/src/main/java/network/mysterium/ui/PriceUtils.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package network.mysterium.ui

import android.util.Log
import network.mysterium.service.core.ProposalPaymentMethod
import network.mysterium.service.core.ProposalPaymentMoney
import java.text.DecimalFormat
Expand All @@ -20,7 +21,7 @@ object PriceUtils {
)
}

val bytesInGiB = 1024 * 1024 * 1024
private const val bytesInGiB = 1024 * 1024 * 1024

fun pricePerGiB(pm: ProposalPaymentMethod?): ProposalPaymentMoney {
if (pm == null || pm.rate.perSeconds == 0L) {
Expand All @@ -31,8 +32,10 @@ object PriceUtils {
return ProposalPaymentMoney(amount = 0.0, currency = currency)
}

var a = if (pm.rate.perBytes > 0L) (bytesInGiB / pm.rate.perBytes) * pm.price.amount else 0.0

return ProposalPaymentMoney(
amount = (bytesInGiB / pm.rate.perBytes) * pm.price.amount,
amount = a,
currency = pm.price.currency
)
}
Expand Down
2 changes: 1 addition & 1 deletion fastlane/android_version_code
Original file line number Diff line number Diff line change
@@ -1 +1 @@
107071
107072

0 comments on commit 1e4a491

Please sign in to comment.