From 789705ea99e0dc38ec0b4840bc2f6cecfcb1cb8e Mon Sep 17 00:00:00 2001 From: Qing Wang Date: Wed, 21 Oct 2020 22:34:03 +0800 Subject: [PATCH] Fix --- .../org/restrpc/test/BasicClientTest.java | 5 ++++ jni/org_restrpc_client_NativeRpcClient.cc | 26 ++++++++++++++----- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/java/src/test/java/org/restrpc/test/BasicClientTest.java b/java/src/test/java/org/restrpc/test/BasicClientTest.java index 0f887b8..8ccd5d2 100644 --- a/java/src/test/java/org/restrpc/test/BasicClientTest.java +++ b/java/src/test/java/org/restrpc/test/BasicClientTest.java @@ -16,6 +16,11 @@ public class BasicClientTest { rpcClient.connect("127.0.0.1:9000"); CompletableFuture future = rpcClient.asyncFunc("add").invoke(Integer.class, 2, 3); System.out.println("The result of add(2, 3) is " + future.get()); + + { + CompletableFuture future1 = rpcClient.asyncFunc("echo").invoke(String.class, "hello world!"); + System.out.println("The result of echo is " + future1.get()); + } } private String getClassStr(Object o) { diff --git a/jni/org_restrpc_client_NativeRpcClient.cc b/jni/org_restrpc_client_NativeRpcClient.cc index 25f2fc1..0e4af5b 100644 --- a/jni/org_restrpc_client_NativeRpcClient.cc +++ b/jni/org_restrpc_client_NativeRpcClient.cc @@ -34,6 +34,13 @@ jint JNI_OnLoad(JavaVM *vm, void *reserved) { //void JNI_OnUnload(JavaVM *vm, void *reserved) {} +inline std::string JavaStringToNativeString(JNIEnv *env, jstring jstr) { + const char *c_str = env->GetStringUTFChars(jstr, nullptr); + std::string result(c_str); + env->ReleaseStringUTFChars(static_cast(jstr), c_str); + return result; +} + /// Convert C++ String to a Java ByteArray. inline jbyteArray NativeStringToJavaByteArray(JNIEnv *env, const std::string &str) { jbyteArray array = env->NewByteArray(str.size()); @@ -76,7 +83,6 @@ JNIEXPORT jlong JNICALL Java_org_restrpc_client_NativeRpcClient_nativeNewRpcClie rest_rpc::rpc_client *native_rpc_client = new rest_rpc::rpc_client( rest_rpc::client_language_t::JAVA, on_result_received); - std::cout << "------native_rpc_client=" << reinterpret_cast(native_rpc_client) << std::endl; return reinterpret_cast(native_rpc_client); } @@ -87,11 +93,15 @@ JNIEXPORT jlong JNICALL Java_org_restrpc_client_NativeRpcClient_nativeNewRpcClie * Signature: (JLjava/lang/String;)V */ JNIEXPORT void JNICALL Java_org_restrpc_client_NativeRpcClient_nativeConnect -(JNIEnv *, jobject o, jlong rpcClientPointer, jstring serverAddress) { +(JNIEnv *env, jobject o, jlong rpcClientPointer, jstring serverAddress) { auto *native_rpc_client = reinterpret_cast(rpcClientPointer); - // TODO(qwang): Do not hard code this. - const bool connected = native_rpc_client->connect("127.0.0.1", 9000); - std::cout << "Connected:" << connected << std::endl; + // TODO(qwang): return a flag or throw exception. + const std::string server_addr = JavaStringToNativeString(env, serverAddress); + // Use a helper to split and handle the exception. + const size_t pos = server_addr.find(":"); + const std::string ip = server_addr.substr(0, pos); + const int port = std::stoi(server_addr.substr(pos + 1, server_addr.size())); + const bool connected = native_rpc_client->connect(ip, static_cast(port)); } /* @@ -112,8 +122,10 @@ JNIEXPORT jlong JNICALL Java_org_restrpc_client_NativeRpcClient_nativeInvoke * Signature: (J)V */ JNIEXPORT void JNICALL Java_org_restrpc_client_NativeRpcClient_nativeDestroy -(JNIEnv *, jobject, jlong) { - +(JNIEnv *, jobject, jlong rpcClientPointer) { + auto *native_rpc_client = reinterpret_cast(rpcClientPointer); + native_rpc_client->close(); + delete native_rpc_client; } #ifdef __cplusplus