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;
54 * The MSG process connected functions implementation.
57 JNIEXPORT void JNICALL
58 Java_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
59 jobject jprocess_arg, jobject jhost)
61 jobject jprocess; /* the global reference to the java process instance */
62 jstring jname; /* the name of the java process instance */
63 const char *name; /* the C name of the process */
64 m_process_t process; /* the native process to create */
65 char alias[MAX_ALIAS_NAME + 1] = { 0 };
66 msg_mailbox_t mailbox;
69 ("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
70 env, cls, jprocess_arg, jhost);
71 /* get the name of the java process */
72 jname = jprocess_get_name(jprocess_arg, env);
77 ("Internal error: Process name cannot be NULL"));
81 /* allocate the data of the simulation */
82 process = xbt_new0(s_m_process_t, 1);
83 process->simdata = xbt_new0(s_simdata_process_t, 1);
85 /* create a global java process instance */
86 jprocess = jprocess_new_global_ref(jprocess_arg, env);
89 free(process->simdata);
91 jxbt_throw_jni(env, "Can't get a global ref to the java process");
95 /* bind the java process instance to the native process */
96 jprocess_bind(jprocess, process, env);
98 /* build the C name of the process */
99 name = (*env)->GetStringUTFChars(env, jname, 0);
100 process->name = xbt_strdup(name);
101 (*env)->ReleaseStringUTFChars(env, jname, name);
103 process->simdata->m_host = jhost_get_native(env, jhost);
106 if (!(process->simdata->m_host)) { /* not binded */
107 free(process->simdata);
110 jxbt_throw_notbound(env, "host", jhost);
113 process->simdata->PID = msg_global->PID++;
115 /* create a new context */
117 ("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
118 process->name, process->simdata->m_host->name, process->simdata->PID,
119 process, process->simdata, process->simdata->m_host,
120 process->simdata->m_host->simdata, env);
122 process->simdata->s_process =
123 SIMIX_process_create(process->name, (xbt_main_func_t)jprocess,
124 /*data */ (void *) process,
125 process->simdata->m_host->simdata->smx_host->name,
128 DEBUG1("context created (s_process=%p)", process->simdata->s_process);
131 if (SIMIX_process_self()) { /* someone created me */
132 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
134 process->simdata->PPID = -1;
137 process->simdata->last_errno = MSG_OK;
139 /* add the process to the list of the processes of the simulation */
140 xbt_fifo_unshift(msg_global->process_list, process);
142 sprintf(alias, "%s:%s", (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 if (MSG_OK != MSG_process_suspend(process))
162 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
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 if (MSG_OK != MSG_process_resume(process))
178 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
181 JNIEXPORT jboolean JNICALL
182 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
185 m_process_t process = jprocess_to_native_process(jprocess, env);
188 jxbt_throw_notbound(env, "process", jprocess);
192 /* true is the process is suspended, false otherwise */
193 return (jboolean) MSG_process_is_suspended(process);
196 JNIEXPORT void JNICALL
197 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
200 /* get the native instances from the java ones */
201 m_process_t process = jprocess_to_native_process(jprocess, env);
204 jxbt_throw_notbound(env, "process", jprocess);
208 /* delete the global reference */
209 jprocess_delete_global_ref(native_to_java_process(process), env);
211 /* kill the native process (this wrapper is call by the destructor of the java
214 MSG_process_kill(process);
217 JNIEXPORT jobject JNICALL
218 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
221 /* get the native instances from the java ones */
222 m_process_t process = jprocess_to_native_process(jprocess, env);
226 jxbt_throw_notbound(env, "process", jprocess);
230 host = MSG_process_get_host(process);
233 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
237 /* return the global reference to the java host instance */
238 return (jobject) host->data;
242 JNIEXPORT jobject JNICALL
243 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
245 m_process_t process = MSG_process_from_PID(PID);
248 jxbt_throw_process_not_found(env, bprintf("PID = %d", PID));
252 if (!native_to_java_process(process)) {
253 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
257 return (jobject) (native_to_java_process(process));
261 JNIEXPORT jint JNICALL
262 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
265 m_process_t process = jprocess_to_native_process(jprocess, env);
268 jxbt_throw_notbound(env, "process", jprocess);
272 return (jint) MSG_process_get_PID(process);
276 JNIEXPORT jint JNICALL
277 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
280 m_process_t process = jprocess_to_native_process(jprocess, env);
283 jxbt_throw_notbound(env, "process", jprocess);
287 return (jint) MSG_process_get_PPID(process);
290 JNIEXPORT jobject JNICALL
291 Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
293 m_process_t process = MSG_process_self();
297 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
301 jprocess = native_to_java_process(process);
304 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
309 JNIEXPORT void JNICALL
310 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
313 m_host_t host = jhost_get_native(env, jhost);
316 jxbt_throw_notbound(env, "host", jhost);
320 /* try to change the host of the process */
321 if (MSG_OK != MSG_process_change_host(host))
322 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
325 JNIEXPORT void JNICALL
326 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
329 if (MSG_OK != MSG_process_sleep((double) seconds))
330 jxbt_throw_native(env,
331 bprintf("MSG_process_change_host(%f) failed",
336 /***************************************************************************************
337 * The MSG host connected functions implementation. *
338 ***************************************************************************************/
340 JNIEXPORT jobject JNICALL
341 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
344 m_host_t host; /* native host */
345 jobject jhost; /* global reference to the java host instance returned */
347 /* get the C string from the java string */
348 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
350 /* get the host by name (the hosts are created during the grid resolution) */
351 host = MSG_get_host_by_name(name);
352 DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
354 (*env)->ReleaseStringUTFChars(env, jname, name);
356 if (!host) { /* invalid name */
357 jxbt_throw_host_not_found(env, name);
361 if (!host->data) { /* native host not associated yet with java host */
363 /* instanciate a new java host */
364 jhost = jhost_new_instance(env);
367 jxbt_throw_jni(env, "java host instantiation failed");
371 /* get a global reference to the newly created host */
372 jhost = jhost_ref(env, jhost);
375 jxbt_throw_jni(env, "new global ref allocation failed");
379 /* bind the java host and the native host */
380 jhost_bind(jhost, host, env);
382 /* the native host data field is set with the global reference to the
383 * java host returned by this function
385 host->data = (void *) jhost;
388 /* return the global reference to the java host instance */
389 return (jobject) host->data;
392 JNIEXPORT jstring JNICALL
393 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
396 m_host_t host = jhost_get_native(env, jhost);
399 jxbt_throw_notbound(env, "host", jhost);
403 return (*env)->NewStringUTF(env, host->name);
406 JNIEXPORT jint JNICALL
407 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
409 return (jint) MSG_get_host_number();
412 JNIEXPORT jobject JNICALL
413 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
417 m_host_t host = MSG_host_self();
420 /* the native host not yet associated with the java host instance */
422 /* instanciate a new java host instance */
423 jhost = jhost_new_instance(env);
426 jxbt_throw_jni(env, "java host instantiation failed");
430 /* get a global reference to the newly created host */
431 jhost = jhost_ref(env, jhost);
434 jxbt_throw_jni(env, "global ref allocation failed");
438 /* Bind & store it */
439 jhost_bind(jhost, host, env);
440 host->data = (void *) jhost;
442 jhost = (jobject) host->data;
448 JNIEXPORT jdouble JNICALL
449 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
452 m_host_t host = jhost_get_native(env, jhost);
455 jxbt_throw_notbound(env, "host", jhost);
459 return (jdouble) MSG_get_host_speed(host);
462 JNIEXPORT jint JNICALL
463 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
466 m_host_t host = jhost_get_native(env, jhost);
469 jxbt_throw_notbound(env, "host", jhost);
473 return (jint) MSG_get_host_msgload(host);
477 JNIEXPORT jboolean JNICALL
478 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
481 m_host_t host = jhost_get_native(env, jhost);
484 jxbt_throw_notbound(env, "host", jhost);
488 return (jboolean) MSG_host_is_avail(host);
492 /***************************************************************************************
493 * The MSG task connected functions implementation. *
494 ***************************************************************************************/
496 JNIEXPORT void JNICALL
497 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
498 jstring jname, jdouble jcomputeDuration,
499 jdouble jmessageSize)
501 m_task_t task; /* the native task to create */
502 const char *name=NULL; /* the name of the task */
504 if (jcomputeDuration < 0) {
505 jxbt_throw_illegal(env,
506 bprintf("Task ComputeDuration (%f) cannot be negative",
507 (double) jcomputeDuration));
511 if (jmessageSize < 0) {
512 jxbt_throw_illegal(env,
513 bprintf("Task MessageSize (%f) cannot be negative",
514 (double) jmessageSize));
519 /* get the C string from the java string */
520 name = (*env)->GetStringUTFChars(env, jname, 0);
524 /* create the task */
526 MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
530 (*env)->ReleaseStringUTFChars(env, jname, name);
532 /* bind & store the task */
533 jtask_bind(jtask, task, env);
535 /* allocate a new global reference to the java task instance */
536 task->data = (void *) jtask_new_global_ref(jtask, env);
539 jxbt_throw_jni(env, "global ref allocation failed");
543 JNIEXPORT void JNICALL
544 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
545 jobject jtask, jstring jname,
548 jcomputeDurations_arg,
549 jdoubleArray jmessageSizes_arg)
552 m_task_t task; /* the native parallel task to create */
553 const char *name; /* the name of the task */
556 double *computeDurations;
557 double *messageSizes;
558 jdouble *jcomputeDurations;
559 jdouble *jmessageSizes;
565 if (!jcomputeDurations_arg) {
568 ("Parallel task compute durations cannot be null"));
572 if (!jmessageSizes_arg) {
574 xbt_strdup("Parallel task message sizes cannot be null"));
579 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
583 host_count = (int) (*env)->GetArrayLength(env, jhosts);
586 hosts = xbt_new0(m_host_t, host_count);
587 computeDurations = xbt_new0(double, host_count);
588 messageSizes = xbt_new0(double, host_count * host_count);
591 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
592 jmessageSizes = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
594 for (index = 0; index < host_count; index++) {
595 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
596 hosts[index] = jhost_get_native(env, jhost);
597 computeDurations[index] = jcomputeDurations[index];
599 for (index = 0; index < host_count * host_count; index++) {
600 messageSizes[index] = jmessageSizes[index];
603 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
604 jcomputeDurations, 0);
605 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
609 /* get the C string from the java string */
610 name = (*env)->GetStringUTFChars(env, jname, 0);
613 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
616 (*env)->ReleaseStringUTFChars(env, jname, name);
618 /* associate the java task object and the native task */
619 jtask_bind(jtask, task, env);
621 task->data = (void *) jtask;
624 jxbt_throw_jni(env, "global ref allocation failed");
627 JNIEXPORT jobject JNICALL
628 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
633 m_task_t task = jtask_to_native_task(jtask, env);
636 jxbt_throw_notbound(env, "task", jtask);
640 process = MSG_task_get_sender(task);
641 return (jobject) native_to_java_process(process);
644 JNIEXPORT jobject JNICALL
645 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
649 m_task_t task = jtask_to_native_task(jtask, env);
652 jxbt_throw_notbound(env, "task", jtask);
656 host = MSG_task_get_source(task);
659 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
663 return (jobject) host->data;
667 JNIEXPORT jstring JNICALL
668 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
671 m_task_t task = jtask_to_native_task(jtask, env);
674 jxbt_throw_notbound(env, "task", jtask);
678 return (*env)->NewStringUTF(env, task->name);
681 JNIEXPORT void JNICALL
682 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
684 m_task_t ptask = jtask_to_native_task(jtask, env);
687 jxbt_throw_notbound(env, "task", jtask);
691 if (MSG_OK != MSG_task_cancel(ptask))
692 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
695 JNIEXPORT jdouble JNICALL
696 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
699 m_task_t ptask = jtask_to_native_task(jtask, env);
702 jxbt_throw_notbound(env, "task", jtask);
705 return (jdouble) MSG_task_get_compute_duration(ptask);
708 JNIEXPORT jdouble JNICALL
709 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env, jclass cls,
712 m_task_t ptask = jtask_to_native_task(jtask, env);
715 jxbt_throw_notbound(env, "task", jtask);
718 return (jdouble) MSG_task_get_remaining_computation(ptask);
721 JNIEXPORT void JNICALL
722 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
723 jobject jtask, jdouble priority)
725 m_task_t task = jtask_to_native_task(jtask, env);
728 jxbt_throw_notbound(env, "task", jtask);
731 MSG_task_set_priority(task, (double) priority);
734 JNIEXPORT void JNICALL
735 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
739 /* get the native task */
740 m_task_t task = jtask_to_native_task(jtask_arg, env);
744 jxbt_throw_notbound(env, "task", task);
747 jtask = (jobject) task->data;
749 if (MSG_OK != MSG_task_destroy(task))
750 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
752 /* delete the global reference to the java task object */
753 jtask_delete_global_ref(jtask, env);
756 JNIEXPORT void JNICALL
757 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
760 m_task_t task = jtask_to_native_task(jtask, env);
763 jxbt_throw_notbound(env, "task", jtask);
767 if (MSG_OK != MSG_task_execute(task))
768 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
771 /***************************************************************************************
772 * Unsortable functions *
773 ***************************************************************************************/
776 JNIEXPORT jint JNICALL
777 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
779 return (jint) MSG_get_errno();
782 JNIEXPORT jdouble JNICALL
783 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
785 return (jdouble) MSG_get_clock();
789 JNIEXPORT void JNICALL
790 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) {
798 argc = (int) (*env)->GetArrayLength(env, jargs);
801 argv = xbt_new0(char *, argc);
802 argv[0] = strdup("java");
804 for (index = 0; index < argc - 1; index++) {
805 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
806 tmp = (*env)->GetStringUTFChars(env, jval, 0);
807 argv[index + 1] = strdup(tmp);
808 (*env)->ReleaseStringUTFChars(env, jval, tmp);
811 MSG_global_init(&argc, argv);
812 SIMIX_context_select_factory("java");
814 for (index = 0; index < argc; index++)
819 (*env)->GetJavaVM(env, &__java_vm);
822 JNIEXPORT void JNICALL
823 JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) {
824 xbt_fifo_item_t item = NULL;
825 m_host_t host = NULL;
829 if (MSG_OK != MSG_main()) {
830 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
833 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
835 DEBUG0("Clean java world");
836 /* Cleanup java hosts */
837 xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
838 jhost = (jobject) host->data;
841 jhost_unref(env, jhost);
844 DEBUG0("Clean native world");
845 /* cleanup native stuff */
846 if (MSG_OK != MSG_clean()){
847 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
851 JNIEXPORT jint JNICALL
852 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
855 return (jint) MSG_process_killall((int) jresetPID);
858 JNIEXPORT void JNICALL
859 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
860 jstring jplatformFile)
863 const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
865 MSG_create_environment(platformFile);
867 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
870 JNIEXPORT void JNICALL
871 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
875 m_process_t process = jprocess_to_native_process(jprocess, env);
878 jxbt_throw_notbound(env, "process", jprocess);
882 SIMIX_context_stop(SIMIX_process_self()->context);
885 JNIEXPORT void JNICALL
886 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
888 const char *s = (*env)->GetStringUTFChars(env, js, 0);
890 (*env)->ReleaseStringUTFChars(env, js, s);
893 JNIEXPORT jobjectArray JNICALL
894 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
902 int count = xbt_fifo_size(msg_global->host);
903 m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
905 jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
911 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
914 jxbt_throw_jni(env, "Hosts table allocation failed");
918 for (index = 0; index < count; index++) {
920 jhost = (jobject) (host->data);
923 jname = (*env)->NewStringUTF(env, host->name);
925 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
926 /* FIXME: leak of jname ? */
929 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
936 JNIEXPORT void JNICALL
937 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
943 /* get the C string from the java string */
944 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
947 SIMIX_context_select_factory(name);
949 errmsg = xbt_strdup(e.msg);
953 (*env)->ReleaseStringUTFChars(env, jname, name);
956 char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
958 jxbt_throw_native(env, thrown);
962 JNIEXPORT void JNICALL
963 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
964 jstring jalias, jobject jtask,
969 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
971 m_task_t task = jtask_to_native_task(jtask, env);
975 (*env)->ReleaseStringUTFChars(env, jalias, alias);
976 jxbt_throw_notbound(env, "task", jtask);
980 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
982 (*env)->ReleaseStringUTFChars(env, jalias, alias);
984 /* throw the right exception corresponding to HostFailureException, TransferFailureException, TimeoutFailureException
985 * Note: these exceptions must be created beforehand
986 * then, you want to create some functions like jxbt_throw_notbound()
987 * then, you must declare in the MsgNative stuff that these native functions can throw these exceptions
992 if ( rv == MSG_TRANSFER_FAILURE )
993 jxbt_throw_transfer_failure(env,MSG_task_get_name(task),alias);
995 else if ( rv == MSG_HOST_FAILURE )
996 jxbt_throw_host_failure(env,MSG_task_get_name(task),alias);
998 else if ( rv == MSG_TIMEOUT_FAILURE )
999 jxbt_throw_time_out_failure(env,MSG_task_get_name(task),alias);
1001 jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
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);
1027 jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1030 JNIEXPORT jobject JNICALL
1031 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1032 jstring jalias, jdouble jtimeout,
1036 m_task_t task = NULL;
1037 m_host_t host = NULL;
1041 host = jhost_get_native(env, jhost);
1044 jxbt_throw_notbound(env, "host", jhost);
1049 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1051 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1053 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1056 jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1060 return (jobject) task->data;
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,
1082 jstring jalias, jobject jhost)
1087 m_host_t host = jhost_get_native(env, jhost);
1090 jxbt_throw_notbound(env, "host", jhost);
1093 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1095 rv = MSG_task_listen_from_host(alias, host);
1097 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1102 JNIEXPORT jint JNICALL
1103 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1108 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1110 rv = MSG_task_listen_from(alias);
1112 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1117 JNIEXPORT void JNICALL
1118 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1119 jstring jdeploymentFile)
1122 const char *deploymentFile =
1123 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1125 surf_parse_reset_parser();
1127 surfxml_add_callback(STag_surfxml_process_cb_list,
1128 japplication_handler_on_begin_process);
1130 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1131 japplication_handler_on_process_arg);
1133 surfxml_add_callback(STag_surfxml_prop_cb_list,
1134 japplication_handler_on_property);
1136 surfxml_add_callback(ETag_surfxml_process_cb_list,
1137 japplication_handler_on_end_process);
1139 surf_parse_open(deploymentFile);
1141 japplication_handler_on_start_document();
1144 jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
1148 japplication_handler_on_end_document();
1150 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);