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 */ 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; } /* 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 (!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); /* 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); }
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); }
jmethodID jxbt_get_static_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature) { jclass cls; jmethodID id; cls = jxbt_get_class(env, classname); if (!cls) return 0; id = (*env)->GetStaticMethodID(env, cls, name, signature); if (!id) { char *m = bprintf("Cannot find static method %s(%s) in %s", name, signature, classname); jxbt_throw_jni(env, m); free(m); return 0; } return id; }
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("org/simgrid/msg/Storage"); jtable = env->NewObjectArray((jsize) count, cls, nullptr); if (!jtable) { jxbt_throw_jni(env, "Storages table allocation failed"); return nullptr; } xbt_dict_cursor_t cursor=nullptr; const char* mount_name; const char* storage_name; xbt_dict_foreach(dict,cursor,mount_name,storage_name) { jname = env->NewStringUTF(storage_name); jstorage = Java_org_simgrid_msg_Storage_getByName(env,cls,jname); env->SetObjectArrayElement(jtable, index, jstorage); index++; }
jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature) { jmethodID id; if (!cls) return 0; id = (*env)->GetStaticMethodID(env, cls, name, signature); if (!id) { jmethodID tostr_id = (*env)->GetMethodID(env, cls, "getName", "()Ljava/lang/String;"); jstring jclassname = (jstring) (*env)->CallObjectMethod(env, cls, tostr_id, NULL); const char *classname = (*env)->GetStringUTFChars(env, jclassname, 0); char *m = bprintf("Cannot find static method %s(%s) in %s", name, signature, classname); (*env)->ReleaseStringUTFChars(env, jclassname, classname); jxbt_throw_jni(env, m); free(m); return 0; } return id; }
JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls) { msg_as_t as = MSG_environment_get_routing_root(); jobject jas = jas_new_instance(env); if (!jas) { jxbt_throw_jni(env, "java As instantiation failed"); return nullptr; } jas = jas_ref(env, jas); if (!jas) { jxbt_throw_jni(env, "new global ref allocation failed"); return nullptr; } jas_bind(jas, as, env); return (jobject) jas; }
JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_getCurrentProcess(JNIEnv * env, jclass cls) { msg_process_t process = MSG_process_self(); jobject jprocess; if (!process) { jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed")); return NULL; } jprocess = native_to_java_process(process); if (!jprocess) jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed")); return jprocess; }
JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_As_getSons(JNIEnv * env, jobject jas) { int index = 0; jobjectArray jtable; jobject tmp_jas; msg_as_t tmp_as; msg_as_t self_as = jas_get_native(env, jas); xbt_dict_t dict = MSG_environment_as_get_routing_sons(self_as); int count = xbt_dict_length(dict); jclass cls = env->FindClass("org/simgrid/msg/As"); if (!cls) { return NULL; } jtable = env->NewObjectArray((jsize) count, cls, NULL); if (!jtable) { jxbt_throw_jni(env, "Hosts table allocation failed"); return NULL; } xbt_dict_cursor_t cursor=NULL; char *key; xbt_dict_foreach(dict,cursor,key,tmp_as) { tmp_jas = jas_new_instance(env); if (!tmp_jas) { jxbt_throw_jni(env, "java As instantiation failed"); return NULL; } tmp_jas = jas_ref(env, tmp_jas); if (!tmp_jas) { jxbt_throw_jni(env, "new global ref allocation failed"); return NULL; } jas_bind(tmp_jas, tmp_as, env); env->SetObjectArrayElement(jtable, index, tmp_jas); index++; }
jclass jxbt_get_class(JNIEnv * env, const char *name) { jclass cls = env->FindClass(name); if (not cls) { jxbt_throw_jni(env, std::string("Class ") + name + " not found"); return nullptr; } return cls; }
jclass jxbt_get_class(JNIEnv * env, const char *name) { jclass cls = (*env)->FindClass(env, name); if (!cls) { char *m = bprintf("Class %s not found", name); jxbt_throw_jni(env, m); free(m); return NULL; } return cls; }
JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Host_currentHost(JNIEnv * env, jclass cls) { jobject jhost; msg_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; }
jmethodID jxbt_get_smethod(JNIEnv * env, const char *classname, const char *name, const char *signature) { jclass cls; jmethodID id; cls = jxbt_get_class(env, classname); if (not cls) return 0; id = env->GetMethodID(cls, name, signature); if (not id) { jxbt_throw_jni(env, std::string("Cannot find method") + name + "(" + signature + ") in " + classname); return 0; } return id; }
jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature) { jclass cls = jxbt_get_class(env, classname); jfieldID id; if (not cls) return 0; id = env->GetFieldID(cls, name, signature); if (not id) { jxbt_throw_jni(env, std::string("Cannot find field") + signature + " " + name + " in " + classname); return 0; } return id; }
JNIEXPORT jobjectArray JNICALL Java_org_simgrid_msg_Host_all(JNIEnv * env, jclass cls_arg) { int index; jobjectArray jtable; jobject jhost; jstring jname; msg_host_t host; xbt_dynar_t table = MSG_hosts_as_dynar(); int count = xbt_dynar_length(table); 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,msg_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; }
JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Process_fromPID(JNIEnv * env, jclass cls, jint PID) { msg_process_t process = MSG_process_from_PID(PID); if (!process) { jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID)); return NULL; } jobject jprocess = native_to_java_process(process); if (!jprocess) { jxbt_throw_jni(env, "get process failed"); return NULL; } return jprocess; }
jmethodID jxbt_get_static_jmethod(JNIEnv * env, jclass cls, const char *name, const char *signature) { jmethodID id; if (not cls) return 0; id = env->GetStaticMethodID(cls, name, signature); if (not id) { jmethodID tostr_id = env->GetMethodID(cls, "getName", "()Ljava/lang/String;"); jstring jclassname = (jstring) env->CallObjectMethod(cls, tostr_id, nullptr); const char *classname = env->GetStringUTFChars(jclassname, 0); env->ReleaseStringUTFChars(jclassname, classname); jxbt_throw_jni(env, std::string("Cannot find static method") + name + "(" + signature + ") in " + classname); return 0; } return id; }
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; }
jfieldID jxbt_get_sfield(JNIEnv * env, const char *classname, const char *name, const char *signature) { jclass cls = jxbt_get_class(env, classname); jfieldID id; if (!cls) return 0; id = (*env)->GetFieldID(env, cls, name, signature); if (!id) { char *m = bprintf("Cannot find field %s %s in %s", signature, name, classname); jxbt_throw_jni(env, m); free(m); return 0; } return id; }
JNIEXPORT void JNICALL Java_org_simgrid_msg_Process_create(JNIEnv * env, jobject jprocess_arg, jobject jhostname) { jobject jprocess; /* the global reference to the java process instance */ jstring jname; /* the name of the java process instance */ const char *name; /* the C name of the process */ const char *hostname; msg_process_t process; /* the native process to create */ msg_host_t host; /* Where that process lives */ hostname = (*env)->GetStringUTFChars(env, jhostname, 0); /* get the name of the java process */ jname = jprocess_get_name(jprocess_arg, env); if (!jname) { jxbt_throw_null(env, xbt_strdup("Internal error: Process name cannot be NULL")); return; } /* bind/retrieve the msg host */ host = MSG_host_by_name(hostname); if (!(host)) { /* not bound */ jxbt_throw_host_not_found(env, hostname); return; } /* create a global java process instance */ jprocess = jprocess_new_global_ref(jprocess_arg, env); if (!jprocess) { jxbt_throw_jni(env, "Can't get a global ref to the java process"); return; } /* build the C name of the process */ name = (*env)->GetStringUTFChars(env, jname, 0); name = xbt_strdup(name); /* Retrieve the kill time from the process */ jdouble jkill = (*env)->GetDoubleField(env, jprocess, jprocess_field_Process_killTime); /* Actually build the MSG process */ process = MSG_process_create_with_environment(name, (xbt_main_func_t) jprocess, /*data*/ jprocess, host, /*argc, argv, properties*/ 0,NULL,NULL); MSG_process_set_kill_time(process, (double)jkill); /* bind the java process instance to the native process */ jprocess_bind(jprocess, process, env); /* release our reference to the process name (variable name becomes invalid) */ //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING //(*env)->ReleaseStringUTFChars(env, jname, name); (*env)->ReleaseStringUTFChars(env, jhostname, hostname); /* sets the PID and the PPID of the process */ (*env)->SetIntField(env, jprocess, jprocess_field_Process_pid,(jint) MSG_process_get_PID(process)); (*env)->SetIntField(env, jprocess, jprocess_field_Process_ppid, (jint) MSG_process_get_PPID(process)); /* sets the Host of the process */ jobject jhost = Java_org_simgrid_msg_Host_getByName(env,NULL,jhostname); (*env)->SetObjectField(env, jprocess, jprocess_field_Process_host, jhost); }