Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initiate java bindings for MSG storage API
[simgrid.git] / src / bindings / java / jmsg_host.c
index 21cfc8a..9cf9977 100644 (file)
@@ -1,16 +1,18 @@
 /* Functions related to the java host instances.                            */
 
-/* Copyright (c) 2007-2012. The SimGrid Team.
+/* Copyright (c) 2007-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. */
+ * 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_host.h"
 #include "jxbt_utilities.h"
+#include "jmsg_storage.h"
+
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
@@ -18,7 +20,6 @@ static jmethodID jhost_method_Host_constructor;
 static jfieldID jhost_field_Host_bind;
 static jfieldID jhost_field_Host_name;
 
-
 jobject jhost_new_instance(JNIEnv * env) {
   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
   return (*env)->NewObject(env, cls, jhost_method_Host_constructor);
@@ -70,11 +71,11 @@ Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
   jobject jhost;                /* global reference to the java host instance returned  */
 
   /* get the C string from the java string */
-  const char *name = (*env)->GetStringUTFChars(env, jname, 0);
-  if (name == NULL) {
+  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);
   /* get the host by name       (the hosts are created during the grid resolution) */
   host = MSG_get_host_by_name(name);
 
@@ -85,7 +86,7 @@ Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
   }
   (*env)->ReleaseStringUTFChars(env, jname, name);
 
-  if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
+  if (!xbt_lib_get_level(host, JAVA_HOST_LEVEL)) {       /* native host not associated yet with java host */
 
     /* Instantiate a new java host */
     jhost = jhost_new_instance(env);
@@ -110,11 +111,11 @@ Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
     /* the native host data field is set with the global reference to the
      * java host returned by this function
      */
-    MSG_host_set_data(host, (void *) jhost);
+    xbt_lib_set(host_lib, host->key, JAVA_HOST_LEVEL, (void *) jhost);
   }
 
   /* return the global reference to the java host instance */
-  return (jobject) MSG_host_get_data(host);
+  return (jobject) xbt_lib_get_level(host, JAVA_HOST_LEVEL);
 }
 
 JNIEXPORT jobject JNICALL
@@ -123,7 +124,7 @@ Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
 
   msg_host_t host = MSG_host_self();
 
-  if (!MSG_host_get_data(host)) {
+  if (!xbt_lib_get_level(host, JAVA_HOST_LEVEL)) {
     /* the native host not yet associated with the java host instance */
 
     /* instanciate a new java host instance */
@@ -147,13 +148,26 @@ Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
     (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
     /* Bind & store it */
     jhost_bind(jhost, host, env);
-    MSG_host_set_data(host, (void *) jhost);
+    xbt_lib_set(host_lib, host->key, JAVA_HOST_LEVEL, (void *) jhost);
   } else {
-    jhost = (jobject) MSG_host_get_data(host);
+    jhost = (jobject) xbt_lib_get_level(host, JAVA_HOST_LEVEL);
   }
 
   return jhost;
 }
+
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Host_on(JNIEnv *env, jobject jhost) {
+  msg_host_t host = jhost_get_native(env, jhost);
+  MSG_host_on(host);
+}
+
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Host_off(JNIEnv *env, jobject jhost) {
+  msg_host_t host = jhost_get_native(env, jhost);
+  MSG_host_off(host);
+}
+
 JNIEXPORT jint JNICALL
 Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
@@ -233,7 +247,7 @@ Java_org_simgrid_msg_Host_setProperty(JNIEnv *env, jobject jhost, jobject jname,
   const char *value_java = (*env)->GetStringUTFChars(env, jvalue, 0);
   char *value = strdup(value_java);
 
-  MSG_host_set_property_value(host,name,value,xbt_free);
+  MSG_host_set_property_value(host, name, value, xbt_free_f);
 
   (*env)->ReleaseStringUTFChars(env, jvalue, value);
   (*env)->ReleaseStringUTFChars(env, jname, name);
@@ -251,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)
 {
@@ -278,7 +368,7 @@ Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
 
   for (index = 0; index < count; index++) {
     host = xbt_dynar_get_as(table,index,msg_host_t);
-    jhost = (jobject) (MSG_host_get_data(host));
+    jhost = (jobject) (xbt_lib_get_level(host, JAVA_HOST_LEVEL));
 
     if (!jhost) {
       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));