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 /* kill the native process (this wrapper is call by the destructor of the java
182 MSG_process_kill(process);
185 JNIEXPORT jobject JNICALL
186 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
189 /* get the native instances from the java ones */
190 m_process_t process = jprocess_to_native_process(jprocess, env);
194 jxbt_throw_notbound(env, "process", jprocess);
198 host = MSG_process_get_host(process);
200 if (!MSG_host_get_data(host)) {
201 jxbt_throw_jni(env, "MSG_process_get_host() failed");
205 /* return the global reference to the java host instance */
206 return (jobject) MSG_host_get_data(host);
210 JNIEXPORT jobject JNICALL
211 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
214 m_process_t process = MSG_process_from_PID(PID);
217 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
221 if (!native_to_java_process(process)) {
222 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
226 return (jobject) (native_to_java_process(process));
230 JNIEXPORT jint JNICALL
231 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
234 m_process_t process = jprocess_to_native_process(jprocess, env);
237 jxbt_throw_notbound(env, "process", jprocess);
241 return (jint) MSG_process_get_PID(process);
245 JNIEXPORT jint JNICALL
246 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
249 m_process_t process = jprocess_to_native_process(jprocess, env);
252 jxbt_throw_notbound(env, "process", jprocess);
256 return (jint) MSG_process_get_PPID(process);
259 JNIEXPORT jobject JNICALL
260 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
262 m_process_t process = MSG_process_self();
266 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
270 jprocess = native_to_java_process(process);
273 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
278 JNIEXPORT void JNICALL
279 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
280 jobject jprocess, jobject jhost)
282 m_process_t process = jprocess_to_native_process(jprocess, env);
285 jxbt_throw_notbound(env, "process", jprocess);
289 m_host_t host = jhost_get_native(env, jhost);
292 jxbt_throw_notbound(env, "host", jhost);
296 /* try to change the host of the process */
297 MSG_error_t rv = MSG_process_migrate(process, host);
298 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
299 bprintf("unexpected error , please report this bug"));
303 JNIEXPORT void JNICALL
304 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
307 MSG_error_t rv = MSG_process_sleep((double) seconds);
309 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
310 bprintf("while process was waiting for %f seconds",
316 /***************************************************************************************
317 * The MSG host connected functions implementation. *
318 ***************************************************************************************/
320 JNIEXPORT jobject JNICALL
321 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
324 m_host_t host; /* native host */
325 jobject jhost; /* global reference to the java host instance returned */
327 /* get the C string from the java string */
328 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
329 XBT_DEBUG("Looking for host '%s'",name);
330 /* get the host by name (the hosts are created during the grid resolution) */
331 host = MSG_get_host_by_name(name);
332 XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
334 if (!host) { /* invalid name */
335 jxbt_throw_host_not_found(env, name);
336 (*env)->ReleaseStringUTFChars(env, jname, name);
339 (*env)->ReleaseStringUTFChars(env, jname, name);
341 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
343 /* instanciate a new java host */
344 jhost = jhost_new_instance(env);
347 jxbt_throw_jni(env, "java host instantiation failed");
351 /* get a global reference to the newly created host */
352 jhost = jhost_ref(env, jhost);
355 jxbt_throw_jni(env, "new global ref allocation failed");
359 /* bind the java host and the native host */
360 jhost_bind(jhost, host, env);
362 /* the native host data field is set with the global reference to the
363 * java host returned by this function
365 MSG_host_set_data(host, (void *) jhost);
368 /* return the global reference to the java host instance */
369 return (jobject) MSG_host_get_data(host);
372 JNIEXPORT jstring JNICALL
373 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
376 m_host_t host = jhost_get_native(env, jhost);
379 jxbt_throw_notbound(env, "host", jhost);
383 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
386 JNIEXPORT jint JNICALL
387 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
389 return (jint) MSG_get_host_number();
392 JNIEXPORT jobject JNICALL
393 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
397 m_host_t host = MSG_host_self();
399 if (!MSG_host_get_data(host)) {
400 /* the native host not yet associated with the java host instance */
402 /* instanciate a new java host instance */
403 jhost = jhost_new_instance(env);
406 jxbt_throw_jni(env, "java host instantiation failed");
410 /* get a global reference to the newly created host */
411 jhost = jhost_ref(env, jhost);
414 jxbt_throw_jni(env, "global ref allocation failed");
418 /* Bind & store it */
419 jhost_bind(jhost, host, env);
420 MSG_host_set_data(host, (void *) jhost);
422 jhost = (jobject) MSG_host_get_data(host);
428 JNIEXPORT jdouble JNICALL
429 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
432 m_host_t host = jhost_get_native(env, jhost);
435 jxbt_throw_notbound(env, "host", jhost);
439 return (jdouble) MSG_get_host_speed(host);
442 JNIEXPORT jint JNICALL
443 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
446 m_host_t host = jhost_get_native(env, jhost);
449 jxbt_throw_notbound(env, "host", jhost);
453 return (jint) MSG_get_host_msgload(host);
457 JNIEXPORT jboolean JNICALL
458 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
461 m_host_t host = jhost_get_native(env, jhost);
464 jxbt_throw_notbound(env, "host", jhost);
468 return (jboolean) MSG_host_is_avail(host);
472 /***************************************************************************************
473 * The MSG task connected functions implementation. *
474 ***************************************************************************************/
476 JNIEXPORT void JNICALL
477 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
478 jobject jtask, jstring jname,
479 jdouble jcomputeDuration,
480 jdouble jmessageSize)
482 m_task_t task; /* the native task to create */
483 const char *name = NULL; /* the name of the task */
485 if (jcomputeDuration < 0) {
486 jxbt_throw_illegal(env,
488 ("Task ComputeDuration (%f) cannot be negative",
489 (double) jcomputeDuration));
493 if (jmessageSize < 0) {
494 jxbt_throw_illegal(env,
495 bprintf("Task MessageSize (%f) cannot be negative",
496 (double) jmessageSize));
501 /* get the C string from the java string */
502 name = (*env)->GetStringUTFChars(env, jname, 0);
506 /* create the task */
508 MSG_task_create(name, (double) jcomputeDuration,
509 (double) jmessageSize, NULL);
512 (*env)->ReleaseStringUTFChars(env, jname, name);
514 /* bind & store the task */
515 jtask_bind(jtask, task, env);
516 MSG_task_set_data(task, jtask);
519 JNIEXPORT void JNICALL
520 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
525 jcomputeDurations_arg,
530 m_task_t task; /* the native parallel task to create */
531 const char *name; /* the name of the task */
534 double *computeDurations;
535 double *messageSizes;
536 jdouble *jcomputeDurations;
537 jdouble *jmessageSizes;
543 if (!jcomputeDurations_arg) {
546 ("Parallel task compute durations cannot be null"));
550 if (!jmessageSizes_arg) {
553 ("Parallel task message sizes cannot be null"));
558 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
562 host_count = (int) (*env)->GetArrayLength(env, jhosts);
565 hosts = xbt_new0(m_host_t, host_count);
566 computeDurations = xbt_new0(double, host_count);
567 messageSizes = xbt_new0(double, host_count * host_count);
570 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
572 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
574 for (index = 0; index < host_count; index++) {
575 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
576 hosts[index] = jhost_get_native(env, jhost);
577 computeDurations[index] = jcomputeDurations[index];
579 for (index = 0; index < host_count * host_count; index++) {
580 messageSizes[index] = jmessageSizes[index];
583 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
584 jcomputeDurations, 0);
585 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
589 /* get the C string from the java string */
590 name = (*env)->GetStringUTFChars(env, jname, 0);
593 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
596 (*env)->ReleaseStringUTFChars(env, jname, name);
598 /* associate the java task object and the native task */
599 jtask_bind(jtask, task, env);
601 MSG_task_set_data(task, (void *) jtask);
603 if (!MSG_task_get_data(task))
604 jxbt_throw_jni(env, "global ref allocation failed");
607 JNIEXPORT jobject JNICALL
608 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
613 m_task_t task = jtask_to_native_task(jtask, env);
616 jxbt_throw_notbound(env, "task", jtask);
620 process = MSG_task_get_sender(task);
621 return (jobject) native_to_java_process(process);
624 JNIEXPORT jobject JNICALL
625 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
629 m_task_t task = jtask_to_native_task(jtask, env);
632 jxbt_throw_notbound(env, "task", jtask);
636 host = MSG_task_get_source(task);
638 if (!MSG_host_get_data(host)) {
639 jxbt_throw_jni(env, "MSG_task_get_source() failed");
643 return (jobject) MSG_host_get_data(host);
647 JNIEXPORT jstring JNICALL
648 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
651 m_task_t task = jtask_to_native_task(jtask, env);
654 jxbt_throw_notbound(env, "task", jtask);
658 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
661 JNIEXPORT void JNICALL
662 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
665 m_task_t ptask = jtask_to_native_task(jtask, env);
668 jxbt_throw_notbound(env, "task", jtask);
672 MSG_error_t rv = MSG_task_cancel(ptask);
674 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
675 bprintf("unexpected error , please report this bug"));
678 JNIEXPORT jdouble JNICALL
679 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
682 m_task_t ptask = jtask_to_native_task(jtask, env);
685 jxbt_throw_notbound(env, "task", jtask);
688 return (jdouble) MSG_task_get_compute_duration(ptask);
691 JNIEXPORT jdouble JNICALL
692 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
696 m_task_t ptask = jtask_to_native_task(jtask, env);
699 jxbt_throw_notbound(env, "task", jtask);
702 return (jdouble) MSG_task_get_remaining_computation(ptask);
705 JNIEXPORT void JNICALL
706 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
707 jobject jtask, jdouble priority)
709 m_task_t task = jtask_to_native_task(jtask, env);
712 jxbt_throw_notbound(env, "task", jtask);
715 MSG_task_set_priority(task, (double) priority);
718 JNIEXPORT void JNICALL
719 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
723 /* get the native task */
724 m_task_t task = jtask_to_native_task(jtask_arg, env);
727 jxbt_throw_notbound(env, "task", 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_CANCELED,
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 (*env)->ReleaseStringUTFChars(env, jval, tmp);
798 MSG_global_init(&argc, argv);
800 for (index = 0; index < argc; index++)
805 (*env)->GetJavaVM(env, &__java_vm);
808 JNIEXPORT void JNICALL
809 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
817 XBT_INFO("Ready to run MSG_MAIN");
819 XBT_INFO("Done running MSG_MAIN");
820 jxbt_check_res("MSG_main()", rv, MSG_OK,
822 ("unexpected error : MSG_main() failed .. please report this bug "));
824 XBT_INFO("MSG_main finished");
826 XBT_INFO("Clean java world");
827 /* Cleanup java hosts */
828 hosts = MSG_get_host_table();
829 for (index = 0; index < MSG_get_host_number() - 1; index++) {
830 jhost = (jobject) hosts[index]->data;
832 jhost_unref(env, jhost);
836 XBT_INFO("Clean native world");
838 JNIEXPORT void JNICALL
839 JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
841 /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
842 MSG_error_t rv = MSG_OK != MSG_clean();
843 jxbt_check_res("MSG_clean()", rv, MSG_OK,
845 ("unexpected error : MSG_clean() failed .. please report this bug "));
848 JNIEXPORT jint JNICALL
849 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
852 return (jint) MSG_process_killall((int) jresetPID);
855 JNIEXPORT void JNICALL
856 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
857 jstring jplatformFile)
860 const char *platformFile =
861 (*env)->GetStringUTFChars(env, jplatformFile, 0);
863 MSG_create_environment(platformFile);
865 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
868 JNIEXPORT void JNICALL
869 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
873 m_process_t process = jprocess_to_native_process(jprocess, env);
876 jxbt_throw_notbound(env, "process", jprocess);
880 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
883 JNIEXPORT void JNICALL
884 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
886 const char *s = (*env)->GetStringUTFChars(env, js, 0);
888 (*env)->ReleaseStringUTFChars(env, js, s);
891 JNIEXPORT jobjectArray JNICALL
892 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
900 int count = MSG_get_host_number();
901 m_host_t *table = MSG_get_host_table();
903 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
909 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
912 jxbt_throw_jni(env, "Hosts table allocation failed");
916 for (index = 0; index < count; index++) {
918 jhost = (jobject) (MSG_host_get_data(host));
921 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
924 Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
925 /* FIXME: leak of jname ? */
928 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
934 JNIEXPORT void JNICALL
935 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
936 jstring jalias, jobject jtask,
941 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
943 m_task_t task = jtask_to_native_task(jtask, env);
947 (*env)->ReleaseStringUTFChars(env, jalias, alias);
948 jxbt_throw_notbound(env, "task", jtask);
952 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
953 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
954 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
956 (*env)->ReleaseStringUTFChars(env, jalias, alias);
958 jxbt_check_res("MSG_task_send_with_timeout()", rv,
959 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
960 bprintf("while sending task %s to mailbox %s",
961 MSG_task_get_name(task), alias));
964 JNIEXPORT void JNICALL
965 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
966 jstring jalias, jobject jtask,
969 m_task_t task = jtask_to_native_task(jtask, env);
974 jxbt_throw_notbound(env, "task", jtask);
978 alias = (*env)->GetStringUTFChars(env, jalias, 0);
980 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
981 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
982 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
984 (*env)->ReleaseStringUTFChars(env, jalias, alias);
986 jxbt_check_res("MSG_task_send_bounded()", rv,
987 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
989 ("while sending task %s to mailbox %s with max rate %f",
990 MSG_task_get_name(task), alias, (double) jmaxRate));
994 JNIEXPORT jobject JNICALL
995 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
996 jstring jalias, jdouble jtimeout,
1000 m_task_t task = NULL;
1001 m_host_t host = NULL;
1002 jobject jtask_global, jtask_local;
1006 host = jhost_get_native(env, jhost);
1009 jxbt_throw_notbound(env, "host", jhost);
1014 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1016 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1017 jtask_global = MSG_task_get_data(task);
1019 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1020 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1021 (*env)->DeleteGlobalRef(env, jtask_global);
1022 MSG_task_set_data(task, NULL);
1024 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1026 jxbt_check_res("MSG_task_receive_ext()", rv,
1027 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1028 bprintf("while receiving from mailbox %s", alias));
1030 return (jobject) jtask_local;
1033 JNIEXPORT jboolean JNICALL
1034 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1041 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1043 rv = MSG_task_listen(alias);
1045 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1047 return (jboolean) rv;
1050 JNIEXPORT jint JNICALL
1051 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1058 m_host_t host = jhost_get_native(env, jhost);
1061 jxbt_throw_notbound(env, "host", jhost);
1064 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1066 rv = MSG_task_listen_from_host(alias, host);
1068 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1073 JNIEXPORT jint JNICALL
1074 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1079 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1081 rv = MSG_task_listen_from(alias);
1083 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1088 JNIEXPORT void JNICALL
1089 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1090 jstring jdeploymentFile)
1093 const char *deploymentFile =
1094 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1096 surf_parse_reset_callbacks();
1098 surfxml_add_callback(STag_surfxml_process_cb_list,
1099 japplication_handler_on_begin_process);
1101 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1102 japplication_handler_on_process_arg);
1104 surfxml_add_callback(STag_surfxml_prop_cb_list,
1105 japplication_handler_on_property);
1107 surfxml_add_callback(ETag_surfxml_process_cb_list,
1108 japplication_handler_on_end_process);
1110 surf_parse_open(deploymentFile);
1112 japplication_handler_on_start_document();
1115 jxbt_throw_jni(env, "surf_parse() failed");
1119 japplication_handler_on_end_document();
1121 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);