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 (*env)->ReleaseStringUTFChars(env, jname, name);
111 /* bind the java process instance to the native process */
112 jprocess_bind(jprocess, process, env);
116 JNIEXPORT void JNICALL
117 Java_org_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
120 m_process_t process = jprocess_to_native_process(jprocess, env);
123 jxbt_throw_notbound(env, "process", jprocess);
127 /* try to suspend the process */
128 MSG_error_t rv = MSG_process_suspend(process);
130 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
131 bprintf("unexpected error , please report this bug"));
135 JNIEXPORT void JNICALL
136 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
139 m_process_t process = jprocess_to_native_process(jprocess, env);
142 jxbt_throw_notbound(env, "process", jprocess);
146 /* try to resume the process */
147 MSG_error_t rv = MSG_process_resume(process);
149 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
150 bprintf("unexpected error , please report this bug"));
153 JNIEXPORT jboolean JNICALL
154 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
157 m_process_t process = jprocess_to_native_process(jprocess, env);
160 jxbt_throw_notbound(env, "process", jprocess);
164 /* true is the process is suspended, false otherwise */
165 return (jboolean) MSG_process_is_suspended(process);
168 JNIEXPORT void JNICALL
169 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
172 /* get the native instances from the java ones */
173 m_process_t process = jprocess_to_native_process(jprocess, env);
176 jxbt_throw_notbound(env, "process", jprocess);
180 /* delete the global reference */
181 jprocess_delete_global_ref(native_to_java_process(process), env);
183 /* kill the native process (this wrapper is call by the destructor of the java
186 MSG_process_kill(process);
189 JNIEXPORT jobject JNICALL
190 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
193 /* get the native instances from the java ones */
194 m_process_t process = jprocess_to_native_process(jprocess, env);
198 jxbt_throw_notbound(env, "process", jprocess);
202 host = MSG_process_get_host(process);
204 if (!MSG_host_get_data(host)) {
205 jxbt_throw_jni(env, "MSG_process_get_host() failed");
209 /* return the global reference to the java host instance */
210 return (jobject) MSG_host_get_data(host);
214 JNIEXPORT jobject JNICALL
215 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
218 m_process_t process = MSG_process_from_PID(PID);
221 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
225 if (!native_to_java_process(process)) {
226 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
230 return (jobject) (native_to_java_process(process));
234 JNIEXPORT jint JNICALL
235 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
238 m_process_t process = jprocess_to_native_process(jprocess, env);
241 jxbt_throw_notbound(env, "process", jprocess);
245 return (jint) MSG_process_get_PID(process);
249 JNIEXPORT jint JNICALL
250 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
253 m_process_t process = jprocess_to_native_process(jprocess, env);
256 jxbt_throw_notbound(env, "process", jprocess);
260 return (jint) MSG_process_get_PPID(process);
263 JNIEXPORT jobject JNICALL
264 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
266 m_process_t process = MSG_process_self();
270 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
274 jprocess = native_to_java_process(process);
277 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
282 JNIEXPORT void JNICALL
283 Java_org_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
286 m_host_t host = jhost_get_native(env, jhost);
289 jxbt_throw_notbound(env, "host", jhost);
293 /* try to change the host of the process */
294 MSG_error_t rv = MSG_process_change_host(host);
296 jxbt_check_res("MSG_process_change_host()", rv, MSG_OK,
297 bprintf("unexpected error , please report this bug"));
301 JNIEXPORT void JNICALL
302 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
305 MSG_error_t rv = MSG_process_sleep((double) seconds);
307 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
308 bprintf("while process was waiting for %f seconds",
314 /***************************************************************************************
315 * The MSG host connected functions implementation. *
316 ***************************************************************************************/
318 JNIEXPORT jobject JNICALL
319 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
322 m_host_t host; /* native host */
323 jobject jhost; /* global reference to the java host instance returned */
325 /* get the C string from the java string */
326 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
327 XBT_DEBUG("Looking for host '%s'",name);
328 /* get the host by name (the hosts are created during the grid resolution) */
329 host = MSG_get_host_by_name(name);
330 XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
332 if (!host) { /* invalid name */
333 jxbt_throw_host_not_found(env, name);
334 (*env)->ReleaseStringUTFChars(env, jname, name);
337 (*env)->ReleaseStringUTFChars(env, jname, name);
339 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
341 /* instanciate a new java host */
342 jhost = jhost_new_instance(env);
345 jxbt_throw_jni(env, "java host instantiation failed");
349 /* get a global reference to the newly created host */
350 jhost = jhost_ref(env, jhost);
353 jxbt_throw_jni(env, "new global ref allocation failed");
357 /* bind the java host and the native host */
358 jhost_bind(jhost, host, env);
360 /* the native host data field is set with the global reference to the
361 * java host returned by this function
363 MSG_host_set_data(host, (void *) jhost);
366 /* return the global reference to the java host instance */
367 return (jobject) MSG_host_get_data(host);
370 JNIEXPORT jstring JNICALL
371 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
374 m_host_t host = jhost_get_native(env, jhost);
377 jxbt_throw_notbound(env, "host", jhost);
381 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
384 JNIEXPORT jint JNICALL
385 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
387 return (jint) MSG_get_host_number();
390 JNIEXPORT jobject JNICALL
391 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
395 m_host_t host = MSG_host_self();
397 if (!MSG_host_get_data(host)) {
398 /* the native host not yet associated with the java host instance */
400 /* instanciate a new java host instance */
401 jhost = jhost_new_instance(env);
404 jxbt_throw_jni(env, "java host instantiation failed");
408 /* get a global reference to the newly created host */
409 jhost = jhost_ref(env, jhost);
412 jxbt_throw_jni(env, "global ref allocation failed");
416 /* Bind & store it */
417 jhost_bind(jhost, host, env);
418 MSG_host_set_data(host, (void *) jhost);
420 jhost = (jobject) MSG_host_get_data(host);
426 JNIEXPORT jdouble JNICALL
427 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
430 m_host_t host = jhost_get_native(env, jhost);
433 jxbt_throw_notbound(env, "host", jhost);
437 return (jdouble) MSG_get_host_speed(host);
440 JNIEXPORT jint JNICALL
441 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
444 m_host_t host = jhost_get_native(env, jhost);
447 jxbt_throw_notbound(env, "host", jhost);
451 return (jint) MSG_get_host_msgload(host);
455 JNIEXPORT jboolean JNICALL
456 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
459 m_host_t host = jhost_get_native(env, jhost);
462 jxbt_throw_notbound(env, "host", jhost);
466 return (jboolean) MSG_host_is_avail(host);
470 /***************************************************************************************
471 * The MSG task connected functions implementation. *
472 ***************************************************************************************/
474 JNIEXPORT void JNICALL
475 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
476 jobject jtask, jstring jname,
477 jdouble jcomputeDuration,
478 jdouble jmessageSize)
480 m_task_t task; /* the native task to create */
481 const char *name = NULL; /* the name of the task */
483 if (jcomputeDuration < 0) {
484 jxbt_throw_illegal(env,
486 ("Task ComputeDuration (%f) cannot be negative",
487 (double) jcomputeDuration));
491 if (jmessageSize < 0) {
492 jxbt_throw_illegal(env,
493 bprintf("Task MessageSize (%f) cannot be negative",
494 (double) jmessageSize));
499 /* get the C string from the java string */
500 name = (*env)->GetStringUTFChars(env, jname, 0);
504 /* create the task */
506 MSG_task_create(name, (double) jcomputeDuration,
507 (double) jmessageSize, NULL);
510 (*env)->ReleaseStringUTFChars(env, jname, name);
512 /* bind & store the task */
513 jtask_bind(jtask, task, env);
514 MSG_task_set_data(task, jtask);
517 JNIEXPORT void JNICALL
518 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
523 jcomputeDurations_arg,
528 m_task_t task; /* the native parallel task to create */
529 const char *name; /* the name of the task */
532 double *computeDurations;
533 double *messageSizes;
534 jdouble *jcomputeDurations;
535 jdouble *jmessageSizes;
541 if (!jcomputeDurations_arg) {
544 ("Parallel task compute durations cannot be null"));
548 if (!jmessageSizes_arg) {
551 ("Parallel task message sizes cannot be null"));
556 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
560 host_count = (int) (*env)->GetArrayLength(env, jhosts);
563 hosts = xbt_new0(m_host_t, host_count);
564 computeDurations = xbt_new0(double, host_count);
565 messageSizes = xbt_new0(double, host_count * host_count);
568 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
570 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
572 for (index = 0; index < host_count; index++) {
573 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
574 hosts[index] = jhost_get_native(env, jhost);
575 computeDurations[index] = jcomputeDurations[index];
577 for (index = 0; index < host_count * host_count; index++) {
578 messageSizes[index] = jmessageSizes[index];
581 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
582 jcomputeDurations, 0);
583 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
587 /* get the C string from the java string */
588 name = (*env)->GetStringUTFChars(env, jname, 0);
591 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
594 (*env)->ReleaseStringUTFChars(env, jname, name);
596 /* associate the java task object and the native task */
597 jtask_bind(jtask, task, env);
599 MSG_task_set_data(task, (void *) jtask);
601 if (!MSG_task_get_data(task))
602 jxbt_throw_jni(env, "global ref allocation failed");
605 JNIEXPORT jobject JNICALL
606 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
611 m_task_t task = jtask_to_native_task(jtask, env);
614 jxbt_throw_notbound(env, "task", jtask);
618 process = MSG_task_get_sender(task);
619 return (jobject) native_to_java_process(process);
622 JNIEXPORT jobject JNICALL
623 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
627 m_task_t task = jtask_to_native_task(jtask, env);
630 jxbt_throw_notbound(env, "task", jtask);
634 host = MSG_task_get_source(task);
636 if (!MSG_host_get_data(host)) {
637 jxbt_throw_jni(env, "MSG_task_get_source() failed");
641 return (jobject) MSG_host_get_data(host);
645 JNIEXPORT jstring JNICALL
646 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
649 m_task_t task = jtask_to_native_task(jtask, env);
652 jxbt_throw_notbound(env, "task", jtask);
656 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
659 JNIEXPORT void JNICALL
660 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
663 m_task_t ptask = jtask_to_native_task(jtask, env);
666 jxbt_throw_notbound(env, "task", jtask);
670 MSG_error_t rv = MSG_task_cancel(ptask);
672 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
673 bprintf("unexpected error , please report this bug"));
676 JNIEXPORT jdouble JNICALL
677 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
680 m_task_t ptask = jtask_to_native_task(jtask, env);
683 jxbt_throw_notbound(env, "task", jtask);
686 return (jdouble) MSG_task_get_compute_duration(ptask);
689 JNIEXPORT jdouble JNICALL
690 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
694 m_task_t ptask = jtask_to_native_task(jtask, env);
697 jxbt_throw_notbound(env, "task", jtask);
700 return (jdouble) MSG_task_get_remaining_computation(ptask);
703 JNIEXPORT void JNICALL
704 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
705 jobject jtask, jdouble priority)
707 m_task_t task = jtask_to_native_task(jtask, env);
710 jxbt_throw_notbound(env, "task", jtask);
713 MSG_task_set_priority(task, (double) priority);
716 JNIEXPORT void JNICALL
717 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
721 /* get the native task */
722 m_task_t task = jtask_to_native_task(jtask_arg, env);
726 jxbt_throw_notbound(env, "task", task);
729 jtask = (jobject) MSG_task_get_data(task);
731 MSG_error_t rv = MSG_task_destroy(task);
733 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
734 bprintf("unexpected error , please report this bug"));
737 JNIEXPORT void JNICALL
738 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
741 m_task_t task = jtask_to_native_task(jtask, env);
744 jxbt_throw_notbound(env, "task", jtask);
748 MSG_error_t rv = MSG_task_execute(task);
750 jxbt_check_res("MSG_task_execute()", rv,
751 MSG_HOST_FAILURE | MSG_TASK_CANCELLED,
752 bprintf("while executing task %s",
753 MSG_task_get_name(task)));
756 /***************************************************************************************
757 * Unsortable functions *
758 ***************************************************************************************/
761 JNIEXPORT jint JNICALL
762 Java_org_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
764 return (jint) MSG_get_errno();
767 JNIEXPORT jdouble JNICALL
768 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
770 return (jdouble) MSG_get_clock();
773 JNIEXPORT void JNICALL
774 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
782 smx_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 //argv[index] = strdup(tmp);
796 (*env)->ReleaseStringUTFChars(env, jval, tmp);
799 MSG_global_init(&argc, argv);
801 for (index = 0; index < argc; index++)
806 (*env)->GetJavaVM(env, &__java_vm);
809 JNIEXPORT void JNICALL
810 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
813 int index; //xbt_fifo_item_t item = NULL;
818 XBT_INFO("Ready to run MSG_MAIN");
820 XBT_INFO("Done running MSG_MAIN");
821 jxbt_check_res("MSG_main()", rv, MSG_OK,
823 ("unexpected error : MSG_main() failed .. please report this bug "));
825 XBT_INFO("MSG_main finished");
827 XBT_INFO("Clean java world");
828 /* Cleanup java hosts */
829 hosts = MSG_get_host_table();
830 for (index = 0; index < MSG_get_host_number() - 1; index++) {
831 jhost = (jobject) hosts[index]->data;
833 jhost_unref(env, jhost);
837 XBT_INFO("Clean native world");
838 /* cleanup native stuff */
839 rv = MSG_OK != MSG_clean();
840 jxbt_check_res("MSG_clean()", rv, MSG_OK,
842 ("unexpected error : MSG_clean() failed .. please report this bug "));
845 JNIEXPORT jint JNICALL
846 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
849 return (jint) MSG_process_killall((int) jresetPID);
852 JNIEXPORT void JNICALL
853 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
854 jstring jplatformFile)
857 const char *platformFile =
858 (*env)->GetStringUTFChars(env, jplatformFile, 0);
860 MSG_create_environment(platformFile);
862 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
865 JNIEXPORT void JNICALL
866 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
870 m_process_t process = jprocess_to_native_process(jprocess, env);
873 jxbt_throw_notbound(env, "process", jprocess);
877 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
880 JNIEXPORT void JNICALL
881 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
883 const char *s = (*env)->GetStringUTFChars(env, js, 0);
885 (*env)->ReleaseStringUTFChars(env, js, s);
888 JNIEXPORT jobjectArray JNICALL
889 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
897 int count = MSG_get_host_number();
898 m_host_t *table = MSG_get_host_table();
900 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
906 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
909 jxbt_throw_jni(env, "Hosts table allocation failed");
913 for (index = 0; index < count; index++) {
915 jhost = (jobject) (MSG_host_get_data(host));
918 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
921 Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
922 /* FIXME: leak of jname ? */
925 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
931 JNIEXPORT void JNICALL
932 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
933 jstring jalias, jobject jtask,
938 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
940 m_task_t task = jtask_to_native_task(jtask, env);
944 (*env)->ReleaseStringUTFChars(env, jalias, alias);
945 jxbt_throw_notbound(env, "task", jtask);
949 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
950 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
951 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
953 (*env)->ReleaseStringUTFChars(env, jalias, alias);
955 jxbt_check_res("MSG_task_send_with_timeout()", rv,
956 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
957 bprintf("while sending task %s to mailbox %s",
958 MSG_task_get_name(task), alias));
961 JNIEXPORT void JNICALL
962 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
963 jstring jalias, jobject jtask,
966 m_task_t task = jtask_to_native_task(jtask, env);
971 jxbt_throw_notbound(env, "task", jtask);
975 alias = (*env)->GetStringUTFChars(env, jalias, 0);
977 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
978 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
979 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
981 (*env)->ReleaseStringUTFChars(env, jalias, alias);
983 jxbt_check_res("MSG_task_send_bounded()", rv,
984 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
986 ("while sending task %s to mailbox %s with max rate %f",
987 MSG_task_get_name(task), alias, (double) jmaxRate));
991 JNIEXPORT jobject JNICALL
992 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
993 jstring jalias, jdouble jtimeout,
997 m_task_t task = NULL;
998 m_host_t host = NULL;
999 jobject jtask_global, jtask_local;
1003 host = jhost_get_native(env, jhost);
1006 jxbt_throw_notbound(env, "host", jhost);
1011 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1013 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1014 jtask_global = MSG_task_get_data(task);
1016 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1017 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1018 (*env)->DeleteGlobalRef(env, jtask_global);
1019 MSG_task_set_data(task, NULL);
1021 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1023 jxbt_check_res("MSG_task_receive_ext()", rv,
1024 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1025 bprintf("while receiving from mailbox %s", alias));
1027 return (jobject) jtask_local;
1030 JNIEXPORT jboolean JNICALL
1031 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1038 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1040 rv = MSG_task_listen(alias);
1042 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1044 return (jboolean) rv;
1047 JNIEXPORT jint JNICALL
1048 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1055 m_host_t host = jhost_get_native(env, jhost);
1058 jxbt_throw_notbound(env, "host", jhost);
1061 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1063 rv = MSG_task_listen_from_host(alias, host);
1065 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1070 JNIEXPORT jint JNICALL
1071 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1076 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1078 rv = MSG_task_listen_from(alias);
1080 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1085 JNIEXPORT void JNICALL
1086 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1087 jstring jdeploymentFile)
1090 const char *deploymentFile =
1091 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1093 surf_parse_reset_callbacks();
1095 surfxml_add_callback(STag_surfxml_process_cb_list,
1096 japplication_handler_on_begin_process);
1098 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1099 japplication_handler_on_process_arg);
1101 surfxml_add_callback(STag_surfxml_prop_cb_list,
1102 japplication_handler_on_property);
1104 surfxml_add_callback(ETag_surfxml_process_cb_list,
1105 japplication_handler_on_end_process);
1107 surf_parse_open(deploymentFile);
1109 japplication_handler_on_start_document();
1112 jxbt_throw_jni(env, "surf_parse() failed");
1116 japplication_handler_on_end_document();
1118 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);