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 "msg/private.h"
11 #include "simix/private.h"
12 #include "simix/smx_context_java.h"
14 #include "jmsg_process.h"
15 #include "jmsg_host.h"
16 #include "jmsg_task.h"
17 #include "jmsg_application_handler.h"
18 #include "jxbt_utilities.h"
21 #include "msg/mailbox.h"
22 #include "surf/surfxml_parse.h"
24 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
26 static JavaVM *__java_vm = NULL;
28 static jobject native_to_java_process(m_process_t process);
30 JavaVM *get_java_VM(void)
35 JNIEnv *get_current_thread_env(void)
39 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
44 static jobject native_to_java_process(m_process_t process)
46 return ((smx_ctx_java_t)
47 (process->simdata->s_process->context))->jprocess;
51 * The MSG process connected functions implementation.
54 JNIEXPORT void JNICALL
55 Java_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 char alias[MAX_ALIAS_NAME + 1] = { 0 };
64 msg_mailbox_t mailbox;
67 ("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
68 env, cls, jprocess_arg, jhost);
69 /* get the name of the java process */
70 jname = jprocess_get_name(jprocess_arg, env);
75 ("Internal error: Process name cannot be NULL"));
79 /* allocate the data of the simulation */
80 process = xbt_new0(s_m_process_t, 1);
81 process->simdata = xbt_new0(s_simdata_process_t, 1);
83 /* create a global java process instance */
84 jprocess = jprocess_new_global_ref(jprocess_arg, env);
87 free(process->simdata);
89 jxbt_throw_jni(env, "Can't get a global ref to the java process");
93 /* bind the java process instance to the native process */
94 jprocess_bind(jprocess, process, env);
96 /* build the C name of the process */
97 name = (*env)->GetStringUTFChars(env, jname, 0);
98 process->name = xbt_strdup(name);
99 (*env)->ReleaseStringUTFChars(env, jname, name);
101 process->simdata->m_host = jhost_get_native(env, jhost);
104 if (!(process->simdata->m_host)) { /* not binded */
105 free(process->simdata);
108 jxbt_throw_notbound(env, "host", jhost);
111 process->simdata->PID = msg_global->PID++;
113 /* create a new context */
115 ("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
116 process->name, process->simdata->m_host->name,
117 process->simdata->PID, process, process->simdata,
118 process->simdata->m_host, process->simdata->m_host->simdata, env);
120 process->simdata->s_process =
121 SIMIX_process_create(process->name, (xbt_main_func_t) jprocess,
122 /*data */ (void *) process,
123 process->simdata->m_host->simdata->
124 smx_host->name, 0, NULL, NULL);
126 DEBUG1("context created (s_process=%p)", process->simdata->s_process);
129 if (SIMIX_process_self()) { /* someone created me */
130 process->simdata->PPID =
131 MSG_process_get_PID(SIMIX_process_self()->data);
133 process->simdata->PPID = -1;
136 process->simdata->last_errno = MSG_OK;
138 /* add the process to the list of the processes of the simulation */
139 xbt_fifo_unshift(msg_global->process_list, process);
141 sprintf(alias, "%s:%s",
142 (process->simdata->m_host->simdata->smx_host)->name,
145 mailbox = MSG_mailbox_new(alias);
149 JNIEXPORT void JNICALL
150 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
153 m_process_t process = jprocess_to_native_process(jprocess, env);
156 jxbt_throw_notbound(env, "process", jprocess);
160 /* try to suspend the process */
161 MSG_error_t rv = MSG_process_suspend(process);
163 jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
164 bprintf("unexpected error , please report this bug"));
168 JNIEXPORT void JNICALL
169 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
172 m_process_t process = jprocess_to_native_process(jprocess, env);
175 jxbt_throw_notbound(env, "process", jprocess);
179 /* try to resume the process */
180 MSG_error_t rv = MSG_process_resume(process);
182 jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
183 bprintf("unexpected error , please report this bug"));
186 JNIEXPORT jboolean JNICALL
187 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
190 m_process_t process = jprocess_to_native_process(jprocess, env);
193 jxbt_throw_notbound(env, "process", jprocess);
197 /* true is the process is suspended, false otherwise */
198 return (jboolean) MSG_process_is_suspended(process);
201 JNIEXPORT void JNICALL
202 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
205 /* get the native instances from the java ones */
206 m_process_t process = jprocess_to_native_process(jprocess, env);
209 jxbt_throw_notbound(env, "process", jprocess);
213 /* delete the global reference */
214 jprocess_delete_global_ref(native_to_java_process(process), env);
216 /* kill the native process (this wrapper is call by the destructor of the java
219 MSG_process_kill(process);
222 JNIEXPORT jobject JNICALL
223 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
226 /* get the native instances from the java ones */
227 m_process_t process = jprocess_to_native_process(jprocess, env);
231 jxbt_throw_notbound(env, "process", jprocess);
235 host = MSG_process_get_host(process);
237 if (!MSG_host_get_data(host)) {
238 jxbt_throw_jni(env, "MSG_process_get_host() failed");
242 /* return the global reference to the java host instance */
243 return (jobject) MSG_host_get_data(host);
247 JNIEXPORT jobject JNICALL
248 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
251 m_process_t process = MSG_process_from_PID(PID);
254 jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
258 if (!native_to_java_process(process)) {
259 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
263 return (jobject) (native_to_java_process(process));
267 JNIEXPORT jint JNICALL
268 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
271 m_process_t process = jprocess_to_native_process(jprocess, env);
274 jxbt_throw_notbound(env, "process", jprocess);
278 return (jint) MSG_process_get_PID(process);
282 JNIEXPORT jint JNICALL
283 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
286 m_process_t process = jprocess_to_native_process(jprocess, env);
289 jxbt_throw_notbound(env, "process", jprocess);
293 return (jint) MSG_process_get_PPID(process);
296 JNIEXPORT jobject JNICALL
297 Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
299 m_process_t process = MSG_process_self();
303 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
307 jprocess = native_to_java_process(process);
310 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
315 JNIEXPORT void JNICALL
316 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
319 m_host_t host = jhost_get_native(env, jhost);
322 jxbt_throw_notbound(env, "host", jhost);
326 /* try to change the host of the process */
327 MSG_error_t rv = MSG_process_change_host(host);
329 jxbt_check_res("MSG_process_change_host()", rv, MSG_OK,
330 bprintf("unexpected error , please report this bug"));
334 JNIEXPORT void JNICALL
335 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
338 MSG_error_t rv = MSG_process_sleep((double) seconds);
340 jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
341 bprintf("while process was waiting for %f seconds",
347 /***************************************************************************************
348 * The MSG host connected functions implementation. *
349 ***************************************************************************************/
351 JNIEXPORT jobject JNICALL
352 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
355 m_host_t host; /* native host */
356 jobject jhost; /* global reference to the java host instance returned */
358 /* get the C string from the java string */
359 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
361 /* get the host by name (the hosts are created during the grid resolution) */
362 host = MSG_get_host_by_name(name);
363 DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
365 (*env)->ReleaseStringUTFChars(env, jname, name);
367 if (!host) { /* invalid name */
368 jxbt_throw_host_not_found(env, name);
372 if (!MSG_host_get_data(host)) { /* native host not associated yet with java host */
374 /* instanciate a new java host */
375 jhost = jhost_new_instance(env);
378 jxbt_throw_jni(env, "java host instantiation failed");
382 /* get a global reference to the newly created host */
383 jhost = jhost_ref(env, jhost);
386 jxbt_throw_jni(env, "new global ref allocation failed");
390 /* bind the java host and the native host */
391 jhost_bind(jhost, host, env);
393 /* the native host data field is set with the global reference to the
394 * java host returned by this function
396 MSG_host_set_data(host, (void *) jhost);
399 /* return the global reference to the java host instance */
400 return (jobject) MSG_host_get_data(host);
403 JNIEXPORT jstring JNICALL
404 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
407 m_host_t host = jhost_get_native(env, jhost);
410 jxbt_throw_notbound(env, "host", jhost);
414 return (*env)->NewStringUTF(env, MSG_host_get_name(host));
417 JNIEXPORT jint JNICALL
418 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
420 return (jint) MSG_get_host_number();
423 JNIEXPORT jobject JNICALL
424 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
428 m_host_t host = MSG_host_self();
430 if (!MSG_host_get_data(host)) {
431 /* the native host not yet associated with the java host instance */
433 /* instanciate a new java host instance */
434 jhost = jhost_new_instance(env);
437 jxbt_throw_jni(env, "java host instantiation failed");
441 /* get a global reference to the newly created host */
442 jhost = jhost_ref(env, jhost);
445 jxbt_throw_jni(env, "global ref allocation failed");
449 /* Bind & store it */
450 jhost_bind(jhost, host, env);
451 MSG_host_set_data(host, (void *) jhost);
453 jhost = (jobject) MSG_host_get_data(host);
459 JNIEXPORT jdouble JNICALL
460 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
463 m_host_t host = jhost_get_native(env, jhost);
466 jxbt_throw_notbound(env, "host", jhost);
470 return (jdouble) MSG_get_host_speed(host);
473 JNIEXPORT jint JNICALL
474 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
477 m_host_t host = jhost_get_native(env, jhost);
480 jxbt_throw_notbound(env, "host", jhost);
484 return (jint) MSG_get_host_msgload(host);
488 JNIEXPORT jboolean JNICALL
489 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
492 m_host_t host = jhost_get_native(env, jhost);
495 jxbt_throw_notbound(env, "host", jhost);
499 return (jboolean) MSG_host_is_avail(host);
503 /***************************************************************************************
504 * The MSG task connected functions implementation. *
505 ***************************************************************************************/
507 JNIEXPORT void JNICALL
508 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
509 jobject jtask, jstring jname,
510 jdouble jcomputeDuration,
511 jdouble jmessageSize)
513 m_task_t task; /* the native task to create */
514 const char *name = NULL; /* the name of the task */
516 if (jcomputeDuration < 0) {
517 jxbt_throw_illegal(env,
519 ("Task ComputeDuration (%f) cannot be negative",
520 (double) jcomputeDuration));
524 if (jmessageSize < 0) {
525 jxbt_throw_illegal(env,
526 bprintf("Task MessageSize (%f) cannot be negative",
527 (double) jmessageSize));
532 /* get the C string from the java string */
533 name = (*env)->GetStringUTFChars(env, jname, 0);
537 /* create the task */
539 MSG_task_create(name, (double) jcomputeDuration,
540 (double) jmessageSize, NULL);
543 (*env)->ReleaseStringUTFChars(env, jname, name);
545 /* bind & store the task */
546 jtask_bind(jtask, task, env);
547 MSG_task_set_data(task, jtask);
550 JNIEXPORT void JNICALL
551 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
556 jcomputeDurations_arg,
561 m_task_t task; /* the native parallel task to create */
562 const char *name; /* the name of the task */
565 double *computeDurations;
566 double *messageSizes;
567 jdouble *jcomputeDurations;
568 jdouble *jmessageSizes;
574 if (!jcomputeDurations_arg) {
577 ("Parallel task compute durations cannot be null"));
581 if (!jmessageSizes_arg) {
584 ("Parallel task message sizes cannot be null"));
589 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
593 host_count = (int) (*env)->GetArrayLength(env, jhosts);
596 hosts = xbt_new0(m_host_t, host_count);
597 computeDurations = xbt_new0(double, host_count);
598 messageSizes = xbt_new0(double, host_count * host_count);
601 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
603 (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
605 for (index = 0; index < host_count; index++) {
606 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
607 hosts[index] = jhost_get_native(env, jhost);
608 computeDurations[index] = jcomputeDurations[index];
610 for (index = 0; index < host_count * host_count; index++) {
611 messageSizes[index] = jmessageSizes[index];
614 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
615 jcomputeDurations, 0);
616 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
620 /* get the C string from the java string */
621 name = (*env)->GetStringUTFChars(env, jname, 0);
624 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
627 (*env)->ReleaseStringUTFChars(env, jname, name);
629 /* associate the java task object and the native task */
630 jtask_bind(jtask, task, env);
632 MSG_task_set_data(task, (void *) jtask);
634 if (!MSG_task_get_data(task))
635 jxbt_throw_jni(env, "global ref allocation failed");
638 JNIEXPORT jobject JNICALL
639 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
644 m_task_t task = jtask_to_native_task(jtask, env);
647 jxbt_throw_notbound(env, "task", jtask);
651 process = MSG_task_get_sender(task);
652 return (jobject) native_to_java_process(process);
655 JNIEXPORT jobject JNICALL
656 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
660 m_task_t task = jtask_to_native_task(jtask, env);
663 jxbt_throw_notbound(env, "task", jtask);
667 host = MSG_task_get_source(task);
669 if (!MSG_host_get_data(host)) {
670 jxbt_throw_jni(env, "MSG_task_get_source() failed");
674 return (jobject) MSG_host_get_data(host);
678 JNIEXPORT jstring JNICALL
679 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
682 m_task_t task = jtask_to_native_task(jtask, env);
685 jxbt_throw_notbound(env, "task", jtask);
689 return (*env)->NewStringUTF(env, MSG_task_get_name(task));
692 JNIEXPORT void JNICALL
693 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
696 m_task_t ptask = jtask_to_native_task(jtask, env);
699 jxbt_throw_notbound(env, "task", jtask);
703 MSG_error_t rv = MSG_task_cancel(ptask);
705 jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
706 bprintf("unexpected error , please report this bug"));
709 JNIEXPORT jdouble JNICALL
710 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
713 m_task_t ptask = jtask_to_native_task(jtask, env);
716 jxbt_throw_notbound(env, "task", jtask);
719 return (jdouble) MSG_task_get_compute_duration(ptask);
722 JNIEXPORT jdouble JNICALL
723 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
727 m_task_t ptask = jtask_to_native_task(jtask, env);
730 jxbt_throw_notbound(env, "task", jtask);
733 return (jdouble) MSG_task_get_remaining_computation(ptask);
736 JNIEXPORT void JNICALL
737 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
738 jobject jtask, jdouble priority)
740 m_task_t task = jtask_to_native_task(jtask, env);
743 jxbt_throw_notbound(env, "task", jtask);
746 MSG_task_set_priority(task, (double) priority);
749 JNIEXPORT void JNICALL
750 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
754 /* get the native task */
755 m_task_t task = jtask_to_native_task(jtask_arg, env);
759 jxbt_throw_notbound(env, "task", task);
762 jtask = (jobject) MSG_task_get_data(task);
764 MSG_error_t rv = MSG_task_destroy(task);
766 jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
767 bprintf("unexpected error , please report this bug"));
770 JNIEXPORT void JNICALL
771 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
774 m_task_t task = jtask_to_native_task(jtask, env);
777 jxbt_throw_notbound(env, "task", jtask);
781 MSG_error_t rv = MSG_task_execute(task);
783 jxbt_check_res("MSG_task_execute()", rv,
784 MSG_HOST_FAILURE | MSG_TASK_CANCELLED,
785 bprintf("while executing task %s",
786 MSG_task_get_name(task)));
789 /***************************************************************************************
790 * Unsortable functions *
791 ***************************************************************************************/
794 JNIEXPORT jint JNICALL
795 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
797 return (jint) MSG_get_errno();
800 JNIEXPORT jdouble JNICALL
801 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
803 return (jdouble) MSG_get_clock();
807 JNIEXPORT void JNICALL
808 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
816 factory_initializer_to_use = SIMIX_ctx_java_factory_init;
819 argc = (int) (*env)->GetArrayLength(env, jargs);
822 argv = xbt_new0(char *, argc);
823 argv[0] = strdup("java");
825 for (index = 0; index < argc - 1; index++) {
826 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
827 tmp = (*env)->GetStringUTFChars(env, jval, 0);
828 argv[index + 1] = strdup(tmp);
829 (*env)->ReleaseStringUTFChars(env, jval, tmp);
832 MSG_global_init(&argc, argv);
834 for (index = 0; index < argc; index++)
839 (*env)->GetJavaVM(env, &__java_vm);
842 JNIEXPORT void JNICALL
843 JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
846 int index; //xbt_fifo_item_t item = NULL;
852 jxbt_check_res("MSG_main()", rv, MSG_OK,
854 ("unexpected error : MSG_main() failed .. please report this bug "));
857 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
859 DEBUG0("Clean java world");
860 /* Cleanup java hosts */
861 hosts = MSG_get_host_table();
862 for (index = 0; index < MSG_get_host_number() - 1; index++) {
863 jhost = (jobject) hosts[index]->data;
865 jhost_unref(env, jhost);
869 DEBUG0("Clean native world");
870 /* cleanup native stuff */
871 rv = MSG_OK != MSG_clean();
872 jxbt_check_res("MSG_clean()", rv, MSG_OK,
874 ("unexpected error : MSG_clean() failed .. please report this bug "));
878 JNIEXPORT jint JNICALL
879 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
882 return (jint) MSG_process_killall((int) jresetPID);
885 JNIEXPORT void JNICALL
886 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
887 jstring jplatformFile)
890 const char *platformFile =
891 (*env)->GetStringUTFChars(env, jplatformFile, 0);
893 MSG_create_environment(platformFile);
895 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
898 JNIEXPORT void JNICALL
899 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
903 m_process_t process = jprocess_to_native_process(jprocess, env);
906 jxbt_throw_notbound(env, "process", jprocess);
910 SIMIX_context_stop(SIMIX_process_self()->context);
913 JNIEXPORT void JNICALL
914 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
916 const char *s = (*env)->GetStringUTFChars(env, js, 0);
918 (*env)->ReleaseStringUTFChars(env, js, s);
921 JNIEXPORT jobjectArray JNICALL
922 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
930 int count = MSG_get_host_number();
931 m_host_t *table = MSG_get_host_table();
933 jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
939 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
942 jxbt_throw_jni(env, "Hosts table allocation failed");
946 for (index = 0; index < count; index++) {
948 jhost = (jobject) (MSG_host_get_data(host));
951 jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
954 Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
955 /* FIXME: leak of jname ? */
958 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
964 JNIEXPORT void JNICALL
965 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
966 jstring jalias, jobject jtask,
971 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
973 m_task_t task = jtask_to_native_task(jtask, env);
977 (*env)->ReleaseStringUTFChars(env, jalias, alias);
978 jxbt_throw_notbound(env, "task", jtask);
982 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
983 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
984 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
986 (*env)->ReleaseStringUTFChars(env, jalias, alias);
988 jxbt_check_res("MSG_task_send_with_timeout()", rv,
989 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
990 bprintf("while sending task %s to mailbox %s",
991 MSG_task_get_name(task), alias));
994 JNIEXPORT void JNICALL
995 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
996 jstring jalias, jobject jtask,
999 m_task_t task = jtask_to_native_task(jtask, env);
1004 jxbt_throw_notbound(env, "task", jtask);
1008 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1010 /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1011 MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1012 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1014 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1016 jxbt_check_res("MSG_task_send_bounded()", rv,
1017 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1019 ("while sending task %s to mailbox %s with max rate %f",
1020 MSG_task_get_name(task), alias, (double) jmaxRate));
1024 JNIEXPORT jobject JNICALL
1025 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1026 jstring jalias, jdouble jtimeout,
1030 m_task_t task = NULL;
1031 m_host_t host = NULL;
1032 jobject jtask_global, jtask_local;
1036 host = jhost_get_native(env, jhost);
1039 jxbt_throw_notbound(env, "host", jhost);
1044 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1046 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1047 jtask_global = MSG_task_get_data(task);
1049 /* Convert the global ref into a local ref so that the JVM can free the stuff */
1050 jtask_local = (*env)->NewLocalRef(env, jtask_global);
1051 (*env)->DeleteGlobalRef(env, jtask_global);
1052 MSG_task_set_data(task, NULL);
1054 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1056 jxbt_check_res("MSG_task_receive_ext()", rv,
1057 MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1058 bprintf("while receiving from mailbox %s", alias));
1060 return (jobject) jtask_local;
1063 JNIEXPORT jboolean JNICALL
1064 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1071 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1073 rv = MSG_task_listen(alias);
1075 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1077 return (jboolean) rv;
1080 JNIEXPORT jint JNICALL
1081 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1088 m_host_t host = jhost_get_native(env, jhost);
1091 jxbt_throw_notbound(env, "host", jhost);
1094 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1096 rv = MSG_task_listen_from_host(alias, host);
1098 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1103 JNIEXPORT jint JNICALL
1104 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1109 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1111 rv = MSG_task_listen_from(alias);
1113 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1118 JNIEXPORT void JNICALL
1119 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1120 jstring jdeploymentFile)
1123 const char *deploymentFile =
1124 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1126 surf_parse_reset_parser();
1128 surfxml_add_callback(STag_surfxml_process_cb_list,
1129 japplication_handler_on_begin_process);
1131 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1132 japplication_handler_on_process_arg);
1134 surfxml_add_callback(STag_surfxml_prop_cb_list,
1135 japplication_handler_on_property);
1137 surfxml_add_callback(ETag_surfxml_process_cb_list,
1138 japplication_handler_on_end_process);
1140 surf_parse_open(deploymentFile);
1142 japplication_handler_on_start_document();
1145 jxbt_throw_jni(env, "surf_parse() failed");
1149 japplication_handler_on_end_document();
1151 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);