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 /* 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,
110 /*argc, argv, properties*/
113 MSG_process_set_data(process,&process);
115 /* release our reference to the process name (variable name becomes invalid) */
116 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
117 //(*env)->ReleaseStringUTFChars(env, jname, name);
119 /* bind the java process instance to the native process */
120 jprocess_bind(jprocess, process, env);
124 JNIEXPORT void JNICALL
125 Java_org_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
128 m_process_t process = jprocess_to_native_process(jprocess, env);
131 jxbt_throw_notbound(env, "process", jprocess);
135 /* try to suspend the process */
136 MSG_error_t rv = MSG_process_suspend(process);
138 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
139 bprintf("unexpected error , please report this bug"));
143 JNIEXPORT void JNICALL
144 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
147 m_process_t process = jprocess_to_native_process(jprocess, env);
150 jxbt_throw_notbound(env, "process", jprocess);
154 /* try to resume the process */
155 MSG_error_t rv = MSG_process_resume(process);
157 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
158 bprintf("unexpected error , please report this bug"));
161 JNIEXPORT jboolean JNICALL
162 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
165 m_process_t process = jprocess_to_native_process(jprocess, env);
168 jxbt_throw_notbound(env, "process", jprocess);
172 /* true is the process is suspended, false otherwise */
173 return (jboolean) MSG_process_is_suspended(process);
176 JNIEXPORT void JNICALL
177 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
180 /* get the native instances from the java ones */
181 m_process_t process = jprocess_to_native_process(jprocess, env);
184 jxbt_throw_notbound(env, "process", jprocess);
188 /* kill the native process (this wrapper is call by the destructor of the java
191 MSG_process_kill(process);
194 JNIEXPORT jobject JNICALL
195 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
198 /* get the native instances from the java ones */
199 m_process_t process = jprocess_to_native_process(jprocess, env);
203 jxbt_throw_notbound(env, "process", jprocess);
207 host = MSG_process_get_host(process);
209 if (!MSG_host_get_data(host)) {
210 jxbt_throw_jni(env, "MSG_process_get_host() failed");
214 /* return the global reference to the java host instance */
215 return (jobject) MSG_host_get_data(host);
219 JNIEXPORT jobject JNICALL
220 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
223 m_process_t process = MSG_process_from_PID(PID);
226 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
230 if (!native_to_java_process(process)) {
231 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
235 return (jobject) (native_to_java_process(process));
239 JNIEXPORT jint JNICALL
240 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
243 m_process_t process = jprocess_to_native_process(jprocess, env);
246 jxbt_throw_notbound(env, "process", jprocess);
250 return (jint) MSG_process_get_PID(process);
254 JNIEXPORT jint JNICALL
255 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
258 m_process_t process = jprocess_to_native_process(jprocess, env);
261 jxbt_throw_notbound(env, "process", jprocess);
265 return (jint) MSG_process_get_PPID(process);
268 JNIEXPORT jobject JNICALL
269 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
271 m_process_t process = MSG_process_self();
275 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
279 jprocess = native_to_java_process(process);
282 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
287 JNIEXPORT void JNICALL
288 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
289 jobject jprocess, jobject jhost)
291 m_process_t process = jprocess_to_native_process(jprocess, env);
294 jxbt_throw_notbound(env, "process", jprocess);
298 m_host_t host = jhost_get_native(env, jhost);
301 jxbt_throw_notbound(env, "host", jhost);
305 /* try to change the host of the process */
306 MSG_error_t rv = MSG_process_migrate(process, host);
307 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
308 bprintf("unexpected error , please report this bug"));
312 JNIEXPORT void JNICALL
313 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
316 MSG_error_t rv = MSG_process_sleep((double) seconds);
318 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
319 bprintf("while process was waiting for %f seconds",
325 /***************************************************************************************
326 * The MSG host connected functions implementation. *
327 ***************************************************************************************/
329 JNIEXPORT jobject JNICALL
330 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
333 m_host_t host; /* native host */
334 jobject jhost; /* global reference to the java host instance returned */
336 /* get the C string from the java string */
337 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
338 XBT_DEBUG("Looking for host '%s'",name);
339 /* get the host by name (the hosts are created during the grid resolution) */
340 host = MSG_get_host_by_name(name);
341 XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
343 if (!host) { /* invalid name */
344 jxbt_throw_host_not_found(env, name);
345 (*env)->ReleaseStringUTFChars(env, jname, name);
348 (*env)->ReleaseStringUTFChars(env, jname, name);
350 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
352 /* Instantiate a new java host */
353 jhost = jhost_new_instance(env);
356 jxbt_throw_jni(env, "java host instantiation failed");
360 /* get a global reference to the newly created host */
361 jhost = jhost_ref(env, jhost);
364 jxbt_throw_jni(env, "new global ref allocation failed");
368 /* bind the java host and the native host */
369 jhost_bind(jhost, host, env);
371 /* the native host data field is set with the global reference to the
372 * java host returned by this function
374 MSG_host_set_data(host, (void *) jhost);
377 /* return the global reference to the java host instance */
378 return (jobject) MSG_host_get_data(host);
381 JNIEXPORT jstring JNICALL
382 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
385 m_host_t host = jhost_get_native(env, jhost);
388 jxbt_throw_notbound(env, "host", jhost);
392 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
395 JNIEXPORT jint JNICALL
396 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
398 xbt_dynar_t hosts = MSG_hosts_as_dynar();
399 int nb_host = xbt_dynar_length(hosts);
400 xbt_dynar_free(&hosts);
401 return (jint) nb_host;
404 JNIEXPORT jobject JNICALL
405 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
409 m_host_t host = MSG_host_self();
411 if (!MSG_host_get_data(host)) {
412 /* the native host not yet associated with the java host instance */
414 /* instanciate a new java host instance */
415 jhost = jhost_new_instance(env);
418 jxbt_throw_jni(env, "java host instantiation failed");
422 /* get a global reference to the newly created host */
423 jhost = jhost_ref(env, jhost);
426 jxbt_throw_jni(env, "global ref allocation failed");
430 /* Bind & store it */
431 jhost_bind(jhost, host, env);
432 MSG_host_set_data(host, (void *) jhost);
434 jhost = (jobject) MSG_host_get_data(host);
440 JNIEXPORT jdouble JNICALL
441 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
444 m_host_t host = jhost_get_native(env, jhost);
447 jxbt_throw_notbound(env, "host", jhost);
451 return (jdouble) MSG_get_host_speed(host);
454 JNIEXPORT jint JNICALL
455 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
458 m_host_t host = jhost_get_native(env, jhost);
461 jxbt_throw_notbound(env, "host", jhost);
465 return (jint) MSG_get_host_msgload(host);
469 JNIEXPORT jboolean JNICALL
470 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
473 m_host_t host = jhost_get_native(env, jhost);
476 jxbt_throw_notbound(env, "host", jhost);
480 return (jboolean) MSG_host_is_avail(host);
484 /***************************************************************************************
485 * The MSG task connected functions implementation. *
486 ***************************************************************************************/
488 JNIEXPORT void JNICALL
489 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
490 jobject jtask, jstring jname,
491 jdouble jcomputeDuration,
492 jdouble jmessageSize)
494 m_task_t task; /* the native task to create */
495 const char *name = NULL; /* the name of the task */
497 if (jcomputeDuration < 0) {
498 jxbt_throw_illegal(env,
500 ("Task ComputeDuration (%f) cannot be negative",
501 (double) jcomputeDuration));
505 if (jmessageSize < 0) {
506 jxbt_throw_illegal(env,
507 bprintf("Task MessageSize (%f) cannot be negative",
508 (double) jmessageSize));
513 /* get the C string from the java string */
514 name = (*env)->GetStringUTFChars(env, jname, 0);
518 /* create the task */
520 MSG_task_create(name, (double) jcomputeDuration,
521 (double) jmessageSize, NULL);
524 (*env)->ReleaseStringUTFChars(env, jname, name);
526 /* bind & store the task */
527 jtask_bind(jtask, task, env);
528 MSG_task_set_data(task, jtask);
531 JNIEXPORT void JNICALL
532 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
537 jcomputeDurations_arg,
542 m_task_t task; /* the native parallel task to create */
543 const char *name; /* the name of the task */
546 double *computeDurations;
547 double *messageSizes;
548 jdouble *jcomputeDurations;
549 jdouble *jmessageSizes;
555 if (!jcomputeDurations_arg) {
558 ("Parallel task compute durations cannot be null"));
562 if (!jmessageSizes_arg) {
565 ("Parallel task message sizes cannot be null"));
570 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
574 host_count = (int) (*env)->GetArrayLength(env, jhosts);
577 hosts = xbt_new0(m_host_t, host_count);
578 computeDurations = xbt_new0(double, host_count);
579 messageSizes = xbt_new0(double, host_count * host_count);
582 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
584 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
586 for (index = 0; index < host_count; index++) {
587 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
588 hosts[index] = jhost_get_native(env, jhost);
589 computeDurations[index] = jcomputeDurations[index];
591 for (index = 0; index < host_count * host_count; index++) {
592 messageSizes[index] = jmessageSizes[index];
595 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
596 jcomputeDurations, 0);
597 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
601 /* get the C string from the java string */
602 name = (*env)->GetStringUTFChars(env, jname, 0);
605 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
608 (*env)->ReleaseStringUTFChars(env, jname, name);
610 /* associate the java task object and the native task */
611 jtask_bind(jtask, task, env);
613 MSG_task_set_data(task, (void *) jtask);
615 if (!MSG_task_get_data(task))
616 jxbt_throw_jni(env, "global ref allocation failed");
619 JNIEXPORT jobject JNICALL
620 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
625 m_task_t task = jtask_to_native_task(jtask, env);
628 jxbt_throw_notbound(env, "task", jtask);
632 process = MSG_task_get_sender(task);
633 return (jobject) native_to_java_process(process);
636 JNIEXPORT jobject JNICALL
637 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
641 m_task_t task = jtask_to_native_task(jtask, env);
644 jxbt_throw_notbound(env, "task", jtask);
648 host = MSG_task_get_source(task);
650 if (!MSG_host_get_data(host)) {
651 jxbt_throw_jni(env, "MSG_task_get_source() failed");
655 return (jobject) MSG_host_get_data(host);
659 JNIEXPORT jstring JNICALL
660 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
663 m_task_t task = jtask_to_native_task(jtask, env);
666 jxbt_throw_notbound(env, "task", jtask);
670 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
673 JNIEXPORT void JNICALL
674 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
677 m_task_t ptask = jtask_to_native_task(jtask, env);
680 jxbt_throw_notbound(env, "task", jtask);
684 MSG_error_t rv = MSG_task_cancel(ptask);
686 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
687 bprintf("unexpected error , please report this bug"));
690 JNIEXPORT jdouble JNICALL
691 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
694 m_task_t ptask = jtask_to_native_task(jtask, env);
697 jxbt_throw_notbound(env, "task", jtask);
700 return (jdouble) MSG_task_get_compute_duration(ptask);
703 JNIEXPORT jdouble JNICALL
704 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
708 m_task_t ptask = jtask_to_native_task(jtask, env);
711 jxbt_throw_notbound(env, "task", jtask);
714 return (jdouble) MSG_task_get_remaining_computation(ptask);
717 JNIEXPORT void JNICALL
718 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
719 jobject jtask, jdouble priority)
721 m_task_t task = jtask_to_native_task(jtask, env);
724 jxbt_throw_notbound(env, "task", jtask);
727 MSG_task_set_priority(task, (double) priority);
730 JNIEXPORT void JNICALL
731 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
735 /* get the native task */
736 m_task_t task = jtask_to_native_task(jtask_arg, env);
739 jxbt_throw_notbound(env, "task", task);
743 MSG_error_t rv = MSG_task_destroy(task);
745 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
746 bprintf("unexpected error , please report this bug"));
749 JNIEXPORT void JNICALL
750 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
753 m_task_t task = jtask_to_native_task(jtask, env);
756 jxbt_throw_notbound(env, "task", jtask);
760 MSG_error_t rv = MSG_task_execute(task);
762 jxbt_check_res("MSG_task_execute()", rv,
763 MSG_HOST_FAILURE | MSG_TASK_CANCELED,
764 bprintf("while executing task %s",
765 MSG_task_get_name(task)));
768 /***************************************************************************************
769 * Unsortable functions *
770 ***************************************************************************************/
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_new(char *, argc + 1);
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 (*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)
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_hosts_as_dynar();
835 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
836 jhost = (jobject) xbt_dynar_get_as(hosts,index,m_host_t)->data;
838 jhost_unref(env, jhost);
841 xbt_dynar_free(&hosts);
842 XBT_INFO("Clean native world");
844 JNIEXPORT void JNICALL
845 JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
847 /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
848 MSG_error_t rv = MSG_OK != MSG_clean();
849 jxbt_check_res("MSG_clean()", rv, MSG_OK,
851 ("unexpected error : MSG_clean() failed .. please report this bug "));
854 JNIEXPORT jint JNICALL
855 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
858 return (jint) MSG_process_killall((int) jresetPID);
861 JNIEXPORT void JNICALL
862 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
863 jstring jplatformFile)
866 const char *platformFile =
867 (*env)->GetStringUTFChars(env, jplatformFile, 0);
869 MSG_create_environment(platformFile);
871 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
874 JNIEXPORT void JNICALL
875 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
879 m_process_t process = jprocess_to_native_process(jprocess, env);
882 jxbt_throw_notbound(env, "process", jprocess);
886 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
889 JNIEXPORT void JNICALL
890 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
892 const char *s = (*env)->GetStringUTFChars(env, js, 0);
894 (*env)->ReleaseStringUTFChars(env, js, s);
897 JNIEXPORT jobjectArray JNICALL
898 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
906 xbt_dynar_t table = MSG_hosts_as_dynar();
907 int count = xbt_dynar_length(table);
909 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
915 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
918 jxbt_throw_jni(env, "Hosts table allocation failed");
922 for (index = 0; index < count; index++) {
923 host = xbt_dynar_get_as(table,index,m_host_t);
924 jhost = (jobject) (MSG_host_get_data(host));
927 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
930 Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
931 /* FIXME: leak of jname ? */
934 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
936 xbt_dynar_free(&table);
940 JNIEXPORT void JNICALL
941 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
942 jstring jalias, jobject jtask,
947 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
949 m_task_t task = jtask_to_native_task(jtask, env);
953 (*env)->ReleaseStringUTFChars(env, jalias, alias);
954 jxbt_throw_notbound(env, "task", jtask);
958 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
959 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
960 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
962 (*env)->ReleaseStringUTFChars(env, jalias, alias);
964 jxbt_check_res("MSG_task_send_with_timeout()", rv,
965 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
966 bprintf("while sending task %s to mailbox %s",
967 MSG_task_get_name(task), alias));
970 JNIEXPORT void JNICALL
971 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
972 jstring jalias, jobject jtask,
975 m_task_t task = jtask_to_native_task(jtask, env);
980 jxbt_throw_notbound(env, "task", jtask);
984 alias = (*env)->GetStringUTFChars(env, jalias, 0);
986 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
987 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
988 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
990 (*env)->ReleaseStringUTFChars(env, jalias, alias);
992 jxbt_check_res("MSG_task_send_bounded()", rv,
993 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
995 ("while sending task %s to mailbox %s with max rate %f",
996 MSG_task_get_name(task), alias, (double) jmaxRate));
1000 JNIEXPORT jobject JNICALL
1001 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1002 jstring jalias, jdouble jtimeout,
1006 m_task_t task = NULL;
1007 m_host_t host = NULL;
1008 jobject jtask_global, jtask_local;
1012 host = jhost_get_native(env, jhost);
1015 jxbt_throw_notbound(env, "host", jhost);
1020 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1022 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1023 jtask_global = MSG_task_get_data(task);
1025 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1026 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1027 (*env)->DeleteGlobalRef(env, jtask_global);
1028 MSG_task_set_data(task, NULL);
1030 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1032 jxbt_check_res("MSG_task_receive_ext()", rv,
1033 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1034 bprintf("while receiving from mailbox %s", alias));
1036 return (jobject) jtask_local;
1039 JNIEXPORT jboolean JNICALL
1040 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1047 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1049 rv = MSG_task_listen(alias);
1051 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1053 return (jboolean) rv;
1056 JNIEXPORT jint JNICALL
1057 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1064 m_host_t host = jhost_get_native(env, jhost);
1067 jxbt_throw_notbound(env, "host", jhost);
1070 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1072 rv = MSG_task_listen_from_host(alias, host);
1074 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1079 JNIEXPORT jint JNICALL
1080 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1085 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1087 rv = MSG_task_listen_from(alias);
1089 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1094 JNIEXPORT void JNICALL
1095 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1096 jstring jdeploymentFile)
1099 const char *deploymentFile =
1100 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1102 surf_parse_reset_callbacks();
1104 surfxml_add_callback(STag_surfxml_process_cb_list,
1105 japplication_handler_on_begin_process);
1107 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1108 japplication_handler_on_process_arg);
1110 surfxml_add_callback(STag_surfxml_prop_cb_list,
1111 japplication_handler_on_property);
1113 surfxml_add_callback(ETag_surfxml_process_cb_list,
1114 japplication_handler_on_end_process);
1116 surf_parse_open(deploymentFile);
1118 japplication_handler_on_start_document();
1121 jxbt_throw_jni(env, "surf_parse() failed");
1125 japplication_handler_on_end_document();
1127 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);