Browse Source

Added callbacks code in abstract engine's .cc

JNI-44
Edouard Tisserant 9 years ago
parent
commit
b1b87f5680
  1. 7
      src/org/pEp/jniadapter/AbstractEngine.java
  2. 58
      src/org_pEp_jniadapter_AbstractEngine.cc

7
src/org/pEp/jniadapter/AbstractEngine.java

@ -155,21 +155,22 @@ abstract class AbstractEngine implements AutoCloseable {
this.showHandshakeCallback = showHandshakeCallback; this.showHandshakeCallback = showHandshakeCallback;
} }
//Call From C is not a good name only for sample purposes public int showHandshakeCallFromC(Identity myself, Identity partner) {
public void callFrom_C_ShowHandshake(Identity myself, Identity partner) {
if (showHandshakeCallback != null) { if (showHandshakeCallback != null) {
showHandshakeCallback.showHandshake(myself, partner); showHandshakeCallback.showHandshake(myself, partner);
} else { } else {
throw new RuntimeException("Callback not set"); throw new RuntimeException("Callback not set");
} }
return 0;
} }
public void callFrom_C_MessageToSend (Message message) { public int messageToSendCallFromC (Message message) {
if (messageToSendCallback != null) { if (messageToSendCallback != null) {
messageToSendCallback.messageToSend(message); messageToSendCallback.messageToSend(message);
} else { } else {
throw new RuntimeException("Callback not set"); throw new RuntimeException("Callback not set");
} }
return 0;
} }
} }

58
src/org_pEp_jniadapter_AbstractEngine.cc

@ -17,8 +17,6 @@ extern "C" {
int inject_sync_msg(void *msg, void *arg); int inject_sync_msg(void *msg, void *arg);
static locked_queue< message * > *sync_queue = NULL;
JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_init( JNIEXPORT void JNICALL Java_org_pEp_jniadapter_AbstractEngine_init(
JNIEnv *env, JNIEnv *env,
jobject me jobject me
@ -189,21 +187,59 @@ extern "C" {
// Sync message callbacks, queue, and thread // Sync message callbacks, queue, and thread
///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////
static locked_queue< message * > *sync_queue = NULL;
static jobject sync_obj = NULL;
static JavaVM* sync_jvm = NULL;
// Called by sync thread only // Called by sync thread only
PEP_STATUS show_handshake(void *obj, pEp_identity *me, pEp_identity *partner) PEP_STATUS show_handshake(void *obj, pEp_identity *me, pEp_identity *partner)
{ {
// TODO : callback JNIEnv* env;
jobject me_ = NULL;
jobject partner_ = NULL;
sync_jvm->AttachCurrentThread(&env, NULL);
return PEP_STATUS_OK; jclass clazz = env->GetObjectClass(sync_obj);
jmethodID methodID = env->GetMethodID(
clazz,
"showHandshakeCallFromC",
"(Lorg/pEp/jniadapter/_Identity;Lorg/pEp/jniadapter/_Identity;)I");
me_ = from_identity(env, me);
partner_ = from_identity(env, partner);
jint result = env->CallIntMethod(sync_obj, methodID, me_, partner_);
return (PEP_STATUS) result;
} }
// Called by sync thread only
PEP_STATUS message_to_send(void *obj, message *msg) PEP_STATUS message_to_send(void *obj, message *msg)
{ {
// TODO : callback JNIEnv* env;
jobject msg_ = NULL;
return PEP_STATUS_OK; sync_jvm->AttachCurrentThread(&env, NULL);
jclass clazz = env->GetObjectClass(sync_obj);
jmethodID methodID = env->GetMethodID(
clazz,
"messageToSendCallFromC",
"(Lorg/pEp/jniadapter/Message;)I");
jclass clazz_msg_ = findClass(env, "org/pEp/jniadapter/Message");
assert(clazz_msg_);
jmethodID constructor_msg_ = env->GetMethodID(clazz_msg_, "<init>", "(J)V");
assert(constructor_msg_);
msg_ = env->NewObject(clazz_msg_, constructor_msg_, (jlong) msg);
jint result = env->CallIntMethod(sync_obj, methodID, msg_);
return (PEP_STATUS) result;
} }
// called indirectly by decrypt message
int inject_sync_msg(void *msg, void *arg) int inject_sync_msg(void *msg, void *arg)
{ {
locked_queue< message * > *queue = (locked_queue< message * > *) arg; locked_queue< message * > *queue = (locked_queue< message * > *) arg;
@ -281,10 +317,15 @@ extern "C" {
queue = new locked_queue< message * >(); queue = new locked_queue< message * >();
env->SetLongField(obj, queue_handle, (jlong) queue); env->SetLongField(obj, queue_handle, (jlong) queue);
pthread_create(thread, NULL, sync_thread_routine, (void *) queue); // for inject and retreive calls
sync_queue = queue; sync_queue = queue;
// for callbacks
env->GetJavaVM(&sync_jvm);
sync_obj = env->NewGlobalRef(obj);
pthread_create(thread, NULL, sync_thread_routine, (void *) queue);
register_sync_callbacks(session, register_sync_callbacks(session,
(void *) queue, (void *) queue,
message_to_send, message_to_send,
@ -327,6 +368,7 @@ extern "C" {
register_sync_callbacks(session, NULL, NULL, NULL, NULL, NULL); register_sync_callbacks(session, NULL, NULL, NULL, NULL, NULL);
sync_queue = NULL; sync_queue = NULL;
sync_obj = NULL;
queue->push_front(NULL); queue->push_front(NULL);
pthread_join(*thread, NULL); pthread_join(*thread, NULL);

Loading…
Cancel
Save