wip implement pingClient call

This commit is contained in:
pierre
2023-08-24 13:01:26 +02:00
parent f1b9cf4d68
commit d108919cf6
4 changed files with 51 additions and 3 deletions
@@ -7,12 +7,12 @@ plugins {
android {
namespace = "net.nymtech.nyms5"
compileSdk = 33
compileSdk = 34
defaultConfig {
applicationId = "net.nymtech.nyms5"
minSdk = 24
targetSdk = 33
targetSdk = 34
versionCode = 1
versionName = "1.0"
@@ -22,6 +22,8 @@ class NymProxy {
Log.i(tag, "loaded native library $nymNativeLib")
}
// this is a blocking call as `startClient` is blocking and will releases when
// the socks5 connection has been terminated
fun start(serviceProvider: String, onStartCbObj: Any, onStopCbObj: Any) {
Log.d(tag, "calling $nymNativeLib:startClient")
try {
@@ -42,6 +44,17 @@ class NymProxy {
}
}
fun ping(): Boolean {
Log.d(tag, "calling $nymNativeLib:pingClient")
try {
return pingClient()
} catch (e: Throwable) {
Log.e(tag, "$nymNativeLib:pingClient internal error: $e")
Sentry.captureException(e)
}
return false
}
fun getState(): State {
Log.d(tag, "calling $nymNativeLib:getClientState")
try {
@@ -66,4 +79,5 @@ class NymProxy {
private external fun stopClient()
private external fun getClientState(): Int
private external fun pingClient(): Boolean
}
@@ -17,6 +17,10 @@ import androidx.work.WorkerParameters
import androidx.work.workDataOf
import com.github.kittinunf.fuel.Fuel
import io.sentry.Sentry
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
@@ -45,6 +49,8 @@ class ProxyWorker(
private val tag = "proxyWorker"
private val pingRate = 1000L
private val spUrl = context.getString(R.string.sp_url)
private val defaultSp = context.getString(R.string.default_sp)
@@ -135,7 +141,24 @@ class ProxyWorker(
Log.w(tag, "using a default service provider $defaultSp")
}
nymProxy.start(serviceProvider ?: defaultSp, onStartCb, onStopCb)
withContext(Dispatchers.IO) {
val pingJob = launch {
do {
val pong = nymProxy.ping()
delay(pingRate)
} while (pong)
}
val proxyJob = launch {
nymProxy.start(serviceProvider ?: defaultSp, onStartCb, onStopCb)
}
// wait for the underlying call to `startClient` to be released which means
// the connection has been terminated (`startClient` is a blocking call)
proxyJob.join()
// stop pinging
pingJob.cancel()
}
// the state should be already set to DISCONNECTED at this point
// but for the sake of it, reset it
+11
View File
@@ -2,6 +2,7 @@ use crate::ClientState;
use ::safer_ffi::prelude::*;
use jni::{
objects::{JClass, JObject, JString},
sys::jboolean,
sys::jint,
JNIEnv,
};
@@ -89,3 +90,13 @@ pub unsafe extern "C" fn Java_net_nymtech_nyms5_NymProxy_getClientState(
ClientState::Disconnected => 2,
}
}
#[no_mangle]
pub unsafe extern "C" fn Java_net_nymtech_nyms5_NymProxy_pingClient(
_env: JNIEnv,
_class: JClass,
) -> jboolean {
// TODO: implement
log::debug!("ping received");
1
}