Skip to content

Commit

Permalink
Add DcJsonrpcInstance class
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Mar 1, 2023
1 parent 63fc131 commit 85a84c0
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 8 deletions.
44 changes: 44 additions & 0 deletions jni/dc_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,11 @@ JNIEXPORT jlong Java_com_b44t_messenger_DcAccounts_getEventEmitterCPtr(JNIEnv *e
return (jlong)dc_accounts_get_event_emitter(get_dc_accounts(env, obj));
}

JNIEXPORT jlong Java_com_b44t_messenger_DcAccounts_getJsonrpcInstanceCPtr(JNIEnv *env, jobject obj)
{
return (jlong)dc_jsonrpc_init(get_dc_accounts(env, obj));
}


JNIEXPORT void Java_com_b44t_messenger_DcAccounts_startIo(JNIEnv *env, jobject obj)
{
Expand Down Expand Up @@ -1947,3 +1952,42 @@ JNIEXPORT jstring Java_com_b44t_messenger_DcProvider_getOverviewPage(JNIEnv *env
return ret;
}


/*******************************************************************************
* DcJsonrpcInstance
******************************************************************************/


static dc_jsonrpc_instance_t* get_dc_jsonrpc_instance(JNIEnv *env, jobject obj)
{
static jfieldID fid = 0;
if (fid==0) {
jclass cls = (*env)->GetObjectClass(env, obj);
fid = (*env)->GetFieldID(env, cls, "jsonrpcInstanceCPtr", "J" /*Signature, J=long*/);
}
if (fid) {
return (dc_jsonrpc_instance_t*)(*env)->GetLongField(env, obj, fid);
}
return NULL;
}


JNIEXPORT void Java_com_b44t_messenger_DcJsonrpcInstance_unrefJsonrpcInstanceCPtr(JNIEnv *env, jobject obj)
{
dc_provider_unref(get_dc_jsonrpc_instance(env, obj));
}

JNIEXPORT void Java_com_b44t_messenger_DcJsonrpcInstance_request(JNIEnv *env, jobject obj, jstring request)
{
CHAR_REF(request);
dc_jsonrpc_request(get_dc_jsonrpc_instance(env, obj), request);
CHAR_UNREF(request);
}

JNIEXPORT jstring Java_com_b44t_messenger_DcJsonrpcInstance_getNextResponse(JNIEnv *env, jobject obj)
{
char* temp = dc_jsonrpc_next_response(get_dc_jsonrpc_instance(env, obj));
jstring ret = JSTRING_NEW(temp);
dc_str_unref(temp);
return ret;
}
8 changes: 4 additions & 4 deletions scripts/ndk-make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ if test -z $1 || test $1 = armeabi-v7a; then
export CFLAGS=-D__ANDROID_API__=16
TARGET_CC=armv7a-linux-androideabi16-clang \
TARGET_AR=llvm-ar \
cargo "+$RUSTUP_TOOLCHAIN" rustc $RELEASEFLAG --target armv7-linux-androideabi -p deltachat_ffi -- -L "$TMPLIB"
cargo "+$RUSTUP_TOOLCHAIN" rustc $RELEASEFLAG --target armv7-linux-androideabi -p deltachat_ffi --features jsonrpc -- -L "$TMPLIB"
cp target/armv7-linux-androideabi/$RELEASE/libdeltachat.a $jnidir/armeabi-v7a
fi

Expand All @@ -124,7 +124,7 @@ if test -z $1 || test $1 = arm64-v8a; then
export CFLAGS=-D__ANDROID_API__=21
TARGET_CC=aarch64-linux-android21-clang \
TARGET_AR=llvm-ar \
cargo "+$RUSTUP_TOOLCHAIN" rustc $RELEASEFLAG --target aarch64-linux-android -p deltachat_ffi -- -L "$TMPLIB"
cargo "+$RUSTUP_TOOLCHAIN" rustc $RELEASEFLAG --target aarch64-linux-android -p deltachat_ffi --features jsonrpc -- -L "$TMPLIB"
cp target/aarch64-linux-android/$RELEASE/libdeltachat.a $jnidir/arm64-v8a
fi

Expand All @@ -133,7 +133,7 @@ if test -z $1 || test $1 = x86; then
export CFLAGS=-D__ANDROID_API__=16
TARGET_CC=i686-linux-android16-clang \
TARGET_AR=llvm-ar \
cargo "+$RUSTUP_TOOLCHAIN" rustc $RELEASEFLAG --target i686-linux-android -p deltachat_ffi -- -L "$TMPLIB"
cargo "+$RUSTUP_TOOLCHAIN" rustc $RELEASEFLAG --target i686-linux-android -p deltachat_ffi --features jsonrpc -- -L "$TMPLIB"
cp target/i686-linux-android/$RELEASE/libdeltachat.a $jnidir/x86
fi

Expand All @@ -142,7 +142,7 @@ if test -z $1 || test $1 = x86_64; then
export CFLAGS=-D__ANDROID_API__=21
TARGET_CC=x86_64-linux-android21-clang \
TARGET_AR=llvm-ar \
cargo "+$RUSTUP_TOOLCHAIN" rustc $RELEASEFLAG --target x86_64-linux-android -p deltachat_ffi -- -L "$TMPLIB"
cargo "+$RUSTUP_TOOLCHAIN" rustc $RELEASEFLAG --target x86_64-linux-android -p deltachat_ffi --features jsonrpc -- -L "$TMPLIB"
cp target/x86_64-linux-android/$RELEASE/libdeltachat.a $jnidir/x86_64
fi

Expand Down
1 change: 1 addition & 0 deletions src/com/b44t/messenger/DcAccounts.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public void unref() {
}

public DcEventEmitter getEventEmitter () { return new DcEventEmitter(getEventEmitterCPtr()); }
public DcJsonrpcInstance getJsonrpcInstance () { return new DcJsonrpcInstance(getJsonrpcInstanceCPtr()); }
public native void startIo ();
public native void stopIo ();
public native void maybeNetwork ();
Expand Down
22 changes: 22 additions & 0 deletions src/com/b44t/messenger/DcJsonrpcInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.b44t.messenger;

public class DcJsonrpcInstance {

public DcJsonrpcInstance(long jsonrpcInstanceCPtr) {
this.jsonrpcInstanceCPtr = jsonrpcInstanceCPtr;
}

@Override protected void finalize() throws Throwable {
super.finalize();
unrefJsonrpcInstanceCPtr();
jsonrpcInstanceCPtr = 0;
}

public native void request(String request);
public native String getNextResponse();

// working with raw c-data
private long jsonrpcInstanceCPtr; // CAVE: the name is referenced in the JNI
private native long getNextEventCPtr ();
private native void unrefJsonrpcInstanceCPtr();
}
10 changes: 6 additions & 4 deletions src/org/thoughtcrime/securesms/ApplicationContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.b44t.messenger.DcContext;
import com.b44t.messenger.DcEvent;
import com.b44t.messenger.DcEventEmitter;
import com.b44t.messenger.DcJsonrpcInstance;

import org.thoughtcrime.securesms.components.emoji.EmojiProvider;
import org.thoughtcrime.securesms.connect.AccountManager;
Expand Down Expand Up @@ -111,13 +112,14 @@ public void onCreate() {
notificationCenter = new NotificationCenter(this);
eventCenter = new DcEventCenter(this);
new Thread(() -> {
DcEventEmitter emitter = dcAccounts.getEventEmitter();
DcJsonrpcInstance emitter = dcAccounts.getJsonrpcInstance();
while (true) {
DcEvent event = emitter.getNextEvent();
if (event==null) {
String response = emitter.getNextResponse();
if (response==null) {
break;
}
eventCenter.handleEvent(event);
// TODO convert notification to event
//eventCenter.handleEvent(event);
}
Log.i("DeltaChat", "shutting down event handler");
}, "eventThread").start();
Expand Down

0 comments on commit 85a84c0

Please sign in to comment.