static void release_arguments(JNIEnv *env, int argc, const char **argv, jstring *strs);
+PeerNativeInterface::Info PeerNativeInterface::infoObject = {
+ .klass = nullptr,
+ .constructor = nullptr,
+ .name = nullptr,
+ .host = nullptr,
+ .port = nullptr,
+ .uuid = nullptr,
+};
+
PeerNativeInterface::PeerNativeInterface(void)
: peer(nullptr)
{
delete[] strs;
}
-jboolean PeerNativeInterface::Java_com_samsung_android_beyond_InferencePeer_set_info(JNIEnv *env, jobject thiz, jlong handle, jstring peer_ip, jint peer_port, jstring uuid)
+jobject PeerNativeInterface::Java_com_samsung_android_beyond_InferencePeer_get_info(JNIEnv *env, jobject thiz, jlong handle)
+{
+ if (env == nullptr) {
+ ErrPrint("JNIEnv is nullptr");
+ return nullptr;
+ }
+
+ PeerNativeInterface *peer_handle = reinterpret_cast<PeerNativeInterface *>(handle);
+ if (peer_handle == nullptr) {
+ ErrPrint("peer_handle == nullptr");
+ return nullptr;
+ }
+
+ const beyond_peer_info *info = nullptr;
+ if (peer_handle->peer->GetInfo(info) < 0) {
+ ErrPrint("Failed to get the peer info");
+ return nullptr;
+ }
+
+ jobject object = env->NewObject(infoObject.klass, infoObject.constructor);
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ return nullptr;
+ }
+ if (object == nullptr) {
+ ErrPrint("Failed to construct a new info object");
+ return nullptr;
+ }
+
+ if (info->host != nullptr) {
+ jstring host = env->NewStringUTF(info->host);
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ return nullptr;
+ }
+
+ if (host == nullptr) {
+ ErrPrint("host is not valid");
+ return nullptr;
+ }
+
+ env->SetObjectField(object, infoObject.host, host);
+ }
+
+ jstring uuid = env->NewStringUTF(info->uuid);
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ return nullptr;
+ }
+
+ if (uuid == nullptr) {
+ ErrPrint("uuid is not valid");
+ return nullptr;
+ }
+
+ env->SetObjectField(object, infoObject.uuid, static_cast<jobject>(uuid));
+
+ if (info->name != nullptr) {
+ jstring name = env->NewStringUTF(info->name);
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ return nullptr;
+ }
+
+ if (name == nullptr) {
+ ErrPrint("_name is not valid");
+ return nullptr;
+ }
+
+ env->SetObjectField(object, infoObject.name, name);
+ }
+
+ // TODO:
+ // Port array should be changed to a single value
+ // No more need to keep the multiple port information
+ env->SetIntField(object, infoObject.port, static_cast<const int>(info->port[0]));
+ return object;
+}
+
+jboolean PeerNativeInterface::Java_com_samsung_android_beyond_InferencePeer_set_info(JNIEnv *env, jobject thiz, jlong handle, jobject info)
{
if (env == nullptr) {
ErrPrint("JNIEnv is nullptr.");
return false;
}
- beyond_peer_info info = {
- .name = const_cast<char *>("serviceName"),
+ beyond_peer_info peerInfo = {
+ .name = nullptr,
.host = nullptr,
- .port = { static_cast<unsigned short>(peer_port) },
+ .port = { 0, 0 },
+ .uuid = {
+ 0,
+ },
.free_memory = 0llu,
.free_storage = 0llu,
.count_of_runtimes = 0,
.runtimes = nullptr,
};
- const char *_uuid = env->GetStringUTFChars(uuid, nullptr);
- if (_uuid == nullptr) {
- ErrPrint("Fail to get StringUTFChars of uuid.");
- return false;
- }
+ jobject name = env->GetObjectField(info, infoObject.name);
+ jobject host = env->GetObjectField(info, infoObject.host);
+ jobject uuid = env->GetObjectField(info, infoObject.uuid);
+ jint port = env->GetIntField(info, infoObject.port);
- snprintf(info.uuid, BEYOND_UUID_LEN, "%s", _uuid);
+ if (name != nullptr) {
+ peerInfo.name = const_cast<char *>(env->GetStringUTFChars(static_cast<jstring>(name), nullptr));
+ }
- env->ReleaseStringUTFChars(uuid, _uuid);
+ if (host != nullptr) {
+ peerInfo.host = const_cast<char *>(env->GetStringUTFChars(static_cast<jstring>(host), nullptr));
+ }
- const char *_peer_ip = env->GetStringUTFChars(peer_ip, nullptr);
- if (_peer_ip == nullptr) {
- ErrPrint("Fail to get StringUTFChars of a peer IP.");
- return false;
+ if (uuid != nullptr) {
+ const char *_uuid = env->GetStringUTFChars(static_cast<jstring>(uuid), nullptr);
+ if (_uuid != nullptr) {
+ snprintf(peerInfo.uuid, BEYOND_UUID_LEN, "%s", _uuid);
+ env->ReleaseStringUTFChars(static_cast<jstring>(uuid), _uuid);
+ }
}
- info.host = const_cast<char *>(_peer_ip);
+ peerInfo.port[0] = static_cast<unsigned short>(port);
+ peerInfo.port[1] = 0;
+ peerInfo.free_memory = 0llu;
+ peerInfo.free_storage = 0llu;
+ peerInfo.count_of_runtimes = 0;
+ peerInfo.runtimes = nullptr;
+
+ int ret = peer_handle->peer->SetInfo(&peerInfo);
+
+ if (peerInfo.name != nullptr) {
+ env->ReleaseStringUTFChars(static_cast<jstring>(name), peerInfo.name);
+ }
- DbgPrint("Peer IP = %s, ports[0] = %d, uuid = %s\n", _peer_ip, peer_port, info.uuid);
+ if (peerInfo.host != nullptr) {
+ env->ReleaseStringUTFChars(static_cast<jstring>(host), peerInfo.host);
+ }
- int ret = peer_handle->peer->SetInfo(&info);
- env->ReleaseStringUTFChars(peer_ip, _peer_ip);
if (ret < 0) {
ErrPrint("Fail to set peer info, ret = %d\n", ret);
return false;
return true;
}
+void PeerNativeInterface::Java_com_samsung_android_beyond_InferencePeer_initialize(JNIEnv *env, jclass klass)
+{
+ jclass infoKlass = env->FindClass("com/samsung/android/beyond/inference/Peer$Info");
+ if (infoKlass == nullptr) {
+ ErrPrint("Unable to get the class");
+ return;
+ }
+
+ infoObject.klass = static_cast<jclass>(env->NewGlobalRef(infoKlass));
+ env->DeleteLocalRef(infoKlass);
+ if (infoObject.klass == nullptr) {
+ ErrPrint("Unable to get the Info class");
+ return;
+ }
+
+ infoObject.constructor = env->GetMethodID(infoObject.klass, "<init>", "()V");
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+ if (infoObject.constructor == nullptr) {
+ ErrPrint("Unable to get the info constructor");
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+
+ infoObject.name = env->GetFieldID(infoObject.klass, "name", "Ljava/lang/String;");
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+ if (infoObject.name == nullptr) {
+ ErrPrint("Unable to get the name field from info");
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+
+ infoObject.host = env->GetFieldID(infoObject.klass, "host", "Ljava/lang/String;");
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+ if (infoObject.host == nullptr) {
+ ErrPrint("Unable to get the host field from info");
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+
+ infoObject.port = env->GetFieldID(infoObject.klass, "port", "I");
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+ if (infoObject.port == nullptr) {
+ ErrPrint("Unable to get the port field from info");
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+
+ infoObject.uuid = env->GetFieldID(infoObject.klass, "uuid", "Ljava/lang/String;");
+ if (env->ExceptionCheck() == true) {
+ JNIHelper::PrintException(env, __FUNCTION__, __LINE__);
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+ if (infoObject.uuid == nullptr) {
+ ErrPrint("Unable to get the uuid field from info");
+ env->DeleteGlobalRef(infoObject.klass);
+ infoObject.klass = nullptr;
+ return;
+ }
+
+ DbgPrint("Successfully initialized");
+}
+
int PeerNativeInterface::RegisterPeerNatives(JNIEnv *env)
{
jclass klass = env->FindClass("com/samsung/android/beyond/inference/Peer");
}
static JNINativeMethod peer_jni_methods[] = {
- { "create", "(Landroid/content/Context;[Ljava/lang/String;)J", (void *)Java_com_samsung_android_beyond_InferencePeer_create },
- { "setInfo", "(JLjava/lang/String;ILjava/lang/String;)Z", (void *)Java_com_samsung_android_beyond_InferencePeer_set_info },
- { "activate", "(J)Z", (void *)Java_com_samsung_android_beyond_InferencePeer_activate },
- { "configure", "(JCLjava/lang/Object;)Z", (void *)Java_com_samsung_android_beyond_InferencePeer_configure },
- { "deactivate", "(J)Z", (void *)Java_com_samsung_android_beyond_InferencePeer_deactivate },
- { "destroy", "(J)V", (void *)Java_com_samsung_android_beyond_InferencePeer_destroy },
+ { "initialize", "()V", reinterpret_cast<void *>(Java_com_samsung_android_beyond_InferencePeer_initialize) },
+ { "create", "(Landroid/content/Context;[Ljava/lang/String;)J", reinterpret_cast<void *>(Java_com_samsung_android_beyond_InferencePeer_create) },
+ { "setInfo", "(JLcom/samsung/android/beyond/inference/Peer$Info;)Z", reinterpret_cast<void *>(Java_com_samsung_android_beyond_InferencePeer_set_info) },
+ { "getInfo", "(J)Lcom/samsung/android/beyond/inference/Peer$Info;", reinterpret_cast<void *>(Java_com_samsung_android_beyond_InferencePeer_get_info) },
+ { "activate", "(J)Z", reinterpret_cast<void *>(Java_com_samsung_android_beyond_InferencePeer_activate) },
+ { "configure", "(JCLjava/lang/Object;)Z", reinterpret_cast<void *>(Java_com_samsung_android_beyond_InferencePeer_configure) },
+ { "deactivate", "(J)Z", reinterpret_cast<void *>(Java_com_samsung_android_beyond_InferencePeer_deactivate) },
+ { "destroy", "(J)V", reinterpret_cast<void *>(Java_com_samsung_android_beyond_InferencePeer_destroy) },
};
- if (env->RegisterNatives(klass, peer_jni_methods,
- sizeof(peer_jni_methods) / sizeof(JNINativeMethod)) != JNI_OK) {
+ if (env->RegisterNatives(klass, peer_jni_methods, sizeof(peer_jni_methods) / sizeof(JNINativeMethod)) != JNI_OK) {
ErrPrint("Failed to register peer jni methods for BeyonD Java APIs.");
return -EFAULT;
}