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_channel.h"
21 #include "jmsg_application_handler.h"
22 #include "jxbt_utilities.h"
27 #include "msg/mailbox.h"
29 #include "surf/surfxml_parse.h"
32 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
34 static JavaVM *__java_vm = NULL;
36 static jobject native_to_java_process(m_process_t process);
38 JavaVM *get_java_VM(void)
43 JNIEnv *get_current_thread_env(void)
47 (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
52 static jobject native_to_java_process(m_process_t process)
54 return ((smx_ctx_java_t)
55 (process->simdata->s_process->context))->jprocess;
60 * The MSG process connected functions implementation.
63 JNIEXPORT void JNICALL
64 Java_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
65 jobject jprocess_arg, jobject jhost)
67 jobject jprocess; /* the global reference to the java process instance */
68 jstring jname; /* the name of the java process instance */
69 const char *name; /* the C name of the process */
70 m_process_t process; /* the native process to create */
71 char alias[MAX_ALIAS_NAME + 1] = { 0 };
72 msg_mailbox_t mailbox;
75 ("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
76 env, cls, jprocess_arg, jhost);
77 /* get the name of the java process */
78 jname = jprocess_get_name(jprocess_arg, env);
83 ("Internal error: Process name cannot be NULL"));
87 /* allocate the data of the simulation */
88 process = xbt_new0(s_m_process_t, 1);
89 process->simdata = xbt_new0(s_simdata_process_t, 1);
91 /* create a global java process instance */
92 jprocess = jprocess_new_global_ref(jprocess_arg, env);
95 free(process->simdata);
97 jxbt_throw_jni(env, "Can't get a global ref to the java process");
101 /* bind the java process instance to the native process */
102 jprocess_bind(jprocess, process, env);
104 /* build the C name of the process */
105 name = (*env)->GetStringUTFChars(env, jname, 0);
106 process->name = xbt_strdup(name);
107 (*env)->ReleaseStringUTFChars(env, jname, name);
109 process->simdata->m_host = jhost_get_native(env, jhost);
112 if (!(process->simdata->m_host)) { /* not binded */
113 free(process->simdata);
116 jxbt_throw_notbound(env, "host", jhost);
119 process->simdata->PID = msg_global->PID++;
121 /* create a new context */
123 ("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
124 process->name, process->simdata->m_host->name, process->simdata->PID,
125 process, process->simdata, process->simdata->m_host,
126 process->simdata->m_host->simdata, env);
128 process->simdata->s_process =
129 SIMIX_process_create(process->name, (xbt_main_func_t)jprocess,
130 /*data */ (void *) process,
131 process->simdata->m_host->simdata->smx_host->name,
134 DEBUG1("context created (s_process=%p)", process->simdata->s_process);
137 if (SIMIX_process_self()) { /* someone created me */
138 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
140 process->simdata->PPID = -1;
143 process->simdata->last_errno = MSG_OK;
145 /* add the process to the list of the processes of the simulation */
146 xbt_fifo_unshift(msg_global->process_list, process);
148 sprintf(alias, "%s:%s", (process->simdata->m_host->simdata->smx_host)->name,
151 mailbox = MSG_mailbox_new(alias);
155 JNIEXPORT void JNICALL
156 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
159 m_process_t process = jprocess_to_native_process(jprocess, env);
162 jxbt_throw_notbound(env, "process", jprocess);
166 /* try to suspend the process */
167 if (MSG_OK != MSG_process_suspend(process))
168 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
171 JNIEXPORT void JNICALL
172 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
175 m_process_t process = jprocess_to_native_process(jprocess, env);
178 jxbt_throw_notbound(env, "process", jprocess);
182 /* try to resume the process */
183 if (MSG_OK != MSG_process_resume(process))
184 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
187 JNIEXPORT jboolean JNICALL
188 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
191 m_process_t process = jprocess_to_native_process(jprocess, env);
194 jxbt_throw_notbound(env, "process", jprocess);
198 /* true is the process is suspended, false otherwise */
199 return (jboolean) MSG_process_is_suspended(process);
202 JNIEXPORT void JNICALL
203 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
206 /* get the native instances from the java ones */
207 m_process_t process = jprocess_to_native_process(jprocess, env);
210 jxbt_throw_notbound(env, "process", jprocess);
214 /* delete the global reference */
215 jprocess_delete_global_ref(native_to_java_process(process), env);
217 /* kill the native process (this wrapper is call by the destructor of the java
220 MSG_process_kill(process);
223 JNIEXPORT jobject JNICALL
224 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
227 /* get the native instances from the java ones */
228 m_process_t process = jprocess_to_native_process(jprocess, env);
232 jxbt_throw_notbound(env, "process", jprocess);
236 host = MSG_process_get_host(process);
239 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
243 /* return the global reference to the java host instance */
244 return (jobject) host->data;
248 JNIEXPORT jobject JNICALL
249 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
251 m_process_t process = MSG_process_from_PID(PID);
254 jxbt_throw_process_not_found(env, bprintf("PID = %d", PID));
258 if (!native_to_java_process(process)) {
259 jxbt_throw_native(env, xbt_strdup("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_native(env, xbt_strdup("MSG_process_self() failed"));
307 jprocess = native_to_java_process(process);
310 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
316 JNIEXPORT jint JNICALL
317 Java_simgrid_msg_MsgNative_processSelfPID(JNIEnv * env, jclass cls)
319 return (jint) MSG_process_self_PID();
323 JNIEXPORT jint JNICALL
324 Java_simgrid_msg_MsgNative_processSelfPPID(JNIEnv * env, jclass cls)
326 return (jint) MSG_process_self_PPID();
329 JNIEXPORT void JNICALL
330 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
333 m_host_t host = jhost_get_native(env, jhost);
336 jxbt_throw_notbound(env, "host", jhost);
340 /* try to change the host of the process */
341 if (MSG_OK != MSG_process_change_host(host))
342 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
345 JNIEXPORT void JNICALL
346 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
349 if (MSG_OK != MSG_process_sleep((double) seconds))
350 jxbt_throw_native(env,
351 bprintf("MSG_process_change_host(%f) failed",
356 /***************************************************************************************
357 * The MSG host connected functions implementation. *
358 ***************************************************************************************/
360 JNIEXPORT jobject JNICALL
361 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
364 m_host_t host; /* native host */
365 jobject jhost; /* global reference to the java host instance returned */
367 /* get the C string from the java string */
368 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
370 /* get the host by name (the hosts are created during the grid resolution) */
371 host = MSG_get_host_by_name(name);
372 DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
374 (*env)->ReleaseStringUTFChars(env, jname, name);
376 if (!host) { /* invalid name */
377 jxbt_throw_host_not_found(env, name);
381 if (!host->data) { /* native host not associated yet with java host */
383 /* instanciate a new java host */
384 jhost = jhost_new_instance(env);
387 jxbt_throw_jni(env, "java host instantiation failed");
391 /* get a global reference to the newly created host */
392 jhost = jhost_ref(env, jhost);
395 jxbt_throw_jni(env, "new global ref allocation failed");
399 /* bind the java host and the native host */
400 jhost_bind(jhost, host, env);
402 /* the native host data field is set with the global reference to the
403 * java host returned by this function
405 host->data = (void *) jhost;
408 /* return the global reference to the java host instance */
409 return (jobject) host->data;
412 JNIEXPORT jstring JNICALL
413 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
416 m_host_t host = jhost_get_native(env, jhost);
419 jxbt_throw_notbound(env, "host", jhost);
423 return (*env)->NewStringUTF(env, host->name);
426 JNIEXPORT jint JNICALL
427 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
429 return (jint) MSG_get_host_number();
432 JNIEXPORT jobject JNICALL
433 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
437 m_host_t host = MSG_host_self();
440 /* the native host not yet associated with the java host instance */
442 /* instanciate a new java host instance */
443 jhost = jhost_new_instance(env);
446 jxbt_throw_jni(env, "java host instantiation failed");
450 /* get a global reference to the newly created host */
451 jhost = jhost_ref(env, jhost);
454 jxbt_throw_jni(env, "global ref allocation failed");
458 /* Bind & store it */
459 jhost_bind(jhost, host, env);
460 host->data = (void *) jhost;
462 jhost = (jobject) host->data;
468 JNIEXPORT jdouble JNICALL
469 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
472 m_host_t host = jhost_get_native(env, jhost);
475 jxbt_throw_notbound(env, "host", jhost);
479 return (jdouble) MSG_get_host_speed(host);
482 JNIEXPORT jint JNICALL
483 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
486 m_host_t host = jhost_get_native(env, jhost);
489 jxbt_throw_notbound(env, "host", jhost);
493 return (jint) MSG_get_host_msgload(host);
497 JNIEXPORT jboolean JNICALL
498 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
501 m_host_t host = jhost_get_native(env, jhost);
504 jxbt_throw_notbound(env, "host", jhost);
508 return (jboolean) MSG_host_is_avail(host);
512 /***************************************************************************************
513 * The MSG task connected functions implementation. *
514 ***************************************************************************************/
516 JNIEXPORT void JNICALL
517 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
518 jstring jname, jdouble jcomputeDuration,
519 jdouble jmessageSize)
521 m_task_t task; /* the native task to create */
522 const char *name; /* the name of the task */
524 if (jcomputeDuration < 0) {
525 jxbt_throw_illegal(env,
526 bprintf("Task ComputeDuration (%f) cannot be negative",
527 (double) jcomputeDuration));
531 if (jmessageSize < 0) {
532 jxbt_throw_illegal(env,
533 bprintf("Task MessageSize (%f) cannot be negative",
534 (double) jmessageSize));
539 jxbt_throw_null(env, xbt_strdup("Task name cannot be null"));
543 /* get the C string from the java string */
544 name = (*env)->GetStringUTFChars(env, jname, 0);
546 /* create the task */
548 MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
551 (*env)->ReleaseStringUTFChars(env, jname, name);
553 /* bind & store the task */
554 jtask_bind(jtask, task, env);
556 /* allocate a new global reference to the java task instance */
557 task->data = (void *) jtask_new_global_ref(jtask, env);
560 jxbt_throw_jni(env, "global ref allocation failed");
564 JNIEXPORT void JNICALL
565 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
566 jobject jtask, jstring jname,
569 jcomputeDurations_arg,
570 jdoubleArray jmessageSizes_arg)
573 m_task_t task; /* the native parallel task to create */
574 const char *name; /* the name of the task */
577 double *computeDurations;
578 double *messageSizes;
579 jdouble *jcomputeDurations;
580 jdouble *jmessageSizes;
586 if (!jcomputeDurations_arg) {
589 ("Parallel task compute durations cannot be null"));
593 if (!jmessageSizes_arg) {
595 xbt_strdup("Parallel task message sizes cannot be null"));
600 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
604 host_count = (int) (*env)->GetArrayLength(env, jhosts);
607 hosts = xbt_new0(m_host_t, host_count);
608 computeDurations = xbt_new0(double, host_count);
609 messageSizes = xbt_new0(double, host_count * host_count);
612 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
613 jmessageSizes = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
615 for (index = 0; index < host_count; index++) {
616 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
617 hosts[index] = jhost_get_native(env, jhost);
618 computeDurations[index] = jcomputeDurations[index];
620 for (index = 0; index < host_count * host_count; index++) {
621 messageSizes[index] = jmessageSizes[index];
624 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
625 jcomputeDurations, 0);
626 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
630 /* get the C string from the java string */
631 name = (*env)->GetStringUTFChars(env, jname, 0);
634 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
637 (*env)->ReleaseStringUTFChars(env, jname, name);
639 /* associate the java task object and the native task */
640 jtask_bind(jtask, task, env);
642 task->data = (void *) jtask;
645 jxbt_throw_jni(env, "global ref allocation failed");
648 JNIEXPORT jobject JNICALL
649 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
654 m_task_t task = jtask_to_native_task(jtask, env);
657 jxbt_throw_notbound(env, "task", jtask);
661 process = MSG_task_get_sender(task);
662 return (jobject) native_to_java_process(process);
665 JNIEXPORT jobject JNICALL
666 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
670 m_task_t task = jtask_to_native_task(jtask, env);
673 jxbt_throw_notbound(env, "task", jtask);
677 host = MSG_task_get_source(task);
680 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
684 return (jobject) host->data;
688 JNIEXPORT jstring JNICALL
689 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
692 m_task_t task = jtask_to_native_task(jtask, env);
695 jxbt_throw_notbound(env, "task", jtask);
699 return (*env)->NewStringUTF(env, task->name);
702 JNIEXPORT void JNICALL
703 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
705 m_task_t ptask = jtask_to_native_task(jtask, env);
708 jxbt_throw_notbound(env, "task", jtask);
712 if (MSG_OK != MSG_task_cancel(ptask))
713 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
716 JNIEXPORT jdouble JNICALL
717 Java_simgrid_msg_MsgNative_taskGetComputeDuration(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_compute_duration(ptask);
729 JNIEXPORT jdouble JNICALL
730 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env, jclass cls,
733 m_task_t ptask = jtask_to_native_task(jtask, env);
736 jxbt_throw_notbound(env, "task", jtask);
739 return (jdouble) MSG_task_get_remaining_computation(ptask);
742 JNIEXPORT void JNICALL
743 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
744 jobject jtask, jdouble priority)
746 m_task_t task = jtask_to_native_task(jtask, env);
749 jxbt_throw_notbound(env, "task", jtask);
752 MSG_task_set_priority(task, (double) priority);
755 JNIEXPORT void JNICALL
756 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
760 /* get the native task */
761 m_task_t task = jtask_to_native_task(jtask_arg, env);
765 jxbt_throw_notbound(env, "task", task);
768 jtask = (jobject) task->data;
770 if (MSG_OK != MSG_task_destroy(task))
771 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
773 /* delete the global reference to the java task object */
774 jtask_delete_global_ref(jtask, env);
777 JNIEXPORT void JNICALL
778 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
781 m_task_t task = jtask_to_native_task(jtask, env);
784 jxbt_throw_notbound(env, "task", jtask);
788 if (MSG_OK != MSG_task_execute(task))
789 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
792 /***************************************************************************************
793 * The Task reception functions *
794 ***************************************************************************************/
796 JNIEXPORT jobject JNICALL
797 Java_simgrid_msg_MsgNative_taskGet(JNIEnv * env, jclass cls,
798 jint chan_id, jdouble jtimeout,
801 m_task_t task = NULL;
802 m_host_t host = NULL;
805 host = jhost_get_native(env, jhost);
807 jxbt_throw_notbound(env, "host", jhost);
813 MSG_task_get_ext(&task, (int) chan_id, (double) jtimeout, host)) {
814 jxbt_throw_native(env, xbt_strdup("MSG_task_get_ext() failed"));
818 return (jobject) task->data;
822 JNIEXPORT jboolean JNICALL
823 Java_simgrid_msg_MsgNative_taskProbe(JNIEnv * env, jclass cls, jint chan_id)
825 return (jboolean) MSG_task_Iprobe(chan_id);
828 JNIEXPORT jobject JNICALL
829 Java_simgrid_msg_MsgNative_taskGetCommunicatingProcess(JNIEnv * env,
833 int pid = MSG_task_probe_from(chan_id);
835 return Java_simgrid_msg_MsgNative_processFromPID(env, cls, (jint) pid);
840 JNIEXPORT jint JNICALL
841 Java_simgrid_msg_MsgNative_taskProbeHost(JNIEnv * env, jclass cls,
842 jobject jhost, jint chan_id)
844 m_host_t host = jhost_get_native(env, jhost);
847 jxbt_throw_notbound(env, "host", jhost);
851 return (jint) MSG_task_probe_from_host(chan_id, host);
855 /***************************************************************************************
856 * The Task reception functions *
857 ***************************************************************************************/
860 JNIEXPORT void JNICALL
861 Java_simgrid_msg_MsgNative_hostPut(JNIEnv * env, jclass cls,
862 jobject jhost, jint chan_id, jobject jtask,
865 m_task_t task = jtask_to_native_task(jtask, env);
866 m_host_t host = jhost_get_native(env, jhost);
869 jxbt_throw_notbound(env, "host", jhost);
873 jxbt_throw_notbound(env, "task", jtask);
878 MSG_task_put_with_timeout(task, host, (int) chan_id, (double) jtimeout))
879 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
884 JNIEXPORT void JNICALL
885 Java_simgrid_msg_MsgNative_hostPutBounded(JNIEnv * env, jclass cls,
886 jobject jhost, jint chan_id,
887 jobject jtask, jdouble jmaxRate)
889 m_task_t task = jtask_to_native_task(jtask, env);
890 m_host_t host = jhost_get_native(env, jhost);
893 jxbt_throw_notbound(env, "host", jhost);
897 jxbt_throw_notbound(env, "task", jtask);
902 MSG_task_put_bounded(task, host, (int) chan_id, (double) jmaxRate))
903 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
906 JNIEXPORT jint JNICALL
907 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
909 return (jint) MSG_get_errno();
912 JNIEXPORT jdouble JNICALL
913 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
915 return (jdouble) MSG_get_clock();
919 JNIEXPORT void JNICALL
920 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
930 argc = (int) (*env)->GetArrayLength(env, jargs);
934 argv = xbt_new0(char *, argc);
936 argv[0] = strdup("java");
938 for (index = 0; index < argc - 1; index++) {
939 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
941 tmp = (*env)->GetStringUTFChars(env, jval, 0);
943 argv[index + 1] = strdup(tmp);
945 (*env)->ReleaseStringUTFChars(env, jval, tmp);
948 MSG_global_init(&argc, argv);
949 MSG_set_channel_number(10); /* FIXME: this should not be fixed statically */
950 SIMIX_context_select_factory("java");
952 for (index = 0; index < argc; index++)
957 (*env)->GetJavaVM(env, &__java_vm);
961 JNIEXPORT void JNICALL
962 JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
964 xbt_fifo_item_t item = NULL;
965 m_host_t host = NULL;
969 if (MSG_OK != MSG_main()) {
970 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
973 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
975 DEBUG0("Clean java world");
976 /* Cleanup java hosts */
977 xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
978 jhost = (jobject) host->data;
981 jhost_unref(env, jhost);
984 DEBUG0("Clean native world");
985 /* cleanup native stuff */
986 if (MSG_OK != MSG_clean()){
987 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
991 JNIEXPORT jint JNICALL
992 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
995 return (jint) MSG_process_killall((int) jresetPID);
998 JNIEXPORT void JNICALL
999 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
1000 jstring jplatformFile)
1003 const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
1005 MSG_create_environment(platformFile);
1007 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
1010 JNIEXPORT void JNICALL
1011 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
1015 m_process_t process = jprocess_to_native_process(jprocess, env);
1018 jxbt_throw_notbound(env, "process", jprocess);
1022 SIMIX_context_stop(SIMIX_process_self()->context);
1025 JNIEXPORT void JNICALL
1026 Java_simgrid_msg_Msg_pajeOutput(JNIEnv * env, jclass cls, jstring jpajeFile)
1028 const char *pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
1030 MSG_paje_output(pajeFile);
1032 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
1036 JNIEXPORT void JNICALL
1037 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
1039 const char *s = (*env)->GetStringUTFChars(env, js, 0);
1041 (*env)->ReleaseStringUTFChars(env, js, s);
1044 JNIEXPORT jobjectArray JNICALL
1045 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
1048 jobjectArray jtable;
1053 int count = xbt_fifo_size(msg_global->host);
1054 m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
1056 jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
1062 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
1065 jxbt_throw_jni(env, "Hosts table allocation failed");
1069 for (index = 0; index < count; index++) {
1070 host = table[index];
1071 jhost = (jobject) (host->data);
1074 jname = (*env)->NewStringUTF(env, host->name);
1076 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
1077 /* FIXME: leak of jname ? */
1080 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
1087 JNIEXPORT void JNICALL
1088 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
1091 char *errmsg = NULL;
1094 /* get the C string from the java string */
1095 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
1098 SIMIX_context_select_factory(name);
1100 errmsg = xbt_strdup(e.msg);
1104 (*env)->ReleaseStringUTFChars(env, jname, name);
1107 char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
1109 jxbt_throw_native(env, thrown);
1113 JNIEXPORT void JNICALL
1114 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
1115 jstring jalias, jobject jtask,
1120 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1122 m_task_t task = jtask_to_native_task(jtask, env);
1126 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1127 jxbt_throw_notbound(env, "task", jtask);
1131 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
1133 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1136 jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
1140 JNIEXPORT void JNICALL
1141 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1142 jstring jalias, jobject jtask,
1145 m_task_t task = jtask_to_native_task(jtask, env);
1150 jxbt_throw_notbound(env, "task", jtask);
1154 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1156 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1158 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1161 jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1164 JNIEXPORT jobject JNICALL
1165 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1166 jstring jalias, jdouble jtimeout,
1170 m_task_t task = NULL;
1171 m_host_t host = NULL;
1175 host = jhost_get_native(env, jhost);
1178 jxbt_throw_notbound(env, "host", jhost);
1183 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1185 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1187 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1190 jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1194 return (jobject) task->data;
1197 JNIEXPORT jboolean JNICALL
1198 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1205 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1207 rv = MSG_task_listen(alias);
1209 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1211 return (jboolean) rv;
1214 JNIEXPORT jint JNICALL
1215 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1216 jstring jalias, jobject jhost)
1222 m_host_t host = jhost_get_native(env, jhost);
1225 jxbt_throw_notbound(env, "host", jhost);
1229 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1231 rv = MSG_task_listen_from_host(alias, host);
1233 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1238 JNIEXPORT jint JNICALL
1239 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1244 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1246 rv = MSG_task_listen_from(alias);
1248 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1253 JNIEXPORT void JNICALL
1254 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1255 jstring jdeploymentFile)
1258 const char *deploymentFile =
1259 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1261 surf_parse_reset_parser();
1263 surfxml_add_callback(STag_surfxml_process_cb_list,
1264 japplication_handler_on_begin_process);
1266 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1267 japplication_handler_on_process_arg);
1269 surfxml_add_callback(STag_surfxml_prop_cb_list,
1270 japplication_handler_on_property);
1272 surfxml_add_callback(ETag_surfxml_process_cb_list,
1273 japplication_handler_on_end_process);
1275 surf_parse_open(deploymentFile);
1277 japplication_handler_on_start_document();
1280 jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
1284 japplication_handler_on_end_document();
1286 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);