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_processMigrate(JNIEnv * env, jclass cls,
284 jobject jprocess, jobject jhost)
286 m_process_t process = jprocess_to_native_process(jprocess, env);
289 jxbt_throw_notbound(env, "process", jprocess);
293 m_host_t host = jhost_get_native(env, jhost);
296 jxbt_throw_notbound(env, "host", jhost);
300 /* try to change the host of the process */
301 MSG_error_t rv = MSG_process_migrate(process, host);
302 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
303 bprintf("unexpected error , please report this bug"));
307 JNIEXPORT void JNICALL
308 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
311 MSG_error_t rv = MSG_process_sleep((double) seconds);
313 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
314 bprintf("while process was waiting for %f seconds",
320 /***************************************************************************************
321 * The MSG host connected functions implementation. *
322 ***************************************************************************************/
324 JNIEXPORT jobject JNICALL
325 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
328 m_host_t host; /* native host */
329 jobject jhost; /* global reference to the java host instance returned */
331 /* get the C string from the java string */
332 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
333 XBT_DEBUG("Looking for host '%s'",name);
334 /* get the host by name (the hosts are created during the grid resolution) */
335 host = MSG_get_host_by_name(name);
336 XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
338 if (!host) { /* invalid name */
339 jxbt_throw_host_not_found(env, name);
340 (*env)->ReleaseStringUTFChars(env, jname, name);
343 (*env)->ReleaseStringUTFChars(env, jname, name);
345 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
347 /* instanciate a new java host */
348 jhost = jhost_new_instance(env);
351 jxbt_throw_jni(env, "java host instantiation failed");
355 /* get a global reference to the newly created host */
356 jhost = jhost_ref(env, jhost);
359 jxbt_throw_jni(env, "new global ref allocation failed");
363 /* bind the java host and the native host */
364 jhost_bind(jhost, host, env);
366 /* the native host data field is set with the global reference to the
367 * java host returned by this function
369 MSG_host_set_data(host, (void *) jhost);
372 /* return the global reference to the java host instance */
373 return (jobject) MSG_host_get_data(host);
376 JNIEXPORT jstring JNICALL
377 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
380 m_host_t host = jhost_get_native(env, jhost);
383 jxbt_throw_notbound(env, "host", jhost);
387 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
390 JNIEXPORT jint JNICALL
391 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
393 return (jint) MSG_get_host_number();
396 JNIEXPORT jobject JNICALL
397 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
401 m_host_t host = MSG_host_self();
403 if (!MSG_host_get_data(host)) {
404 /* the native host not yet associated with the java host instance */
406 /* instanciate a new java host instance */
407 jhost = jhost_new_instance(env);
410 jxbt_throw_jni(env, "java host instantiation failed");
414 /* get a global reference to the newly created host */
415 jhost = jhost_ref(env, jhost);
418 jxbt_throw_jni(env, "global ref allocation failed");
422 /* Bind & store it */
423 jhost_bind(jhost, host, env);
424 MSG_host_set_data(host, (void *) jhost);
426 jhost = (jobject) MSG_host_get_data(host);
432 JNIEXPORT jdouble JNICALL
433 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
436 m_host_t host = jhost_get_native(env, jhost);
439 jxbt_throw_notbound(env, "host", jhost);
443 return (jdouble) MSG_get_host_speed(host);
446 JNIEXPORT jint JNICALL
447 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
450 m_host_t host = jhost_get_native(env, jhost);
453 jxbt_throw_notbound(env, "host", jhost);
457 return (jint) MSG_get_host_msgload(host);
461 JNIEXPORT jboolean JNICALL
462 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
465 m_host_t host = jhost_get_native(env, jhost);
468 jxbt_throw_notbound(env, "host", jhost);
472 return (jboolean) MSG_host_is_avail(host);
476 /***************************************************************************************
477 * The MSG task connected functions implementation. *
478 ***************************************************************************************/
480 JNIEXPORT void JNICALL
481 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
482 jobject jtask, jstring jname,
483 jdouble jcomputeDuration,
484 jdouble jmessageSize)
486 m_task_t task; /* the native task to create */
487 const char *name = NULL; /* the name of the task */
489 if (jcomputeDuration < 0) {
490 jxbt_throw_illegal(env,
492 ("Task ComputeDuration (%f) cannot be negative",
493 (double) jcomputeDuration));
497 if (jmessageSize < 0) {
498 jxbt_throw_illegal(env,
499 bprintf("Task MessageSize (%f) cannot be negative",
500 (double) jmessageSize));
505 /* get the C string from the java string */
506 name = (*env)->GetStringUTFChars(env, jname, 0);
510 /* create the task */
512 MSG_task_create(name, (double) jcomputeDuration,
513 (double) jmessageSize, NULL);
516 (*env)->ReleaseStringUTFChars(env, jname, name);
518 /* bind & store the task */
519 jtask_bind(jtask, task, env);
520 MSG_task_set_data(task, jtask);
523 JNIEXPORT void JNICALL
524 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
529 jcomputeDurations_arg,
534 m_task_t task; /* the native parallel task to create */
535 const char *name; /* the name of the task */
538 double *computeDurations;
539 double *messageSizes;
540 jdouble *jcomputeDurations;
541 jdouble *jmessageSizes;
547 if (!jcomputeDurations_arg) {
550 ("Parallel task compute durations cannot be null"));
554 if (!jmessageSizes_arg) {
557 ("Parallel task message sizes cannot be null"));
562 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
566 host_count = (int) (*env)->GetArrayLength(env, jhosts);
569 hosts = xbt_new0(m_host_t, host_count);
570 computeDurations = xbt_new0(double, host_count);
571 messageSizes = xbt_new0(double, host_count * host_count);
574 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
576 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
578 for (index = 0; index < host_count; index++) {
579 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
580 hosts[index] = jhost_get_native(env, jhost);
581 computeDurations[index] = jcomputeDurations[index];
583 for (index = 0; index < host_count * host_count; index++) {
584 messageSizes[index] = jmessageSizes[index];
587 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
588 jcomputeDurations, 0);
589 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
593 /* get the C string from the java string */
594 name = (*env)->GetStringUTFChars(env, jname, 0);
597 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
600 (*env)->ReleaseStringUTFChars(env, jname, name);
602 /* associate the java task object and the native task */
603 jtask_bind(jtask, task, env);
605 MSG_task_set_data(task, (void *) jtask);
607 if (!MSG_task_get_data(task))
608 jxbt_throw_jni(env, "global ref allocation failed");
611 JNIEXPORT jobject JNICALL
612 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
617 m_task_t task = jtask_to_native_task(jtask, env);
620 jxbt_throw_notbound(env, "task", jtask);
624 process = MSG_task_get_sender(task);
625 return (jobject) native_to_java_process(process);
628 JNIEXPORT jobject JNICALL
629 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
633 m_task_t task = jtask_to_native_task(jtask, env);
636 jxbt_throw_notbound(env, "task", jtask);
640 host = MSG_task_get_source(task);
642 if (!MSG_host_get_data(host)) {
643 jxbt_throw_jni(env, "MSG_task_get_source() failed");
647 return (jobject) MSG_host_get_data(host);
651 JNIEXPORT jstring JNICALL
652 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
655 m_task_t task = jtask_to_native_task(jtask, env);
658 jxbt_throw_notbound(env, "task", jtask);
662 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
665 JNIEXPORT void JNICALL
666 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
669 m_task_t ptask = jtask_to_native_task(jtask, env);
672 jxbt_throw_notbound(env, "task", jtask);
676 MSG_error_t rv = MSG_task_cancel(ptask);
678 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
679 bprintf("unexpected error , please report this bug"));
682 JNIEXPORT jdouble JNICALL
683 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
686 m_task_t ptask = jtask_to_native_task(jtask, env);
689 jxbt_throw_notbound(env, "task", jtask);
692 return (jdouble) MSG_task_get_compute_duration(ptask);
695 JNIEXPORT jdouble JNICALL
696 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
700 m_task_t ptask = jtask_to_native_task(jtask, env);
703 jxbt_throw_notbound(env, "task", jtask);
706 return (jdouble) MSG_task_get_remaining_computation(ptask);
709 JNIEXPORT void JNICALL
710 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
711 jobject jtask, jdouble priority)
713 m_task_t task = jtask_to_native_task(jtask, env);
716 jxbt_throw_notbound(env, "task", jtask);
719 MSG_task_set_priority(task, (double) priority);
722 JNIEXPORT void JNICALL
723 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
727 /* get the native task */
728 m_task_t task = jtask_to_native_task(jtask_arg, env);
732 jxbt_throw_notbound(env, "task", task);
735 jtask = (jobject) MSG_task_get_data(task);
737 MSG_error_t rv = MSG_task_destroy(task);
739 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
740 bprintf("unexpected error , please report this bug"));
743 JNIEXPORT void JNICALL
744 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
747 m_task_t task = jtask_to_native_task(jtask, env);
750 jxbt_throw_notbound(env, "task", jtask);
754 MSG_error_t rv = MSG_task_execute(task);
756 jxbt_check_res("MSG_task_execute()", rv,
757 MSG_HOST_FAILURE | MSG_TASK_CANCELLED,
758 bprintf("while executing task %s",
759 MSG_task_get_name(task)));
762 /***************************************************************************************
763 * Unsortable functions *
764 ***************************************************************************************/
767 JNIEXPORT jint JNICALL
768 Java_org_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
770 return (jint) MSG_get_errno();
773 JNIEXPORT jdouble JNICALL
774 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
776 return (jdouble) MSG_get_clock();
779 JNIEXPORT void JNICALL
780 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
788 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
791 argc = (int) (*env)->GetArrayLength(env, jargs);
794 argv = xbt_new0(char *, argc);
795 argv[0] = strdup("java");
797 for (index = 0; index < argc - 1; index++) {
798 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
799 tmp = (*env)->GetStringUTFChars(env, jval, 0);
800 argv[index + 1] = strdup(tmp);
801 //argv[index] = strdup(tmp);
802 (*env)->ReleaseStringUTFChars(env, jval, tmp);
805 MSG_global_init(&argc, argv);
807 for (index = 0; index < argc; index++)
812 (*env)->GetJavaVM(env, &__java_vm);
815 JNIEXPORT void JNICALL
816 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
819 int index; //xbt_fifo_item_t item = NULL;
824 XBT_INFO("Ready to run MSG_MAIN");
826 XBT_INFO("Done running MSG_MAIN");
827 jxbt_check_res("MSG_main()", rv, MSG_OK,
829 ("unexpected error : MSG_main() failed .. please report this bug "));
831 XBT_INFO("MSG_main finished");
833 XBT_INFO("Clean java world");
834 /* Cleanup java hosts */
835 hosts = MSG_get_host_table();
836 for (index = 0; index < MSG_get_host_number() - 1; index++) {
837 jhost = (jobject) hosts[index]->data;
839 jhost_unref(env, jhost);
843 XBT_INFO("Clean native world");
844 /* cleanup native stuff */
845 rv = MSG_OK != MSG_clean();
846 jxbt_check_res("MSG_clean()", rv, MSG_OK,
848 ("unexpected error : MSG_clean() failed .. please report this bug "));
851 JNIEXPORT jint JNICALL
852 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
855 return (jint) MSG_process_killall((int) jresetPID);
858 JNIEXPORT void JNICALL
859 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
860 jstring jplatformFile)
863 const char *platformFile =
864 (*env)->GetStringUTFChars(env, jplatformFile, 0);
866 MSG_create_environment(platformFile);
868 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
871 JNIEXPORT void JNICALL
872 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
876 m_process_t process = jprocess_to_native_process(jprocess, env);
879 jxbt_throw_notbound(env, "process", jprocess);
883 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
886 JNIEXPORT void JNICALL
887 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
889 const char *s = (*env)->GetStringUTFChars(env, js, 0);
891 (*env)->ReleaseStringUTFChars(env, js, s);
894 JNIEXPORT jobjectArray JNICALL
895 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
903 int count = MSG_get_host_number();
904 m_host_t *table = MSG_get_host_table();
906 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
912 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
915 jxbt_throw_jni(env, "Hosts table allocation failed");
919 for (index = 0; index < count; index++) {
921 jhost = (jobject) (MSG_host_get_data(host));
924 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
927 Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
928 /* FIXME: leak of jname ? */
931 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
937 JNIEXPORT void JNICALL
938 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
939 jstring jalias, jobject jtask,
944 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
946 m_task_t task = jtask_to_native_task(jtask, env);
950 (*env)->ReleaseStringUTFChars(env, jalias, alias);
951 jxbt_throw_notbound(env, "task", jtask);
955 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
956 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
957 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
959 (*env)->ReleaseStringUTFChars(env, jalias, alias);
961 jxbt_check_res("MSG_task_send_with_timeout()", rv,
962 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
963 bprintf("while sending task %s to mailbox %s",
964 MSG_task_get_name(task), alias));
967 JNIEXPORT void JNICALL
968 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
969 jstring jalias, jobject jtask,
972 m_task_t task = jtask_to_native_task(jtask, env);
977 jxbt_throw_notbound(env, "task", jtask);
981 alias = (*env)->GetStringUTFChars(env, jalias, 0);
983 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
984 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
985 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
987 (*env)->ReleaseStringUTFChars(env, jalias, alias);
989 jxbt_check_res("MSG_task_send_bounded()", rv,
990 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
992 ("while sending task %s to mailbox %s with max rate %f",
993 MSG_task_get_name(task), alias, (double) jmaxRate));
997 JNIEXPORT jobject JNICALL
998 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
999 jstring jalias, jdouble jtimeout,
1003 m_task_t task = NULL;
1004 m_host_t host = NULL;
1005 jobject jtask_global, jtask_local;
1009 host = jhost_get_native(env, jhost);
1012 jxbt_throw_notbound(env, "host", jhost);
1017 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1019 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1020 jtask_global = MSG_task_get_data(task);
1022 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1023 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1024 (*env)->DeleteGlobalRef(env, jtask_global);
1025 MSG_task_set_data(task, NULL);
1027 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1029 jxbt_check_res("MSG_task_receive_ext()", rv,
1030 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1031 bprintf("while receiving from mailbox %s", alias));
1033 return (jobject) jtask_local;
1036 JNIEXPORT jboolean JNICALL
1037 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1044 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1046 rv = MSG_task_listen(alias);
1048 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1050 return (jboolean) rv;
1053 JNIEXPORT jint JNICALL
1054 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1061 m_host_t host = jhost_get_native(env, jhost);
1064 jxbt_throw_notbound(env, "host", jhost);
1067 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1069 rv = MSG_task_listen_from_host(alias, host);
1071 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1076 JNIEXPORT jint JNICALL
1077 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1082 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1084 rv = MSG_task_listen_from(alias);
1086 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1091 JNIEXPORT void JNICALL
1092 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1093 jstring jdeploymentFile)
1096 const char *deploymentFile =
1097 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1099 surf_parse_reset_callbacks();
1101 surfxml_add_callback(STag_surfxml_process_cb_list,
1102 japplication_handler_on_begin_process);
1104 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1105 japplication_handler_on_process_arg);
1107 surfxml_add_callback(STag_surfxml_prop_cb_list,
1108 japplication_handler_on_property);
1110 surfxml_add_callback(ETag_surfxml_process_cb_list,
1111 japplication_handler_on_end_process);
1113 surf_parse_open(deploymentFile);
1115 japplication_handler_on_start_document();
1118 jxbt_throw_jni(env, "surf_parse() failed");
1122 japplication_handler_on_end_document();
1124 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);