4 * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved.
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package.
9 * This contains the implementation of the wrapper functions used to interface
10 * the java object with the native functions of the MSG API.
13 #include "msg/private.h"
14 #include "simix/private.h"
15 #include "simix/smx_context_java.h"
17 #include "jmsg_process.h"
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jmsg_application_handler.h"
21 #include "jxbt_utilities.h"
26 #include "msg/mailbox.h"
28 #include "surf/surfxml_parse.h"
31 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
33 static JavaVM *__java_vm = NULL;
35 static jobject native_to_java_process(m_process_t process);
37 JavaVM *get_java_VM(void)
42 JNIEnv *get_current_thread_env(void)
46 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
51 static jobject native_to_java_process(m_process_t process)
53 return ((smx_ctx_java_t)
54 (process->simdata->s_process->context))->jprocess;
59 * The MSG process connected functions implementation.
62 JNIEXPORT void JNICALL
63 Java_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
64 jobject jprocess_arg, jobject jhost)
66 jobject jprocess; /* the global reference to the java process instance */
67 jstring jname; /* the name of the java process instance */
68 const char *name; /* the C name of the process */
69 m_process_t process; /* the native process to create */
70 char alias[MAX_ALIAS_NAME + 1] = { 0 };
71 msg_mailbox_t mailbox;
74 ("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
75 env, cls, jprocess_arg, jhost);
76 /* get the name of the java process */
77 jname = jprocess_get_name(jprocess_arg, env);
82 ("Internal error: Process name cannot be NULL"));
86 /* allocate the data of the simulation */
87 process = xbt_new0(s_m_process_t, 1);
88 process->simdata = xbt_new0(s_simdata_process_t, 1);
90 /* create a global java process instance */
91 jprocess = jprocess_new_global_ref(jprocess_arg, env);
94 free(process->simdata);
96 jxbt_throw_jni(env, "Can't get a global ref to the java process");
100 /* bind the java process instance to the native process */
101 jprocess_bind(jprocess, process, env);
103 /* build the C name of the process */
104 name = (*env)->GetStringUTFChars(env, jname, 0);
105 process->name = xbt_strdup(name);
106 (*env)->ReleaseStringUTFChars(env, jname, name);
108 process->simdata->m_host = jhost_get_native(env, jhost);
111 if (!(process->simdata->m_host)) { /* not binded */
112 free(process->simdata);
115 jxbt_throw_notbound(env, "host", jhost);
118 process->simdata->PID = msg_global->PID++;
120 /* create a new context */
122 ("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
123 process->name, process->simdata->m_host->name, process->simdata->PID,
124 process, process->simdata, process->simdata->m_host,
125 process->simdata->m_host->simdata, env);
127 process->simdata->s_process =
128 SIMIX_process_create(process->name, (xbt_main_func_t)jprocess,
129 /*data */ (void *) process,
130 process->simdata->m_host->simdata->smx_host->name,
133 DEBUG1("context created (s_process=%p)", process->simdata->s_process);
136 if (SIMIX_process_self()) { /* someone created me */
137 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
139 process->simdata->PPID = -1;
142 process->simdata->last_errno = MSG_OK;
144 /* add the process to the list of the processes of the simulation */
145 xbt_fifo_unshift(msg_global->process_list, process);
147 sprintf(alias, "%s:%s", (process->simdata->m_host->simdata->smx_host)->name,
150 mailbox = MSG_mailbox_new(alias);
154 JNIEXPORT void JNICALL
155 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
158 m_process_t process = jprocess_to_native_process(jprocess, env);
161 jxbt_throw_notbound(env, "process", jprocess);
165 /* try to suspend the process */
166 if (MSG_OK != MSG_process_suspend(process))
167 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
170 JNIEXPORT void JNICALL
171 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
174 m_process_t process = jprocess_to_native_process(jprocess, env);
177 jxbt_throw_notbound(env, "process", jprocess);
181 /* try to resume the process */
182 if (MSG_OK != MSG_process_resume(process))
183 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
186 JNIEXPORT jboolean JNICALL
187 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
190 m_process_t process = jprocess_to_native_process(jprocess, env);
193 jxbt_throw_notbound(env, "process", jprocess);
197 /* true is the process is suspended, false otherwise */
198 return (jboolean) MSG_process_is_suspended(process);
201 JNIEXPORT void JNICALL
202 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
205 /* get the native instances from the java ones */
206 m_process_t process = jprocess_to_native_process(jprocess, env);
209 jxbt_throw_notbound(env, "process", jprocess);
213 /* delete the global reference */
214 jprocess_delete_global_ref(native_to_java_process(process), env);
216 /* kill the native process (this wrapper is call by the destructor of the java
219 MSG_process_kill(process);
222 JNIEXPORT jobject JNICALL
223 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
226 /* get the native instances from the java ones */
227 m_process_t process = jprocess_to_native_process(jprocess, env);
231 jxbt_throw_notbound(env, "process", jprocess);
235 host = MSG_process_get_host(process);
238 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
242 /* return the global reference to the java host instance */
243 return (jobject) host->data;
247 JNIEXPORT jobject JNICALL
248 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
250 m_process_t process = MSG_process_from_PID(PID);
253 jxbt_throw_process_not_found(env, bprintf("PID = %d", PID));
257 if (!native_to_java_process(process)) {
258 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
262 return (jobject) (native_to_java_process(process));
266 JNIEXPORT jint JNICALL
267 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
270 m_process_t process = jprocess_to_native_process(jprocess, env);
273 jxbt_throw_notbound(env, "process", jprocess);
277 return (jint) MSG_process_get_PID(process);
281 JNIEXPORT jint JNICALL
282 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
285 m_process_t process = jprocess_to_native_process(jprocess, env);
288 jxbt_throw_notbound(env, "process", jprocess);
292 return (jint) MSG_process_get_PPID(process);
295 JNIEXPORT jobject JNICALL
296 Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
298 m_process_t process = MSG_process_self();
302 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
306 jprocess = native_to_java_process(process);
309 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
314 JNIEXPORT void JNICALL
315 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
318 m_host_t host = jhost_get_native(env, jhost);
321 jxbt_throw_notbound(env, "host", jhost);
325 /* try to change the host of the process */
326 if (MSG_OK != MSG_process_change_host(host))
327 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
330 JNIEXPORT void JNICALL
331 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
334 if (MSG_OK != MSG_process_sleep((double) seconds))
335 jxbt_throw_native(env,
336 bprintf("MSG_process_change_host(%f) failed",
341 /***************************************************************************************
342 * The MSG host connected functions implementation. *
343 ***************************************************************************************/
345 JNIEXPORT jobject JNICALL
346 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
349 m_host_t host; /* native host */
350 jobject jhost; /* global reference to the java host instance returned */
352 /* get the C string from the java string */
353 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
355 /* get the host by name (the hosts are created during the grid resolution) */
356 host = MSG_get_host_by_name(name);
357 DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
359 (*env)->ReleaseStringUTFChars(env, jname, name);
361 if (!host) { /* invalid name */
362 jxbt_throw_host_not_found(env, name);
366 if (!host->data) { /* native host not associated yet with java host */
368 /* instanciate a new java host */
369 jhost = jhost_new_instance(env);
372 jxbt_throw_jni(env, "java host instantiation failed");
376 /* get a global reference to the newly created host */
377 jhost = jhost_ref(env, jhost);
380 jxbt_throw_jni(env, "new global ref allocation failed");
384 /* bind the java host and the native host */
385 jhost_bind(jhost, host, env);
387 /* the native host data field is set with the global reference to the
388 * java host returned by this function
390 host->data = (void *) jhost;
393 /* return the global reference to the java host instance */
394 return (jobject) host->data;
397 JNIEXPORT jstring JNICALL
398 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
401 m_host_t host = jhost_get_native(env, jhost);
404 jxbt_throw_notbound(env, "host", jhost);
408 return (*env)->NewStringUTF(env, host->name);
411 JNIEXPORT jint JNICALL
412 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
414 return (jint) MSG_get_host_number();
417 JNIEXPORT jobject JNICALL
418 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
422 m_host_t host = MSG_host_self();
425 /* the native host not yet associated with the java host instance */
427 /* instanciate a new java host instance */
428 jhost = jhost_new_instance(env);
431 jxbt_throw_jni(env, "java host instantiation failed");
435 /* get a global reference to the newly created host */
436 jhost = jhost_ref(env, jhost);
439 jxbt_throw_jni(env, "global ref allocation failed");
443 /* Bind & store it */
444 jhost_bind(jhost, host, env);
445 host->data = (void *) jhost;
447 jhost = (jobject) host->data;
453 JNIEXPORT jdouble JNICALL
454 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
457 m_host_t host = jhost_get_native(env, jhost);
460 jxbt_throw_notbound(env, "host", jhost);
464 return (jdouble) MSG_get_host_speed(host);
467 JNIEXPORT jint JNICALL
468 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
471 m_host_t host = jhost_get_native(env, jhost);
474 jxbt_throw_notbound(env, "host", jhost);
478 return (jint) MSG_get_host_msgload(host);
482 JNIEXPORT jboolean JNICALL
483 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
486 m_host_t host = jhost_get_native(env, jhost);
489 jxbt_throw_notbound(env, "host", jhost);
493 return (jboolean) MSG_host_is_avail(host);
497 /***************************************************************************************
498 * The MSG task connected functions implementation. *
499 ***************************************************************************************/
501 JNIEXPORT void JNICALL
502 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
503 jstring jname, jdouble jcomputeDuration,
504 jdouble jmessageSize)
506 m_task_t task; /* the native task to create */
507 const char *name=NULL; /* the name of the task */
509 if (jcomputeDuration < 0) {
510 jxbt_throw_illegal(env,
511 bprintf("Task ComputeDuration (%f) cannot be negative",
512 (double) jcomputeDuration));
516 if (jmessageSize < 0) {
517 jxbt_throw_illegal(env,
518 bprintf("Task MessageSize (%f) cannot be negative",
519 (double) jmessageSize));
524 /* get the C string from the java string */
525 name = (*env)->GetStringUTFChars(env, jname, 0);
529 /* create the task */
531 MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
535 (*env)->ReleaseStringUTFChars(env, jname, name);
537 /* bind & store the task */
538 jtask_bind(jtask, task, env);
540 /* allocate a new global reference to the java task instance */
541 task->data = (void *) jtask_new_global_ref(jtask, env);
544 jxbt_throw_jni(env, "global ref allocation failed");
548 JNIEXPORT void JNICALL
549 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
550 jobject jtask, jstring jname,
553 jcomputeDurations_arg,
554 jdoubleArray jmessageSizes_arg)
557 m_task_t task; /* the native parallel task to create */
558 const char *name; /* the name of the task */
561 double *computeDurations;
562 double *messageSizes;
563 jdouble *jcomputeDurations;
564 jdouble *jmessageSizes;
570 if (!jcomputeDurations_arg) {
573 ("Parallel task compute durations cannot be null"));
577 if (!jmessageSizes_arg) {
579 xbt_strdup("Parallel task message sizes cannot be null"));
584 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
588 host_count = (int) (*env)->GetArrayLength(env, jhosts);
591 hosts = xbt_new0(m_host_t, host_count);
592 computeDurations = xbt_new0(double, host_count);
593 messageSizes = xbt_new0(double, host_count * host_count);
596 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
597 jmessageSizes = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
599 for (index = 0; index < host_count; index++) {
600 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
601 hosts[index] = jhost_get_native(env, jhost);
602 computeDurations[index] = jcomputeDurations[index];
604 for (index = 0; index < host_count * host_count; index++) {
605 messageSizes[index] = jmessageSizes[index];
608 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
609 jcomputeDurations, 0);
610 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
614 /* get the C string from the java string */
615 name = (*env)->GetStringUTFChars(env, jname, 0);
618 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
621 (*env)->ReleaseStringUTFChars(env, jname, name);
623 /* associate the java task object and the native task */
624 jtask_bind(jtask, task, env);
626 task->data = (void *) jtask;
629 jxbt_throw_jni(env, "global ref allocation failed");
632 JNIEXPORT jobject JNICALL
633 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
638 m_task_t task = jtask_to_native_task(jtask, env);
641 jxbt_throw_notbound(env, "task", jtask);
645 process = MSG_task_get_sender(task);
646 return (jobject) native_to_java_process(process);
649 JNIEXPORT jobject JNICALL
650 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
654 m_task_t task = jtask_to_native_task(jtask, env);
657 jxbt_throw_notbound(env, "task", jtask);
661 host = MSG_task_get_source(task);
664 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
668 return (jobject) host->data;
672 JNIEXPORT jstring JNICALL
673 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
676 m_task_t task = jtask_to_native_task(jtask, env);
679 jxbt_throw_notbound(env, "task", jtask);
683 return (*env)->NewStringUTF(env, task->name);
686 JNIEXPORT void JNICALL
687 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
689 m_task_t ptask = jtask_to_native_task(jtask, env);
692 jxbt_throw_notbound(env, "task", jtask);
696 if (MSG_OK != MSG_task_cancel(ptask))
697 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
700 JNIEXPORT jdouble JNICALL
701 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
704 m_task_t ptask = jtask_to_native_task(jtask, env);
707 jxbt_throw_notbound(env, "task", jtask);
710 return (jdouble) MSG_task_get_compute_duration(ptask);
713 JNIEXPORT jdouble JNICALL
714 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env, jclass cls,
717 m_task_t ptask = jtask_to_native_task(jtask, env);
720 jxbt_throw_notbound(env, "task", jtask);
723 return (jdouble) MSG_task_get_remaining_computation(ptask);
726 JNIEXPORT void JNICALL
727 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
728 jobject jtask, jdouble priority)
730 m_task_t task = jtask_to_native_task(jtask, env);
733 jxbt_throw_notbound(env, "task", jtask);
736 MSG_task_set_priority(task, (double) priority);
739 JNIEXPORT void JNICALL
740 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
744 /* get the native task */
745 m_task_t task = jtask_to_native_task(jtask_arg, env);
749 jxbt_throw_notbound(env, "task", task);
752 jtask = (jobject) task->data;
754 if (MSG_OK != MSG_task_destroy(task))
755 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
757 /* delete the global reference to the java task object */
758 jtask_delete_global_ref(jtask, env);
761 JNIEXPORT void JNICALL
762 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
765 m_task_t task = jtask_to_native_task(jtask, env);
768 jxbt_throw_notbound(env, "task", jtask);
772 if (MSG_OK != MSG_task_execute(task))
773 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
776 /***************************************************************************************
777 * Unsortable functions *
778 ***************************************************************************************/
781 JNIEXPORT jint JNICALL
782 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
784 return (jint) MSG_get_errno();
787 JNIEXPORT jdouble JNICALL
788 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
790 return (jdouble) MSG_get_clock();
794 JNIEXPORT void JNICALL
795 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) {
803 argc = (int) (*env)->GetArrayLength(env, jargs);
806 argv = xbt_new0(char *, argc);
807 argv[0] = strdup("java");
809 for (index = 0; index < argc - 1; index++) {
810 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
811 tmp = (*env)->GetStringUTFChars(env, jval, 0);
812 argv[index + 1] = strdup(tmp);
813 (*env)->ReleaseStringUTFChars(env, jval, tmp);
816 MSG_global_init(&argc, argv);
817 SIMIX_context_select_factory("java");
819 for (index = 0; index < argc; index++)
824 (*env)->GetJavaVM(env, &__java_vm);
827 JNIEXPORT void JNICALL
828 JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) {
829 xbt_fifo_item_t item = NULL;
830 m_host_t host = NULL;
834 if (MSG_OK != MSG_main()) {
835 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
838 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
840 DEBUG0("Clean java world");
841 /* Cleanup java hosts */
842 xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
843 jhost = (jobject) host->data;
846 jhost_unref(env, jhost);
849 DEBUG0("Clean native world");
850 /* cleanup native stuff */
851 if (MSG_OK != MSG_clean()){
852 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
856 JNIEXPORT jint JNICALL
857 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
860 return (jint) MSG_process_killall((int) jresetPID);
863 JNIEXPORT void JNICALL
864 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
865 jstring jplatformFile)
868 const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
870 MSG_create_environment(platformFile);
872 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
875 JNIEXPORT void JNICALL
876 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
880 m_process_t process = jprocess_to_native_process(jprocess, env);
883 jxbt_throw_notbound(env, "process", jprocess);
887 SIMIX_context_stop(SIMIX_process_self()->context);
890 JNIEXPORT void JNICALL
891 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
893 const char *s = (*env)->GetStringUTFChars(env, js, 0);
895 (*env)->ReleaseStringUTFChars(env, js, s);
898 JNIEXPORT jobjectArray JNICALL
899 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
907 int count = xbt_fifo_size(msg_global->host);
908 m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
910 jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
916 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
919 jxbt_throw_jni(env, "Hosts table allocation failed");
923 for (index = 0; index < count; index++) {
925 jhost = (jobject) (host->data);
928 jname = (*env)->NewStringUTF(env, host->name);
930 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
931 /* FIXME: leak of jname ? */
934 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
941 JNIEXPORT void JNICALL
942 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
948 /* get the C string from the java string */
949 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
952 SIMIX_context_select_factory(name);
954 errmsg = xbt_strdup(e.msg);
958 (*env)->ReleaseStringUTFChars(env, jname, name);
961 char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
963 jxbt_throw_native(env, thrown);
967 JNIEXPORT void JNICALL
968 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
969 jstring jalias, jobject jtask,
974 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
976 m_task_t task = jtask_to_native_task(jtask, env);
980 (*env)->ReleaseStringUTFChars(env, jalias, alias);
981 jxbt_throw_notbound(env, "task", jtask);
985 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
987 (*env)->ReleaseStringUTFChars(env, jalias, alias);
989 /* FIXME throw the right exception corresponding to HostFailureException, TransferFailureException, TimeoutFailureException
990 * Note: these exceptions must be created beforehand
991 * then, you want to create some functions like jxbt_throw_notbound()
992 * then, you must declare in the MsgNative stuff that these native functions can throw these exceptions
995 jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
999 JNIEXPORT void JNICALL
1000 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1001 jstring jalias, jobject jtask,
1004 m_task_t task = jtask_to_native_task(jtask, env);
1009 jxbt_throw_notbound(env, "task", jtask);
1013 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1015 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1017 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1020 jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1023 JNIEXPORT jobject JNICALL
1024 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1025 jstring jalias, jdouble jtimeout,
1029 m_task_t task = NULL;
1030 m_host_t host = NULL;
1034 host = jhost_get_native(env, jhost);
1037 jxbt_throw_notbound(env, "host", jhost);
1042 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1044 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1046 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1049 jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1053 return (jobject) task->data;
1056 JNIEXPORT jboolean JNICALL
1057 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1064 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1066 rv = MSG_task_listen(alias);
1068 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1070 return (jboolean) rv;
1073 JNIEXPORT jint JNICALL
1074 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1075 jstring jalias, jobject jhost)
1081 m_host_t host = jhost_get_native(env, jhost);
1084 jxbt_throw_notbound(env, "host", jhost);
1088 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1090 rv = MSG_task_listen_from_host(alias, host);
1092 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1097 JNIEXPORT jint JNICALL
1098 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1103 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1105 rv = MSG_task_listen_from(alias);
1107 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1112 JNIEXPORT void JNICALL
1113 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1114 jstring jdeploymentFile)
1117 const char *deploymentFile =
1118 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1120 surf_parse_reset_parser();
1122 surfxml_add_callback(STag_surfxml_process_cb_list,
1123 japplication_handler_on_begin_process);
1125 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1126 japplication_handler_on_process_arg);
1128 surfxml_add_callback(STag_surfxml_prop_cb_list,
1129 japplication_handler_on_property);
1131 surfxml_add_callback(ETag_surfxml_process_cb_list,
1132 japplication_handler_on_end_process);
1134 surf_parse_open(deploymentFile);
1136 japplication_handler_on_start_document();
1139 jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
1143 japplication_handler_on_end_document();
1145 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);