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 <simgrid/simix.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 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
30 /* end of eclipse-mandated pimple */
32 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
34 static JavaVM *__java_vm = NULL;
36 static jobject native_to_java_process(m_process_t process);
38 JavaVM *get_java_VM(void)
43 JNIEnv *get_current_thread_env(void)
47 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
52 static jobject native_to_java_process(m_process_t process)
54 return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
58 * The MSG process connected functions implementation.
61 JNIEXPORT void JNICALL
62 Java_org_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
68 jobject jprocess; /* the global reference to the java process instance */
69 jstring jname; /* the name of the java process instance */
70 const char *name; /* the C name of the process */
71 m_process_t process; /* the native process to create */
72 m_host_t host; /* Where that process lives */
74 XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
75 env, cls, jprocess_arg, jhost);
78 /* get the name of the java process */
79 jname = jprocess_get_name(jprocess_arg, env);
82 xbt_strdup("Internal error: Process name cannot be NULL"));
86 /* bind/retrieve the msg host */
87 host = jhost_get_native(env, jhost);
89 if (!(host)) { /* not binded */
90 jxbt_throw_notbound(env, "host", jhost);
94 /* create a global java process instance */
95 jprocess = jprocess_new_global_ref(jprocess_arg, env);
97 jxbt_throw_jni(env, "Can't get a global ref to the java process");
101 /* build the C name of the process */
102 name = (*env)->GetStringUTFChars(env, jname, 0);
103 name = xbt_strdup(name);
105 /* Actually build the MSG process */
106 process = MSG_process_create_with_environment(name,
107 (xbt_main_func_t) jprocess,
111 /*argc, argv, properties*/
114 MSG_process_set_data(process,&process);
116 /* release our reference to the process name (variable name becomes invalid) */
117 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
118 //(*env)->ReleaseStringUTFChars(env, jname, name);
120 /* bind the java process instance to the native process */
121 jprocess_bind(jprocess, process, env);
125 JNIEXPORT void JNICALL
126 Java_org_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
129 m_process_t process = jprocess_to_native_process(jprocess, env);
132 jxbt_throw_notbound(env, "process", jprocess);
136 /* try to suspend the process */
137 MSG_error_t rv = MSG_process_suspend(process);
139 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
140 bprintf("unexpected error , please report this bug"));
144 JNIEXPORT void JNICALL
145 Java_org_simgrid_msg_Process_simulatedSleep(JNIEnv * env, jobject jprocess,
147 m_process_t process = jprocess_to_native_process(jprocess, env);
150 jxbt_throw_notbound(env, "process", jprocess);
153 MSG_error_t rv = MSG_process_sleep((double)jseconds);
155 jxbt_check_res("MSG_process_sleep()", rv, MSG_OK,
156 bprintf("unexpected error , please report this bug"));
160 JNIEXPORT void JNICALL
161 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
164 m_process_t process = jprocess_to_native_process(jprocess, env);
167 jxbt_throw_notbound(env, "process", jprocess);
171 /* try to resume the process */
172 MSG_error_t rv = MSG_process_resume(process);
174 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
175 bprintf("unexpected error , please report this bug"));
178 JNIEXPORT jboolean JNICALL
179 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
182 m_process_t process = jprocess_to_native_process(jprocess, env);
185 jxbt_throw_notbound(env, "process", jprocess);
189 /* true is the process is suspended, false otherwise */
190 return (jboolean) MSG_process_is_suspended(process);
193 JNIEXPORT void JNICALL
194 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
197 /* get the native instances from the java ones */
198 m_process_t process = jprocess_to_native_process(jprocess, env);
201 jxbt_throw_notbound(env, "process", jprocess);
205 /* kill the native process (this wrapper is call by the destructor of the java
208 MSG_process_kill(process);
211 JNIEXPORT jobject JNICALL
212 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
215 /* get the native instances from the java ones */
216 m_process_t process = jprocess_to_native_process(jprocess, env);
220 jxbt_throw_notbound(env, "process", jprocess);
224 host = MSG_process_get_host(process);
226 if (!MSG_host_get_data(host)) {
227 jxbt_throw_jni(env, "MSG_process_get_host() failed");
231 /* return the global reference to the java host instance */
232 return (jobject) MSG_host_get_data(host);
236 JNIEXPORT jobject JNICALL
237 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
240 m_process_t process = MSG_process_from_PID(PID);
243 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
247 if (!native_to_java_process(process)) {
248 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
252 return (jobject) (native_to_java_process(process));
256 JNIEXPORT jint JNICALL
257 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
260 m_process_t process = jprocess_to_native_process(jprocess, env);
263 jxbt_throw_notbound(env, "process", jprocess);
267 return (jint) MSG_process_get_PID(process);
271 JNIEXPORT jint JNICALL
272 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
275 m_process_t process = jprocess_to_native_process(jprocess, env);
278 jxbt_throw_notbound(env, "process", jprocess);
282 return (jint) MSG_process_get_PPID(process);
285 JNIEXPORT jobject JNICALL
286 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
288 m_process_t process = MSG_process_self();
292 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
296 jprocess = native_to_java_process(process);
299 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
304 JNIEXPORT void JNICALL
305 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
306 jobject jprocess, jobject jhost)
308 m_process_t process = jprocess_to_native_process(jprocess, env);
311 jxbt_throw_notbound(env, "process", jprocess);
315 m_host_t host = jhost_get_native(env, jhost);
318 jxbt_throw_notbound(env, "host", jhost);
322 /* try to change the host of the process */
323 MSG_error_t rv = MSG_process_migrate(process, host);
324 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
325 bprintf("unexpected error , please report this bug"));
329 JNIEXPORT void JNICALL
330 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
333 MSG_error_t rv = MSG_process_sleep((double) seconds);
335 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
336 bprintf("while process was waiting for %f seconds",
342 /***************************************************************************************
343 * The MSG host connected functions implementation. *
344 ***************************************************************************************/
346 JNIEXPORT jobject JNICALL
347 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
350 m_host_t host; /* native host */
351 jobject jhost; /* global reference to the java host instance returned */
353 /* get the C string from the java string */
354 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
355 XBT_DEBUG("Looking for host '%s'",name);
356 /* get the host by name (the hosts are created during the grid resolution) */
357 host = MSG_get_host_by_name(name);
358 XBT_DEBUG("MSG gave %p as native host (smx_host=%p)", host,host? host->smx_host:NULL);
360 if (!host) { /* invalid name */
361 jxbt_throw_host_not_found(env, name);
362 (*env)->ReleaseStringUTFChars(env, jname, name);
365 (*env)->ReleaseStringUTFChars(env, jname, name);
367 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
369 /* Instantiate a new java host */
370 jhost = jhost_new_instance(env);
373 jxbt_throw_jni(env, "java host instantiation failed");
377 /* get a global reference to the newly created host */
378 jhost = jhost_ref(env, jhost);
381 jxbt_throw_jni(env, "new global ref allocation failed");
385 /* bind the java host and the native host */
386 jhost_bind(jhost, host, env);
388 /* the native host data field is set with the global reference to the
389 * java host returned by this function
391 MSG_host_set_data(host, (void *) jhost);
394 /* return the global reference to the java host instance */
395 return (jobject) MSG_host_get_data(host);
398 JNIEXPORT jstring JNICALL
399 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
402 m_host_t host = jhost_get_native(env, jhost);
405 jxbt_throw_notbound(env, "host", jhost);
409 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
412 JNIEXPORT jint JNICALL
413 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
415 xbt_dynar_t hosts = MSG_hosts_as_dynar();
416 int nb_host = xbt_dynar_length(hosts);
417 xbt_dynar_free(&hosts);
418 return (jint) nb_host;
421 JNIEXPORT jobject JNICALL
422 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
426 m_host_t host = MSG_host_self();
428 if (!MSG_host_get_data(host)) {
429 /* the native host not yet associated with the java host instance */
431 /* instanciate a new java host instance */
432 jhost = jhost_new_instance(env);
435 jxbt_throw_jni(env, "java host instantiation failed");
439 /* get a global reference to the newly created host */
440 jhost = jhost_ref(env, jhost);
443 jxbt_throw_jni(env, "global ref allocation failed");
447 /* Bind & store it */
448 jhost_bind(jhost, host, env);
449 MSG_host_set_data(host, (void *) jhost);
451 jhost = (jobject) MSG_host_get_data(host);
457 JNIEXPORT jdouble JNICALL
458 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
461 m_host_t host = jhost_get_native(env, jhost);
464 jxbt_throw_notbound(env, "host", jhost);
468 return (jdouble) MSG_get_host_speed(host);
471 JNIEXPORT jint JNICALL
472 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
475 m_host_t host = jhost_get_native(env, jhost);
478 jxbt_throw_notbound(env, "host", jhost);
482 return (jint) MSG_get_host_msgload(host);
486 JNIEXPORT jboolean JNICALL
487 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
490 m_host_t host = jhost_get_native(env, jhost);
493 jxbt_throw_notbound(env, "host", jhost);
497 return (jboolean) MSG_host_is_avail(host);
501 /***************************************************************************************
502 * The MSG task connected functions implementation. *
503 ***************************************************************************************/
505 JNIEXPORT void JNICALL
506 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
507 jobject jtask, jstring jname,
508 jdouble jcomputeDuration,
509 jdouble jmessageSize)
511 m_task_t task; /* the native task to create */
512 const char *name = NULL; /* the name of the task */
514 if (jcomputeDuration < 0) {
515 jxbt_throw_illegal(env,
517 ("Task ComputeDuration (%f) cannot be negative",
518 (double) jcomputeDuration));
522 if (jmessageSize < 0) {
523 jxbt_throw_illegal(env,
524 bprintf("Task MessageSize (%f) cannot be negative",
525 (double) jmessageSize));
530 /* get the C string from the java string */
531 name = (*env)->GetStringUTFChars(env, jname, 0);
535 /* create the task */
537 MSG_task_create(name, (double) jcomputeDuration,
538 (double) jmessageSize, NULL);
541 (*env)->ReleaseStringUTFChars(env, jname, name);
543 /* bind & store the task */
544 jtask_bind(jtask, task, env);
545 MSG_task_set_data(task, jtask);
548 JNIEXPORT void JNICALL
549 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
554 jcomputeDurations_arg,
559 m_task_t task; /* the native parallel task to create */
560 const char *name; /* the name of the task */
563 double *computeDurations;
564 double *messageSizes;
565 jdouble *jcomputeDurations;
566 jdouble *jmessageSizes;
572 if (!jcomputeDurations_arg) {
575 ("Parallel task compute durations cannot be null"));
579 if (!jmessageSizes_arg) {
582 ("Parallel task message sizes cannot be null"));
587 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
591 host_count = (int) (*env)->GetArrayLength(env, jhosts);
594 hosts = xbt_new0(m_host_t, host_count);
595 computeDurations = xbt_new0(double, host_count);
596 messageSizes = xbt_new0(double, host_count * host_count);
599 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
601 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
603 for (index = 0; index < host_count; index++) {
604 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
605 hosts[index] = jhost_get_native(env, jhost);
606 computeDurations[index] = jcomputeDurations[index];
608 for (index = 0; index < host_count * host_count; index++) {
609 messageSizes[index] = jmessageSizes[index];
612 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
613 jcomputeDurations, 0);
614 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
618 /* get the C string from the java string */
619 name = (*env)->GetStringUTFChars(env, jname, 0);
622 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
625 (*env)->ReleaseStringUTFChars(env, jname, name);
627 /* associate the java task object and the native task */
628 jtask_bind(jtask, task, env);
630 MSG_task_set_data(task, (void *) jtask);
632 if (!MSG_task_get_data(task))
633 jxbt_throw_jni(env, "global ref allocation failed");
636 JNIEXPORT jobject JNICALL
637 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
642 m_task_t task = jtask_to_native_task(jtask, env);
645 jxbt_throw_notbound(env, "task", jtask);
649 process = MSG_task_get_sender(task);
650 return (jobject) native_to_java_process(process);
653 JNIEXPORT jobject JNICALL
654 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
658 m_task_t task = jtask_to_native_task(jtask, env);
661 jxbt_throw_notbound(env, "task", jtask);
665 host = MSG_task_get_source(task);
667 if (!MSG_host_get_data(host)) {
668 jxbt_throw_jni(env, "MSG_task_get_source() failed");
672 return (jobject) MSG_host_get_data(host);
676 JNIEXPORT jstring JNICALL
677 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
680 m_task_t task = jtask_to_native_task(jtask, env);
683 jxbt_throw_notbound(env, "task", jtask);
687 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
690 JNIEXPORT void JNICALL
691 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
694 m_task_t ptask = jtask_to_native_task(jtask, env);
697 jxbt_throw_notbound(env, "task", jtask);
701 MSG_error_t rv = MSG_task_cancel(ptask);
703 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
704 bprintf("unexpected error , please report this bug"));
707 JNIEXPORT jdouble JNICALL
708 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
711 m_task_t ptask = jtask_to_native_task(jtask, env);
714 jxbt_throw_notbound(env, "task", jtask);
717 return (jdouble) MSG_task_get_compute_duration(ptask);
720 JNIEXPORT jdouble JNICALL
721 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
725 m_task_t ptask = jtask_to_native_task(jtask, env);
728 jxbt_throw_notbound(env, "task", jtask);
731 return (jdouble) MSG_task_get_remaining_computation(ptask);
734 JNIEXPORT void JNICALL
735 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
736 jobject jtask, jdouble priority)
738 m_task_t task = jtask_to_native_task(jtask, env);
741 jxbt_throw_notbound(env, "task", jtask);
744 MSG_task_set_priority(task, (double) priority);
747 JNIEXPORT void JNICALL
748 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
752 /* get the native task */
753 m_task_t task = jtask_to_native_task(jtask_arg, env);
756 jxbt_throw_notbound(env, "task", task);
760 MSG_error_t rv = MSG_task_destroy(task);
762 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
763 bprintf("unexpected error , please report this bug"));
766 JNIEXPORT void JNICALL
767 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
770 m_task_t task = jtask_to_native_task(jtask, env);
773 jxbt_throw_notbound(env, "task", jtask);
777 MSG_error_t rv = MSG_task_execute(task);
779 jxbt_check_res("MSG_task_execute()", rv,
780 MSG_HOST_FAILURE | MSG_TASK_CANCELED,
781 bprintf("while executing task %s",
782 MSG_task_get_name(task)));
785 /***************************************************************************************
786 * Unsortable functions *
787 ***************************************************************************************/
789 JNIEXPORT jdouble JNICALL
790 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
792 return (jdouble) MSG_get_clock();
795 JNIEXPORT void JNICALL
796 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
804 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
807 argc = (int) (*env)->GetArrayLength(env, jargs);
810 argv = xbt_new(char *, argc + 1);
811 argv[0] = strdup("java");
813 for (index = 0; index < argc - 1; index++) {
814 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
815 tmp = (*env)->GetStringUTFChars(env, jval, 0);
816 argv[index + 1] = strdup(tmp);
817 (*env)->ReleaseStringUTFChars(env, jval, tmp);
821 MSG_global_init(&argc, argv);
823 for (index = 0; index < argc; index++)
828 (*env)->GetJavaVM(env, &__java_vm);
831 JNIEXPORT void JNICALL
832 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
840 XBT_INFO("Ready to run MSG_MAIN");
842 XBT_INFO("Done running MSG_MAIN");
843 jxbt_check_res("MSG_main()", rv, MSG_OK,
845 ("unexpected error : MSG_main() failed .. please report this bug "));
847 XBT_INFO("MSG_main finished");
849 XBT_INFO("Clean java world");
850 /* Cleanup java hosts */
851 hosts = MSG_hosts_as_dynar();
852 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
853 jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
855 jhost_unref(env, jhost);
858 xbt_dynar_free(&hosts);
859 XBT_INFO("Clean native world");
861 JNIEXPORT void JNICALL
862 JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
864 /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
865 MSG_error_t rv = MSG_OK != MSG_clean();
866 jxbt_check_res("MSG_clean()", rv, MSG_OK,
868 ("unexpected error : MSG_clean() failed .. please report this bug "));
871 JNIEXPORT jint JNICALL
872 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
875 return (jint) MSG_process_killall((int) jresetPID);
878 JNIEXPORT void JNICALL
879 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
880 jstring jplatformFile)
883 const char *platformFile =
884 (*env)->GetStringUTFChars(env, jplatformFile, 0);
886 MSG_create_environment(platformFile);
888 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
891 JNIEXPORT void JNICALL
892 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
896 m_process_t process = jprocess_to_native_process(jprocess, env);
899 jxbt_throw_notbound(env, "process", jprocess);
903 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
906 JNIEXPORT void JNICALL
907 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
909 const char *s = (*env)->GetStringUTFChars(env, js, 0);
911 (*env)->ReleaseStringUTFChars(env, js, s);
914 JNIEXPORT jobjectArray JNICALL
915 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
923 xbt_dynar_t table = MSG_hosts_as_dynar();
924 int count = xbt_dynar_length(table);
926 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
932 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
935 jxbt_throw_jni(env, "Hosts table allocation failed");
939 for (index = 0; index < count; index++) {
940 host = xbt_dynar_get_as(table,index,m_host_t);
941 jhost = (jobject) (MSG_host_get_data(host));
944 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
947 Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
948 /* FIXME: leak of jname ? */
951 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
953 xbt_dynar_free(&table);
957 JNIEXPORT void JNICALL
958 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
959 jstring jalias, jobject jtask,
964 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
966 m_task_t task = jtask_to_native_task(jtask, env);
970 (*env)->ReleaseStringUTFChars(env, jalias, alias);
971 jxbt_throw_notbound(env, "task", jtask);
975 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
976 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
977 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
979 (*env)->ReleaseStringUTFChars(env, jalias, alias);
981 jxbt_check_res("MSG_task_send_with_timeout()", rv,
982 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
983 bprintf("while sending task %s to mailbox %s",
984 MSG_task_get_name(task), alias));
987 static void msg_task_cancel_on_failed_dsend(void*t) {
989 JNIEnv *env =get_current_thread_env();
990 jobject jtask_global = MSG_task_get_data(task);
992 /* Destroy the global ref so that the JVM can free the stuff */
993 (*env)->DeleteGlobalRef(env, jtask_global);
994 MSG_task_set_data(task, NULL);
995 MSG_task_destroy(task);
998 JNIEXPORT void JNICALL
999 Java_org_simgrid_msg_MsgNative_taskDSend(JNIEnv * env, jclass cls,
1000 jstring jalias, jobject jtask)
1003 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1005 m_task_t task = jtask_to_native_task(jtask, env);
1009 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1010 jxbt_throw_notbound(env, "task", jtask);
1014 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1015 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1016 MSG_task_dsend(task, alias, msg_task_cancel_on_failed_dsend);
1018 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1022 JNIEXPORT void JNICALL
1023 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1024 jstring jalias, jobject jtask,
1027 m_task_t task = jtask_to_native_task(jtask, env);
1032 jxbt_throw_notbound(env, "task", jtask);
1036 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1038 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1039 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1040 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1042 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1044 jxbt_check_res("MSG_task_send_bounded()", rv,
1045 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1047 ("while sending task %s to mailbox %s with max rate %f",
1048 MSG_task_get_name(task), alias, (double) jmaxRate));
1052 JNIEXPORT jobject JNICALL
1053 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1054 jstring jalias, jdouble jtimeout,
1058 m_task_t task = NULL;
1059 m_host_t host = NULL;
1060 jobject jtask_global, jtask_local;
1064 host = jhost_get_native(env, jhost);
1067 jxbt_throw_notbound(env, "host", jhost);
1072 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1074 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1078 jxbt_throw_time_out_failure(env,NULL);
1080 case MSG_TRANSFER_FAILURE:
1081 jxbt_throw_transfer_failure(env,NULL);
1083 case MSG_HOST_FAILURE:
1084 jxbt_throw_host_failure(env,NULL);
1087 jxbt_throw_native(env,bprintf("receive failed"));
1091 jtask_global = MSG_task_get_data(task);
1093 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1094 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1095 (*env)->DeleteGlobalRef(env, jtask_global);
1096 MSG_task_set_data(task, NULL);
1098 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1100 jxbt_check_res("MSG_task_receive_ext()", rv,
1101 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1102 bprintf("while receiving from mailbox %s", alias));
1104 return (jobject) jtask_local;
1107 JNIEXPORT jboolean JNICALL
1108 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1115 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1117 rv = MSG_task_listen(alias);
1119 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1121 return (jboolean) rv;
1124 JNIEXPORT jint JNICALL
1125 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1132 m_host_t host = jhost_get_native(env, jhost);
1135 jxbt_throw_notbound(env, "host", jhost);
1138 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1140 rv = MSG_task_listen_from_host(alias, host);
1142 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1147 JNIEXPORT jint JNICALL
1148 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1153 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1155 rv = MSG_task_listen_from(alias);
1157 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1162 JNIEXPORT void JNICALL
1163 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1164 jstring jdeploymentFile)
1167 const char *deploymentFile =
1168 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1170 surf_parse_reset_callbacks();
1172 surfxml_add_callback(STag_surfxml_process_cb_list,
1173 japplication_handler_on_begin_process);
1175 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1176 japplication_handler_on_process_arg);
1178 surfxml_add_callback(STag_surfxml_prop_cb_list,
1179 japplication_handler_on_property);
1181 surfxml_add_callback(ETag_surfxml_process_cb_list,
1182 japplication_handler_on_end_process);
1184 surf_parse_open(deploymentFile);
1186 japplication_handler_on_start_document();
1189 jxbt_throw_jni(env, "surf_parse() failed");
1193 japplication_handler_on_end_document();
1195 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);