From: Pierre Veyre Date: Fri, 20 Jun 2014 13:30:27 +0000 (+0200) Subject: Initiate java bindings for MSG storage API X-Git-Tag: v3_12~964^2~18 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/8199067e1f2a6ea4d3d8a38df1a4fb3f25d364da?ds=sidebyside Initiate java bindings for MSG storage API --- diff --git a/buildtools/Cmake/DefinePackages.cmake b/buildtools/Cmake/DefinePackages.cmake index 9be667fa48..ca2166553d 100644 --- a/buildtools/Cmake/DefinePackages.cmake +++ b/buildtools/Cmake/DefinePackages.cmake @@ -498,6 +498,8 @@ set(JMSG_C_SRC src/bindings/java/smx_context_cojava.h src/bindings/java/smx_context_java.c src/bindings/java/smx_context_java.h + src/bindings/java/jmsg_storage.c + src/bindings/java/jmsg_storage.h ) set(JSURF_C_SRC @@ -524,6 +526,8 @@ set(JMSG_JAVA_SRC src/bindings/java/org/simgrid/msg/ProcessNotFoundException.java src/bindings/java/org/simgrid/msg/RngStream.java src/bindings/java/org/simgrid/msg/Semaphore.java + src/bindings/java/org/simgrid/msg/Storage.java + src/bindings/java/org/simgrid/msg/StorageNotFoundException.java src/bindings/java/org/simgrid/msg/Task.java src/bindings/java/org/simgrid/msg/TaskCancelledException.java src/bindings/java/org/simgrid/msg/TimeoutException.java diff --git a/examples/java/io/CMakeLists.txt b/examples/java/io/CMakeLists.txt index 3f77d90121..e88f7f64d8 100644 --- a/examples/java/io/CMakeLists.txt +++ b/examples/java/io/CMakeLists.txt @@ -4,6 +4,8 @@ set(example java_io) set(sources ${CMAKE_CURRENT_SOURCE_DIR}/IO.java ${CMAKE_CURRENT_SOURCE_DIR}/Node.java + ${CMAKE_CURRENT_SOURCE_DIR}/Storage.java + ${CMAKE_CURRENT_SOURCE_DIR}/Client.java ) if(enable_java) diff --git a/examples/java/io/Client.java b/examples/java/io/Client.java new file mode 100644 index 0000000000..487878c11d --- /dev/null +++ b/examples/java/io/Client.java @@ -0,0 +1,78 @@ +/* Copyright (c) 2012-2014. 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. */ + +/********************* Files and Storage handling **************************** + * This example implements all main storage and file functions of the MSG API + * + * Scenario : + * - display information on the disks mounted by the current host + * - create a 200,000 bytes file + * - completely read the created file + * - write 100,000 bytes in the file + * - rename the created file + * - attach some user data to a disk + * - dump disk's contents + * +******************************************************************************/ + +package io; + +import org.simgrid.msg.Host; +import org.simgrid.msg.Msg; +import org.simgrid.msg.MsgException; +import org.simgrid.msg.Process; +import org.simgrid.msg.HostNotFoundException; +import org.simgrid.msg.Storage; + +public class Client extends Process { + + public Client(Host host, int number) throws HostNotFoundException { + super(host, Integer.toString(number), null); + } + + public void main(String[] args) throws MsgException { + + // Retrieve all mount points of current host + Storage[] storages = host.getMountedStorage(); + + for (int i = 0; i < storages.length; i++) { + // For each disk mounted on host + Msg.info("------------------------------------"); + Msg.info("Disk name: "+storages[i].getName()); + Msg.info("Size: "+storages[i].getSize()+" bytes."); + Msg.info("Free Size: "+storages[i].getFreeSize()+" bytes."); + Msg.info("Used Size: "+storages[i].getUsedSize()+" bytes."); + + } + + Storage st = Storage.getByName("Disk2"); + Msg.info("Disk name: "+st.getName()); + Msg.info("Attached to host:"+st.getHost()); + + + st.setProperty("key","Pierre"); + Msg.info("Property key: "+st.getProperty("key")); + + Host h = Host.currentHost(); + h.setProperty("key2","Pierre"); + Msg.info("Property key2: "+h.getProperty("key")); + + + String[] attach = h.getAttachedStorage(); + for (int j = 0; j < attach.length; j++) { + Msg.info("Disk attached: "+attach[j]); + } + + Msg.info("**************** ALL *************************"); + + Storage[] stos = Storage.all(); + for (int i = 0; i < stos.length; i++) { + Msg.info("Disk: "+ stos[i].getName()); + } + + + } +} \ No newline at end of file diff --git a/examples/java/io/Node.java b/examples/java/io/Node.java index a0a88cd12b..755d68fc87 100644 --- a/examples/java/io/Node.java +++ b/examples/java/io/Node.java @@ -26,26 +26,26 @@ public class Node extends Process { this.number = number; } public void main(String[] args) throws MsgException { - String mount = "C:"; + String mount = "c:"; String filename; switch (number) { case 0: - filename = FILENAME1; + filename = mount + FILENAME1; break; case 1: - filename = FILENAME2; + filename = mount + FILENAME2; break; case 2: - filename = FILENAME3; + filename = mount + FILENAME3; break; case 3: - filename = FILENAME4; + filename = mount + FILENAME4; break; default: - filename = FILENAME1; + filename = mount + FILENAME1; } Msg.info("Open file " + filename); - File file = new File(mount,filename); + File file = new File(filename); long read = file.read(10000000,1); Msg.info("Having read " + read + " on " + filename); diff --git a/examples/java/io/Storage.java b/examples/java/io/Storage.java new file mode 100644 index 0000000000..dc2c9e6b52 --- /dev/null +++ b/examples/java/io/Storage.java @@ -0,0 +1,32 @@ +/* Copyright (c) 2012-2014. 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. */ + + + +package io; + +import org.simgrid.msg.Host; +import org.simgrid.msg.Msg; +import org.simgrid.msg.MsgException; + + + +public class Storage { + public static void main(String[] args) throws MsgException { + Msg.init(args); + if(args.length < 1) { + Msg.info("Usage : storage platform_file "); + Msg.info("example : storage platform.xml "); + System.exit(1); + } + Msg.createEnvironment(args[0]); + + Host[] hosts = Host.all(); + new io.Client(hosts[0],0).start(); + + Msg.run(); + } +} diff --git a/examples/java/io/storage.xml b/examples/java/io/storage.xml deleted file mode 100644 index 8036fa8cde..0000000000 --- a/examples/java/io/storage.xml +++ /dev/null @@ -1,58 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/include/msg/msg.h b/include/msg/msg.h index b145614b7e..0d07825c76 100644 --- a/include/msg/msg.h +++ b/include/msg/msg.h @@ -107,6 +107,7 @@ XBT_PUBLIC(sg_size_t) MSG_storage_get_used_size(msg_storage_t storage); XBT_PUBLIC(msg_storage_t) MSG_storage_get_by_name(const char *name); XBT_PUBLIC(xbt_dict_t) MSG_storage_get_properties(msg_storage_t storage); XBT_PUBLIC(void) MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn); +XBT_PUBLIC(const char *)MSG_storage_get_property_value(msg_storage_t storage, const char *name); XBT_PUBLIC(xbt_dynar_t) MSG_storages_as_dynar(void); XBT_PUBLIC(msg_error_t) MSG_storage_set_data(msg_storage_t host, void *data); XBT_PUBLIC(void *) MSG_storage_get_data(msg_storage_t storage); diff --git a/src/bindings/java/jmsg.c b/src/bindings/java/jmsg.c index ce3444afa8..4e0671781b 100644 --- a/src/bindings/java/jmsg.c +++ b/src/bindings/java/jmsg.c @@ -19,6 +19,7 @@ #include "jmsg_as.h" #include "jmsg_host.h" +#include "jmsg_storage.h" #include "jmsg_task.h" #include "jxbt_utilities.h" @@ -34,6 +35,7 @@ /* end of eclipse-mandated pimple */ int JAVA_HOST_LEVEL; +int JAVA_STORAGE_LEVEL; static int create_jprocess(int argc, char *argv[]); @@ -85,6 +87,10 @@ static void __JAVA_host_priv_free(void *host) { } +static void __JAVA_storage_priv_free(void *storage) +{ +} + JNIEXPORT void JNICALL Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) { @@ -130,6 +136,7 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) MSG_init(&argc, argv); JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free); + JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free); for (index = 0; index < argc; index++) free(argv[index]); @@ -149,8 +156,9 @@ JNIEXPORT void JNICALL { msg_error_t rv; int index; - xbt_dynar_t hosts; - jobject jhost; + xbt_dynar_t hosts, storages; + jobject jhost, jstorage; + /* Run everything */ XBT_DEBUG("Ready to run MSG_MAIN"); @@ -169,6 +177,17 @@ JNIEXPORT void JNICALL } xbt_dynar_free(&hosts); + + /* Cleanup java storages */ + storages = MSG_storages_as_dynar(); + for (index = 0; index < xbt_dynar_length(storages) - 1; index++) { + jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL); + if (jstorage) + jstorage_unref(env, jstorage); + + } + xbt_dynar_free(&storages); + } JNIEXPORT void JNICALL diff --git a/src/bindings/java/jmsg.h b/src/bindings/java/jmsg.h index c6c8625502..da2b16f25e 100644 --- a/src/bindings/java/jmsg.h +++ b/src/bindings/java/jmsg.h @@ -14,6 +14,9 @@ extern int JAVA_HOST_LEVEL; extern xbt_lib_t host_lib; +extern int JAVA_STORAGE_LEVEL; +extern xbt_lib_t storage_lib; + JavaVM *get_java_VM(void); JNIEnv *get_current_thread_env(void); /** diff --git a/src/bindings/java/jmsg_host.c b/src/bindings/java/jmsg_host.c index 9a75cfb987..9cf997703f 100644 --- a/src/bindings/java/jmsg_host.c +++ b/src/bindings/java/jmsg_host.c @@ -11,6 +11,8 @@ #include "jmsg.h" #include "jmsg_host.h" #include "jxbt_utilities.h" +#include "jmsg_storage.h" + XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); @@ -263,6 +265,82 @@ Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) { return (jboolean) MSG_host_is_avail(host); } +JNIEXPORT jobjectArray JNICALL +Java_org_simgrid_msg_Host_getMountedStorage(JNIEnv * env, jobject jhost){ + + msg_host_t host = jhost_get_native(env, jhost); + jobject jstorage; + jstring jname; + + if (!host) { + jxbt_throw_notbound(env, "host", jhost); + return 0; + } + + int index = 0; + jobjectArray jtable; + xbt_dict_t dict = MSG_host_get_mounted_storage_list(host); + int count = xbt_dict_length(dict); + jclass cls = (*env)->FindClass(env, "org/simgrid/msg/Storage"); + + jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL); + + if (!jtable) { + jxbt_throw_jni(env, "Storages table allocation failed"); + return NULL; + } + + xbt_dict_cursor_t cursor=NULL; + const char *mount_name, *storage_name; + + xbt_dict_foreach(dict,cursor,mount_name,storage_name) { + jname = (*env)->NewStringUTF(env, storage_name); + jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname); + (*env)->SetObjectArrayElement(env, jtable, index, jstorage); + index++; + } + xbt_dict_free(&dict); + return jtable; +} + +JNIEXPORT jobjectArray JNICALL +Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost){ + msg_host_t host = jhost_get_native(env, jhost); + + if (!host) { + jxbt_throw_notbound(env, "host", jhost); + return 0; + } + jobjectArray jtable; + + xbt_dynar_t dyn = MSG_host_get_attached_storage_list(host); + int count = xbt_dynar_length(dyn); + jclass cls = jxbt_get_class(env, "java/lang/String"); + jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL); + int index; + char *storage_name; + jstring jstorage_name; + for (index = 0; index < count; index++) { + storage_name = xbt_dynar_get_as(dyn,index,char*); + jstorage_name = (*env)->NewStringUTF(env,storage_name); + (*env)->SetObjectArrayElement(env, jtable, index, jstorage_name); + } + + return jtable; +} + +JNIEXPORT jobjectArray JNICALL +Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost){ + msg_host_t host = jhost_get_native(env, jhost); + + if (!host) { + jxbt_throw_notbound(env, "host", jhost); + return 0; + } + return (jobjectArray)MSG_host_get_storage_content(host); +} + + JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg) { diff --git a/src/bindings/java/jmsg_host.h b/src/bindings/java/jmsg_host.h index 429f8f3660..9033531f54 100644 --- a/src/bindings/java/jmsg_host.h +++ b/src/bindings/java/jmsg_host.h @@ -189,6 +189,27 @@ Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname, JNIEXPORT jboolean JNICALL Java_org_simgrid_msg_Host_isAvail (JNIEnv *, jobject); + +/* + * Class org_simgrid_msg_Host + * Method getMountedStorage + * Signature: ()[Lorg/simgrid/msg/Storage; + */ +JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getMountedStorage(JNIEnv * env, jobject jhost); + +/* + * Class org_simgrid_msg_Host + * Method getAttachedStorageList + */ +JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getAttachedStorage(JNIEnv * env, jobject jhost); + +/* + * Class org_simgrid_msg_Host + * Method getStorageContent + */ +JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_getStorageContent(JNIEnv * env, jobject jhost); + + /** * Class org_simgrid_msg_Host * Method all diff --git a/src/bindings/java/jmsg_storage.c b/src/bindings/java/jmsg_storage.c new file mode 100644 index 0000000000..150ae146c8 --- /dev/null +++ b/src/bindings/java/jmsg_storage.c @@ -0,0 +1,244 @@ +/* Functions related to the java storage API. */ + +/* Copyright (c) 2012-2014. 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 "xbt/str.h" +#include "msg/msg.h" +#include "jmsg.h" +#include "jmsg_storage.h" +#include "jxbt_utilities.h" + +XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg); + +static jmethodID jstorage_method_Storage_constructor; +static jfieldID jstorage_field_Storage_bind; +static jfieldID jstorage_field_Storage_name; + +jobject jstorage_new_instance(JNIEnv * env) { + jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage"); + return (*env)->NewObject(env, cls, jstorage_method_Storage_constructor); +} + +msg_storage_t jstorage_get_native(JNIEnv * env, jobject jstorage) { + return (msg_storage_t) (long) (*env)->GetLongField(env, jstorage, jstorage_field_Storage_bind); +} + +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Storage_nativeInit(JNIEnv *env, jclass cls) { + jclass class_Storage = (*env)->FindClass(env, "org/simgrid/msg/Storage"); + jstorage_method_Storage_constructor = (*env)->GetMethodID(env, 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.")); + } +} + +void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env) { + (*env)->SetLongField(env, jstorage, jstorage_field_Storage_bind, (jlong) (long) (storage)); +} + +jobject jstorage_ref(JNIEnv * env, jobject jstorage) { + return (*env)->NewGlobalRef(env, jstorage); +} + +void jstorage_unref(JNIEnv * env, jobject jstorage) { + (*env)->DeleteGlobalRef(env, 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) { + msg_storage_t storage; + jobject jstorage; + + /* get the C string from the java string */ + if (jname == NULL) { + jxbt_throw_null(env,bprintf("No host can have a null name")); + return NULL; + } + const char *name = (*env)->GetStringUTFChars(env, jname, 0); + storage = MSG_storage_get_by_name(name); + + if (!storage) { /* invalid name */ + jxbt_throw_storage_not_found(env, name); + (*env)->ReleaseStringUTFChars(env, jname, name); + return NULL; + } + (*env)->ReleaseStringUTFChars(env, jname, name); + + if (!xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL)) { /* native host not associated yet with java host */ + + /* Instantiate a new java storage */ + jstorage = jstorage_new_instance(env); + + if (!jstorage) { + jxbt_throw_jni(env, "java storage instantiation failed"); + return NULL; + } + + /* get a global reference to the newly created storage */ + jstorage = jstorage_ref(env, jstorage); + + if (!jstorage) { + jxbt_throw_jni(env, "new global ref allocation failed"); + return NULL; + } + /* Sets the java storage name */ + (*env)->SetObjectField(env, jstorage, jstorage_field_Storage_name, jname); + /* bind the java storage and the native storage */ + jstorage_bind(jstorage, storage, env); + + /* 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); + } + + /* return the global reference to the java storage instance */ + return (jobject) xbt_lib_get_level(storage, JAVA_STORAGE_LEVEL); +} + + +JNIEXPORT jlong JNICALL +Java_org_simgrid_msg_Storage_getSize(JNIEnv * env,jobject jstorage) { + msg_storage_t storage = jstorage_get_native(env, jstorage); + + if (!storage) { + jxbt_throw_notbound(env, "storage", jstorage); + return -1; + } + + return (jlong) MSG_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); + + if (!storage) { + jxbt_throw_notbound(env, "storage", jstorage); + return -1; + } + + return (jlong) MSG_storage_get_free_size(storage); +} + +JNIEXPORT jlong JNICALL +Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv * env,jobject jstorage) { + msg_storage_t storage = jstorage_get_native(env, jstorage); + + if (!storage) { + jxbt_throw_notbound(env, "storage", jstorage); + return -1; + } + + return (jlong) MSG_storage_get_used_size(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); + + if (!storage) { + jxbt_throw_notbound(env, "storage", jstorage); + return NULL; + } + const char *name = (*env)->GetStringUTFChars(env, jname, 0); + + const char *property = MSG_storage_get_property_value(storage, name); + if (!property) { + return NULL; + } + jobject jproperty = (*env)->NewStringUTF(env, property); + + (*env)->ReleaseStringUTFChars(env, jname, name); + + return jproperty; +} + +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); + + if (!storage) { + jxbt_throw_notbound(env, "storage", jstorage); + return; + } + const char *name = (*env)->GetStringUTFChars(env, jname, 0); + const char *value_java = (*env)->GetStringUTFChars(env, jvalue, 0); + char *value = strdup(value_java); + + MSG_storage_set_property_value(storage, name, value, xbt_free_f); + + (*env)->ReleaseStringUTFChars(env, jvalue, value); + (*env)->ReleaseStringUTFChars(env, jname, name); + +} + +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; + } + const char *host_name = MSG_storage_get_host(storage); + if (!host_name) { + return NULL; + } + jobject jhost_name = (*env)->NewStringUTF(env, host_name); + + return jhost_name; +} + +JNIEXPORT jobjectArray JNICALL +Java_org_simgrid_msg_Storage_all(JNIEnv * env, jclass cls_arg) +{ + int index; + jobjectArray jtable; + jobject jstorage; + jstring jname; + msg_storage_t storage; + + xbt_dynar_t table = MSG_storages_as_dynar(); + int count = xbt_dynar_length(table); + + jclass cls = jxbt_get_class(env, "org/simgrid/msg/Storage"); + + if (!cls) { + return NULL; + } + + jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL); + + if (!jtable) { + jxbt_throw_jni(env, "Storages table allocation failed"); + return NULL; + } + + 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(env, MSG_storage_get_name(storage)); + jstorage = Java_org_simgrid_msg_Storage_getByName(env, cls_arg, jname); + } + + (*env)->SetObjectArrayElement(env, jtable, index, jstorage); + } + xbt_dynar_free(&table); + return jtable; +} + diff --git a/src/bindings/java/jmsg_storage.h b/src/bindings/java/jmsg_storage.h new file mode 100644 index 0000000000..eeda61e123 --- /dev/null +++ b/src/bindings/java/jmsg_storage.h @@ -0,0 +1,157 @@ +/* Functions related to the java storage API. */ + +/* Copyright (c) 2012-2014. 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. */ + +#ifndef MSG_JSTORAGE_H +#define MSG_JSTORAGE_H +#include +#include "msg/msg.h" + +/** + * This function returns a new java storage instance. + * + * @param env The environment of the current thread + * + * @return A new java storage object. + * + * @exception If the class Storage is not found the function throws + * the ClassNotFoundException. If the constructor of + * this class is not found the function throws the exception + * NotSuchMethodException. + */ +jobject jstorage_new_instance(JNIEnv * env); + +/** + * This function associated a native storage to a java storage instance. + * + * @param jstorage The java storage instance. + * @param storage The native storage to bind. + * @param env The environment of the current thread + * + * @exception If the class Storage is not found the function throws + * the ClassNotFoundException. If the field bind of + * this class is not found the function throws the exception + * NotSuchFieldException. + */ +void jstorage_bind(jobject jstorage, msg_storage_t storage, JNIEnv * env); + + +/** + * This function returns a native storage from a java storage instance. + * + * @param jstorage The java storage object from which get the native storage. + * @param env The environment of the current thread + * + * @return The function returns the native storage associated to the + * java storage object. + * + * @exception If the class Storage is not found the function throws + * the ClassNotFoundException. If the field bind of + * this class is not found the function throws the exception + * NotSuchFieldException. + */ +msg_storage_t jstorage_get_native(JNIEnv * env, jobject jstorage); + + +/** + * Class org_simgrid_msg_Storage + * Method nativeInit + * Signature ()V + */ +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Storage_nativeInit(JNIEnv*, jclass); + + +/** + * This function returns a global reference to the java storage instance + * specified by the parameter jstorage. + * + * @param jstorage The original java storage instance. + * @param env The environment of the current thread + * + * @return The global reference to the original java storage + * instance. + */ +jobject jstorage_ref(JNIEnv * env, jobject jstorage); +/** + * This function delete a global reference to a java storage instance. + * + * @param The global reference to delete. + * @param env The environment of the current thread + */ +void jstorage_unref(JNIEnv * env, jobject jstorage); + +/** + * This function returns the name of a MSG storage. + * + * @param jstorage A java storage object. + * @param env The environment of the current thread + * + * @return The name of the storage. + */ +const char *jstorage_get_name(jobject jstorage, JNIEnv * env); + +/* + * Class org_simgrid_msg_Storage + * Method getByName + * Signature (Ljava/lang/String;)Lsimgrid/msg/Storage; + */ +JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Storage_getByName(JNIEnv *, jclass, jstring); + +/* + * Class org_simgrid_msg_Storage + * Method getSize + * Signature ()D + */ +JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getSize(JNIEnv *, jobject); + +/* + * Class org_simgrid_msg_Storage + * Method getFreeSize + * Signature ()D + */ +JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getFreeSize(JNIEnv *, jobject); + +/* + * Class org_simgrid_msg_Storage + * Method getUsedSize + * Signature ()D + */ +JNIEXPORT jlong JNICALL Java_org_simgrid_msg_Storage_getUsedSize(JNIEnv *, jobject); + +/* + * Class org_simgrid_msg_Storage + * Method getProperty + * Signature (Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jobject JNICALL +Java_org_simgrid_msg_Storage_getProperty(JNIEnv *env, jobject jstorage, jobject jname); +/* + * Class org_simgrid_msg_Storage + * Method setProperty + * Signature (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT void JNICALL +Java_org_simgrid_msg_Storage_setProperty(JNIEnv *env, jobject jstorage, jobject jname, jobject jvalue); + +/* + * Class org_simgrid_msg_Storage + * Method getHost + * Signature (Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jobject JNICALL +Java_org_simgrid_msg_Storage_getHost(JNIEnv * env,jobject jstorage); + +/** + * Class org_simgrid_msg_Storage + * Method all + */ +JNIEXPORT jobjectArray JNICALL +Java_org_simgrid_msg_Storage_all(JNIEnv *, jclass); + + +#endif /*!MSG_JSTORAGE_H */ diff --git a/src/bindings/java/jxbt_utilities.c b/src/bindings/java/jxbt_utilities.c index 9d6e309748..afd919026f 100644 --- a/src/bindings/java/jxbt_utilities.c +++ b/src/bindings/java/jxbt_utilities.c @@ -254,6 +254,14 @@ void jxbt_throw_host_not_found(JNIEnv * env, const char *invalid_name) bprintf("No such host: %s", invalid_name)); } +void jxbt_throw_storage_not_found(JNIEnv * env, const char *invalid_name) +{ + jxbt_throw_by_name(env, + "org/simgrid/msg/StorageNotFoundException", + bprintf("No such storage: %s", invalid_name)); +} + + void jxbt_throw_process_not_found(JNIEnv * env, const char *invalid_name) { jxbt_throw_by_name(env, diff --git a/src/bindings/java/jxbt_utilities.h b/src/bindings/java/jxbt_utilities.h index 401fe85785..015a4f779a 100644 --- a/src/bindings/java/jxbt_utilities.h +++ b/src/bindings/java/jxbt_utilities.h @@ -93,5 +93,6 @@ void jxbt_throw_host_failure(JNIEnv * env, char *details); void jxbt_throw_time_out_failure(JNIEnv * env, char *details); /**Thrown when a task is canceled */ void jxbt_throw_task_cancelled(JNIEnv * env, char *details); - +/** Thrown when looking for a storage from name does not lead to anything */ +void jxbt_throw_storage_not_found(JNIEnv * env, const char *invalid_name); #endif /* ! JXBT_UTILITY_H */ diff --git a/src/bindings/java/org/simgrid/msg/File.java b/src/bindings/java/org/simgrid/msg/File.java index db3ff43ac6..ed657705ff 100644 --- a/src/bindings/java/org/simgrid/msg/File.java +++ b/src/bindings/java/org/simgrid/msg/File.java @@ -7,7 +7,6 @@ package org.simgrid.msg; public class File { - protected String storage; /** * Represents the bind between the java comm and the * native C comm. You must never access it, since it is @@ -19,19 +18,17 @@ public class File { * @param storage is the name where you can find the file descriptor * @param path is the file location on the storage */ - public File(String storage, String path) { - this.storage = storage; - open(storage, path); + public File(String path) { + open(path); } protected void finalize() { } /** - * Opens the file whose name is the string pointed to by path. - * @param storage is the name where you can find the file descriptor + * Opens the file whose name is the string pointed to by path. * @param path is the file location on the storage */ - protected native void open(String storage, String path); + protected native void open(String path); /** * Read elements of a file. * @param size of each element diff --git a/src/bindings/java/org/simgrid/msg/Host.java b/src/bindings/java/org/simgrid/msg/Host.java index a1d57363e3..839c84ce68 100644 --- a/src/bindings/java/org/simgrid/msg/Host.java +++ b/src/bindings/java/org/simgrid/msg/Host.java @@ -8,6 +8,8 @@ package org.simgrid.msg; +import org.simgrid.msg.Storage; + /** * A host object represents a location (any possible place) where a process may run. * Thus it is represented as a physical resource with computing capabilities, some @@ -201,6 +203,17 @@ public class Host { * @return True if the host is available. */ public native boolean isAvail(); + + /** This methods returns the list of mount point names on an host + * @return An array containing all mounted storages on the host + */ + public native Storage[] getMountedStorage(); + + /** This methods returns the list of storages attached to an host + * @return An array containing all storages (name) attached to the host + */ + public native String[] getAttachedStorage(); + /** * Class initializer, to initialize various JNI stuff diff --git a/src/bindings/java/org/simgrid/msg/Storage.java b/src/bindings/java/org/simgrid/msg/Storage.java new file mode 100644 index 0000000000..9f5a45a2e4 --- /dev/null +++ b/src/bindings/java/org/simgrid/msg/Storage.java @@ -0,0 +1,123 @@ +/* Bindings to the MSG storage */ + +/* Copyright (c) 2006-2014. 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. */ + +package org.simgrid.msg; + +public class Storage { + + /** + * This attribute represents a bind between a java storage object and + * a native storage. Even if this attribute is public you must never + * access to it. + */ + private long bind; + + /** + * Storage name + */ + protected String name; + + /** + * User data. + */ + private Object data; + /** + * + */ + protected Storage() { + this.bind = 0; + this.data = null; + }; + + public String toString (){ + return this.name; + + } + + /** + * This static method gets a storage instance associated with a native + * storage of your platform. This is the best way to get a java storage object. + * + * @param name The name of the storage to get. + * + * @return The storage object with the given name. + * @exception StorageNotFoundException if the name of the storage is not valid. + * NativeException if the native version of this method failed. + */ + public native static Storage getByName(String name) + throws HostNotFoundException, NullPointerException; + + /** + * This method returns the name of a storage. + * @return The name of the storage. + * + */ + public String getName() { + return name; + } + + /** + * This method returns the size (in bytes) of a storage element. + * + * @return The size (in bytes) of the storage element. + * + */ + public native long getSize(); + + /** + * This method returns the free size (in bytes) of a storage element. + * + * @return The free size (in bytes) of the storage element. + * + */ + public native long getFreeSize(); + + /** + * This method returns the used size (in bytes) of a storage element. + * + * @return The used size (in bytes) of the storage element. + * + */ + public native long getUsedSize(); + + /** + * Returns the value of a given storage property. + */ + public native String getProperty(String name); + + /** + * Change the value of a given storage property. + */ + public native void setProperty(String name, String value); + + + /** + * + * Returns the host name the storage is attached to + * + * @return the host name the storage is attached to + */ + public native String getHost(); + + /** + * This static method returns all of the storages of the installed platform. + * + * @return An array containing all the storages installed. + * + */ + public native static Storage[] all(); + + /** + * Class initializer, to initialize various JNI stuff + */ + public static native void nativeInit(); + static { + nativeInit(); + } + +} \ No newline at end of file diff --git a/src/bindings/java/org/simgrid/msg/StorageNotFoundException.java b/src/bindings/java/org/simgrid/msg/StorageNotFoundException.java new file mode 100644 index 0000000000..5f8e6d57e4 --- /dev/null +++ b/src/bindings/java/org/simgrid/msg/StorageNotFoundException.java @@ -0,0 +1,29 @@ +/* This exception is raised when looking for a non-existing storage. */ + +/* Copyright (c) 2006-2014. 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. */ + +package org.simgrid.msg; + +/** + * This exception is raised when looking for a non-existing storage. + */ +public class StorageNotFoundException extends MsgException { + private static final long serialVersionUID = 1L; + + /** Constructs an StorageNotFoundException without a detail message. */ + public StorageNotFoundException() { + super(); + } + /** + * Constructs an StorageNotFoundException with a detail message. + * + * @param s the detail message. + */ + public StorageNotFoundException(String s) { + super(s); + } +} diff --git a/src/msg/msg_io.c b/src/msg/msg_io.c index 2cf7bb5cd4..a35e59899b 100644 --- a/src/msg/msg_io.c +++ b/src/msg/msg_io.c @@ -492,6 +492,19 @@ void MSG_storage_set_property_value(msg_storage_t storage, const char *name, cha xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn); } +/** \ingroup m_storage_management + * \brief Returns the value of a given storage property + * + * \param storage a storage + * \param name a property name + * \return value of a property (or NULL if property not set) + */ +const char *MSG_storage_get_property_value(msg_storage_t storage, const char *name) +{ + return xbt_dict_get_or_null(MSG_storage_get_properties(storage), name); +} + + /** \ingroup msg_storage_management * \brief Finds a msg_storage_t using its name. * \param name the name of a storage