1 /* Java Wrappers to the MSG API. */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights 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. */
10 #include "simix/context.h"
11 #include "smx_context_java.h"
13 #include "jmsg_process.h"
14 #include "jmsg_host.h"
15 #include "jmsg_task.h"
16 #include "jmsg_application_handler.h"
17 #include "jxbt_utilities.h"
20 #include "surf/surfxml_parse.h"
22 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
24 static JavaVM *__java_vm = NULL;
26 static jobject native_to_java_process(m_process_t process);
28 JavaVM *get_java_VM(void)
33 JNIEnv *get_current_thread_env(void)
37 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
42 static jobject native_to_java_process(m_process_t process)
44 return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
48 * The MSG process connected functions implementation.
51 JNIEXPORT void JNICALL
52 Java_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
58 jobject jprocess; /* the global reference to the java process instance */
59 jstring jname; /* the name of the java process instance */
60 const char *name; /* the C name of the process */
61 m_process_t process; /* the native process to create */
62 m_host_t host; /* Where that process lives */
64 DEBUG4("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
65 env, cls, jprocess_arg, jhost);
68 /* get the name of the java process */
69 jname = jprocess_get_name(jprocess_arg, env);
72 xbt_strdup("Internal error: Process name cannot be NULL"));
76 /* bind/retrieve the msg host */
77 host = jhost_get_native(env, jhost);
79 if (!(host)) { /* not binded */
80 jxbt_throw_notbound(env, "host", jhost);
84 /* create a global java process instance */
85 jprocess = jprocess_new_global_ref(jprocess_arg, env);
87 jxbt_throw_jni(env, "Can't get a global ref to the java process");
91 /* build the C name of the process */
92 name = (*env)->GetStringUTFChars(env, jname, 0);
93 name = xbt_strdup(name);
95 /* Actually build the MSG process */
96 process = MSG_process_create_with_environment(name,
97 (xbt_main_func_t) jprocess,
100 /*argc, argv, properties*/
103 MSG_process_set_data(process,&process);
105 /* release our reference to the process name (variable name becomes invalid) */
106 (*env)->ReleaseStringUTFChars(env, jname, name);
110 /* bind the java process instance to the native process */
111 jprocess_bind(jprocess, process, env);
115 JNIEXPORT void JNICALL
116 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
119 m_process_t process = jprocess_to_native_process(jprocess, env);
122 jxbt_throw_notbound(env, "process", jprocess);
126 /* try to suspend the process */
127 MSG_error_t rv = MSG_process_suspend(process);
129 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
130 bprintf("unexpected error , please report this bug"));
134 JNIEXPORT void JNICALL
135 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
138 m_process_t process = jprocess_to_native_process(jprocess, env);
141 jxbt_throw_notbound(env, "process", jprocess);
145 /* try to resume the process */
146 MSG_error_t rv = MSG_process_resume(process);
148 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
149 bprintf("unexpected error , please report this bug"));
152 JNIEXPORT jboolean JNICALL
153 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
156 m_process_t process = jprocess_to_native_process(jprocess, env);
159 jxbt_throw_notbound(env, "process", jprocess);
163 /* true is the process is suspended, false otherwise */
164 return (jboolean) MSG_process_is_suspended(process);
167 JNIEXPORT void JNICALL
168 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
171 /* get the native instances from the java ones */
172 m_process_t process = jprocess_to_native_process(jprocess, env);
175 jxbt_throw_notbound(env, "process", jprocess);
179 /* delete the global reference */
180 jprocess_delete_global_ref(native_to_java_process(process), env);
182 /* kill the native process (this wrapper is call by the destructor of the java
185 MSG_process_kill(process);
188 JNIEXPORT jobject JNICALL
189 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
192 /* get the native instances from the java ones */
193 m_process_t process = jprocess_to_native_process(jprocess, env);
197 jxbt_throw_notbound(env, "process", jprocess);
201 host = MSG_process_get_host(process);
203 if (!MSG_host_get_data(host)) {
204 jxbt_throw_jni(env, "MSG_process_get_host() failed");
208 /* return the global reference to the java host instance */
209 return (jobject) MSG_host_get_data(host);
213 JNIEXPORT jobject JNICALL
214 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
217 m_process_t process = MSG_process_from_PID(PID);
220 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
224 if (!native_to_java_process(process)) {
225 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
229 return (jobject) (native_to_java_process(process));
233 JNIEXPORT jint JNICALL
234 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
237 m_process_t process = jprocess_to_native_process(jprocess, env);
240 jxbt_throw_notbound(env, "process", jprocess);
244 return (jint) MSG_process_get_PID(process);
248 JNIEXPORT jint JNICALL
249 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
252 m_process_t process = jprocess_to_native_process(jprocess, env);
255 jxbt_throw_notbound(env, "process", jprocess);
259 return (jint) MSG_process_get_PPID(process);
262 JNIEXPORT jobject JNICALL
263 Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
265 m_process_t process = MSG_process_self();
269 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
273 jprocess = native_to_java_process(process);
276 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
281 JNIEXPORT void JNICALL
282 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
285 m_host_t host = jhost_get_native(env, jhost);
288 jxbt_throw_notbound(env, "host", jhost);
292 /* try to change the host of the process */
293 MSG_error_t rv = MSG_process_change_host(host);
295 jxbt_check_res("MSG_process_change_host()", rv, MSG_OK,
296 bprintf("unexpected error , please report this bug"));
300 JNIEXPORT void JNICALL
301 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
304 MSG_error_t rv = MSG_process_sleep((double) seconds);
306 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
307 bprintf("while process was waiting for %f seconds",
313 /***************************************************************************************
314 * The MSG host connected functions implementation. *
315 ***************************************************************************************/
317 JNIEXPORT jobject JNICALL
318 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
321 m_host_t host; /* native host */
322 jobject jhost; /* global reference to the java host instance returned */
324 /* get the C string from the java string */
325 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
327 /* get the host by name (the hosts are created during the grid resolution) */
328 host = MSG_get_host_by_name(name);
329 DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
331 (*env)->ReleaseStringUTFChars(env, jname, name);
333 if (!host) { /* invalid name */
334 jxbt_throw_host_not_found(env, name);
338 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
340 /* instanciate a new java host */
341 jhost = jhost_new_instance(env);
344 jxbt_throw_jni(env, "java host instantiation failed");
348 /* get a global reference to the newly created host */
349 jhost = jhost_ref(env, jhost);
352 jxbt_throw_jni(env, "new global ref allocation failed");
356 /* bind the java host and the native host */
357 jhost_bind(jhost, host, env);
359 /* the native host data field is set with the global reference to the
360 * java host returned by this function
362 MSG_host_set_data(host, (void *) jhost);
365 /* return the global reference to the java host instance */
366 return (jobject) MSG_host_get_data(host);
369 JNIEXPORT jstring JNICALL
370 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
373 m_host_t host = jhost_get_native(env, jhost);
376 jxbt_throw_notbound(env, "host", jhost);
380 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
383 JNIEXPORT jint JNICALL
384 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
386 return (jint) MSG_get_host_number();
389 JNIEXPORT jobject JNICALL
390 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
394 m_host_t host = MSG_host_self();
396 if (!MSG_host_get_data(host)) {
397 /* the native host not yet associated with the java host instance */
399 /* instanciate a new java host instance */
400 jhost = jhost_new_instance(env);
403 jxbt_throw_jni(env, "java host instantiation failed");
407 /* get a global reference to the newly created host */
408 jhost = jhost_ref(env, jhost);
411 jxbt_throw_jni(env, "global ref allocation failed");
415 /* Bind & store it */
416 jhost_bind(jhost, host, env);
417 MSG_host_set_data(host, (void *) jhost);
419 jhost = (jobject) MSG_host_get_data(host);
425 JNIEXPORT jdouble JNICALL
426 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
429 m_host_t host = jhost_get_native(env, jhost);
432 jxbt_throw_notbound(env, "host", jhost);
436 return (jdouble) MSG_get_host_speed(host);
439 JNIEXPORT jint JNICALL
440 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
443 m_host_t host = jhost_get_native(env, jhost);
446 jxbt_throw_notbound(env, "host", jhost);
450 return (jint) MSG_get_host_msgload(host);
454 JNIEXPORT jboolean JNICALL
455 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
458 m_host_t host = jhost_get_native(env, jhost);
461 jxbt_throw_notbound(env, "host", jhost);
465 return (jboolean) MSG_host_is_avail(host);
469 /***************************************************************************************
470 * The MSG task connected functions implementation. *
471 ***************************************************************************************/
473 JNIEXPORT void JNICALL
474 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
475 jobject jtask, jstring jname,
476 jdouble jcomputeDuration,
477 jdouble jmessageSize)
479 m_task_t task; /* the native task to create */
480 const char *name = NULL; /* the name of the task */
482 if (jcomputeDuration < 0) {
483 jxbt_throw_illegal(env,
485 ("Task ComputeDuration (%f) cannot be negative",
486 (double) jcomputeDuration));
490 if (jmessageSize < 0) {
491 jxbt_throw_illegal(env,
492 bprintf("Task MessageSize (%f) cannot be negative",
493 (double) jmessageSize));
498 /* get the C string from the java string */
499 name = (*env)->GetStringUTFChars(env, jname, 0);
503 /* create the task */
505 MSG_task_create(name, (double) jcomputeDuration,
506 (double) jmessageSize, NULL);
509 (*env)->ReleaseStringUTFChars(env, jname, name);
511 /* bind & store the task */
512 jtask_bind(jtask, task, env);
513 MSG_task_set_data(task, jtask);
516 JNIEXPORT void JNICALL
517 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
522 jcomputeDurations_arg,
527 m_task_t task; /* the native parallel task to create */
528 const char *name; /* the name of the task */
531 double *computeDurations;
532 double *messageSizes;
533 jdouble *jcomputeDurations;
534 jdouble *jmessageSizes;
540 if (!jcomputeDurations_arg) {
543 ("Parallel task compute durations cannot be null"));
547 if (!jmessageSizes_arg) {
550 ("Parallel task message sizes cannot be null"));
555 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
559 host_count = (int) (*env)->GetArrayLength(env, jhosts);
562 hosts = xbt_new0(m_host_t, host_count);
563 computeDurations = xbt_new0(double, host_count);
564 messageSizes = xbt_new0(double, host_count * host_count);
567 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
569 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
571 for (index = 0; index < host_count; index++) {
572 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
573 hosts[index] = jhost_get_native(env, jhost);
574 computeDurations[index] = jcomputeDurations[index];
576 for (index = 0; index < host_count * host_count; index++) {
577 messageSizes[index] = jmessageSizes[index];
580 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
581 jcomputeDurations, 0);
582 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
586 /* get the C string from the java string */
587 name = (*env)->GetStringUTFChars(env, jname, 0);
590 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
593 (*env)->ReleaseStringUTFChars(env, jname, name);
595 /* associate the java task object and the native task */
596 jtask_bind(jtask, task, env);
598 MSG_task_set_data(task, (void *) jtask);
600 if (!MSG_task_get_data(task))
601 jxbt_throw_jni(env, "global ref allocation failed");
604 JNIEXPORT jobject JNICALL
605 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
610 m_task_t task = jtask_to_native_task(jtask, env);
613 jxbt_throw_notbound(env, "task", jtask);
617 process = MSG_task_get_sender(task);
618 return (jobject) native_to_java_process(process);
621 JNIEXPORT jobject JNICALL
622 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
626 m_task_t task = jtask_to_native_task(jtask, env);
629 jxbt_throw_notbound(env, "task", jtask);
633 host = MSG_task_get_source(task);
635 if (!MSG_host_get_data(host)) {
636 jxbt_throw_jni(env, "MSG_task_get_source() failed");
640 return (jobject) MSG_host_get_data(host);
644 JNIEXPORT jstring JNICALL
645 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
648 m_task_t task = jtask_to_native_task(jtask, env);
651 jxbt_throw_notbound(env, "task", jtask);
655 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
658 JNIEXPORT void JNICALL
659 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
662 m_task_t ptask = jtask_to_native_task(jtask, env);
665 jxbt_throw_notbound(env, "task", jtask);
669 MSG_error_t rv = MSG_task_cancel(ptask);
671 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
672 bprintf("unexpected error , please report this bug"));
675 JNIEXPORT jdouble JNICALL
676 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
679 m_task_t ptask = jtask_to_native_task(jtask, env);
682 jxbt_throw_notbound(env, "task", jtask);
685 return (jdouble) MSG_task_get_compute_duration(ptask);
688 JNIEXPORT jdouble JNICALL
689 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
693 m_task_t ptask = jtask_to_native_task(jtask, env);
696 jxbt_throw_notbound(env, "task", jtask);
699 return (jdouble) MSG_task_get_remaining_computation(ptask);
702 JNIEXPORT void JNICALL
703 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
704 jobject jtask, jdouble priority)
706 m_task_t task = jtask_to_native_task(jtask, env);
709 jxbt_throw_notbound(env, "task", jtask);
712 MSG_task_set_priority(task, (double) priority);
715 JNIEXPORT void JNICALL
716 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
720 /* get the native task */
721 m_task_t task = jtask_to_native_task(jtask_arg, env);
725 jxbt_throw_notbound(env, "task", task);
728 jtask = (jobject) MSG_task_get_data(task);
730 MSG_error_t rv = MSG_task_destroy(task);
732 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
733 bprintf("unexpected error , please report this bug"));
736 JNIEXPORT void JNICALL
737 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
740 m_task_t task = jtask_to_native_task(jtask, env);
743 jxbt_throw_notbound(env, "task", jtask);
747 MSG_error_t rv = MSG_task_execute(task);
749 jxbt_check_res("MSG_task_execute()", rv,
750 MSG_HOST_FAILURE | MSG_TASK_CANCELLED,
751 bprintf("while executing task %s",
752 MSG_task_get_name(task)));
755 /***************************************************************************************
756 * Unsortable functions *
757 ***************************************************************************************/
760 JNIEXPORT jint JNICALL
761 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
763 return (jint) MSG_get_errno();
766 JNIEXPORT jdouble JNICALL
767 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
769 return (jdouble) MSG_get_clock();
773 JNIEXPORT void JNICALL
774 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
782 factory_initializer_to_use = SIMIX_ctx_java_factory_init;
785 argc = (int) (*env)->GetArrayLength(env, jargs);
788 argv = xbt_new0(char *, argc);
789 argv[0] = strdup("java");
791 for (index = 0; index < argc - 1; index++) {
792 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
793 tmp = (*env)->GetStringUTFChars(env, jval, 0);
794 argv[index + 1] = strdup(tmp);
795 (*env)->ReleaseStringUTFChars(env, jval, tmp);
798 MSG_global_init(&argc, argv);
800 for (index = 0; index < argc; index++)
805 (*env)->GetJavaVM(env, &__java_vm);
808 JNIEXPORT void JNICALL
809 JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
812 int index; //xbt_fifo_item_t item = NULL;
818 jxbt_check_res("MSG_main()", rv, MSG_OK,
820 ("unexpected error : MSG_main() failed .. please report this bug "));
823 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
825 DEBUG0("Clean java world");
826 /* Cleanup java hosts */
827 hosts = MSG_get_host_table();
828 for (index = 0; index < MSG_get_host_number() - 1; index++) {
829 jhost = (jobject) hosts[index]->data;
831 jhost_unref(env, jhost);
835 DEBUG0("Clean native world");
836 /* cleanup native stuff */
837 rv = MSG_OK != MSG_clean();
838 jxbt_check_res("MSG_clean()", rv, MSG_OK,
840 ("unexpected error : MSG_clean() failed .. please report this bug "));
844 JNIEXPORT jint JNICALL
845 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
848 return (jint) MSG_process_killall((int) jresetPID);
851 JNIEXPORT void JNICALL
852 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
853 jstring jplatformFile)
856 const char *platformFile =
857 (*env)->GetStringUTFChars(env, jplatformFile, 0);
859 MSG_create_environment(platformFile);
861 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
864 JNIEXPORT void JNICALL
865 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
869 m_process_t process = jprocess_to_native_process(jprocess, env);
872 jxbt_throw_notbound(env, "process", jprocess);
876 MSG_process_kill(process);
879 JNIEXPORT void JNICALL
880 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
882 const char *s = (*env)->GetStringUTFChars(env, js, 0);
884 (*env)->ReleaseStringUTFChars(env, js, s);
887 JNIEXPORT jobjectArray JNICALL
888 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
896 int count = MSG_get_host_number();
897 m_host_t *table = MSG_get_host_table();
899 jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
905 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
908 jxbt_throw_jni(env, "Hosts table allocation failed");
912 for (index = 0; index < count; index++) {
914 jhost = (jobject) (MSG_host_get_data(host));
917 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
920 Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
921 /* FIXME: leak of jname ? */
924 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
930 JNIEXPORT void JNICALL
931 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
932 jstring jalias, jobject jtask,
937 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
939 m_task_t task = jtask_to_native_task(jtask, env);
943 (*env)->ReleaseStringUTFChars(env, jalias, alias);
944 jxbt_throw_notbound(env, "task", jtask);
948 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
949 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
950 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
952 (*env)->ReleaseStringUTFChars(env, jalias, alias);
954 jxbt_check_res("MSG_task_send_with_timeout()", rv,
955 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
956 bprintf("while sending task %s to mailbox %s",
957 MSG_task_get_name(task), alias));
960 JNIEXPORT void JNICALL
961 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
962 jstring jalias, jobject jtask,
965 m_task_t task = jtask_to_native_task(jtask, env);
970 jxbt_throw_notbound(env, "task", jtask);
974 alias = (*env)->GetStringUTFChars(env, jalias, 0);
976 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
977 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
978 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
980 (*env)->ReleaseStringUTFChars(env, jalias, alias);
982 jxbt_check_res("MSG_task_send_bounded()", rv,
983 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
985 ("while sending task %s to mailbox %s with max rate %f",
986 MSG_task_get_name(task), alias, (double) jmaxRate));
990 JNIEXPORT jobject JNICALL
991 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
992 jstring jalias, jdouble jtimeout,
996 m_task_t task = NULL;
997 m_host_t host = NULL;
998 jobject jtask_global, jtask_local;
1002 host = jhost_get_native(env, jhost);
1005 jxbt_throw_notbound(env, "host", jhost);
1010 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1012 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1013 jtask_global = MSG_task_get_data(task);
1015 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1016 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1017 (*env)->DeleteGlobalRef(env, jtask_global);
1018 MSG_task_set_data(task, NULL);
1020 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1022 jxbt_check_res("MSG_task_receive_ext()", rv,
1023 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1024 bprintf("while receiving from mailbox %s", alias));
1026 return (jobject) jtask_local;
1029 JNIEXPORT jboolean JNICALL
1030 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1037 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1039 rv = MSG_task_listen(alias);
1041 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1043 return (jboolean) rv;
1046 JNIEXPORT jint JNICALL
1047 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1054 m_host_t host = jhost_get_native(env, jhost);
1057 jxbt_throw_notbound(env, "host", jhost);
1060 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1062 rv = MSG_task_listen_from_host(alias, host);
1064 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1069 JNIEXPORT jint JNICALL
1070 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1075 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1077 rv = MSG_task_listen_from(alias);
1079 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1084 JNIEXPORT void JNICALL
1085 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1086 jstring jdeploymentFile)
1089 const char *deploymentFile =
1090 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1092 surf_parse_reset_parser();
1094 surfxml_add_callback(STag_surfxml_process_cb_list,
1095 japplication_handler_on_begin_process);
1097 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1098 japplication_handler_on_process_arg);
1100 surfxml_add_callback(STag_surfxml_prop_cb_list,
1101 japplication_handler_on_property);
1103 surfxml_add_callback(ETag_surfxml_process_cb_list,
1104 japplication_handler_on_end_process);
1106 surf_parse_open(deploymentFile);
1108 japplication_handler_on_start_document();
1111 jxbt_throw_jni(env, "surf_parse() failed");
1115 japplication_handler_on_end_document();
1117 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);