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,
56 jobject jprocess_arg, jobject jhost)
58 jobject jprocess; /* the global reference to the java process instance */
59 jstring jname; /* the name of the java process instance */
60 const char *name; /* the C name of the process */
61 m_process_t process; /* the native process to create */
62 char alias[MAX_ALIAS_NAME + 1] = { 0 };
63 msg_mailbox_t mailbox;
66 ("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
67 env, cls, jprocess_arg, jhost);
68 /* get the name of the java process */
69 jname = jprocess_get_name(jprocess_arg, env);
74 ("Internal error: Process name cannot be NULL"));
78 /* allocate the data of the simulation */
79 process = xbt_new0(s_m_process_t, 1);
80 process->simdata = xbt_new0(s_simdata_process_t, 1);
82 /* create a global java process instance */
83 jprocess = jprocess_new_global_ref(jprocess_arg, env);
86 free(process->simdata);
88 jxbt_throw_jni(env, "Can't get a global ref to the java process");
92 /* bind the java process instance to the native process */
93 jprocess_bind(jprocess, process, env);
95 /* build the C name of the process */
96 name = (*env)->GetStringUTFChars(env, jname, 0);
97 process->name = xbt_strdup(name);
98 (*env)->ReleaseStringUTFChars(env, jname, name);
100 process->simdata->m_host = jhost_get_native(env, jhost);
103 if (!(process->simdata->m_host)) { /* not binded */
104 free(process->simdata);
107 jxbt_throw_notbound(env, "host", jhost);
110 process->simdata->PID = msg_global->PID++;
112 /* create a new context */
114 ("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
115 process->name, process->simdata->m_host->name, process->simdata->PID,
116 process, process->simdata, process->simdata->m_host,
117 process->simdata->m_host->simdata, env);
119 process->simdata->s_process =
120 SIMIX_process_create(process->name, (xbt_main_func_t)jprocess,
121 /*data */ (void *) process,
122 process->simdata->m_host->simdata->smx_host->name,
125 DEBUG1("context created (s_process=%p)", process->simdata->s_process);
128 if (SIMIX_process_self()) { /* someone created me */
129 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
131 process->simdata->PPID = -1;
134 process->simdata->last_errno = MSG_OK;
136 /* add the process to the list of the processes of the simulation */
137 xbt_fifo_unshift(msg_global->process_list, process);
139 sprintf(alias, "%s:%s", (process->simdata->m_host->simdata->smx_host)->name,
142 mailbox = MSG_mailbox_new(alias);
146 JNIEXPORT void JNICALL
147 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
150 m_process_t process = jprocess_to_native_process(jprocess, env);
153 jxbt_throw_notbound(env, "process", jprocess);
157 /* try to suspend the process */
158 MSG_error_t rv = MSG_process_suspend(process);
160 jxbt_check_res("MSG_process_suspend()",rv,MSG_OK,
161 bprintf("unexpected error , please report this bug"));
165 JNIEXPORT void JNICALL
166 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
169 m_process_t process = jprocess_to_native_process(jprocess, env);
172 jxbt_throw_notbound(env,"process", jprocess);
176 /* try to resume the process */
177 MSG_error_t rv = MSG_process_resume(process);
179 jxbt_check_res("MSG_process_resume()",rv,MSG_OK,
180 bprintf("unexpected error , please report this bug"));
183 JNIEXPORT jboolean JNICALL
184 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
187 m_process_t process = jprocess_to_native_process(jprocess, env);
190 jxbt_throw_notbound(env, "process", jprocess);
194 /* true is the process is suspended, false otherwise */
195 return (jboolean) MSG_process_is_suspended(process);
198 JNIEXPORT void JNICALL
199 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
202 /* get the native instances from the java ones */
203 m_process_t process = jprocess_to_native_process(jprocess, env);
206 jxbt_throw_notbound(env, "process", jprocess);
210 /* delete the global reference */
211 jprocess_delete_global_ref(native_to_java_process(process), env);
213 /* kill the native process (this wrapper is call by the destructor of the java
216 MSG_process_kill(process);
219 JNIEXPORT jobject JNICALL
220 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
223 /* get the native instances from the java ones */
224 m_process_t process = jprocess_to_native_process(jprocess, env);
228 jxbt_throw_notbound(env, "process", jprocess);
232 host = MSG_process_get_host(process);
235 jxbt_throw_jni(env, "MSG_process_get_host() failed");
239 /* return the global reference to the java host instance */
240 return (jobject) host->data;
244 JNIEXPORT jobject JNICALL
245 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
247 m_process_t process = MSG_process_from_PID(PID);
250 jxbt_throw_process_not_found(env, bprintf("PID = %d", PID));
254 if (!native_to_java_process(process)) {
255 jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
259 return (jobject) (native_to_java_process(process));
263 JNIEXPORT jint JNICALL
264 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
267 m_process_t process = jprocess_to_native_process(jprocess, env);
270 jxbt_throw_notbound(env, "process", jprocess);
274 return (jint) MSG_process_get_PID(process);
278 JNIEXPORT jint JNICALL
279 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
282 m_process_t process = jprocess_to_native_process(jprocess, env);
285 jxbt_throw_notbound(env, "process", jprocess);
289 return (jint) MSG_process_get_PPID(process);
292 JNIEXPORT jobject JNICALL
293 Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
295 m_process_t process = MSG_process_self();
299 jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
303 jprocess = native_to_java_process(process);
306 jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
311 JNIEXPORT void JNICALL
312 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
315 m_host_t host = jhost_get_native(env, jhost);
318 jxbt_throw_notbound(env, "host", jhost);
322 /* try to change the host of the process */
323 MSG_error_t rv = MSG_process_change_host(host);
325 jxbt_check_res("MSG_process_change_host()",rv,MSG_OK,
326 bprintf("unexpected error , please report this bug"));
330 JNIEXPORT void JNICALL
331 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
334 MSG_error_t rv= MSG_process_sleep((double) seconds);
336 jxbt_check_res("MSG_process_sleep()",rv, MSG_HOST_FAILURE,
337 bprintf("while process was waiting for %f seconds",(double)seconds));
342 /***************************************************************************************
343 * The MSG host connected functions implementation. *
344 ***************************************************************************************/
346 JNIEXPORT jobject JNICALL
347 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
350 m_host_t host; /* native host */
351 jobject jhost; /* global reference to the java host instance returned */
353 /* get the C string from the java string */
354 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
356 /* get the host by name (the hosts are created during the grid resolution) */
357 host = MSG_get_host_by_name(name);
358 DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
360 (*env)->ReleaseStringUTFChars(env, jname, name);
362 if (!host) { /* invalid name */
363 jxbt_throw_host_not_found(env, name);
367 if (!host->data) { /* native host not associated yet with java host */
369 /* instanciate a new java host */
370 jhost = jhost_new_instance(env);
373 jxbt_throw_jni(env, "java host instantiation failed");
377 /* get a global reference to the newly created host */
378 jhost = jhost_ref(env, jhost);
381 jxbt_throw_jni(env, "new global ref allocation failed");
385 /* bind the java host and the native host */
386 jhost_bind(jhost, host, env);
388 /* the native host data field is set with the global reference to the
389 * java host returned by this function
391 host->data = (void *) jhost;
394 /* return the global reference to the java host instance */
395 return (jobject) host->data;
398 JNIEXPORT jstring JNICALL
399 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
402 m_host_t host = jhost_get_native(env, jhost);
405 jxbt_throw_notbound(env, "host", jhost);
409 return (*env)->NewStringUTF(env, host->name);
412 JNIEXPORT jint JNICALL
413 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
415 return (jint) MSG_get_host_number();
418 JNIEXPORT jobject JNICALL
419 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
423 m_host_t host = MSG_host_self();
426 /* the native host not yet associated with the java host instance */
428 /* instanciate a new java host instance */
429 jhost = jhost_new_instance(env);
432 jxbt_throw_jni(env, "java host instantiation failed");
436 /* get a global reference to the newly created host */
437 jhost = jhost_ref(env, jhost);
440 jxbt_throw_jni(env, "global ref allocation failed");
444 /* Bind & store it */
445 jhost_bind(jhost, host, env);
446 host->data = (void *) jhost;
448 jhost = (jobject) host->data;
454 JNIEXPORT jdouble JNICALL
455 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
458 m_host_t host = jhost_get_native(env, jhost);
461 jxbt_throw_notbound(env, "host", jhost);
465 return (jdouble) MSG_get_host_speed(host);
468 JNIEXPORT jint JNICALL
469 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
472 m_host_t host = jhost_get_native(env, jhost);
475 jxbt_throw_notbound(env, "host", jhost);
479 return (jint) MSG_get_host_msgload(host);
483 JNIEXPORT jboolean JNICALL
484 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
487 m_host_t host = jhost_get_native(env, jhost);
490 jxbt_throw_notbound(env, "host", jhost);
494 return (jboolean) MSG_host_is_avail(host);
498 /***************************************************************************************
499 * The MSG task connected functions implementation. *
500 ***************************************************************************************/
502 JNIEXPORT void JNICALL
503 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
504 jstring jname, jdouble jcomputeDuration,
505 jdouble jmessageSize)
507 m_task_t task; /* the native task to create */
508 const char *name=NULL; /* the name of the task */
510 if (jcomputeDuration < 0) {
511 jxbt_throw_illegal(env,
512 bprintf("Task ComputeDuration (%f) cannot be negative",
513 (double) jcomputeDuration));
517 if (jmessageSize < 0) {
518 jxbt_throw_illegal(env,
519 bprintf("Task MessageSize (%f) cannot be negative",
520 (double) jmessageSize));
525 /* get the C string from the java string */
526 name = (*env)->GetStringUTFChars(env, jname, 0);
530 /* create the task */
532 MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
536 (*env)->ReleaseStringUTFChars(env, jname, name);
538 /* bind & store the task */
539 jtask_bind(jtask, task, env);
541 /* allocate a new global reference to the java task instance */
542 task->data = (void *) jtask_new_global_ref(jtask, env);
545 jxbt_throw_jni(env, "global ref allocation failed");
549 JNIEXPORT void JNICALL
550 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
551 jobject jtask, jstring jname,
554 jcomputeDurations_arg,
555 jdoubleArray jmessageSizes_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) {
580 xbt_strdup("Parallel task message sizes cannot be null"));
585 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
589 host_count = (int) (*env)->GetArrayLength(env, jhosts);
592 hosts = xbt_new0(m_host_t, host_count);
593 computeDurations = xbt_new0(double, host_count);
594 messageSizes = xbt_new0(double, host_count * host_count);
597 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
598 jmessageSizes = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
600 for (index = 0; index < host_count; index++) {
601 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
602 hosts[index] = jhost_get_native(env, jhost);
603 computeDurations[index] = jcomputeDurations[index];
605 for (index = 0; index < host_count * host_count; index++) {
606 messageSizes[index] = jmessageSizes[index];
609 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
610 jcomputeDurations, 0);
611 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
615 /* get the C string from the java string */
616 name = (*env)->GetStringUTFChars(env, jname, 0);
619 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
622 (*env)->ReleaseStringUTFChars(env, jname, name);
624 /* associate the java task object and the native task */
625 jtask_bind(jtask, task, env);
627 task->data = (void *) jtask;
630 jxbt_throw_jni(env, "global ref allocation failed");
633 JNIEXPORT jobject JNICALL
634 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
639 m_task_t task = jtask_to_native_task(jtask, env);
642 jxbt_throw_notbound(env, "task", jtask);
646 process = MSG_task_get_sender(task);
647 return (jobject) native_to_java_process(process);
650 JNIEXPORT jobject JNICALL
651 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
655 m_task_t task = jtask_to_native_task(jtask, env);
658 jxbt_throw_notbound(env, "task", jtask);
662 host = MSG_task_get_source(task);
665 jxbt_throw_jni(env, "MSG_task_get_source() failed");
669 return (jobject) host->data;
673 JNIEXPORT jstring JNICALL
674 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
677 m_task_t task = jtask_to_native_task(jtask, env);
680 jxbt_throw_notbound(env, "task", jtask);
684 return (*env)->NewStringUTF(env, task->name);
687 JNIEXPORT void JNICALL
688 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
690 m_task_t ptask = jtask_to_native_task(jtask, env);
693 jxbt_throw_notbound(env, "task", jtask);
697 MSG_error_t rv = MSG_task_cancel(ptask);
699 jxbt_check_res("MSG_task_cancel()",rv,MSG_OK,
700 bprintf("unexpected error , please report this bug"));
703 JNIEXPORT jdouble JNICALL
704 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
707 m_task_t ptask = jtask_to_native_task(jtask, env);
710 jxbt_throw_notbound(env, "task", jtask);
713 return (jdouble) MSG_task_get_compute_duration(ptask);
716 JNIEXPORT jdouble JNICALL
717 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env, jclass cls,
720 m_task_t ptask = jtask_to_native_task(jtask, env);
723 jxbt_throw_notbound(env, "task", jtask);
726 return (jdouble) MSG_task_get_remaining_computation(ptask);
729 JNIEXPORT void JNICALL
730 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
731 jobject jtask, jdouble priority)
733 m_task_t task = jtask_to_native_task(jtask, env);
736 jxbt_throw_notbound(env, "task", jtask);
739 MSG_task_set_priority(task, (double) priority);
742 JNIEXPORT void JNICALL
743 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
747 /* get the native task */
748 m_task_t task = jtask_to_native_task(jtask_arg, env);
752 jxbt_throw_notbound(env, "task", task);
755 jtask = (jobject) task->data;
757 MSG_error_t rv = MSG_task_destroy(task);
759 jxbt_check_res("MSG_task_destroy()",rv,MSG_OK,
760 bprintf("unexpected error , please report this bug"));
763 /* delete the global reference to the java task object */
764 jtask_delete_global_ref(jtask, env);
767 JNIEXPORT void JNICALL
768 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
771 m_task_t task = jtask_to_native_task(jtask, env);
774 jxbt_throw_notbound(env, "task", jtask);
778 MSG_error_t rv = MSG_task_execute(task);
780 jxbt_check_res("MSG_task_execute()",rv,MSG_HOST_FAILURE|MSG_TASK_CANCELLED,
781 bprintf("while executing task %s", MSG_task_get_name(task)));
784 /***************************************************************************************
785 * Unsortable functions *
786 ***************************************************************************************/
789 JNIEXPORT jint JNICALL
790 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
792 return (jint) MSG_get_errno();
795 JNIEXPORT jdouble JNICALL
796 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
798 return (jdouble) MSG_get_clock();
802 JNIEXPORT void JNICALL
803 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) {
811 argc = (int) (*env)->GetArrayLength(env, jargs);
814 argv = xbt_new0(char *, argc);
815 argv[0] = strdup("java");
817 for (index = 0; index < argc - 1; index++) {
818 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
819 tmp = (*env)->GetStringUTFChars(env, jval, 0);
820 argv[index + 1] = strdup(tmp);
821 (*env)->ReleaseStringUTFChars(env, jval, tmp);
824 MSG_global_init(&argc, argv);
825 SIMIX_context_select_factory("java");
827 for (index = 0; index < argc; index++)
832 (*env)->GetJavaVM(env, &__java_vm);
835 JNIEXPORT void JNICALL
836 JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) {
838 int index;//xbt_fifo_item_t item = NULL;
844 jxbt_check_res("MSG_main()",rv,MSG_OK,
845 bprintf("unexpected error : MSG_main() failed .. please report this bug "));
848 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
850 DEBUG0("Clean java world");
851 /* Cleanup java hosts */
852 hosts = MSG_get_host_table();
853 for (index=0;index<MSG_get_host_number()-1;index++)
855 jhost = (jobject)hosts[index]->data;
857 jhost_unref(env,jhost);
861 DEBUG0("Clean native world");
862 /* cleanup native stuff */
863 rv = MSG_OK != MSG_clean();
864 jxbt_check_res("MSG_clean()",rv,MSG_OK,
865 bprintf("unexpected error : MSG_clean() failed .. please report this bug "));
869 JNIEXPORT jint JNICALL
870 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
873 return (jint) MSG_process_killall((int) jresetPID);
876 JNIEXPORT void JNICALL
877 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
878 jstring jplatformFile)
881 const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
883 MSG_create_environment(platformFile);
885 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
888 JNIEXPORT void JNICALL
889 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
893 m_process_t process = jprocess_to_native_process(jprocess, env);
896 jxbt_throw_notbound(env, "process", jprocess);
900 SIMIX_context_stop(SIMIX_process_self()->context);
903 JNIEXPORT void JNICALL
904 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
906 const char *s = (*env)->GetStringUTFChars(env, js, 0);
908 (*env)->ReleaseStringUTFChars(env, js, s);
911 JNIEXPORT jobjectArray JNICALL
912 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
920 int count = MSG_get_host_number();
921 m_host_t *table = MSG_get_host_table();
923 jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
929 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
932 jxbt_throw_jni(env, "Hosts table allocation failed");
936 for (index = 0; index < count; index++) {
938 jhost = (jobject) (host->data);
941 jname = (*env)->NewStringUTF(env, host->name);
943 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
944 /* FIXME: leak of jname ? */
947 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
954 JNIEXPORT void JNICALL
955 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
961 /* get the C string from the java string */
962 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
965 SIMIX_context_select_factory(name);
967 errmsg = xbt_strdup(e.msg);
971 (*env)->ReleaseStringUTFChars(env, jname, name);
974 char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
976 jxbt_throw_jni(env, thrown);
980 JNIEXPORT void JNICALL
981 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
982 jstring jalias, jobject jtask,
987 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
989 m_task_t task = jtask_to_native_task(jtask, env);
993 (*env)->ReleaseStringUTFChars(env, jalias, alias);
994 jxbt_throw_notbound(env, "task", jtask);
998 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
1000 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1002 jxbt_check_res("MSG_task_send_with_timeout()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT,
1003 bprintf("while sending task %s to mailbox %s", MSG_task_get_name(task),alias));
1006 JNIEXPORT void JNICALL
1007 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1008 jstring jalias, jobject jtask,
1011 m_task_t task = jtask_to_native_task(jtask, env);
1016 jxbt_throw_notbound(env, "task", jtask);
1020 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1022 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1024 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1026 jxbt_check_res("MSG_task_send_bounded()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT,
1027 bprintf("while sending task %s to mailbox %s with max rate %f", MSG_task_get_name(task),alias,(double)jmaxRate));
1031 JNIEXPORT jobject JNICALL
1032 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1033 jstring jalias, jdouble jtimeout,
1037 m_task_t task = NULL;
1038 m_host_t host = NULL;
1042 host = jhost_get_native(env, jhost);
1045 jxbt_throw_notbound(env, "host", jhost);
1050 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1052 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1054 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1056 jxbt_check_res("MSG_task_receive_ext()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT,
1057 bprintf("while receiving from mailbox %s",alias));
1059 return (jobject) task->data;
1062 JNIEXPORT jboolean JNICALL
1063 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1070 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1072 rv = MSG_task_listen(alias);
1074 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1076 return (jboolean) rv;
1079 JNIEXPORT jint JNICALL
1080 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1081 jstring jalias, jobject jhost)
1086 m_host_t host = jhost_get_native(env, jhost);
1089 jxbt_throw_notbound(env, "host", jhost);
1092 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1094 rv = MSG_task_listen_from_host(alias, host);
1096 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1101 JNIEXPORT jint JNICALL
1102 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1107 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1109 rv = MSG_task_listen_from(alias);
1111 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1116 JNIEXPORT void JNICALL
1117 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1118 jstring jdeploymentFile)
1121 const char *deploymentFile =
1122 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1124 surf_parse_reset_parser();
1126 surfxml_add_callback(STag_surfxml_process_cb_list,
1127 japplication_handler_on_begin_process);
1129 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1130 japplication_handler_on_process_arg);
1132 surfxml_add_callback(STag_surfxml_prop_cb_list,
1133 japplication_handler_on_property);
1135 surfxml_add_callback(ETag_surfxml_process_cb_list,
1136 japplication_handler_on_end_process);
1138 surf_parse_open(deploymentFile);
1140 japplication_handler_on_start_document();
1143 jxbt_throw_jni(env,"surf_parse() failed");
1147 japplication_handler_on_end_document();
1149 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);