Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use jxbt_get_jfield instead of jxbt_get_sfield (faster)
[simgrid.git] / src / jmsg_host.c
index 26edfa2..c01a132 100644 (file)
 #include "jmsg_host.h"
 #include "jxbt_utilities.h"
 
-jobject jhost_new_instance(JNIEnv * env)
-{
+XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
-  jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
-  jmethodID constructor = jxbt_get_jmethod(env, cls, "<init>", "()V");
+static jmethodID jhost_method_Host_constructor;
+static jfieldID jhost_field_Host_bind;
+static jfieldID jhost_field_Host_name;
 
-  if (!constructor)
-    return NULL;
 
-  return (*env)->NewObject(env, cls, constructor);
+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);
 }
 
-jobject jhost_ref(JNIEnv * env, jobject jhost)
-{
+jobject jhost_ref(JNIEnv * env, jobject jhost) {
   return (*env)->NewGlobalRef(env, jhost);
 }
 
-void jhost_unref(JNIEnv * env, jobject jhost)
-{
+void jhost_unref(JNIEnv * env, jobject jhost) {
   (*env)->DeleteGlobalRef(env, jhost);
 }
 
-void jhost_bind(jobject jhost, m_host_t host, JNIEnv * env)
-{
-  jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Host", "bind", "J");
+void jhost_bind(jobject jhost, m_host_t host, JNIEnv * env) {
+  (*env)->SetLongField(env, jhost, jhost_field_Host_bind, (jlong) (long) (host));
+}
 
-  if (!id)
-    return;
+m_host_t jhost_get_native(JNIEnv * env, jobject jhost) {
+  return (m_host_t) (long) (*env)->GetLongField(env, jhost, jhost_field_Host_bind);
+}
 
-  (*env)->SetLongField(env, jhost, id, (jlong) (long) (host));
+const char *jhost_get_name(jobject jhost, JNIEnv * env) {
+  m_host_t host = jhost_get_native(env, jhost);
+  return MSG_host_get_name(host);
 }
 
-m_host_t jhost_get_native(JNIEnv * env, jobject jhost)
-{
-  jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Host", "bind", "J");
+jboolean jhost_is_valid(jobject jhost, JNIEnv * env) {
+  if ((*env)->GetLongField(env, jhost, jhost_field_Host_bind)) {
+    return JNI_TRUE;
+  } else {
+    return JNI_FALSE;
+  }
+}
 
-  if (!id)
+JNIEXPORT void JNICALL
+Java_org_simgrid_msg_Host_nativeInit(JNIEnv *env, jclass cls) {
+       jclass class_Host = (*env)->FindClass(env, "org/simgrid/msg/Host");
+       jhost_method_Host_constructor = (*env)->GetMethodID(env, class_Host, "<init>", "()V");
+       //FIXME: Don't use jxbt_get_sfield directly, it is slower.
+       jhost_field_Host_bind = jxbt_get_jfield(env,class_Host, "bind", "J");
+       jhost_field_Host_name = jxbt_get_jfield(env, class_Host, "name", "Ljava/lang/String;");
+       if (!class_Host || !jhost_field_Host_name || !jhost_method_Host_constructor || !jhost_field_Host_bind) {
+       jxbt_throw_native(env,bprintf("Can't find some fields in Java class. You should report this bug."));
+       }
+}
+JNIEXPORT jobject JNICALL
+Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls,
+                                         jstring jname) {
+  m_host_t host;                /* native host                                          */
+  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) {
+       jxbt_throw_null(env,bprintf("No host can have a null name"));
+       return NULL;
+  }
+  XBT_DEBUG("Looking for host '%s'",name);
+  /* get the host by name       (the hosts are created during the grid resolution) */
+  host = MSG_get_host_by_name(name);
+  XBT_DEBUG("MSG gave %p as native host", host);
+
+  if (!host) {                  /* invalid name */
+    jxbt_throw_host_not_found(env, name);
+    (*env)->ReleaseStringUTFChars(env, jname, name);
     return NULL;
+  }
+  (*env)->ReleaseStringUTFChars(env, jname, name);
+
+  if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
+
+    /* Instantiate a new java host */
+    jhost = jhost_new_instance(env);
+
+    if (!jhost) {
+      jxbt_throw_jni(env, "java host instantiation failed");
+      return NULL;
+    }
+
+    /* get a global reference to the newly created host */
+    jhost = jhost_ref(env, jhost);
+
+    if (!jhost) {
+      jxbt_throw_jni(env, "new global ref allocation failed");
+      return NULL;
+    }
+    /* Sets the java host name */
+    (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
+    /* bind the java host and the native host */
+    jhost_bind(jhost, host, env);
 
-  return (m_host_t) (long) (*env)->GetLongField(env, jhost, id);
+    /* 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);
+  }
+
+  /* return the global reference to the java host instance */
+  return (jobject) MSG_host_get_data(host);
 }
 
-const char *jhost_get_name(jobject jhost, JNIEnv * env)
-{
-  m_host_t host = jhost_get_native(env, jhost);
-  return (const char *) host->name;
+JNIEXPORT jobject JNICALL
+Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) {
+  jobject jhost;
+
+  m_host_t host = MSG_host_self();
+
+  if (!MSG_host_get_data(host)) {
+    /* the native host not yet associated with the java host instance */
+
+    /* instanciate a new java host instance */
+    jhost = jhost_new_instance(env);
+
+    if (!jhost) {
+      jxbt_throw_jni(env, "java host instantiation failed");
+      return NULL;
+    }
+
+    /* get a global reference to the newly created host */
+    jhost = jhost_ref(env, jhost);
+
+    if (!jhost) {
+      jxbt_throw_jni(env, "global ref allocation failed");
+      return NULL;
+    }
+    /* Sets the host name */
+    const char *name = MSG_host_get_name(host);
+    jobject jname = (*env)->NewStringUTF(env,name);
+    (*env)->SetObjectField(env, jhost, jhost_field_Host_name, jname);
+    /* Bind & store it */
+    jhost_bind(jhost, host, env);
+    MSG_host_set_data(host, (void *) jhost);
+  } else {
+    jhost = (jobject) MSG_host_get_data(host);
+  }
+
+  return jhost;
+}
+JNIEXPORT jint JNICALL
+Java_org_simgrid_msg_Host_getCount(JNIEnv * env, jclass cls) {
+  xbt_dynar_t hosts =  MSG_hosts_as_dynar();
+  int nb_host = xbt_dynar_length(hosts);
+  xbt_dynar_free(&hosts);
+  return (jint) nb_host;
 }
 
-void jhost_set_name(jobject jhost, jstring jname, JNIEnv * env)
-{
-  const char *name;
+JNIEXPORT jdouble JNICALL
+Java_org_simgrid_msg_Host_getSpeed(JNIEnv * env,
+                                        jobject jhost) {
   m_host_t host = jhost_get_native(env, jhost);
 
-  name = (*env)->GetStringUTFChars(env, jname, 0);
+  if (!host) {
+    jxbt_throw_notbound(env, "host", jhost);
+    return -1;
+  }
+
+  return (jdouble) MSG_get_host_speed(host);
+}
+JNIEXPORT jint JNICALL
+Java_org_simgrid_msg_Host_getLoad(JNIEnv * env, jobject jhost) {
+  m_host_t host = jhost_get_native(env, jhost);
 
-  if (host->name)
-    free(host->name);
+  if (!host) {
+    jxbt_throw_notbound(env, "host", jhost);
+    return -1;
+  }
 
-  host->name = xbt_strdup(name);
-  (*env)->ReleaseStringUTFChars(env, jname, name);
+  return (jint) MSG_get_host_msgload(host);
 }
+JNIEXPORT jboolean JNICALL
+Java_org_simgrid_msg_Host_isAvail(JNIEnv * env, jobject jhost) {
+  m_host_t host = jhost_get_native(env, jhost);
+
+  if (!host) {
+    jxbt_throw_notbound(env, "host", jhost);
+    return 0;
+  }
 
-jboolean jhost_is_valid(jobject jhost, JNIEnv * env)
+  return (jboolean) MSG_host_is_avail(host);
+}
+
+JNIEXPORT jobjectArray JNICALL
+Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg)
 {
-  jfieldID id = jxbt_get_sfield(env, "simgrid/msg/Host", "bind", "J");
+  int index;
+  jobjectArray jtable;
+  jobject jhost;
+  jstring jname;
+  m_host_t host;
 
-  if (!id)
-    return 0;
+  xbt_dynar_t table =  MSG_hosts_as_dynar();
+  int count = xbt_dynar_length(table);
 
-  if ((*env)->GetLongField(env, jhost, id)) {
-    return JNI_TRUE;
-  } else {
-    return JNI_FALSE;
+  jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
+
+  if (!cls) {
+    return NULL;
+  }
+
+  jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
+
+  if (!jtable) {
+    jxbt_throw_jni(env, "Hosts table allocation failed");
+    return NULL;
+  }
+
+  for (index = 0; index < count; index++) {
+    host = xbt_dynar_get_as(table,index,m_host_t);
+    jhost = (jobject) (MSG_host_get_data(host));
+
+    if (!jhost) {
+      jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
+
+      jhost =
+               Java_org_simgrid_msg_Host_getByName(env, cls_arg, jname);
+      /* FIXME: leak of jname ? */
+    }
+
+    (*env)->SetObjectArrayElement(env, jtable, index, jhost);
   }
+  xbt_dynar_free(&table);
+  return jtable;
 }