diff --git a/src/basic_api.cc b/src/basic_api.cc index bc24ec9..9aa8e63 100644 --- a/src/basic_api.cc +++ b/src/basic_api.cc @@ -1,5 +1,6 @@ #include #include +#include #include "throw_pEp_exception.hh" #include "jniutils.hh" @@ -259,5 +260,22 @@ JNIEXPORT jboolean JNICALL Java_org_pEp_jniadapter_Engine_blacklist_1is_1listed( return (jboolean)_listed; } +JNIEXPORT void JNICALL Java_org_pEp_jniadapter_Engine_sync_1hanshake_1result( + JNIEnv *env, + jobject obj, + jint result + ) +{ + PEP_SESSION session = (PEP_SESSION) callLongMethod(env, obj, "getHandle"); + + PEP_STATUS status = + ::deliverHandshakeResult(session, (sync_handshake_result) result); + + if (status != PEP_STATUS_OK) { + throw_pEp_Exception(env, status); + return; + } +} + } // extern "C" diff --git a/src/gen_java_Engine.ysl2 b/src/gen_java_Engine.ysl2 index deac117..874d2dd 100644 --- a/src/gen_java_Engine.ysl2 +++ b/src/gen_java_Engine.ysl2 @@ -42,6 +42,8 @@ tstylesheet { |> «$pitype» _«$pname» = «$pname».getBytes(); when "$ptype = 'bool'" |> «$pitype» _«$pname» = «$pname».booleanValue(); + when "ancestor::namespace/child::enum[@name=$ptype]" + |> «$pitype» _«$pname» = «$pname».value(); otherwise |> «$pitype» _«$pname» = new «$pitype»(«$pname»); } diff --git a/src/org/pEp/jniadapter/AbstractEngine.java b/src/org/pEp/jniadapter/AbstractEngine.java index 7978cf7..71c4db9 100644 --- a/src/org/pEp/jniadapter/AbstractEngine.java +++ b/src/org/pEp/jniadapter/AbstractEngine.java @@ -28,11 +28,17 @@ abstract class AbstractEngine implements AutoCloseable { } private long keyserverThread; - private long queueThread; + private long keyserverQueue; + + private long syncThread; + private long syncQueue; 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; @@ -40,7 +46,6 @@ abstract class AbstractEngine implements AutoCloseable { try { String _str = Normalizer.normalize(str, Normalizer.Form.NFC); byte _buf[] = _str.getBytes("UTF-8"); - // F*ck you, Java ! byte _cpy[] = new byte[_buf.length]; System.arraycopy(_buf,0,_cpy,0,_buf.length); return _cpy; diff --git a/src/org_pEp_jniadapter_AbstractEngine.cc b/src/org_pEp_jniadapter_AbstractEngine.cc index b480794..0ba6111 100644 --- a/src/org_pEp_jniadapter_AbstractEngine.cc +++ b/src/org_pEp_jniadapter_AbstractEngine.cc @@ -5,6 +5,8 @@ #include #include #include +#include +#include #include "throw_pEp_exception.hh" #include "jniutils.hh" @@ -85,7 +87,7 @@ extern "C" { return ident; } - static void *start_routine(void *arg) + static void *keyserver_thread_routine(void *arg) { PEP_STATUS status = do_keymanagement(retrieve_next_identity, arg); @@ -117,7 +119,7 @@ extern "C" { try { thread_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "keyserverThread", "J"); - queue_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "queueThread", "J"); + queue_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "keyserverQueue", "J"); } catch (std::exception& ex) { assert(0); @@ -135,7 +137,7 @@ extern "C" { queue = new locked_queue< pEp_identity * >(); env->SetLongField(obj, queue_handle, (jlong) queue); - pthread_create(thread, NULL, start_routine, (void *) queue); + pthread_create(thread, NULL, keyserver_thread_routine, (void *) queue); register_examine_function(session, examine_identity, (void *) queue); } @@ -154,7 +156,7 @@ extern "C" { try { thread_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "keyserverThread", "J"); - queue_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "queueThread", "J"); + queue_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "keyserverQueue", "J"); } catch (std::exception& ex) { assert(0); @@ -177,6 +179,142 @@ extern "C" { free(thread); } + ///////////////////////////////////////////////////////////////////////// + // Sync message callbacks, queue, and thread + ///////////////////////////////////////////////////////////////////////// + + // Called by sync thread only + PEP_STATUS show_handshake(void *obj, pEp_identity *me, pEp_identity *partner) + { + // TODO : callback + + return PEP_STATUS_OK; + } + + PEP_STATUS message_to_send(void *obj, message *msg) + { + // TODO : callback + + return PEP_STATUS_OK; + } + + int inject_sync_msg(void *msg, void *arg) + { + locked_queue< message * > *queue = (locked_queue< message * > *) arg; + queue->push_back(message_dup((message*)msg)); + return 0; + } + + void *retrieve_next_sync_msg(void *arg) + { + locked_queue< message * > *queue = (locked_queue< message * > *) arg; + + while (!queue->size()) + // TODO: add blocking dequeue + usleep(100000); + + void *msg = queue->front(); + queue->pop_front(); + return msg; + } + + static void *sync_thread_routine(void *arg) + { + PEP_STATUS status = do_keymanagement(retrieve_next_identity, arg); + + locked_queue< message * > *queue = (locked_queue< message * > *) arg; + + while (queue->size()) { + message *msg = queue->front(); + queue->pop_front(); + free_message(msg); + } + + delete queue; + + return (void *) status; + } + + JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_startSync( + JNIEnv *env, + jobject obj + ) + { + PEP_SESSION session = (PEP_SESSION) callLongMethod(env, obj, "getHandle"); + + pthread_t *thread = NULL; + locked_queue< message * > *queue = NULL; + + jfieldID thread_handle; + jfieldID queue_handle; + + try { + thread_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "syncThread", "J"); + queue_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "syncQueue", "J"); + } + catch (std::exception& ex) { + assert(0); + return; + } + + thread = (pthread_t *) env->GetLongField(obj, thread_handle); + if (thread) + return; + + thread = (pthread_t *) calloc(1, sizeof(pthread_t)); + assert(thread); + env->SetLongField(obj, thread_handle, (jlong) thread); + + queue = new locked_queue< message * >(); + env->SetLongField(obj, queue_handle, (jlong) queue); + + pthread_create(thread, NULL, sync_thread_routine, (void *) queue); + + register_sync_callbacks(session, + (void *) queue, + message_to_send, + show_handshake, + inject_sync_msg, + retrieve_next_sync_msg); + } + + JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_stopSync( + JNIEnv *env, + jobject obj + ) + { + PEP_SESSION session = (PEP_SESSION) callLongMethod(env, obj, "getHandle"); + + pthread_t *thread = NULL; + locked_queue< message * > *queue = NULL; + + jfieldID thread_handle; + jfieldID queue_handle; + + try { + thread_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "syncThread", "J"); + queue_handle = getFieldID(env, "org/pEp/jniadapter/Engine", "syncQueue", "J"); + } + catch (std::exception& ex) { + assert(0); + return; + } + + thread = (pthread_t *) env->GetLongField(obj, thread_handle); + if (!thread) + return; + + queue = (locked_queue< message * > *) env->GetLongField(obj, queue_handle); + + env->SetLongField(obj, queue_handle, (jlong) 0); + env->SetLongField(obj, thread_handle, (jlong) 0); + + register_sync_callbacks(session, NULL, NULL, NULL, NULL, NULL); + + queue->push_front(NULL); + pthread_join(*thread, NULL); + free(thread); + } } // extern "C" diff --git a/src/pEp.yml2 b/src/pEp.yml2 index d0e2d9e..938710a 100644 --- a/src/pEp.yml2 +++ b/src/pEp.yml2 @@ -57,23 +57,27 @@ namespace pEp { }; enum Color { - pEp_rating_undefined > 0 - pEp_rating_cannot_decrypt > 1 - pEp_rating_have_no_key > 2 - pEp_rating_unencrypted > 3 - pEp_rating_unencrypted_for_some > 4 - pEp_rating_unreliable > 5 - pEp_rating_reliable > 6 - pEp_rating_yellow > 6 - pEp_rating_trusted > 7 - pEp_rating_green > 7 - pEp_rating_trusted_and_anonymized > 8 - pEp_rating_fully_anonymous > 9 - - pEp_rating_mistrust > -1 - pEp_rating_red > -1 - pEp_rating_b0rken > -2 - pEp_rating_under_attack > -3 + PEP_color_no_color > 0 + PEP_color_yellow > 1 + PEP_color_green > 2 + PEP_color_red > -1 + }; + + enum Rating { + PEP_rating_undefined > 0 + PEP_rating_cannot_decrypt > 1 + PEP_rating_have_no_key > 2 + PEP_rating_unencrypted > 3 + PEP_rating_unencrypted_for_some > 4 + PEP_rating_unreliable > 5 + PEP_rating_reliable > 6 + PEP_rating_trusted > 7 + PEP_rating_trusted_and_anonymized > 8 + PEP_rating_fully_anonymous > 9 + + PEP_rating_mistrust > -1 + PEP_rating_b0rken > -2 + PEP_rating_under_attack > -3 }; enum DecryptFlags { @@ -91,7 +95,8 @@ namespace pEp { in message src, in stringlist extra, creates message dst, - Cconst PEP_enc_format encformat "PEP_enc_PEP" + Cconst PEP_enc_format encformat "PEP_enc_PEP", + Cconst PEP_encrypt_flags flage "0" ); method encrypt_message_for_self( @@ -105,13 +110,13 @@ namespace pEp { in message src, creates message dst, creates stringlist keylist, - returns Color color, + returns Rating rating, returns DecryptFlags flags ); - method outgoing_message_color( + method outgoing_message_rating( in message msg, - returns Color color + returns Rating rating ); method get_identity( @@ -120,9 +125,9 @@ namespace pEp { returns identity ident ); - method identity_color( + method identity_rating( in identity ident, - returns Color color + returns Rating rating ); method blacklist_retrieve( @@ -146,6 +151,7 @@ namespace pEp { basic bool blacklist_is_listed(string fpr); basic void config_passive_mode(bool enable); basic void config_unencrypted_subject(bool enable); + basic void deliver_handshake_result(SyncHandshakeResult result); }; diff --git a/src/types_c.ysl2 b/src/types_c.ysl2 index 8c1de1b..6bb9eee 100644 --- a/src/types_c.ysl2 +++ b/src/types_c.ysl2 @@ -14,7 +14,7 @@ function "toC" { when "$type='bool'" > bool when "$type='DecryptFlags'" > PEP_decrypt_flags_t when "$type='Color'" > PEP_color - when "$type='SyncHandshakeResult'" > sync_handshake_result + when "$type='Rating'" > PEP_rating otherwise value "$type"; } diff --git a/src/types_java.ysl2 b/src/types_java.ysl2 index c52fd03..9d6b847 100644 --- a/src/types_java.ysl2 +++ b/src/types_java.ysl2 @@ -1,4 +1,4 @@ - function "toJava" { +function "toJava" { param "type"; choose { @@ -34,6 +34,7 @@ function "toJavaDeclare" { when "$type='stringpairlist'" | # cannot declare "stringpairlist" when "$type='message'" > Message when "$type='Color'" > Color + when "$type='Rating'" > Rating //when "$type='bool'" > boolean otherwise error | # cannot declare "«$type»" @@ -60,8 +61,8 @@ function "toSig" { when "$type='stringpairlist'" > java/util/ArrayList when "$type='message'" > org/pEp/jniadapter/Message when "$type='Color'" > org/pEp/jniadapter/Color + when "$type='Rating'" > org/pEp/jniadapter/Rating when "$type='DecryptFlags'" > org/pEp/jniadapter/DecryptFlags - when "$type='SyncHandshakeResult'" > org/pEp/jniadapter/SyncHandshakeResult } > ; } @@ -80,7 +81,7 @@ function "toIntermediate" { when "$type='bloblist'" > Vector<_Blob> when "$type='bool'" > boolean when "$type='DecryptFlags'" > DecryptFlags - when "$type='SyncHandshakeResult'" > SyncHandshakeResult + when "$type='SyncHandshakeResult'" > int otherwise call "toJava" with "type", "$type"; }