diff --git a/src/basic_api.cc b/src/basic_api.cc index 439da33..bdbaf8c 100644 --- a/src/basic_api.cc +++ b/src/basic_api.cc @@ -3,9 +3,9 @@ #include "throw_pEp_exception.hh" #include "jniutils.hh" -using namespace pEp::JNIAdapter; - extern "C" { + using namespace pEp::JNIAdapter; + JNIEXPORT jobject JNICALL Java_org_pEp_jniadapter_Engine_trustwords( JNIEnv *env, jobject obj, diff --git a/src/jniutils.hh b/src/jniutils.hh index 6dca348..b7598fd 100644 --- a/src/jniutils.hh +++ b/src/jniutils.hh @@ -1,11 +1,90 @@ #pragma once +#include +#include #include #include #include #include namespace pEp { + namespace utility { + using namespace std; + + class mutex { + pthread_mutex_t _mutex; + + public: + mutex() { + pthread_mutex_init(&_mutex, NULL); + } + ~mutex() { + pthread_mutex_destroy(&_mutex); + } + void lock() { + pthread_mutex_lock(&_mutex); + } + void unlock() { + pthread_mutex_unlock(&_mutex); + } + }; + + template class lock_guard { + T& _mtx; + + public: + lock_guard(T& mtx) : _mtx(mtx) { + _mtx.lock(); + } + ~lock_guard() { + _mtx.unlock(); + } + }; + + template class locked_queue + { + mutex _mtx; + list _q; + + public: + T& back() + { + lock_guard lg(_mtx); + return _q.back(); + } + T& front() + { + lock_guard lg(_mtx); + return _q.front(); + } + void pop_back() + { + lock_guard lg(_mtx); + _q.pop_back(); + } + void pop_front() + { + lock_guard lg(_mtx); + _q.pop_front(); + } + void push_back(const T& data) + { + lock_guard lg(_mtx); + _q.push_back(data); + } + void push_front(const T& data) + { + lock_guard lg(_mtx); + _q.push_front(data); + } + size_t size() + { + lock_guard lg(_mtx); + return _q.size(); + } + }; + } + namespace JNIAdapter { jclass findClass(JNIEnv *env, const char *classname); diff --git a/src/org/pEp/jniadapter/AbstractEngine.java b/src/org/pEp/jniadapter/AbstractEngine.java index 48d1e91..799cd82 100644 --- a/src/org/pEp/jniadapter/AbstractEngine.java +++ b/src/org/pEp/jniadapter/AbstractEngine.java @@ -27,6 +27,11 @@ abstract class AbstractEngine implements AutoCloseable { release(); } + private long keyserverThread; + + public native void startKeyserverLookup(); + public native void stopKeyserverLookup(); + public static byte[] toUTF8(String str) { try { String _str = Normalizer.normalize(str, Normalizer.Form.NFC); diff --git a/src/org_pEp_jniadapter_AbstractEngine.cc b/src/org_pEp_jniadapter_AbstractEngine.cc index 26cdf16..0e2cebe 100644 --- a/src/org_pEp_jniadapter_AbstractEngine.cc +++ b/src/org_pEp_jniadapter_AbstractEngine.cc @@ -2,7 +2,8 @@ #include #include -#include +#include +#include #include "throw_pEp_exception.hh" #include "jniutils.hh" @@ -62,5 +63,62 @@ extern "C" { else env->SetLongField(me, handle, jlong(0)); } -} + + static void *start_routine(void *arg) + { + return NULL; + } + + JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_startKeyserverLookup( + JNIEnv *env, + jobject obj + ) + { + pthread_t *thread = NULL; + jfieldID handle; + + try { + handle = getFieldID(env, "org/pEp/jniadapter/Engine", "keyserverThread", "J"); + } + catch (std::exception& ex) { + assert(0); + return; + } + + thread = (pthread_t *) env->GetLongField(obj, handle); + if (thread) + return; + + thread = (pthread_t *) calloc(1, sizeof(pthread_t)); + assert(thread); + env->SetLongField(obj, handle, (jlong) thread); + + pthread_create(thread, NULL, start_routine, NULL); + } + + JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_stopKeyserverLookup( + JNIEnv *env, + jobject obj + ) + { + pthread_t *thread = NULL; + jfieldID handle; + + try { + handle = getFieldID(env, "org/pEp/jniadapter/Engine", "keyserverThread", "J"); + } + catch (std::exception& ex) { + assert(0); + return; + } + + thread = (pthread_t *) env->GetLongField(obj, handle); + if (!thread) + return; + + // stop thread + } + +} // extern "C" +