|
@ -1,17 +1,4 @@ |
|
|
#include "foundation_pEp_jniadapter_AbstractEngine.h" |
|
|
#include "foundation_pEp_jniadapter_AbstractEngine.h" |
|
|
|
|
|
|
|
|
#ifndef NDEBUG |
|
|
|
|
|
#include <iostream> |
|
|
|
|
|
auto& debug_log = std::cerr; |
|
|
|
|
|
#else |
|
|
|
|
|
// the compiler should optimize this away
|
|
|
|
|
|
static struct _debug_log { |
|
|
|
|
|
_debug_log& operator<<(const char*) { return *this; } |
|
|
|
|
|
_debug_log& operator<<(int) { return *this; } |
|
|
|
|
|
_debug_log& operator<<(double) { return *this; } |
|
|
|
|
|
} debug_log; |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#include <stdexcept> |
|
|
#include <stdexcept> |
|
|
#include <unistd.h> |
|
|
#include <unistd.h> |
|
|
#include <assert.h> |
|
|
#include <assert.h> |
|
@ -20,13 +7,13 @@ static struct _debug_log { |
|
|
#include <pEp/message_api.h> |
|
|
#include <pEp/message_api.h> |
|
|
#include <pEp/sync_api.h> |
|
|
#include <pEp/sync_api.h> |
|
|
#include <pEp/Adapter.hh> |
|
|
#include <pEp/Adapter.hh> |
|
|
|
|
|
#include <pEp/utils.hh> |
|
|
#include "throw_pEp_exception.hh" |
|
|
#include "throw_pEp_exception.hh" |
|
|
#include "jniutils.hh" |
|
|
#include "jniutils.hh" |
|
|
|
|
|
|
|
|
namespace pEp { |
|
|
namespace pEp { |
|
|
using namespace pEp::JNIAdapter; |
|
|
using namespace pEp::JNIAdapter; |
|
|
using namespace utility; |
|
|
using namespace utility; // for libpEpAdapter locked queue impl. TODO:rename
|
|
|
|
|
|
|
|
|
bool first = true; |
|
|
bool first = true; |
|
|
|
|
|
|
|
@ -52,6 +39,7 @@ namespace pEp { |
|
|
public: |
|
|
public: |
|
|
JNIEnv * env() |
|
|
JNIEnv * env() |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
JNIEnv *thread_env = nullptr; |
|
|
JNIEnv *thread_env = nullptr; |
|
|
int status = jvm->GetEnv((void**)&thread_env, JNI_VERSION_1_6); |
|
|
int status = jvm->GetEnv((void**)&thread_env, JNI_VERSION_1_6); |
|
|
if (status < 0) { |
|
|
if (status < 0) { |
|
@ -67,16 +55,19 @@ namespace pEp { |
|
|
|
|
|
|
|
|
void onSyncStartup() |
|
|
void onSyncStartup() |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
env(); |
|
|
env(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void onSyncShutdown() |
|
|
void onSyncShutdown() |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
jvm->DetachCurrentThread(); |
|
|
jvm->DetachCurrentThread(); |
|
|
} |
|
|
} |
|
|
} o; |
|
|
} o; |
|
|
|
|
|
|
|
|
void jni_init() { |
|
|
void jni_init() { |
|
|
|
|
|
pEpLog("called"); |
|
|
JNIEnv *_env = o.env(); |
|
|
JNIEnv *_env = o.env(); |
|
|
|
|
|
|
|
|
messageClass = reinterpret_cast<jclass>( |
|
|
messageClass = reinterpret_cast<jclass>( |
|
@ -108,9 +99,10 @@ namespace pEp { |
|
|
|
|
|
|
|
|
PEP_STATUS messageToSend(message *msg) |
|
|
PEP_STATUS messageToSend(message *msg) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
std::lock_guard<std::mutex> l(mutex_obj); |
|
|
std::lock_guard<std::mutex> l(mutex_obj); |
|
|
|
|
|
|
|
|
debug_log << "\n############### messageToSend() called\n"; |
|
|
pEpLog("############### messageToSend() called"); |
|
|
jobject msg_ = nullptr; |
|
|
jobject msg_ = nullptr; |
|
|
|
|
|
|
|
|
assert(messageClass && messageConstructorMethodID && obj && messageToSendMethodID); |
|
|
assert(messageClass && messageConstructorMethodID && obj && messageToSendMethodID); |
|
@ -130,9 +122,10 @@ namespace pEp { |
|
|
|
|
|
|
|
|
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) |
|
|
PEP_STATUS notifyHandshake(pEp_identity *me, pEp_identity *partner, sync_handshake_signal signal) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
std::lock_guard<std::mutex> l(mutex_obj); |
|
|
std::lock_guard<std::mutex> l(mutex_obj); |
|
|
|
|
|
|
|
|
debug_log << "\n############### notifyHandshake() called\n"; |
|
|
pEpLog("############### notifyHandshake() called"); |
|
|
jobject me_ = nullptr; |
|
|
jobject me_ = nullptr; |
|
|
jobject partner_ = nullptr; |
|
|
jobject partner_ = nullptr; |
|
|
|
|
|
|
|
@ -185,7 +178,9 @@ extern "C" { |
|
|
jobject me |
|
|
jobject me |
|
|
) |
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
if (first) { |
|
|
if (first) { |
|
|
|
|
|
pEpLog("first Engine instance"); |
|
|
first = false; |
|
|
first = false; |
|
|
env->GetJavaVM(&jvm); |
|
|
env->GetJavaVM(&jvm); |
|
|
jni_init(); |
|
|
jni_init(); |
|
@ -200,21 +195,31 @@ extern "C" { |
|
|
jobject me |
|
|
jobject me |
|
|
) |
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
Adapter::session(pEp::Adapter::release); |
|
|
Adapter::session(pEp::Adapter::release); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_getVersion(JNIEnv *env, jobject) |
|
|
JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_getVersion( |
|
|
|
|
|
JNIEnv *env, |
|
|
|
|
|
jobject |
|
|
|
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
return env->NewStringUTF(::get_engine_version()); |
|
|
return env->NewStringUTF(::get_engine_version()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_getProtocolVersion(JNIEnv *env, jobject) |
|
|
JNIEXPORT jstring JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_getProtocolVersion( |
|
|
|
|
|
JNIEnv *env, |
|
|
|
|
|
jobject |
|
|
|
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
return env->NewStringUTF(::get_protocol_version()); |
|
|
return env->NewStringUTF(::get_protocol_version()); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int examine_identity(pEp_identity *ident, void *arg) |
|
|
int examine_identity(pEp_identity *ident, void *arg) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg; |
|
|
locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg; |
|
|
queue->push_back(identity_dup(ident)); |
|
|
queue->push_back(identity_dup(ident)); |
|
|
return 0; |
|
|
return 0; |
|
@ -222,6 +227,7 @@ extern "C" { |
|
|
|
|
|
|
|
|
pEp_identity *retrieve_next_identity(void *arg) |
|
|
pEp_identity *retrieve_next_identity(void *arg) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg; |
|
|
locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg; |
|
|
|
|
|
|
|
|
while (!queue->size()) |
|
|
while (!queue->size()) |
|
@ -234,6 +240,7 @@ extern "C" { |
|
|
|
|
|
|
|
|
static void *keyserver_thread_routine(void *arg) |
|
|
static void *keyserver_thread_routine(void *arg) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
PEP_STATUS status = do_keymanagement(retrieve_next_identity, arg); |
|
|
PEP_STATUS status = do_keymanagement(retrieve_next_identity, arg); |
|
|
locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg; |
|
|
locked_queue< pEp_identity * > *queue = (locked_queue< pEp_identity * > *) arg; |
|
|
|
|
|
|
|
@ -249,9 +256,10 @@ extern "C" { |
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_startKeyserverLookup( |
|
|
JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_startKeyserverLookup( |
|
|
JNIEnv *env, |
|
|
JNIEnv *env, |
|
|
jobject obj |
|
|
jobject me |
|
|
) |
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
pthread_t *thread = nullptr; |
|
|
pthread_t *thread = nullptr; |
|
|
locked_queue< pEp_identity * > *queue = nullptr; |
|
|
locked_queue< pEp_identity * > *queue = nullptr; |
|
|
|
|
|
|
|
@ -267,16 +275,16 @@ extern "C" { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
thread = (pthread_t *) env->GetLongField(obj, thread_handle); |
|
|
thread = (pthread_t *) env->GetLongField(me, thread_handle); |
|
|
if (thread) |
|
|
if (thread) |
|
|
return; |
|
|
return; |
|
|
|
|
|
|
|
|
thread = (pthread_t *) calloc(1, sizeof(pthread_t)); |
|
|
thread = (pthread_t *) calloc(1, sizeof(pthread_t)); |
|
|
assert(thread); |
|
|
assert(thread); |
|
|
env->SetLongField(obj, thread_handle, (jlong) thread); |
|
|
env->SetLongField(me, thread_handle, (jlong) thread); |
|
|
|
|
|
|
|
|
queue = new locked_queue< pEp_identity * >(); |
|
|
queue = new locked_queue< pEp_identity * >(); |
|
|
env->SetLongField(obj, queue_handle, (jlong) queue); |
|
|
env->SetLongField(me, queue_handle, (jlong) queue); |
|
|
|
|
|
|
|
|
register_examine_function(Adapter::session(), examine_identity, (void *) queue); |
|
|
register_examine_function(Adapter::session(), examine_identity, (void *) queue); |
|
|
|
|
|
|
|
@ -285,9 +293,10 @@ extern "C" { |
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_stopKeyserverLookup( |
|
|
JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_stopKeyserverLookup( |
|
|
JNIEnv *env, |
|
|
JNIEnv *env, |
|
|
jobject obj |
|
|
jobject me |
|
|
) |
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
pthread_t *thread = nullptr; |
|
|
pthread_t *thread = nullptr; |
|
|
locked_queue< pEp_identity * > *queue = nullptr; |
|
|
locked_queue< pEp_identity * > *queue = nullptr; |
|
|
|
|
|
|
|
@ -303,14 +312,14 @@ extern "C" { |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
thread = (pthread_t *) env->GetLongField(obj, thread_handle); |
|
|
thread = (pthread_t *) env->GetLongField(me, thread_handle); |
|
|
if (!thread) |
|
|
if (!thread) |
|
|
return; |
|
|
return; |
|
|
|
|
|
|
|
|
queue = (locked_queue< pEp_identity * > *) env->GetLongField(obj, queue_handle); |
|
|
queue = (locked_queue< pEp_identity * > *) env->GetLongField(me, queue_handle); |
|
|
|
|
|
|
|
|
env->SetLongField(obj, queue_handle, (jlong) 0); |
|
|
env->SetLongField(me, queue_handle, (jlong) 0); |
|
|
env->SetLongField(obj, thread_handle, (jlong) 0); |
|
|
env->SetLongField(me, thread_handle, (jlong) 0); |
|
|
|
|
|
|
|
|
register_examine_function(Adapter::session(), nullptr, nullptr); |
|
|
register_examine_function(Adapter::session(), nullptr, nullptr); |
|
|
|
|
|
|
|
@ -321,10 +330,11 @@ extern "C" { |
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_startSync( |
|
|
JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_startSync( |
|
|
JNIEnv *env, |
|
|
JNIEnv *env, |
|
|
jobject obj |
|
|
jobject me |
|
|
) |
|
|
) |
|
|
{ |
|
|
{ |
|
|
debug_log << "######## starting sync\n"; |
|
|
pEpLog("called"); |
|
|
|
|
|
pEpLog("######## starting sync"); |
|
|
try { |
|
|
try { |
|
|
Adapter::startup<JNISync>(messageToSend, notifyHandshake, &o, &JNISync::onSyncStartup, &JNISync::onSyncShutdown); |
|
|
Adapter::startup<JNISync>(messageToSend, notifyHandshake, &o, &JNISync::onSyncStartup, &JNISync::onSyncShutdown); |
|
|
} |
|
|
} |
|
@ -336,15 +346,19 @@ extern "C" { |
|
|
|
|
|
|
|
|
JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_stopSync( |
|
|
JNIEXPORT void JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_stopSync( |
|
|
JNIEnv *env, |
|
|
JNIEnv *env, |
|
|
jobject obj |
|
|
jobject me |
|
|
) |
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
Adapter::shutdown(); |
|
|
Adapter::shutdown(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_isSyncRunning |
|
|
JNIEXPORT jboolean JNICALL Java_foundation_pEp_jniadapter_AbstractEngine_isSyncRunning( |
|
|
(JNIEnv *, jobject) |
|
|
JNIEnv *env, |
|
|
|
|
|
jobject me |
|
|
|
|
|
) |
|
|
{ |
|
|
{ |
|
|
|
|
|
pEpLog("called"); |
|
|
return (jboolean) Adapter::is_sync_running(); |
|
|
return (jboolean) Adapter::is_sync_running(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|