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 <surf/surfxml_parse.h>
13 #include "smx_context_java.h"
15 #include "jmsg_process.h"
16 #include "jmsg_host.h"
17 #include "jmsg_task.h"
18 #include "jmsg_application_handler.h"
19 #include "jxbt_utilities.h"
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
25 static JavaVM *__java_vm = NULL;
27 static jobject native_to_java_process(m_process_t process);
29 JavaVM *get_java_VM(void)
34 JNIEnv *get_current_thread_env(void)
38 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
43 static jobject native_to_java_process(m_process_t process)
45 return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
49 * The MSG process connected functions implementation.
52 JNIEXPORT void JNICALL
53 Java_org_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
59 jobject jprocess; /* the global reference to the java process instance */
60 jstring jname; /* the name of the java process instance */
61 const char *name; /* the C name of the process */
62 m_process_t process; /* the native process to create */
63 m_host_t host; /* Where that process lives */
65 XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
66 env, cls, jprocess_arg, jhost);
69 /* get the name of the java process */
70 jname = jprocess_get_name(jprocess_arg, env);
73 xbt_strdup("Internal error: Process name cannot be NULL"));
77 /* bind/retrieve the msg host */
78 host = jhost_get_native(env, jhost);
80 if (!(host)) { /* not binded */
81 jxbt_throw_notbound(env, "host", jhost);
85 /* create a global java process instance */
86 jprocess = jprocess_new_global_ref(jprocess_arg, env);
88 jxbt_throw_jni(env, "Can't get a global ref to the java process");
92 /* build the C name of the process */
93 name = (*env)->GetStringUTFChars(env, jname, 0);
94 name = xbt_strdup(name);
96 /* Actually build the MSG process */
97 process = MSG_process_create_with_environment(name,
98 (xbt_main_func_t) jprocess,
101 /*argc, argv, properties*/
104 MSG_process_set_data(process,&process);
106 /* release our reference to the process name (variable name becomes invalid) */
107 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
108 //(*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_org_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_org_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_org_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_org_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_org_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_org_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_org_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_org_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_org_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_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
283 jobject jprocess, jobject jhost)
285 m_process_t process = jprocess_to_native_process(jprocess, env);
288 jxbt_throw_notbound(env, "process", jprocess);
292 m_host_t host = jhost_get_native(env, jhost);
295 jxbt_throw_notbound(env, "host", jhost);
299 /* try to change the host of the process */
300 MSG_error_t rv = MSG_process_migrate(process, host);
301 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
302 bprintf("unexpected error , please report this bug"));
306 JNIEXPORT void JNICALL
307 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
310 MSG_error_t rv = MSG_process_sleep((double) seconds);
312 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
313 bprintf("while process was waiting for %f seconds",
319 /***************************************************************************************
320 * The MSG host connected functions implementation. *
321 ***************************************************************************************/
323 JNIEXPORT jobject JNICALL
324 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
327 m_host_t host; /* native host */
328 jobject jhost; /* global reference to the java host instance returned */
330 /* get the C string from the java string */
331 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
332 XBT_DEBUG("Looking for host '%s'",name);
333 /* get the host by name (the hosts are created during the grid resolution) */
334 host = MSG_get_host_by_name(name);
335 XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
337 if (!host) { /* invalid name */
338 jxbt_throw_host_not_found(env, name);
339 (*env)->ReleaseStringUTFChars(env, jname, name);
342 (*env)->ReleaseStringUTFChars(env, jname, name);
344 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
346 /* instanciate a new java host */
347 jhost = jhost_new_instance(env);
350 jxbt_throw_jni(env, "java host instantiation failed");
354 /* get a global reference to the newly created host */
355 jhost = jhost_ref(env, jhost);
358 jxbt_throw_jni(env, "new global ref allocation failed");
362 /* bind the java host and the native host */
363 jhost_bind(jhost, host, env);
365 /* the native host data field is set with the global reference to the
366 * java host returned by this function
368 MSG_host_set_data(host, (void *) jhost);
371 /* return the global reference to the java host instance */
372 return (jobject) MSG_host_get_data(host);
375 JNIEXPORT jstring JNICALL
376 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
379 m_host_t host = jhost_get_native(env, jhost);
382 jxbt_throw_notbound(env, "host", jhost);
386 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
389 JNIEXPORT jint JNICALL
390 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
392 return (jint) MSG_get_host_number();
395 JNIEXPORT jobject JNICALL
396 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
400 m_host_t host = MSG_host_self();
402 if (!MSG_host_get_data(host)) {
403 /* the native host not yet associated with the java host instance */
405 /* instanciate a new java host instance */
406 jhost = jhost_new_instance(env);
409 jxbt_throw_jni(env, "java host instantiation failed");
413 /* get a global reference to the newly created host */
414 jhost = jhost_ref(env, jhost);
417 jxbt_throw_jni(env, "global ref allocation failed");
421 /* Bind & store it */
422 jhost_bind(jhost, host, env);
423 MSG_host_set_data(host, (void *) jhost);
425 jhost = (jobject) MSG_host_get_data(host);
431 JNIEXPORT jdouble JNICALL
432 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
435 m_host_t host = jhost_get_native(env, jhost);
438 jxbt_throw_notbound(env, "host", jhost);
442 return (jdouble) MSG_get_host_speed(host);
445 JNIEXPORT jint JNICALL
446 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
449 m_host_t host = jhost_get_native(env, jhost);
452 jxbt_throw_notbound(env, "host", jhost);
456 return (jint) MSG_get_host_msgload(host);
460 JNIEXPORT jboolean JNICALL
461 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
464 m_host_t host = jhost_get_native(env, jhost);
467 jxbt_throw_notbound(env, "host", jhost);
471 return (jboolean) MSG_host_is_avail(host);
475 /***************************************************************************************
476 * The MSG task connected functions implementation. *
477 ***************************************************************************************/
479 JNIEXPORT void JNICALL
480 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
481 jobject jtask, jstring jname,
482 jdouble jcomputeDuration,
483 jdouble jmessageSize)
485 m_task_t task; /* the native task to create */
486 const char *name = NULL; /* the name of the task */
488 if (jcomputeDuration < 0) {
489 jxbt_throw_illegal(env,
491 ("Task ComputeDuration (%f) cannot be negative",
492 (double) jcomputeDuration));
496 if (jmessageSize < 0) {
497 jxbt_throw_illegal(env,
498 bprintf("Task MessageSize (%f) cannot be negative",
499 (double) jmessageSize));
504 /* get the C string from the java string */
505 name = (*env)->GetStringUTFChars(env, jname, 0);
509 /* create the task */
511 MSG_task_create(name, (double) jcomputeDuration,
512 (double) jmessageSize, NULL);
515 (*env)->ReleaseStringUTFChars(env, jname, name);
517 /* bind & store the task */
518 jtask_bind(jtask, task, env);
519 MSG_task_set_data(task, jtask);
522 JNIEXPORT void JNICALL
523 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
528 jcomputeDurations_arg,
533 m_task_t task; /* the native parallel task to create */
534 const char *name; /* the name of the task */
537 double *computeDurations;
538 double *messageSizes;
539 jdouble *jcomputeDurations;
540 jdouble *jmessageSizes;
546 if (!jcomputeDurations_arg) {
549 ("Parallel task compute durations cannot be null"));
553 if (!jmessageSizes_arg) {
556 ("Parallel task message sizes cannot be null"));
561 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
565 host_count = (int) (*env)->GetArrayLength(env, jhosts);
568 hosts = xbt_new0(m_host_t, host_count);
569 computeDurations = xbt_new0(double, host_count);
570 messageSizes = xbt_new0(double, host_count * host_count);
573 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
575 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
577 for (index = 0; index < host_count; index++) {
578 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
579 hosts[index] = jhost_get_native(env, jhost);
580 computeDurations[index] = jcomputeDurations[index];
582 for (index = 0; index < host_count * host_count; index++) {
583 messageSizes[index] = jmessageSizes[index];
586 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
587 jcomputeDurations, 0);
588 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
592 /* get the C string from the java string */
593 name = (*env)->GetStringUTFChars(env, jname, 0);
596 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
599 (*env)->ReleaseStringUTFChars(env, jname, name);
601 /* associate the java task object and the native task */
602 jtask_bind(jtask, task, env);
604 MSG_task_set_data(task, (void *) jtask);
606 if (!MSG_task_get_data(task))
607 jxbt_throw_jni(env, "global ref allocation failed");
610 JNIEXPORT jobject JNICALL
611 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
616 m_task_t task = jtask_to_native_task(jtask, env);
619 jxbt_throw_notbound(env, "task", jtask);
623 process = MSG_task_get_sender(task);
624 return (jobject) native_to_java_process(process);
627 JNIEXPORT jobject JNICALL
628 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
632 m_task_t task = jtask_to_native_task(jtask, env);
635 jxbt_throw_notbound(env, "task", jtask);
639 host = MSG_task_get_source(task);
641 if (!MSG_host_get_data(host)) {
642 jxbt_throw_jni(env, "MSG_task_get_source() failed");
646 return (jobject) MSG_host_get_data(host);
650 JNIEXPORT jstring JNICALL
651 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
654 m_task_t task = jtask_to_native_task(jtask, env);
657 jxbt_throw_notbound(env, "task", jtask);
661 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
664 JNIEXPORT void JNICALL
665 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
668 m_task_t ptask = jtask_to_native_task(jtask, env);
671 jxbt_throw_notbound(env, "task", jtask);
675 MSG_error_t rv = MSG_task_cancel(ptask);
677 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
678 bprintf("unexpected error , please report this bug"));
681 JNIEXPORT jdouble JNICALL
682 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
685 m_task_t ptask = jtask_to_native_task(jtask, env);
688 jxbt_throw_notbound(env, "task", jtask);
691 return (jdouble) MSG_task_get_compute_duration(ptask);
694 JNIEXPORT jdouble JNICALL
695 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
699 m_task_t ptask = jtask_to_native_task(jtask, env);
702 jxbt_throw_notbound(env, "task", jtask);
705 return (jdouble) MSG_task_get_remaining_computation(ptask);
708 JNIEXPORT void JNICALL
709 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
710 jobject jtask, jdouble priority)
712 m_task_t task = jtask_to_native_task(jtask, env);
715 jxbt_throw_notbound(env, "task", jtask);
718 MSG_task_set_priority(task, (double) priority);
721 JNIEXPORT void JNICALL
722 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
726 /* get the native task */
727 m_task_t task = jtask_to_native_task(jtask_arg, env);
731 jxbt_throw_notbound(env, "task", task);
734 jtask = (jobject) MSG_task_get_data(task);
736 MSG_error_t rv = MSG_task_destroy(task);
738 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
739 bprintf("unexpected error , please report this bug"));
742 JNIEXPORT void JNICALL
743 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
746 m_task_t task = jtask_to_native_task(jtask, env);
749 jxbt_throw_notbound(env, "task", jtask);
753 MSG_error_t rv = MSG_task_execute(task);
755 jxbt_check_res("MSG_task_execute()", rv,
756 MSG_HOST_FAILURE | MSG_TASK_CANCELLED,
757 bprintf("while executing task %s",
758 MSG_task_get_name(task)));
761 /***************************************************************************************
762 * Unsortable functions *
763 ***************************************************************************************/
766 JNIEXPORT jint JNICALL
767 Java_org_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
769 return (jint) MSG_get_errno();
772 JNIEXPORT jdouble JNICALL
773 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
775 return (jdouble) MSG_get_clock();
778 JNIEXPORT void JNICALL
779 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
787 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
790 argc = (int) (*env)->GetArrayLength(env, jargs);
793 argv = xbt_new0(char *, argc);
794 argv[0] = strdup("java");
796 for (index = 0; index < argc - 1; index++) {
797 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
798 tmp = (*env)->GetStringUTFChars(env, jval, 0);
799 argv[index + 1] = strdup(tmp);
800 //argv[index] = strdup(tmp);
801 (*env)->ReleaseStringUTFChars(env, jval, tmp);
804 MSG_global_init(&argc, argv);
806 for (index = 0; index < argc; index++)
811 (*env)->GetJavaVM(env, &__java_vm);
814 JNIEXPORT void JNICALL
815 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
818 int index; //xbt_fifo_item_t item = NULL;
823 XBT_INFO("Ready to run MSG_MAIN");
825 XBT_INFO("Done running MSG_MAIN");
826 jxbt_check_res("MSG_main()", rv, MSG_OK,
828 ("unexpected error : MSG_main() failed .. please report this bug "));
830 XBT_INFO("MSG_main finished");
832 XBT_INFO("Clean java world");
833 /* Cleanup java hosts */
834 hosts = MSG_get_host_table();
835 for (index = 0; index < MSG_get_host_number() - 1; index++) {
836 jhost = (jobject) hosts[index]->data;
838 jhost_unref(env, jhost);
842 XBT_INFO("Clean native world");
843 /* cleanup native stuff */
844 rv = MSG_OK != MSG_clean();
845 jxbt_check_res("MSG_clean()", rv, MSG_OK,
847 ("unexpected error : MSG_clean() failed .. please report this bug "));
850 JNIEXPORT jint JNICALL
851 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
854 return (jint) MSG_process_killall((int) jresetPID);
857 JNIEXPORT void JNICALL
858 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
859 jstring jplatformFile)
862 const char *platformFile =
863 (*env)->GetStringUTFChars(env, jplatformFile, 0);
865 MSG_create_environment(platformFile);
867 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
870 JNIEXPORT void JNICALL
871 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
875 m_process_t process = jprocess_to_native_process(jprocess, env);
878 jxbt_throw_notbound(env, "process", jprocess);
882 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
885 JNIEXPORT void JNICALL
886 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
888 const char *s = (*env)->GetStringUTFChars(env, js, 0);
890 (*env)->ReleaseStringUTFChars(env, js, s);
893 JNIEXPORT jobjectArray JNICALL
894 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
902 int count = MSG_get_host_number();
903 m_host_t *table = MSG_get_host_table();
905 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
911 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
914 jxbt_throw_jni(env, "Hosts table allocation failed");
918 for (index = 0; index < count; index++) {
920 jhost = (jobject) (MSG_host_get_data(host));
923 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
926 Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
927 /* FIXME: leak of jname ? */
930 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
936 JNIEXPORT void JNICALL
937 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
938 jstring jalias, jobject jtask,
943 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
945 m_task_t task = jtask_to_native_task(jtask, env);
949 (*env)->ReleaseStringUTFChars(env, jalias, alias);
950 jxbt_throw_notbound(env, "task", jtask);
954 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
955 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
956 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
958 (*env)->ReleaseStringUTFChars(env, jalias, alias);
960 jxbt_check_res("MSG_task_send_with_timeout()", rv,
961 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
962 bprintf("while sending task %s to mailbox %s",
963 MSG_task_get_name(task), alias));
966 JNIEXPORT void JNICALL
967 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
968 jstring jalias, jobject jtask,
971 m_task_t task = jtask_to_native_task(jtask, env);
976 jxbt_throw_notbound(env, "task", jtask);
980 alias = (*env)->GetStringUTFChars(env, jalias, 0);
982 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
983 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
984 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
986 (*env)->ReleaseStringUTFChars(env, jalias, alias);
988 jxbt_check_res("MSG_task_send_bounded()", rv,
989 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
991 ("while sending task %s to mailbox %s with max rate %f",
992 MSG_task_get_name(task), alias, (double) jmaxRate));
996 JNIEXPORT jobject JNICALL
997 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
998 jstring jalias, jdouble jtimeout,
1002 m_task_t task = NULL;
1003 m_host_t host = NULL;
1004 jobject jtask_global, jtask_local;
1008 host = jhost_get_native(env, jhost);
1011 jxbt_throw_notbound(env, "host", jhost);
1016 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1018 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1019 jtask_global = MSG_task_get_data(task);
1021 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1022 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1023 (*env)->DeleteGlobalRef(env, jtask_global);
1024 MSG_task_set_data(task, NULL);
1026 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1028 jxbt_check_res("MSG_task_receive_ext()", rv,
1029 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1030 bprintf("while receiving from mailbox %s", alias));
1032 return (jobject) jtask_local;
1035 JNIEXPORT jboolean JNICALL
1036 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1043 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1045 rv = MSG_task_listen(alias);
1047 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1049 return (jboolean) rv;
1052 JNIEXPORT jint JNICALL
1053 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1060 m_host_t host = jhost_get_native(env, jhost);
1063 jxbt_throw_notbound(env, "host", jhost);
1066 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1068 rv = MSG_task_listen_from_host(alias, host);
1070 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1075 JNIEXPORT jint JNICALL
1076 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1081 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1083 rv = MSG_task_listen_from(alias);
1085 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1090 JNIEXPORT void JNICALL
1091 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1092 jstring jdeploymentFile)
1095 const char *deploymentFile =
1096 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1098 surf_parse_reset_callbacks();
1100 surfxml_add_callback(STag_surfxml_process_cb_list,
1101 japplication_handler_on_begin_process);
1103 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1104 japplication_handler_on_process_arg);
1106 surfxml_add_callback(STag_surfxml_prop_cb_list,
1107 japplication_handler_on_property);
1109 surfxml_add_callback(ETag_surfxml_process_cb_list,
1110 japplication_handler_on_end_process);
1112 surf_parse_open(deploymentFile);
1114 japplication_handler_on_start_document();
1117 jxbt_throw_jni(env, "surf_parse() failed");
1121 japplication_handler_on_end_document();
1123 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);