X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/blobdiff_plain/ce9cbf0e2aea0198cb08a735a3ad36c6471f5468..b9625f82f86db0674e911887addce45dca31b57f:/src/bindings/java/jmsg_storage.cpp?ds=sidebyside diff --git a/src/bindings/java/jmsg_storage.cpp b/src/bindings/java/jmsg_storage.cpp index 8afa119525..b88ed1c696 100644 --- a/src/bindings/java/jmsg_storage.cpp +++ b/src/bindings/java/jmsg_storage.cpp @@ -1,20 +1,20 @@ /* Java bindings of the Storage API. */ -/* Copyright (c) 2012-2017. The SimGrid Team. All rights reserved. */ +/* Copyright (c) 2012-2020. 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 "simgrid/msg.h" -#include "surf/surf_routing.h" +#include "simgrid/plugins/file_system.h" +#include "simgrid/storage.h" -#include "jmsg.h" +#include "include/xbt/signal.hpp" +#include "jmsg.hpp" #include "jmsg_storage.h" -#include "jxbt_utilities.h" +#include "jxbt_utilities.hpp" +#include "simgrid/s4u/Storage.hpp" -XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); - -SG_BEGIN_DECL() +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(java); static jmethodID jstorage_method_Storage_constructor; static jfieldID jstorage_field_Storage_bind; @@ -39,7 +39,8 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jcla "Native initialization of msg/Storage failed. Please report that bug"); } -void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) { +void jstorage_bind(jobject jstorage, sg_storage_t storage, JNIEnv* env) +{ env->SetLongField(jstorage, jstorage_field_Storage_bind, (jlong) (uintptr_t) (storage)); } @@ -52,30 +53,29 @@ void jstorage_unref(JNIEnv * env, jobject jstorage) { } JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, jclass cls, jstring jname) { - msg_storage_t storage; - jobject jstorage; + sg_storage_t storage; + jobject jstorage = nullptr; /* get the C string from the java string */ if (jname == nullptr) { - jxbt_throw_null(env,bprintf("No host can have a null name")); + jxbt_throw_null(env, "No host can have a null name"); return nullptr; } const char *name = env->GetStringUTFChars(jname, 0); - storage = MSG_storage_get_by_name(name); + storage = sg_storage_get_by_name(name); - if (!storage) { /* invalid name */ + if (not storage) { /* invalid name */ jxbt_throw_storage_not_found(env, name); env->ReleaseStringUTFChars(jname, name); 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) == java_storage_map.end()) { /* Instantiate a new java storage */ jstorage = jstorage_new_instance(env); - if (!jstorage) { + if (not jstorage) { jxbt_throw_jni(env, "java storage instantiation failed"); return nullptr; } @@ -83,7 +83,7 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, j /* get a global reference to the newly created storage */ jstorage = jstorage_ref(env, jstorage); - if (!jstorage) { + if (not jstorage) { jxbt_throw_jni(env, "new global ref allocation failed"); return nullptr; } @@ -95,57 +95,58 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv * env, j /* 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, jstorage}); + } else + jstorage = java_storage_map.at(storage); /* 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) { - msg_storage_t storage = jstorage_get_native(env, jstorage); + const_sg_storage_t storage = jstorage_get_native(env, jstorage); - if (!storage) { + if (not storage) { jxbt_throw_notbound(env, "storage", jstorage); return -1; } - return (jlong) MSG_storage_get_size(storage); + return (jlong)sg_storage_get_size(storage); } JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv * env,jobject jstorage) { - msg_storage_t storage = jstorage_get_native(env, jstorage); + const_sg_storage_t storage = jstorage_get_native(env, jstorage); - if (!storage) { + if (not storage) { jxbt_throw_notbound(env, "storage", jstorage); return -1; } - return (jlong) MSG_storage_get_free_size(storage); + return (jlong)sg_storage_get_size_free(storage); } JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) { - msg_storage_t storage = jstorage_get_native(env, jstorage); + const_sg_storage_t storage = jstorage_get_native(env, jstorage); - if (!storage) { + if (not storage) { jxbt_throw_notbound(env, "storage", jstorage); return -1; } - return (jlong) MSG_storage_get_used_size(storage); + return (jlong)sg_storage_get_size_used(storage); } JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname) { - msg_storage_t storage = jstorage_get_native(env, jstorage); + const_sg_storage_t storage = jstorage_get_native(env, jstorage); - if (!storage) { + if (not storage) { jxbt_throw_notbound(env, "storage", jstorage); return nullptr; } const char *name = env->GetStringUTFChars((jstring) jname, 0); - const char *property = MSG_storage_get_property_value(storage, name); - if (!property) { + const char* property = sg_storage_get_property_value(storage, name); + if (not property) { return nullptr; } jobject jproperty = env->NewStringUTF(property); @@ -157,32 +158,30 @@ JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, JNIEXPORT void JNICALL Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue) { - msg_storage_t storage = jstorage_get_native(env, jstorage); + sg_storage_t storage = jstorage_get_native(env, jstorage); - if (!storage) { + if (not storage) { jxbt_throw_notbound(env, "storage", jstorage); return; } const char *name = env->GetStringUTFChars((jstring) jname, 0); const char *value_java = env->GetStringUTFChars((jstring) jvalue, 0); - char *value = xbt_strdup(value_java); - MSG_storage_set_property_value(storage, name, value); + storage->set_property(name, std::string(value_java)); env->ReleaseStringUTFChars((jstring) jvalue, value_java); env->ReleaseStringUTFChars((jstring) jname, name); - } JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage) { - msg_storage_t storage = jstorage_get_native(env, jstorage); + const_sg_storage_t storage = jstorage_get_native(env, jstorage); - if (!storage) { + if (not storage) { jxbt_throw_notbound(env, "storage", jstorage); return nullptr; } - const char *host_name = MSG_storage_get_host(storage); - if (!host_name) { + const char* host_name = sg_storage_get_host(storage); + if (not host_name) { return nullptr; } jobject jhost_name = env->NewStringUTF(host_name); @@ -196,30 +195,30 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jc jobjectArray jtable; jobject jstorage; jstring jname; - msg_storage_t storage; + sg_storage_t storage; - xbt_dynar_t table = MSG_storages_as_dynar(); + xbt_dynar_t table = sg_storages_as_dynar(); int count = xbt_dynar_length(table); jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage"); - if (!cls) { + if (not cls) { return nullptr; } jtable = env->NewObjectArray((jsize) count, cls, nullptr); - if (!jtable) { + if (not jtable) { jxbt_throw_jni(env, "Storages table allocation failed"); 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) { - jname = env->NewStringUTF(MSG_storage_get_name(storage)); + storage = xbt_dynar_get_as(table, index, sg_storage_t); + if (java_storage_map.find(storage) != java_storage_map.end()) { + jstorage = java_storage_map.at(storage); + } else { + jname = env->NewStringUTF(sg_storage_get_name(storage)); jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname); } @@ -228,5 +227,3 @@ JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Storage_all(JNIEnv * env, jc xbt_dynar_free(&table); return jtable; } - -SG_END_DECL()