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_Process_simulatedSleep(JNIEnv * env, jobject jprocess,
146 m_process_t process = jprocess_to_native_process(jprocess, env);
149 jxbt_throw_notbound(env, "process", jprocess);
152 MSG_error_t rv = MSG_process_sleep((double)jseconds);
154 jxbt_check_res("MSG_process_sleep()", rv, MSG_OK,
155 bprintf("unexpected error , please report this bug"));
159 JNIEXPORT void JNICALL
160 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
163 m_process_t process = jprocess_to_native_process(jprocess, env);
166 jxbt_throw_notbound(env, "process", jprocess);
170 /* try to resume the process */
171 MSG_error_t rv = MSG_process_resume(process);
173 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
174 bprintf("unexpected error , please report this bug"));
177 JNIEXPORT jboolean JNICALL
178 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
181 m_process_t process = jprocess_to_native_process(jprocess, env);
184 jxbt_throw_notbound(env, "process", jprocess);
188 /* true is the process is suspended, false otherwise */
189 return (jboolean) MSG_process_is_suspended(process);
192 JNIEXPORT void JNICALL
193 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
196 /* get the native instances from the java ones */
197 m_process_t process = jprocess_to_native_process(jprocess, env);
200 jxbt_throw_notbound(env, "process", jprocess);
204 /* kill the native process (this wrapper is call by the destructor of the java
207 MSG_process_kill(process);
210 JNIEXPORT jobject JNICALL
211 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
214 /* get the native instances from the java ones */
215 m_process_t process = jprocess_to_native_process(jprocess, env);
219 jxbt_throw_notbound(env, "process", jprocess);
223 host = MSG_process_get_host(process);
225 if (!MSG_host_get_data(host)) {
226 jxbt_throw_jni(env, "MSG_process_get_host() failed");
230 /* return the global reference to the java host instance */
231 return (jobject) MSG_host_get_data(host);
235 JNIEXPORT jobject JNICALL
236 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
239 m_process_t process = MSG_process_from_PID(PID);
242 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
246 if (!native_to_java_process(process)) {
247 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
251 return (jobject) (native_to_java_process(process));
255 JNIEXPORT jint JNICALL
256 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
259 m_process_t process = jprocess_to_native_process(jprocess, env);
262 jxbt_throw_notbound(env, "process", jprocess);
266 return (jint) MSG_process_get_PID(process);
270 JNIEXPORT jint JNICALL
271 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
274 m_process_t process = jprocess_to_native_process(jprocess, env);
277 jxbt_throw_notbound(env, "process", jprocess);
281 return (jint) MSG_process_get_PPID(process);
284 JNIEXPORT jobject JNICALL
285 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
287 m_process_t process = MSG_process_self();
291 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
295 jprocess = native_to_java_process(process);
298 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
303 JNIEXPORT void JNICALL
304 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
305 jobject jprocess, jobject jhost)
307 m_process_t process = jprocess_to_native_process(jprocess, env);
310 jxbt_throw_notbound(env, "process", jprocess);
314 m_host_t host = jhost_get_native(env, jhost);
317 jxbt_throw_notbound(env, "host", jhost);
321 /* try to change the host of the process */
322 MSG_error_t rv = MSG_process_migrate(process, host);
323 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
324 bprintf("unexpected error , please report this bug"));
328 JNIEXPORT void JNICALL
329 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
332 MSG_error_t rv = MSG_process_sleep((double) seconds);
334 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
335 bprintf("while process was waiting for %f seconds",
341 /***************************************************************************************
342 * The MSG host connected functions implementation. *
343 ***************************************************************************************/
345 JNIEXPORT jobject JNICALL
346 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
349 m_host_t host; /* native host */
350 jobject jhost; /* global reference to the java host instance returned */
352 /* get the C string from the java string */
353 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
354 XBT_DEBUG("Looking for host '%s'",name);
355 /* get the host by name (the hosts are created during the grid resolution) */
356 host = MSG_get_host_by_name(name);
357 XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
359 if (!host) { /* invalid name */
360 jxbt_throw_host_not_found(env, name);
361 (*env)->ReleaseStringUTFChars(env, jname, name);
364 (*env)->ReleaseStringUTFChars(env, jname, name);
366 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
368 /* Instantiate a new java host */
369 jhost = jhost_new_instance(env);
372 jxbt_throw_jni(env, "java host instantiation failed");
376 /* get a global reference to the newly created host */
377 jhost = jhost_ref(env, jhost);
380 jxbt_throw_jni(env, "new global ref allocation failed");
384 /* bind the java host and the native host */
385 jhost_bind(jhost, host, env);
387 /* the native host data field is set with the global reference to the
388 * java host returned by this function
390 MSG_host_set_data(host, (void *) jhost);
393 /* return the global reference to the java host instance */
394 return (jobject) MSG_host_get_data(host);
397 JNIEXPORT jstring JNICALL
398 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
401 m_host_t host = jhost_get_native(env, jhost);
404 jxbt_throw_notbound(env, "host", jhost);
408 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
411 JNIEXPORT jint JNICALL
412 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
414 xbt_dynar_t hosts = MSG_hosts_as_dynar();
415 int nb_host = xbt_dynar_length(hosts);
416 xbt_dynar_free(&hosts);
417 return (jint) nb_host;
420 JNIEXPORT jobject JNICALL
421 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
425 m_host_t host = MSG_host_self();
427 if (!MSG_host_get_data(host)) {
428 /* the native host not yet associated with the java host instance */
430 /* instanciate a new java host instance */
431 jhost = jhost_new_instance(env);
434 jxbt_throw_jni(env, "java host instantiation failed");
438 /* get a global reference to the newly created host */
439 jhost = jhost_ref(env, jhost);
442 jxbt_throw_jni(env, "global ref allocation failed");
446 /* Bind & store it */
447 jhost_bind(jhost, host, env);
448 MSG_host_set_data(host, (void *) jhost);
450 jhost = (jobject) MSG_host_get_data(host);
456 JNIEXPORT jdouble JNICALL
457 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
460 m_host_t host = jhost_get_native(env, jhost);
463 jxbt_throw_notbound(env, "host", jhost);
467 return (jdouble) MSG_get_host_speed(host);
470 JNIEXPORT jint JNICALL
471 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
474 m_host_t host = jhost_get_native(env, jhost);
477 jxbt_throw_notbound(env, "host", jhost);
481 return (jint) MSG_get_host_msgload(host);
485 JNIEXPORT jboolean JNICALL
486 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
489 m_host_t host = jhost_get_native(env, jhost);
492 jxbt_throw_notbound(env, "host", jhost);
496 return (jboolean) MSG_host_is_avail(host);
500 /***************************************************************************************
501 * The MSG task connected functions implementation. *
502 ***************************************************************************************/
504 JNIEXPORT void JNICALL
505 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
506 jobject jtask, jstring jname,
507 jdouble jcomputeDuration,
508 jdouble jmessageSize)
510 m_task_t task; /* the native task to create */
511 const char *name = NULL; /* the name of the task */
513 if (jcomputeDuration < 0) {
514 jxbt_throw_illegal(env,
516 ("Task ComputeDuration (%f) cannot be negative",
517 (double) jcomputeDuration));
521 if (jmessageSize < 0) {
522 jxbt_throw_illegal(env,
523 bprintf("Task MessageSize (%f) cannot be negative",
524 (double) jmessageSize));
529 /* get the C string from the java string */
530 name = (*env)->GetStringUTFChars(env, jname, 0);
534 /* create the task */
536 MSG_task_create(name, (double) jcomputeDuration,
537 (double) jmessageSize, NULL);
540 (*env)->ReleaseStringUTFChars(env, jname, name);
542 /* bind & store the task */
543 jtask_bind(jtask, task, env);
544 MSG_task_set_data(task, jtask);
547 JNIEXPORT void JNICALL
548 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
553 jcomputeDurations_arg,
558 m_task_t task; /* the native parallel task to create */
559 const char *name; /* the name of the task */
562 double *computeDurations;
563 double *messageSizes;
564 jdouble *jcomputeDurations;
565 jdouble *jmessageSizes;
571 if (!jcomputeDurations_arg) {
574 ("Parallel task compute durations cannot be null"));
578 if (!jmessageSizes_arg) {
581 ("Parallel task message sizes cannot be null"));
586 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
590 host_count = (int) (*env)->GetArrayLength(env, jhosts);
593 hosts = xbt_new0(m_host_t, host_count);
594 computeDurations = xbt_new0(double, host_count);
595 messageSizes = xbt_new0(double, host_count * host_count);
598 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
600 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
602 for (index = 0; index < host_count; index++) {
603 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
604 hosts[index] = jhost_get_native(env, jhost);
605 computeDurations[index] = jcomputeDurations[index];
607 for (index = 0; index < host_count * host_count; index++) {
608 messageSizes[index] = jmessageSizes[index];
611 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
612 jcomputeDurations, 0);
613 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
617 /* get the C string from the java string */
618 name = (*env)->GetStringUTFChars(env, jname, 0);
621 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
624 (*env)->ReleaseStringUTFChars(env, jname, name);
626 /* associate the java task object and the native task */
627 jtask_bind(jtask, task, env);
629 MSG_task_set_data(task, (void *) jtask);
631 if (!MSG_task_get_data(task))
632 jxbt_throw_jni(env, "global ref allocation failed");
635 JNIEXPORT jobject JNICALL
636 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
641 m_task_t task = jtask_to_native_task(jtask, env);
644 jxbt_throw_notbound(env, "task", jtask);
648 process = MSG_task_get_sender(task);
649 return (jobject) native_to_java_process(process);
652 JNIEXPORT jobject JNICALL
653 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
657 m_task_t task = jtask_to_native_task(jtask, env);
660 jxbt_throw_notbound(env, "task", jtask);
664 host = MSG_task_get_source(task);
666 if (!MSG_host_get_data(host)) {
667 jxbt_throw_jni(env, "MSG_task_get_source() failed");
671 return (jobject) MSG_host_get_data(host);
675 JNIEXPORT jstring JNICALL
676 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
679 m_task_t task = jtask_to_native_task(jtask, env);
682 jxbt_throw_notbound(env, "task", jtask);
686 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
689 JNIEXPORT void JNICALL
690 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
693 m_task_t ptask = jtask_to_native_task(jtask, env);
696 jxbt_throw_notbound(env, "task", jtask);
700 MSG_error_t rv = MSG_task_cancel(ptask);
702 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
703 bprintf("unexpected error , please report this bug"));
706 JNIEXPORT jdouble JNICALL
707 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
710 m_task_t ptask = jtask_to_native_task(jtask, env);
713 jxbt_throw_notbound(env, "task", jtask);
716 return (jdouble) MSG_task_get_compute_duration(ptask);
719 JNIEXPORT jdouble JNICALL
720 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
724 m_task_t ptask = jtask_to_native_task(jtask, env);
727 jxbt_throw_notbound(env, "task", jtask);
730 return (jdouble) MSG_task_get_remaining_computation(ptask);
733 JNIEXPORT void JNICALL
734 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
735 jobject jtask, jdouble priority)
737 m_task_t task = jtask_to_native_task(jtask, env);
740 jxbt_throw_notbound(env, "task", jtask);
743 MSG_task_set_priority(task, (double) priority);
746 JNIEXPORT void JNICALL
747 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
751 /* get the native task */
752 m_task_t task = jtask_to_native_task(jtask_arg, env);
755 jxbt_throw_notbound(env, "task", task);
759 MSG_error_t rv = MSG_task_destroy(task);
761 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
762 bprintf("unexpected error , please report this bug"));
765 JNIEXPORT void JNICALL
766 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
769 m_task_t task = jtask_to_native_task(jtask, env);
772 jxbt_throw_notbound(env, "task", jtask);
776 MSG_error_t rv = MSG_task_execute(task);
778 jxbt_check_res("MSG_task_execute()", rv,
779 MSG_HOST_FAILURE | MSG_TASK_CANCELED,
780 bprintf("while executing task %s",
781 MSG_task_get_name(task)));
784 /***************************************************************************************
785 * Unsortable functions *
786 ***************************************************************************************/
788 JNIEXPORT jdouble JNICALL
789 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
791 return (jdouble) MSG_get_clock();
794 JNIEXPORT void JNICALL
795 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
803 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
806 argc = (int) (*env)->GetArrayLength(env, jargs);
809 argv = xbt_new(char *, argc + 1);
810 argv[0] = strdup("java");
812 for (index = 0; index < argc - 1; index++) {
813 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
814 tmp = (*env)->GetStringUTFChars(env, jval, 0);
815 argv[index + 1] = strdup(tmp);
816 (*env)->ReleaseStringUTFChars(env, jval, tmp);
820 MSG_global_init(&argc, argv);
822 for (index = 0; index < argc; index++)
827 (*env)->GetJavaVM(env, &__java_vm);
830 JNIEXPORT void JNICALL
831 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
839 XBT_INFO("Ready to run MSG_MAIN");
841 XBT_INFO("Done running MSG_MAIN");
842 jxbt_check_res("MSG_main()", rv, MSG_OK,
844 ("unexpected error : MSG_main() failed .. please report this bug "));
846 XBT_INFO("MSG_main finished");
848 XBT_INFO("Clean java world");
849 /* Cleanup java hosts */
850 hosts = MSG_hosts_as_dynar();
851 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
852 jhost = (jobject) xbt_dynar_get_as(hosts,index,m_host_t)->data;
854 jhost_unref(env, jhost);
857 xbt_dynar_free(&hosts);
858 XBT_INFO("Clean native world");
860 JNIEXPORT void JNICALL
861 JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
863 /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
864 MSG_error_t rv = MSG_OK != MSG_clean();
865 jxbt_check_res("MSG_clean()", rv, MSG_OK,
867 ("unexpected error : MSG_clean() failed .. please report this bug "));
870 JNIEXPORT jint JNICALL
871 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
874 return (jint) MSG_process_killall((int) jresetPID);
877 JNIEXPORT void JNICALL
878 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
879 jstring jplatformFile)
882 const char *platformFile =
883 (*env)->GetStringUTFChars(env, jplatformFile, 0);
885 MSG_create_environment(platformFile);
887 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
890 JNIEXPORT void JNICALL
891 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
895 m_process_t process = jprocess_to_native_process(jprocess, env);
898 jxbt_throw_notbound(env, "process", jprocess);
902 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
905 JNIEXPORT void JNICALL
906 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
908 const char *s = (*env)->GetStringUTFChars(env, js, 0);
910 (*env)->ReleaseStringUTFChars(env, js, s);
913 JNIEXPORT jobjectArray JNICALL
914 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
922 xbt_dynar_t table = MSG_hosts_as_dynar();
923 int count = xbt_dynar_length(table);
925 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
931 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
934 jxbt_throw_jni(env, "Hosts table allocation failed");
938 for (index = 0; index < count; index++) {
939 host = xbt_dynar_get_as(table,index,m_host_t);
940 jhost = (jobject) (MSG_host_get_data(host));
943 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
946 Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
947 /* FIXME: leak of jname ? */
950 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
952 xbt_dynar_free(&table);
956 JNIEXPORT void JNICALL
957 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
958 jstring jalias, jobject jtask,
963 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
965 m_task_t task = jtask_to_native_task(jtask, env);
969 (*env)->ReleaseStringUTFChars(env, jalias, alias);
970 jxbt_throw_notbound(env, "task", jtask);
974 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
975 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
976 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
978 (*env)->ReleaseStringUTFChars(env, jalias, alias);
980 jxbt_check_res("MSG_task_send_with_timeout()", rv,
981 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
982 bprintf("while sending task %s to mailbox %s",
983 MSG_task_get_name(task), alias));
986 static void msg_task_cancel_on_failed_dsend(void*t) {
988 JNIEnv *env =get_current_thread_env();
989 jobject jtask_global = MSG_task_get_data(task);
991 /* Destroy the global ref so that the JVM can free the stuff */
992 (*env)->DeleteGlobalRef(env, jtask_global);
993 MSG_task_set_data(task, NULL);
994 MSG_task_destroy(task);
997 JNIEXPORT void JNICALL
998 Java_org_simgrid_msg_MsgNative_taskDSend(JNIEnv * env, jclass cls,
999 jstring jalias, jobject jtask)
1002 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1004 m_task_t task = jtask_to_native_task(jtask, env);
1008 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1009 jxbt_throw_notbound(env, "task", jtask);
1013 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1014 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1015 MSG_task_dsend(task, alias, msg_task_cancel_on_failed_dsend);
1017 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1021 JNIEXPORT void JNICALL
1022 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1023 jstring jalias, jobject jtask,
1026 m_task_t task = jtask_to_native_task(jtask, env);
1031 jxbt_throw_notbound(env, "task", jtask);
1035 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1037 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1038 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1039 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1041 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1043 jxbt_check_res("MSG_task_send_bounded()", rv,
1044 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1046 ("while sending task %s to mailbox %s with max rate %f",
1047 MSG_task_get_name(task), alias, (double) jmaxRate));
1051 JNIEXPORT jobject JNICALL
1052 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1053 jstring jalias, jdouble jtimeout,
1057 m_task_t task = NULL;
1058 m_host_t host = NULL;
1059 jobject jtask_global, jtask_local;
1063 host = jhost_get_native(env, jhost);
1066 jxbt_throw_notbound(env, "host", jhost);
1071 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1073 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1074 jtask_global = MSG_task_get_data(task);
1076 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1077 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1078 (*env)->DeleteGlobalRef(env, jtask_global);
1079 MSG_task_set_data(task, NULL);
1081 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1083 jxbt_check_res("MSG_task_receive_ext()", rv,
1084 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1085 bprintf("while receiving from mailbox %s", alias));
1087 return (jobject) jtask_local;
1090 JNIEXPORT jboolean JNICALL
1091 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1098 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1100 rv = MSG_task_listen(alias);
1102 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1104 return (jboolean) rv;
1107 JNIEXPORT jint JNICALL
1108 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1115 m_host_t host = jhost_get_native(env, jhost);
1118 jxbt_throw_notbound(env, "host", jhost);
1121 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1123 rv = MSG_task_listen_from_host(alias, host);
1125 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1130 JNIEXPORT jint JNICALL
1131 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1136 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1138 rv = MSG_task_listen_from(alias);
1140 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1145 JNIEXPORT void JNICALL
1146 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1147 jstring jdeploymentFile)
1150 const char *deploymentFile =
1151 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1153 surf_parse_reset_callbacks();
1155 surfxml_add_callback(STag_surfxml_process_cb_list,
1156 japplication_handler_on_begin_process);
1158 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1159 japplication_handler_on_process_arg);
1161 surfxml_add_callback(STag_surfxml_prop_cb_list,
1162 japplication_handler_on_property);
1164 surfxml_add_callback(ETag_surfxml_process_cb_list,
1165 japplication_handler_on_end_process);
1167 surf_parse_open(deploymentFile);
1169 japplication_handler_on_start_document();
1172 jxbt_throw_jni(env, "surf_parse() failed");
1176 japplication_handler_on_end_document();
1178 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);