JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_getByName(JNIEnv * env, jclass cls, jstring jname) { msg_host_t host; /* native host */ jobject jhost; /* global reference to the java host instance returned */ /* 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); /* get the host by name (the hosts are created during the grid resolution) */ host = MSG_get_host_by_name(name); if (!host) { /* invalid name */ jxbt_throw_host_not_found(env, name); (*env)->ReleaseStringUTFChars(env, jname, name); return NULL; } (*env)->ReleaseStringUTFChars(env, jname, name); 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); 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); /* the native host data field is set with the global reference to the * java host returned by this function */ xbt_lib_set(host_lib, host->key, JAVA_HOST_LEVEL, (void *) jhost); } /* return the global reference to the java host instance */ return (jobject) xbt_lib_get_level(host, JAVA_HOST_LEVEL); }
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 void JNICALL JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) { /* Run everything */ XBT_DEBUG("Ready to run MSG_MAIN"); msg_error_t rv = MSG_main(); XBT_DEBUG("Done running MSG_MAIN"); jxbt_check_res("MSG_main()", rv, MSG_OK, xbt_strdup("unexpected error : MSG_main() failed .. please report this bug ")); XBT_INFO("MSG_main finished; Cleaning up the simulation..."); /* Cleanup java hosts */ xbt_dynar_t hosts = MSG_hosts_as_dynar(); for (unsigned long index = 0; index < xbt_dynar_length(hosts) - 1; index++) { msg_host_t msg_host = xbt_dynar_get_as(hosts,index,msg_host_t); jobject jhost = (jobject) msg_host->extension(JAVA_HOST_LEVEL); if (jhost) jhost_unref(env, jhost); } xbt_dynar_free(&hosts); /* Cleanup java storages */ xbt_dynar_t storages = MSG_storages_as_dynar(); if(!xbt_dynar_is_empty(storages)){ for (unsigned long index = 0; index < xbt_dynar_length(storages) - 1; index++) { jobject 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 jobject JNICALL Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) { jobject jhost; msg_host_t host = MSG_host_self(); 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 */ 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); xbt_lib_set(host_lib, host->key, JAVA_HOST_LEVEL, (void *) jhost); } else { jhost = (jobject) xbt_lib_get_level(host, JAVA_HOST_LEVEL); } return jhost; }
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; }