1 /* Java Wrappers to the MSG API. */
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
13 #include "smx_context_java.h"
15 #include "jmsg_process.h"
16 #include "jmsg_host.h"
17 #include "jmsg_task.h"
18 #include "jmsg_application_handler.h"
19 #include "jxbt_utilities.h"
23 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
30 /* end of eclipse-mandated pimple */
32 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
34 static JavaVM *__java_vm = NULL;
36 static jobject native_to_java_process(m_process_t process);
38 JavaVM *get_java_VM(void)
43 JNIEnv *get_current_thread_env(void)
47 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
52 static jobject native_to_java_process(m_process_t process)
54 return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
58 * The MSG process connected functions implementation.
61 JNIEXPORT void JNICALL
62 Java_org_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
68 jobject jprocess; /* the global reference to the java process instance */
69 jstring jname; /* the name of the java process instance */
70 const char *name; /* the C name of the process */
72 m_process_t process; /* the native process to create */
73 m_host_t host; /* Where that process lives */
75 hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
77 XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,host=%s)",
78 env, cls, jprocess_arg, hostname);
81 /* get the name of the java process */
82 jname = jprocess_get_name(jprocess_arg, env);
85 xbt_strdup("Internal error: Process name cannot be NULL"));
89 /* bind/retrieve the msg host */
90 host = MSG_get_host_by_name(hostname);
92 if (!(host)) { /* not binded */
93 jxbt_throw_host_not_found(env, hostname);
97 /* create a global java process instance */
98 jprocess = jprocess_new_global_ref(jprocess_arg, env);
100 jxbt_throw_jni(env, "Can't get a global ref to the java process");
104 /* build the C name of the process */
105 name = (*env)->GetStringUTFChars(env, jname, 0);
106 name = xbt_strdup(name);
108 /* Actually build the MSG process */
109 process = MSG_process_create_with_environment(name,
110 (xbt_main_func_t) jprocess,
114 /*argc, argv, properties*/
117 MSG_process_set_data(process,&process);
119 /* release our reference to the process name (variable name becomes invalid) */
120 //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
121 //(*env)->ReleaseStringUTFChars(env, jname, name);
122 (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
124 /* bind the java process instance to the native process */
125 jprocess_bind(jprocess, process, env);
129 JNIEXPORT void JNICALL
130 Java_org_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
133 m_process_t process = jprocess_to_native_process(jprocess, env);
136 jxbt_throw_notbound(env, "process", jprocess);
140 /* try to suspend the process */
141 MSG_error_t rv = MSG_process_suspend(process);
143 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
144 bprintf("unexpected error , please report this bug"));
148 JNIEXPORT void JNICALL
149 Java_org_simgrid_msg_Process_simulatedSleep(JNIEnv * env, jobject jprocess,
151 m_process_t process = jprocess_to_native_process(jprocess, env);
154 jxbt_throw_notbound(env, "process", jprocess);
157 MSG_error_t rv = MSG_process_sleep((double)jseconds);
159 jxbt_check_res("MSG_process_sleep()", rv, MSG_OK,
160 bprintf("unexpected error , please report this bug"));
164 JNIEXPORT void JNICALL
165 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
168 m_process_t process = jprocess_to_native_process(jprocess, env);
171 jxbt_throw_notbound(env, "process", jprocess);
175 /* try to resume the process */
176 MSG_error_t rv = MSG_process_resume(process);
178 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
179 bprintf("unexpected error , please report this bug"));
182 JNIEXPORT jboolean JNICALL
183 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
186 m_process_t process = jprocess_to_native_process(jprocess, env);
189 jxbt_throw_notbound(env, "process", jprocess);
193 /* true is the process is suspended, false otherwise */
194 return (jboolean) MSG_process_is_suspended(process);
197 JNIEXPORT void JNICALL
198 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
201 /* get the native instances from the java ones */
202 m_process_t process = jprocess_to_native_process(jprocess, env);
205 jxbt_throw_notbound(env, "process", jprocess);
209 /* kill the native process (this wrapper is call by the destructor of the java
212 MSG_process_kill(process);
215 JNIEXPORT jobject JNICALL
216 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
219 /* get the native instances from the java ones */
220 m_process_t process = jprocess_to_native_process(jprocess, env);
224 jxbt_throw_notbound(env, "process", jprocess);
228 host = MSG_process_get_host(process);
229 jobject res = (jobject)MSG_host_get_data(host);
232 XBT_INFO("Binding error for host %s ",MSG_host_get_name(host));
233 jxbt_throw_jni(env, bprintf("Binding error for host %s ",MSG_host_get_name(host)));
237 /* return the global reference to the java host instance */
242 JNIEXPORT jobject JNICALL
243 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
246 m_process_t process = MSG_process_from_PID(PID);
249 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
253 if (!native_to_java_process(process)) {
254 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
258 return (jobject) (native_to_java_process(process));
262 JNIEXPORT jint JNICALL
263 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
266 m_process_t process = jprocess_to_native_process(jprocess, env);
269 jxbt_throw_notbound(env, "process", jprocess);
273 return (jint) MSG_process_get_PID(process);
277 JNIEXPORT jint JNICALL
278 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
281 m_process_t process = jprocess_to_native_process(jprocess, env);
284 jxbt_throw_notbound(env, "process", jprocess);
288 return (jint) MSG_process_get_PPID(process);
291 JNIEXPORT jobject JNICALL
292 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
294 m_process_t process = MSG_process_self();
298 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
302 jprocess = native_to_java_process(process);
305 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
310 JNIEXPORT void JNICALL
311 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
312 jobject jprocess, jobject jhost)
314 m_process_t process = jprocess_to_native_process(jprocess, env);
317 jxbt_throw_notbound(env, "process", jprocess);
321 m_host_t host = jhost_get_native(env, jhost);
324 jxbt_throw_notbound(env, "host", jhost);
328 /* try to change the host of the process */
329 MSG_error_t rv = MSG_process_migrate(process, host);
330 jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
331 bprintf("unexpected error , please report this bug"));
335 JNIEXPORT void JNICALL
336 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
339 MSG_error_t rv = MSG_process_sleep((double) seconds);
341 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
342 bprintf("while process was waiting for %f seconds",
348 /***************************************************************************************
349 * The MSG host connected functions implementation. *
350 ***************************************************************************************/
352 JNIEXPORT jobject JNICALL
353 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
356 m_host_t host; /* native host */
357 jobject jhost; /* global reference to the java host instance returned */
359 /* get the C string from the java string */
360 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
361 XBT_DEBUG("Looking for host '%s'",name);
362 /* get the host by name (the hosts are created during the grid resolution) */
363 host = MSG_get_host_by_name(name);
364 XBT_DEBUG("MSG gave %p as native host", host);
366 if (!host) { /* invalid name */
367 jxbt_throw_host_not_found(env, name);
368 (*env)->ReleaseStringUTFChars(env, jname, name);
371 (*env)->ReleaseStringUTFChars(env, jname, name);
373 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
375 /* Instantiate a new java host */
376 jhost = jhost_new_instance(env);
379 jxbt_throw_jni(env, "java host instantiation failed");
383 /* get a global reference to the newly created host */
384 jhost = jhost_ref(env, jhost);
387 jxbt_throw_jni(env, "new global ref allocation failed");
391 /* bind the java host and the native host */
392 jhost_bind(jhost, host, env);
394 /* the native host data field is set with the global reference to the
395 * java host returned by this function
397 MSG_host_set_data(host, (void *) jhost);
400 /* return the global reference to the java host instance */
401 return (jobject) MSG_host_get_data(host);
404 JNIEXPORT jstring JNICALL
405 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
408 m_host_t host = jhost_get_native(env, jhost);
412 jxbt_throw_notbound(env, "host", jhost);
416 name = MSG_host_get_name(host);
418 xbt_die("This host has no name...");
420 return (*env)->NewStringUTF(env, name);
423 JNIEXPORT jint JNICALL
424 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
426 xbt_dynar_t hosts = MSG_hosts_as_dynar();
427 int nb_host = xbt_dynar_length(hosts);
428 xbt_dynar_free(&hosts);
429 return (jint) nb_host;
432 JNIEXPORT jobject JNICALL
433 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
437 m_host_t host = MSG_host_self();
439 if (!MSG_host_get_data(host)) {
440 /* the native host not yet associated with the java host instance */
442 /* instanciate a new java host instance */
443 jhost = jhost_new_instance(env);
446 jxbt_throw_jni(env, "java host instantiation failed");
450 /* get a global reference to the newly created host */
451 jhost = jhost_ref(env, jhost);
454 jxbt_throw_jni(env, "global ref allocation failed");
458 /* Bind & store it */
459 jhost_bind(jhost, host, env);
460 MSG_host_set_data(host, (void *) jhost);
462 jhost = (jobject) MSG_host_get_data(host);
468 JNIEXPORT jdouble JNICALL
469 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
472 m_host_t host = jhost_get_native(env, jhost);
475 jxbt_throw_notbound(env, "host", jhost);
479 return (jdouble) MSG_get_host_speed(host);
482 JNIEXPORT jint JNICALL
483 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
486 m_host_t host = jhost_get_native(env, jhost);
489 jxbt_throw_notbound(env, "host", jhost);
493 return (jint) MSG_get_host_msgload(host);
497 JNIEXPORT jboolean JNICALL
498 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
501 m_host_t host = jhost_get_native(env, jhost);
504 jxbt_throw_notbound(env, "host", jhost);
508 return (jboolean) MSG_host_is_avail(host);
512 /***************************************************************************************
513 * The MSG task connected functions implementation. *
514 ***************************************************************************************/
516 JNIEXPORT void JNICALL
517 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
518 jobject jtask, jstring jname,
519 jdouble jcomputeDuration,
520 jdouble jmessageSize)
522 m_task_t task; /* the native task to create */
523 const char *name = NULL; /* the name of the task */
525 if (jcomputeDuration < 0) {
526 jxbt_throw_illegal(env,
528 ("Task ComputeDuration (%f) cannot be negative",
529 (double) jcomputeDuration));
533 if (jmessageSize < 0) {
534 jxbt_throw_illegal(env,
535 bprintf("Task MessageSize (%f) cannot be negative",
536 (double) jmessageSize));
541 /* get the C string from the java string */
542 name = (*env)->GetStringUTFChars(env, jname, 0);
546 /* create the task */
548 MSG_task_create(name, (double) jcomputeDuration,
549 (double) jmessageSize, NULL);
551 (*env)->ReleaseStringUTFChars(env, jname, name);
553 /* bind & store the task */
554 jtask_bind(jtask, task, env);
555 MSG_task_set_data(task, jtask);
558 JNIEXPORT void JNICALL
559 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
564 jcomputeDurations_arg,
569 m_task_t task; /* the native parallel task to create */
570 const char *name; /* the name of the task */
573 double *computeDurations;
574 double *messageSizes;
575 jdouble *jcomputeDurations;
576 jdouble *jmessageSizes;
582 if (!jcomputeDurations_arg) {
585 ("Parallel task compute durations cannot be null"));
589 if (!jmessageSizes_arg) {
592 ("Parallel task message sizes cannot be null"));
597 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
601 host_count = (int) (*env)->GetArrayLength(env, jhosts);
604 hosts = xbt_new0(m_host_t, host_count);
605 computeDurations = xbt_new0(double, host_count);
606 messageSizes = xbt_new0(double, host_count * host_count);
609 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
611 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
613 for (index = 0; index < host_count; index++) {
614 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
615 hosts[index] = jhost_get_native(env, jhost);
616 computeDurations[index] = jcomputeDurations[index];
618 for (index = 0; index < host_count * host_count; index++) {
619 messageSizes[index] = jmessageSizes[index];
622 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
623 jcomputeDurations, 0);
624 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
628 /* get the C string from the java string */
629 name = (*env)->GetStringUTFChars(env, jname, 0);
632 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
635 (*env)->ReleaseStringUTFChars(env, jname, name);
637 /* associate the java task object and the native task */
638 jtask_bind(jtask, task, env);
640 MSG_task_set_data(task, (void *) jtask);
642 if (!MSG_task_get_data(task))
643 jxbt_throw_jni(env, "global ref allocation failed");
646 JNIEXPORT jobject JNICALL
647 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
652 m_task_t task = jtask_to_native_task(jtask, env);
655 jxbt_throw_notbound(env, "task", jtask);
659 process = MSG_task_get_sender(task);
660 return (jobject) native_to_java_process(process);
663 JNIEXPORT jobject JNICALL
664 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
668 m_task_t task = jtask_to_native_task(jtask, env);
671 jxbt_throw_notbound(env, "task", jtask);
675 host = MSG_task_get_source(task);
677 if (!MSG_host_get_data(host)) {
678 jxbt_throw_jni(env, "MSG_task_get_source() failed");
682 return (jobject) MSG_host_get_data(host);
686 JNIEXPORT jstring JNICALL
687 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
690 m_task_t task = jtask_to_native_task(jtask, env);
693 jxbt_throw_notbound(env, "task", jtask);
697 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
700 JNIEXPORT void JNICALL
701 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
704 m_task_t ptask = jtask_to_native_task(jtask, env);
707 jxbt_throw_notbound(env, "task", jtask);
711 MSG_error_t rv = MSG_task_cancel(ptask);
713 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
714 bprintf("unexpected error , please report this bug"));
717 JNIEXPORT jdouble JNICALL
718 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
721 m_task_t ptask = jtask_to_native_task(jtask, env);
724 jxbt_throw_notbound(env, "task", jtask);
727 return (jdouble) MSG_task_get_compute_duration(ptask);
730 JNIEXPORT jdouble JNICALL
731 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
735 m_task_t ptask = jtask_to_native_task(jtask, env);
738 jxbt_throw_notbound(env, "task", jtask);
741 return (jdouble) MSG_task_get_remaining_computation(ptask);
744 JNIEXPORT void JNICALL
745 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
746 jobject jtask, jdouble priority)
748 m_task_t task = jtask_to_native_task(jtask, env);
751 jxbt_throw_notbound(env, "task", jtask);
754 MSG_task_set_priority(task, (double) priority);
757 JNIEXPORT void JNICALL
758 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
762 /* get the native task */
763 m_task_t task = jtask_to_native_task(jtask_arg, env);
766 jxbt_throw_notbound(env, "task", task);
770 MSG_error_t rv = MSG_task_destroy(task);
772 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
773 bprintf("unexpected error , please report this bug"));
776 JNIEXPORT void JNICALL
777 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
780 m_task_t task = jtask_to_native_task(jtask, env);
783 jxbt_throw_notbound(env, "task", jtask);
787 MSG_error_t rv = MSG_task_execute(task);
789 jxbt_check_res("MSG_task_execute()", rv,
790 MSG_HOST_FAILURE | MSG_TASK_CANCELED,
791 bprintf("while executing task %s",
792 MSG_task_get_name(task)));
795 /***************************************************************************************
796 * Unsortable functions *
797 ***************************************************************************************/
799 JNIEXPORT jdouble JNICALL
800 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
802 return (jdouble) MSG_get_clock();
805 JNIEXPORT void JNICALL
806 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
814 smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
817 argc = (int) (*env)->GetArrayLength(env, jargs);
820 argv = xbt_new(char *, argc + 1);
821 argv[0] = strdup("java");
823 for (index = 0; index < argc - 1; index++) {
824 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
825 tmp = (*env)->GetStringUTFChars(env, jval, 0);
826 argv[index + 1] = strdup(tmp);
827 (*env)->ReleaseStringUTFChars(env, jval, tmp);
831 MSG_global_init(&argc, argv);
833 for (index = 0; index < argc; index++)
838 (*env)->GetJavaVM(env, &__java_vm);
841 JNIEXPORT void JNICALL
842 JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
850 XBT_INFO("Ready to run MSG_MAIN");
852 XBT_INFO("Done running MSG_MAIN");
853 jxbt_check_res("MSG_main()", rv, MSG_OK,
855 ("unexpected error : MSG_main() failed .. please report this bug "));
857 XBT_INFO("MSG_main finished");
859 XBT_INFO("Clean java world");
860 /* Cleanup java hosts */
861 hosts = MSG_hosts_as_dynar();
862 for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
863 jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
865 jhost_unref(env, jhost);
868 xbt_dynar_free(&hosts);
869 XBT_INFO("Clean native world");
871 JNIEXPORT void JNICALL
872 JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
874 /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
875 MSG_error_t rv = MSG_OK != MSG_clean();
876 jxbt_check_res("MSG_clean()", rv, MSG_OK,
878 ("unexpected error : MSG_clean() failed .. please report this bug "));
881 JNIEXPORT jint JNICALL
882 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
885 return (jint) MSG_process_killall((int) jresetPID);
888 JNIEXPORT void JNICALL
889 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
890 jstring jplatformFile)
893 const char *platformFile =
894 (*env)->GetStringUTFChars(env, jplatformFile, 0);
896 MSG_create_environment(platformFile);
898 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
901 JNIEXPORT void JNICALL
902 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
906 m_process_t process = jprocess_to_native_process(jprocess, env);
909 jxbt_throw_notbound(env, "process", jprocess);
913 smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
916 JNIEXPORT void JNICALL
917 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
919 const char *s = (*env)->GetStringUTFChars(env, js, 0);
921 (*env)->ReleaseStringUTFChars(env, js, s);
924 JNIEXPORT jobjectArray JNICALL
925 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
933 xbt_dynar_t table = MSG_hosts_as_dynar();
934 int count = xbt_dynar_length(table);
936 jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
942 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
945 jxbt_throw_jni(env, "Hosts table allocation failed");
949 for (index = 0; index < count; index++) {
950 host = xbt_dynar_get_as(table,index,m_host_t);
951 jhost = (jobject) (MSG_host_get_data(host));
954 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
957 Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
958 /* FIXME: leak of jname ? */
961 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
963 xbt_dynar_free(&table);
967 JNIEXPORT void JNICALL
968 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
969 jstring jalias, jobject jtask,
974 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
976 m_task_t task = jtask_to_native_task(jtask, env);
980 (*env)->ReleaseStringUTFChars(env, jalias, alias);
981 jxbt_throw_notbound(env, "task", jtask);
985 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
986 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
987 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
989 (*env)->ReleaseStringUTFChars(env, jalias, alias);
991 jxbt_check_res("MSG_task_send_with_timeout()", rv,
992 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
993 bprintf("while sending task %s to mailbox %s",
994 MSG_task_get_name(task), alias));
997 static void msg_task_cancel_on_failed_dsend(void*t) {
999 JNIEnv *env =get_current_thread_env();
1000 jobject jtask_global = MSG_task_get_data(task);
1002 /* Destroy the global ref so that the JVM can free the stuff */
1003 (*env)->DeleteGlobalRef(env, jtask_global);
1004 MSG_task_set_data(task, NULL);
1005 MSG_task_destroy(task);
1008 JNIEXPORT void JNICALL
1009 Java_org_simgrid_msg_MsgNative_taskDSend(JNIEnv * env, jclass cls,
1010 jstring jalias, jobject jtask)
1013 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1015 m_task_t task = jtask_to_native_task(jtask, env);
1019 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1020 jxbt_throw_notbound(env, "task", jtask);
1024 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1025 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1026 MSG_task_dsend(task, alias, msg_task_cancel_on_failed_dsend);
1028 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1032 JNIEXPORT void JNICALL
1033 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1034 jstring jalias, jobject jtask,
1037 m_task_t task = jtask_to_native_task(jtask, env);
1042 jxbt_throw_notbound(env, "task", jtask);
1046 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1048 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1049 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1050 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1052 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1054 jxbt_check_res("MSG_task_send_bounded()", rv,
1055 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1057 ("while sending task %s to mailbox %s with max rate %f",
1058 MSG_task_get_name(task), alias, (double) jmaxRate));
1062 JNIEXPORT jobject JNICALL
1063 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1064 jstring jalias, jdouble jtimeout,
1068 m_task_t task = NULL;
1069 m_host_t host = NULL;
1070 jobject jtask_global, jtask_local;
1074 host = jhost_get_native(env, jhost);
1077 jxbt_throw_notbound(env, "host", jhost);
1082 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1084 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1088 jxbt_throw_time_out_failure(env,NULL);
1090 case MSG_TRANSFER_FAILURE:
1091 jxbt_throw_transfer_failure(env,NULL);
1093 case MSG_HOST_FAILURE:
1094 jxbt_throw_host_failure(env,NULL);
1097 jxbt_throw_native(env,bprintf("receive failed"));
1101 jtask_global = MSG_task_get_data(task);
1103 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1104 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1105 (*env)->DeleteGlobalRef(env, jtask_global);
1106 MSG_task_set_data(task, NULL);
1108 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1110 jxbt_check_res("MSG_task_receive_ext()", rv,
1111 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1112 bprintf("while receiving from mailbox %s", alias));
1114 return (jobject) jtask_local;
1117 JNIEXPORT jboolean JNICALL
1118 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1125 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1127 rv = MSG_task_listen(alias);
1129 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1131 return (jboolean) rv;
1134 JNIEXPORT jint JNICALL
1135 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1142 m_host_t host = jhost_get_native(env, jhost);
1145 jxbt_throw_notbound(env, "host", jhost);
1148 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1150 rv = MSG_task_listen_from_host(alias, host);
1152 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1157 JNIEXPORT jint JNICALL
1158 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1163 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1165 rv = MSG_task_listen_from(alias);
1167 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1172 JNIEXPORT void JNICALL
1173 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1174 jstring jdeploymentFile)
1177 const char *deploymentFile =
1178 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1180 surf_parse_reset_callbacks();
1182 surfxml_add_callback(STag_surfxml_process_cb_list,
1183 japplication_handler_on_begin_process);
1185 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1186 japplication_handler_on_process_arg);
1188 surfxml_add_callback(STag_surfxml_prop_cb_list,
1189 japplication_handler_on_property);
1191 surfxml_add_callback(ETag_surfxml_process_cb_list,
1192 japplication_handler_on_end_process);
1194 surf_parse_open(deploymentFile);
1196 japplication_handler_on_start_document();
1199 jxbt_throw_jni(env, "surf_parse() failed");
1203 japplication_handler_on_end_document();
1205 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);