4 * Copyright 2006,2007 Martin Quinson, Malek Cherier All right 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.
9 * This contains the implementation of the wrapper functions used to interface
10 * the java object with the native functions of the MSG API.
13 #include "msg/private.h"
14 #include "simix/private.h"
15 #include "simix/smx_context_java.h"
17 #include "jmsg_process.h"
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jmsg_application_handler.h"
21 #include "jxbt_utilities.h"
24 #include "msg/mailbox.h"
25 #include "surf/surfxml_parse.h"
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
29 static JavaVM *__java_vm = NULL;
31 static jobject native_to_java_process(m_process_t process);
33 JavaVM *get_java_VM(void)
38 JNIEnv *get_current_thread_env(void)
42 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
47 static jobject native_to_java_process(m_process_t process)
49 return ((smx_ctx_java_t)
50 (process->simdata->s_process->context))->jprocess;
55 * The MSG process connected functions implementation.
58 JNIEXPORT void JNICALL
59 Java_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
60 jobject jprocess_arg, jobject jhost)
62 jobject jprocess; /* the global reference to the java process instance */
63 jstring jname; /* the name of the java process instance */
64 const char *name; /* the C name of the process */
65 m_process_t process; /* the native process to create */
66 char alias[MAX_ALIAS_NAME + 1] = { 0 };
67 msg_mailbox_t mailbox;
70 ("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
71 env, cls, jprocess_arg, jhost);
72 /* get the name of the java process */
73 jname = jprocess_get_name(jprocess_arg, env);
78 ("Internal error: Process name cannot be NULL"));
82 /* allocate the data of the simulation */
83 process = xbt_new0(s_m_process_t, 1);
84 process->simdata = xbt_new0(s_simdata_process_t, 1);
86 /* create a global java process instance */
87 jprocess = jprocess_new_global_ref(jprocess_arg, env);
90 free(process->simdata);
92 jxbt_throw_jni(env, "Can't get a global ref to the java process");
96 /* bind the java process instance to the native process */
97 jprocess_bind(jprocess, process, env);
99 /* build the C name of the process */
100 name = (*env)->GetStringUTFChars(env, jname, 0);
101 process->name = xbt_strdup(name);
102 (*env)->ReleaseStringUTFChars(env, jname, name);
104 process->simdata->m_host = jhost_get_native(env, jhost);
107 if (!(process->simdata->m_host)) { /* not binded */
108 free(process->simdata);
111 jxbt_throw_notbound(env, "host", jhost);
114 process->simdata->PID = msg_global->PID++;
116 /* create a new context */
118 ("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
119 process->name, process->simdata->m_host->name, process->simdata->PID,
120 process, process->simdata, process->simdata->m_host,
121 process->simdata->m_host->simdata, env);
123 process->simdata->s_process =
124 SIMIX_process_create(process->name, (xbt_main_func_t)jprocess,
125 /*data */ (void *) process,
126 process->simdata->m_host->simdata->smx_host->name,
129 DEBUG1("context created (s_process=%p)", process->simdata->s_process);
132 if (SIMIX_process_self()) { /* someone created me */
133 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
135 process->simdata->PPID = -1;
138 process->simdata->last_errno = MSG_OK;
140 /* add the process to the list of the processes of the simulation */
141 xbt_fifo_unshift(msg_global->process_list, process);
143 sprintf(alias, "%s:%s", (process->simdata->m_host->simdata->smx_host)->name,
146 mailbox = MSG_mailbox_new(alias);
150 JNIEXPORT void JNICALL
151 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
154 m_process_t process = jprocess_to_native_process(jprocess, env);
157 jxbt_throw_notbound(env, "process", jprocess);
161 /* try to suspend the process */
162 if (MSG_OK != MSG_process_suspend(process))
163 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
166 JNIEXPORT void JNICALL
167 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
170 m_process_t process = jprocess_to_native_process(jprocess, env);
173 jxbt_throw_notbound(env, "process", jprocess);
177 /* try to resume the process */
178 if (MSG_OK != MSG_process_resume(process))
179 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
182 JNIEXPORT jboolean JNICALL
183 Java_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_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 /* delete the global reference */
210 jprocess_delete_global_ref(native_to_java_process(process), env);
212 /* kill the native process (this wrapper is call by the destructor of the java
215 MSG_process_kill(process);
218 JNIEXPORT jobject JNICALL
219 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
222 /* get the native instances from the java ones */
223 m_process_t process = jprocess_to_native_process(jprocess, env);
227 jxbt_throw_notbound(env, "process", jprocess);
231 host = MSG_process_get_host(process);
234 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
238 /* return the global reference to the java host instance */
239 return (jobject) host->data;
243 JNIEXPORT jobject JNICALL
244 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
246 m_process_t process = MSG_process_from_PID(PID);
249 jxbt_throw_process_not_found(env, bprintf("PID = %d", PID));
253 if (!native_to_java_process(process)) {
254 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
258 return (jobject) (native_to_java_process(process));
262 JNIEXPORT jint JNICALL
263 Java_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_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_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
294 m_process_t process = MSG_process_self();
298 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
302 jprocess = native_to_java_process(process);
305 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
310 JNIEXPORT void JNICALL
311 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
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 if (MSG_OK != MSG_process_change_host(host))
323 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
326 JNIEXPORT void JNICALL
327 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
330 if (MSG_OK != MSG_process_sleep((double) seconds))
331 jxbt_throw_native(env,
332 bprintf("MSG_process_change_host(%f) failed",
337 /***************************************************************************************
338 * The MSG host connected functions implementation. *
339 ***************************************************************************************/
341 JNIEXPORT jobject JNICALL
342 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
345 m_host_t host; /* native host */
346 jobject jhost; /* global reference to the java host instance returned */
348 /* get the C string from the java string */
349 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
351 /* get the host by name (the hosts are created during the grid resolution) */
352 host = MSG_get_host_by_name(name);
353 DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
355 (*env)->ReleaseStringUTFChars(env, jname, name);
357 if (!host) { /* invalid name */
358 jxbt_throw_host_not_found(env, name);
362 if (!host->data) { /* native host not associated yet with java host */
364 /* instanciate a new java host */
365 jhost = jhost_new_instance(env);
368 jxbt_throw_jni(env, "java host instantiation failed");
372 /* get a global reference to the newly created host */
373 jhost = jhost_ref(env, jhost);
376 jxbt_throw_jni(env, "new global ref allocation failed");
380 /* bind the java host and the native host */
381 jhost_bind(jhost, host, env);
383 /* the native host data field is set with the global reference to the
384 * java host returned by this function
386 host->data = (void *) jhost;
389 /* return the global reference to the java host instance */
390 return (jobject) host->data;
393 JNIEXPORT jstring JNICALL
394 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
397 m_host_t host = jhost_get_native(env, jhost);
400 jxbt_throw_notbound(env, "host", jhost);
404 return (*env)->NewStringUTF(env, host->name);
407 JNIEXPORT jint JNICALL
408 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
410 return (jint) MSG_get_host_number();
413 JNIEXPORT jobject JNICALL
414 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
418 m_host_t host = MSG_host_self();
421 /* the native host not yet associated with the java host instance */
423 /* instanciate a new java host instance */
424 jhost = jhost_new_instance(env);
427 jxbt_throw_jni(env, "java host instantiation failed");
431 /* get a global reference to the newly created host */
432 jhost = jhost_ref(env, jhost);
435 jxbt_throw_jni(env, "global ref allocation failed");
439 /* Bind & store it */
440 jhost_bind(jhost, host, env);
441 host->data = (void *) jhost;
443 jhost = (jobject) host->data;
449 JNIEXPORT jdouble JNICALL
450 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
453 m_host_t host = jhost_get_native(env, jhost);
456 jxbt_throw_notbound(env, "host", jhost);
460 return (jdouble) MSG_get_host_speed(host);
463 JNIEXPORT jint JNICALL
464 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
467 m_host_t host = jhost_get_native(env, jhost);
470 jxbt_throw_notbound(env, "host", jhost);
474 return (jint) MSG_get_host_msgload(host);
478 JNIEXPORT jboolean JNICALL
479 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
482 m_host_t host = jhost_get_native(env, jhost);
485 jxbt_throw_notbound(env, "host", jhost);
489 return (jboolean) MSG_host_is_avail(host);
493 /***************************************************************************************
494 * The MSG task connected functions implementation. *
495 ***************************************************************************************/
497 JNIEXPORT void JNICALL
498 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
499 jstring jname, jdouble jcomputeDuration,
500 jdouble jmessageSize)
502 m_task_t task; /* the native task to create */
503 const char *name=NULL; /* the name of the task */
505 if (jcomputeDuration < 0) {
506 jxbt_throw_illegal(env,
507 bprintf("Task ComputeDuration (%f) cannot be negative",
508 (double) jcomputeDuration));
512 if (jmessageSize < 0) {
513 jxbt_throw_illegal(env,
514 bprintf("Task MessageSize (%f) cannot be negative",
515 (double) jmessageSize));
520 /* get the C string from the java string */
521 name = (*env)->GetStringUTFChars(env, jname, 0);
525 /* create the task */
527 MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
531 (*env)->ReleaseStringUTFChars(env, jname, name);
533 /* bind & store the task */
534 jtask_bind(jtask, task, env);
536 /* allocate a new global reference to the java task instance */
537 task->data = (void *) jtask_new_global_ref(jtask, env);
540 jxbt_throw_jni(env, "global ref allocation failed");
544 JNIEXPORT void JNICALL
545 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
546 jobject jtask, jstring jname,
549 jcomputeDurations_arg,
550 jdoubleArray jmessageSizes_arg)
553 m_task_t task; /* the native parallel task to create */
554 const char *name; /* the name of the task */
557 double *computeDurations;
558 double *messageSizes;
559 jdouble *jcomputeDurations;
560 jdouble *jmessageSizes;
566 if (!jcomputeDurations_arg) {
569 ("Parallel task compute durations cannot be null"));
573 if (!jmessageSizes_arg) {
575 xbt_strdup("Parallel task message sizes cannot be null"));
580 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
584 host_count = (int) (*env)->GetArrayLength(env, jhosts);
587 hosts = xbt_new0(m_host_t, host_count);
588 computeDurations = xbt_new0(double, host_count);
589 messageSizes = xbt_new0(double, host_count * host_count);
592 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
593 jmessageSizes = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
595 for (index = 0; index < host_count; index++) {
596 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
597 hosts[index] = jhost_get_native(env, jhost);
598 computeDurations[index] = jcomputeDurations[index];
600 for (index = 0; index < host_count * host_count; index++) {
601 messageSizes[index] = jmessageSizes[index];
604 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
605 jcomputeDurations, 0);
606 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
610 /* get the C string from the java string */
611 name = (*env)->GetStringUTFChars(env, jname, 0);
614 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
617 (*env)->ReleaseStringUTFChars(env, jname, name);
619 /* associate the java task object and the native task */
620 jtask_bind(jtask, task, env);
622 task->data = (void *) jtask;
625 jxbt_throw_jni(env, "global ref allocation failed");
628 JNIEXPORT jobject JNICALL
629 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
634 m_task_t task = jtask_to_native_task(jtask, env);
637 jxbt_throw_notbound(env, "task", jtask);
641 process = MSG_task_get_sender(task);
642 return (jobject) native_to_java_process(process);
645 JNIEXPORT jobject JNICALL
646 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
650 m_task_t task = jtask_to_native_task(jtask, env);
653 jxbt_throw_notbound(env, "task", jtask);
657 host = MSG_task_get_source(task);
660 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
664 return (jobject) host->data;
668 JNIEXPORT jstring JNICALL
669 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
672 m_task_t task = jtask_to_native_task(jtask, env);
675 jxbt_throw_notbound(env, "task", jtask);
679 return (*env)->NewStringUTF(env, task->name);
682 JNIEXPORT void JNICALL
683 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
685 m_task_t ptask = jtask_to_native_task(jtask, env);
688 jxbt_throw_notbound(env, "task", jtask);
692 if (MSG_OK != MSG_task_cancel(ptask))
693 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
696 JNIEXPORT jdouble JNICALL
697 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
700 m_task_t ptask = jtask_to_native_task(jtask, env);
703 jxbt_throw_notbound(env, "task", jtask);
706 return (jdouble) MSG_task_get_compute_duration(ptask);
709 JNIEXPORT jdouble JNICALL
710 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(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_remaining_computation(ptask);
722 JNIEXPORT void JNICALL
723 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
724 jobject jtask, jdouble priority)
726 m_task_t task = jtask_to_native_task(jtask, env);
729 jxbt_throw_notbound(env, "task", jtask);
732 MSG_task_set_priority(task, (double) priority);
735 JNIEXPORT void JNICALL
736 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
740 /* get the native task */
741 m_task_t task = jtask_to_native_task(jtask_arg, env);
745 jxbt_throw_notbound(env, "task", task);
748 jtask = (jobject) task->data;
750 if (MSG_OK != MSG_task_destroy(task))
751 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
753 /* delete the global reference to the java task object */
754 jtask_delete_global_ref(jtask, env);
757 JNIEXPORT void JNICALL
758 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
761 m_task_t task = jtask_to_native_task(jtask, env);
764 jxbt_throw_notbound(env, "task", jtask);
768 if (MSG_OK != MSG_task_execute(task))
769 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
772 /***************************************************************************************
773 * Unsortable functions *
774 ***************************************************************************************/
777 JNIEXPORT jint JNICALL
778 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
780 return (jint) MSG_get_errno();
783 JNIEXPORT jdouble JNICALL
784 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
786 return (jdouble) MSG_get_clock();
790 JNIEXPORT void JNICALL
791 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) {
799 argc = (int) (*env)->GetArrayLength(env, jargs);
802 argv = xbt_new0(char *, argc);
803 argv[0] = strdup("java");
805 for (index = 0; index < argc - 1; index++) {
806 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
807 tmp = (*env)->GetStringUTFChars(env, jval, 0);
808 argv[index + 1] = strdup(tmp);
809 (*env)->ReleaseStringUTFChars(env, jval, tmp);
812 MSG_global_init(&argc, argv);
813 SIMIX_context_select_factory("java");
815 for (index = 0; index < argc; index++)
820 (*env)->GetJavaVM(env, &__java_vm);
823 JNIEXPORT void JNICALL
824 JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) {
825 xbt_fifo_item_t item = NULL;
826 m_host_t host = NULL;
830 if (MSG_OK != MSG_main()) {
831 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
834 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
836 DEBUG0("Clean java world");
837 /* Cleanup java hosts */
838 xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
839 jhost = (jobject) host->data;
842 jhost_unref(env, jhost);
845 DEBUG0("Clean native world");
846 /* cleanup native stuff */
847 if (MSG_OK != MSG_clean()){
848 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
852 JNIEXPORT jint JNICALL
853 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
856 return (jint) MSG_process_killall((int) jresetPID);
859 JNIEXPORT void JNICALL
860 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
861 jstring jplatformFile)
864 const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
866 MSG_create_environment(platformFile);
868 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
871 JNIEXPORT void JNICALL
872 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
876 m_process_t process = jprocess_to_native_process(jprocess, env);
879 jxbt_throw_notbound(env, "process", jprocess);
883 SIMIX_context_stop(SIMIX_process_self()->context);
886 JNIEXPORT void JNICALL
887 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
889 const char *s = (*env)->GetStringUTFChars(env, js, 0);
891 (*env)->ReleaseStringUTFChars(env, js, s);
894 JNIEXPORT jobjectArray JNICALL
895 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
903 int count = xbt_fifo_size(msg_global->host);
904 m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
906 jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
912 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
915 jxbt_throw_jni(env, "Hosts table allocation failed");
919 for (index = 0; index < count; index++) {
921 jhost = (jobject) (host->data);
924 jname = (*env)->NewStringUTF(env, host->name);
926 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
927 /* FIXME: leak of jname ? */
930 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
937 JNIEXPORT void JNICALL
938 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
944 /* get the C string from the java string */
945 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
948 SIMIX_context_select_factory(name);
950 errmsg = xbt_strdup(e.msg);
954 (*env)->ReleaseStringUTFChars(env, jname, name);
957 char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
959 jxbt_throw_native(env, thrown);
963 JNIEXPORT void JNICALL
964 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
965 jstring jalias, jobject jtask,
970 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
972 m_task_t task = jtask_to_native_task(jtask, env);
976 (*env)->ReleaseStringUTFChars(env, jalias, alias);
977 jxbt_throw_notbound(env, "task", jtask);
981 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
983 (*env)->ReleaseStringUTFChars(env, jalias, alias);
985 /* throw the right exception corresponding to HostFailureException, TransferFailureException, TimeoutFailureException
986 * Note: these exceptions must be created beforehand
987 * then, you want to create some functions like jxbt_throw_notbound()
988 * then, you must declare in the MsgNative stuff that these native functions can throw these exceptions
993 if ( rv == MSG_TRANSFER_FAILURE )
994 jxbt_throw_transfer_failure(env,MSG_task_get_name(task),alias);
996 else if ( rv == MSG_HOST_FAILURE )
997 jxbt_throw_host_failure(env,MSG_task_get_name(task),alias);
1000 jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
1005 JNIEXPORT void JNICALL
1006 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1007 jstring jalias, jobject jtask,
1010 m_task_t task = jtask_to_native_task(jtask, env);
1015 jxbt_throw_notbound(env, "task", jtask);
1019 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1021 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1023 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1026 jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1029 JNIEXPORT jobject JNICALL
1030 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1031 jstring jalias, jdouble jtimeout,
1035 m_task_t task = NULL;
1036 m_host_t host = NULL;
1040 host = jhost_get_native(env, jhost);
1043 jxbt_throw_notbound(env, "host", jhost);
1048 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1050 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1052 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1055 jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
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_native(env, xbt_strdup("surf_parse() failed"));
1147 japplication_handler_on_end_document();
1149 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);