X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/347996b4a10c4e8579080692afa60e0afb88b60a..3b4740f2c34e232bcf203b9407af5637acaecaec:/src/bindings/java/jmsg_storage.cpp diff --git a/src/bindings/java/jmsg_storage.cpp b/src/bindings/java/jmsg_storage.cpp index 1af9c1eff7..15bd3fe4ed 100644 --- a/src/bindings/java/jmsg_storage.cpp +++ b/src/bindings/java/jmsg_storage.cpp @@ -1,20 +1,20 @@ -/* Functions related to the java storage API. */ +/* Java bindings of the Storage API. */ -/* Copyright (c) 2012-2015. The SimGrid Team. - * All rights reserved. */ +/* Copyright (c) 2012-2017. The SimGrid Team. All rights reserved. */ /* This program is free software; you can redistribute it and/or modify it * under the terms of the license (GNU LGPL) which comes with this package. */ -#include -#include - #include "simgrid/msg.h" +#include "surf/surf_routing.h" + #include "jmsg.h" #include "jmsg_storage.h" #include "jxbt_utilities.h" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java); + +SG_BEGIN_DECL() static jmethodID jstorage_method_Storage_constructor; static jfieldID jstorage_field_Storage_bind; @@ -29,15 +29,14 @@ msg_storage_t jstorage_get_native(JNIEnv * env, jobject jstorage) { return (msg_storage_t) (uintptr_t) env->GetLongField(jstorage, jstorage_field_Storage_bind); } -JNIEXPORT void JNICALL -Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jclass cls) { +JNIEXPORT void JNICALL Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jclass cls) { jclass class_Storage = env->FindClass("org/simgrid/msg/Storage"); + xbt_assert(class_Storage, "Native initialization of msg/Storage failed. Please report that bug"); jstorage_method_Storage_constructor = env->GetMethodID(class_Storage, "", "()V"); jstorage_field_Storage_bind = jxbt_get_jfield(env,class_Storage, "bind", "J"); jstorage_field_Storage_name = jxbt_get_jfield(env, class_Storage, "name", "Ljava/lang/String;"); - if (!class_Storage || !jstorage_field_Storage_name || !jstorage_method_Storage_constructor || !jstorage_field_Storage_bind) { - jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug.")); - } + xbt_assert(jstorage_field_Storage_name && jstorage_method_Storage_constructor && jstorage_field_Storage_bind, + "Native initialization of msg/Storage failed. Please report that bug"); } void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) { @@ -52,21 +51,14 @@ void jstorage_unref(JNIEnv * env, jobject jstorage) { env->DeleteGlobalRef(jstorage); } -const char *jstorage_get_name(jobject jstorage, JNIEnv * env) { - msg_storage_t storage = jstorage_get_native(env, jstorage); - return MSG_storage_get_name(storage); -} - -JNIEXPORT jobject JNICALL -Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, - jstring jname) { +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, jstring jname) { msg_storage_t storage; - jobject jstorage; + jobject jstorage = nullptr; /* get the C string from the java string */ - if (jname == NULL) { + if (jname == nullptr) { jxbt_throw_null(env,bprintf("No host can have a null name")); - return NULL; + return nullptr; } const char *name = env->GetStringUTFChars(jname, 0); storage = MSG_storage_get_by_name(name); @@ -74,18 +66,17 @@ Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, if (!storage) { /* invalid name */ jxbt_throw_storage_not_found(env, name); env->ReleaseStringUTFChars(jname, name); - return NULL; + return nullptr; } env->ReleaseStringUTFChars(jname, name); - if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) { /* native host not associated yet with java host */ - + if (java_storage_map.find(storage->key) == java_storage_map.end()) { /* Instantiate a new java storage */ jstorage = jstorage_new_instance(env); if (!jstorage) { jxbt_throw_jni(env, "java storage instantiation failed"); - return NULL; + return nullptr; } /* get a global reference to the newly created storage */ @@ -93,7 +84,7 @@ Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, if (!jstorage) { jxbt_throw_jni(env, "new global ref allocation failed"); - return NULL; + return nullptr; } /* Sets the java storage name */ env->SetObjectField(jstorage, jstorage_field_Storage_name, jname); @@ -103,16 +94,15 @@ Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, /* the native storage data field is set with the global reference to the * java storage returned by this function */ - xbt_lib_set(storage_lib, storage->key, JAVA_STORAGE_LEVEL, (void *) jstorage); - } + java_storage_map.insert({storage->key, jstorage}); + } else + jstorage = java_storage_map.at(storage->key); /* return the global reference to the java storage instance */ - return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL); + return (jobject)jstorage; } - -JNIEXPORT jlong JNICALL -Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) { +JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) { msg_storage_t storage = jstorage_get_native(env, jstorage); if (!storage) { @@ -123,8 +113,7 @@ Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) { return (jlong) MSG_storage_get_size(storage); } -JNIEXPORT jlong JNICALL -Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) { +JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) { msg_storage_t storage = jstorage_get_native(env, jstorage); if (!storage) { @@ -135,8 +124,7 @@ Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) { return (jlong) MSG_storage_get_free_size(storage); } -JNIEXPORT jlong JNICALL -Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) { +JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) { msg_storage_t storage = jstorage_get_native(env, jstorage); if (!storage) { @@ -147,19 +135,18 @@ Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) { return (jlong) MSG_storage_get_used_size(storage); } -JNIEXPORT jobject JNICALL -Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) { +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) { msg_storage_t storage = jstorage_get_native(env, jstorage); if (!storage) { jxbt_throw_notbound(env, "storage", jstorage); - return NULL; + return nullptr; } const char *name = env->GetStringUTFChars((jstring) jname, 0); const char *property = MSG_storage_get_property_value(storage, name); if (!property) { - return NULL; + return nullptr; } jobject jproperty = env->NewStringUTF(property); @@ -180,32 +167,30 @@ Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0); char *value = xbt_strdup(value_java); - MSG_storage_set_property_value(storage, name, value, xbt_free_f); + MSG_storage_set_property_value(storage, name, value); env->ReleaseStringUTFChars((jstring) jvalue, value_java); env->ReleaseStringUTFChars((jstring) jname, name); } -JNIEXPORT jobject JNICALL -Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) { +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) { msg_storage_t storage = jstorage_get_native(env, jstorage); if (!storage) { jxbt_throw_notbound(env, "storage", jstorage); - return NULL; + return nullptr; } const char *host_name = MSG_storage_get_host(storage); if (!host_name) { - return NULL; + return nullptr; } jobject jhost_name = env->NewStringUTF(host_name); return jhost_name; } -JNIEXPORT jobjectArray JNICALL -Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg) +JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg) { int index; jobjectArray jtable; @@ -219,21 +204,21 @@ Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg) jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage"); if (!cls) { - return NULL; + return nullptr; } - jtable = env->NewObjectArray((jsize) count, cls, NULL); + jtable = env->NewObjectArray((jsize) count, cls, nullptr); if (!jtable) { jxbt_throw_jni(env, "Storages table allocation failed"); - return NULL; + return nullptr; } for (index = 0; index < count; index++) { storage = xbt_dynar_get_as(table,index,msg_storage_t); - jstorage = (jobject) (xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)); - - if (!jstorage) { + if (java_storage_map.find(storage->key) != java_storage_map.end()) { + jstorage = java_storage_map.at(storage->key); + } else { jname = env->NewStringUTF(MSG_storage_get_name(storage)); jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname); } @@ -243,3 +228,5 @@ Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg) xbt_dynar_free(&table); return jtable; } + +SG_END_DECL()