From 14ebadc78648301c8a66953a2909391bb1c58b10 Mon Sep 17 00:00:00 2001 From: Volker Birk Date: Sun, 5 May 2019 08:11:22 +0200 Subject: [PATCH] introduce engine.startSync() and engine.stopSync() --- src/org/pEp/jniadapter/AbstractEngine.java | 3 ++ src/org_pEp_jniadapter_AbstractEngine.cc | 44 +++++++++++++--------- test/Makefile | 4 +- test/Testing.java | 30 ++++++++++----- 4 files changed, 53 insertions(+), 28 deletions(-) diff --git a/src/org/pEp/jniadapter/AbstractEngine.java b/src/org/pEp/jniadapter/AbstractEngine.java index 326e60e..afe22df 100644 --- a/src/org/pEp/jniadapter/AbstractEngine.java +++ b/src/org/pEp/jniadapter/AbstractEngine.java @@ -39,6 +39,9 @@ abstract class AbstractEngine implements AutoCloseable { public native void startKeyserverLookup(); public native void stopKeyserverLookup(); + public native void startSync(); + public native void stopSync(); + public static byte[] toUTF8(String str) { if (str == null) return null; diff --git a/src/org_pEp_jniadapter_AbstractEngine.cc b/src/org_pEp_jniadapter_AbstractEngine.cc index 8a454b4..91831b6 100644 --- a/src/org_pEp_jniadapter_AbstractEngine.cc +++ b/src/org_pEp_jniadapter_AbstractEngine.cc @@ -24,12 +24,6 @@ static struct _debug_log { #include "throw_pEp_exception.hh" #include "jniutils.hh" -#ifdef ANDROID -#define ATTACH_CURRENT_THREAD(env, args) jvm->AttachCurrentThread(&env, args); -#else -#define ATTACH_CURRENT_THREAD(env, args) jvm->AttachCurrentThread((void **) &env, args); -#endif - namespace pEp { using namespace pEp::JNIAdapter; using namespace pEp::Adapter; @@ -61,8 +55,13 @@ namespace pEp { { JNIEnv *thread_env = nullptr; int status = jvm->GetEnv((void**)&thread_env, JNI_VERSION_1_6); - if (status < 0) - status = ATTACH_CURRENT_THREAD(thread_env, nullptr); + if (status < 0) { +#ifdef ANDROID + status = jvm->AttachCurrentThread(&thread_env, nullptr); +#else + status = jvm->AttachCurrentThread((void **) &thread_env, nullptr); +#endif + } assert(status >= 0); return thread_env; } @@ -188,18 +187,10 @@ extern "C" { env->GetJavaVM(&jvm); jni_init(); obj = env->NewGlobalRef(me); + _messageToSend = messageToSend; } - -#ifdef DISABLE_SYNC - _messageToSend = messageToSend; - session(); -#else - if (first) { - debug_log << "######## starting sync\n"; - startup(messageToSend, notifyHandshake, &o, &JNISync::startup_sync, &JNISync::shutdown_sync); - } -#endif first = false; + session(); } JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_release( @@ -316,5 +307,22 @@ extern "C" { free(thread); } + JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_startSync( + JNIEnv *env, + jobject obj + ) + { + debug_log << "######## starting sync\n"; + startup(messageToSend, notifyHandshake, &o, &JNISync::startup_sync, &JNISync::shutdown_sync); + } + + JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_stopSync( + JNIEnv *env, + jobject obj + ) + { + shutdown(); + } + } // extern "C" diff --git a/test/Makefile b/test/Makefile index a20f0a8..450f4a6 100644 --- a/test/Makefile +++ b/test/Makefile @@ -13,4 +13,6 @@ test: Testing.class SyncCallbacks.class clean: rm -f *.class rm -f *.log - rm -f *.pEp_* + rm -f .pEp_* + rm -Rf .gnupg + rm -Rf .lldb diff --git a/test/Testing.java b/test/Testing.java index 0fea731..a7fd4f1 100644 --- a/test/Testing.java +++ b/test/Testing.java @@ -114,17 +114,29 @@ class Testing { ex.printStackTrace(); } -// try { -// Thread.sleep(1000); -// } -// catch (InterruptedException ex) { -// -// } + e.startSync(); + + // Keygen + System.out.println("Generating keys: "); + Identity user2 = new Identity(); + user2.user_id = "pEp_own_userId"; + user2.me = true; + user2.username = "Test User 2"; + user2.address = "jniTestUser2@peptest.ch"; + user2 = e.myself(user2); + System.out.print("Keys generated: "); + System.out.println(user2.fpr); + + // it's not necessary - you can just shutdown Sync and that's it + // but for this test give sync a chance to process all messages + try { + Thread.sleep(200); + } + catch (InterruptedException ex) { } + + e.stopSync(); System.exit(0); } - - - }