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 "xbt/xbt_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 ((xbt_ctx_java_t)
55 (process->simdata->s_process->simdata->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 SIMIX_jprocess_create(process->name,
129 process->simdata->m_host->simdata->smx_host,
130 /*data */ (void *) process,
131 jprocess, env, &process->simdata->s_process);
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);
152 MSG_mailbox_set_hostname(mailbox,
153 process->simdata->m_host->simdata->smx_host->name);
158 JNIEXPORT void JNICALL
159 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
162 m_process_t process = jprocess_to_native_process(jprocess, env);
165 jxbt_throw_notbound(env, "process", jprocess);
169 /* try to suspend the process */
170 if (MSG_OK != MSG_process_suspend(process))
171 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
174 JNIEXPORT void JNICALL
175 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
178 m_process_t process = jprocess_to_native_process(jprocess, env);
181 jxbt_throw_notbound(env, "process", jprocess);
185 /* try to resume the process */
186 if (MSG_OK != MSG_process_resume(process))
187 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
190 JNIEXPORT jboolean JNICALL
191 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
194 m_process_t process = jprocess_to_native_process(jprocess, env);
197 jxbt_throw_notbound(env, "process", jprocess);
201 /* true is the process is suspended, false otherwise */
202 return (jboolean) MSG_process_is_suspended(process);
205 JNIEXPORT void JNICALL
206 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
209 /* get the native instances from the java ones */
210 m_process_t process = jprocess_to_native_process(jprocess, env);
213 jxbt_throw_notbound(env, "process", jprocess);
217 /* delete the global reference */
218 jprocess_delete_global_ref(native_to_java_process(process), env);
220 /* kill the native process (this wrapper is call by the destructor of the java
223 MSG_process_kill(process);
226 JNIEXPORT jobject JNICALL
227 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
230 /* get the native instances from the java ones */
231 m_process_t process = jprocess_to_native_process(jprocess, env);
235 jxbt_throw_notbound(env, "process", jprocess);
239 host = MSG_process_get_host(process);
242 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
246 /* return the global reference to the java host instance */
247 return (jobject) host->data;
251 JNIEXPORT jobject JNICALL
252 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
254 m_process_t process = MSG_process_from_PID(PID);
257 jxbt_throw_process_not_found(env, bprintf("PID = %d", PID));
261 if (!native_to_java_process(process)) {
262 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
266 return (jobject) (native_to_java_process(process));
270 JNIEXPORT jint JNICALL
271 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
274 m_process_t process = jprocess_to_native_process(jprocess, env);
277 jxbt_throw_notbound(env, "process", jprocess);
281 return (jint) MSG_process_get_PID(process);
285 JNIEXPORT jint JNICALL
286 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
289 m_process_t process = jprocess_to_native_process(jprocess, env);
292 jxbt_throw_notbound(env, "process", jprocess);
296 return (jint) MSG_process_get_PPID(process);
299 JNIEXPORT jobject JNICALL
300 Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
302 m_process_t process = MSG_process_self();
306 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
310 jprocess = native_to_java_process(process);
313 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
319 JNIEXPORT jint JNICALL
320 Java_simgrid_msg_MsgNative_processSelfPID(JNIEnv * env, jclass cls)
322 return (jint) MSG_process_self_PID();
326 JNIEXPORT jint JNICALL
327 Java_simgrid_msg_MsgNative_processSelfPPID(JNIEnv * env, jclass cls)
329 return (jint) MSG_process_self_PPID();
332 JNIEXPORT void JNICALL
333 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
336 m_host_t host = jhost_get_native(env, jhost);
339 jxbt_throw_notbound(env, "host", jhost);
343 /* try to change the host of the process */
344 if (MSG_OK != MSG_process_change_host(host))
345 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
348 JNIEXPORT void JNICALL
349 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
352 if (MSG_OK != MSG_process_sleep((double) seconds))
353 jxbt_throw_native(env,
354 bprintf("MSG_process_change_host(%f) failed",
359 /***************************************************************************************
360 * The MSG host connected functions implementation. *
361 ***************************************************************************************/
363 JNIEXPORT jobject JNICALL
364 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
367 m_host_t host; /* native host */
368 jobject jhost; /* global reference to the java host instance returned */
370 /* get the C string from the java string */
371 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
373 /* get the host by name (the hosts are created during the grid resolution) */
374 host = MSG_get_host_by_name(name);
375 DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
377 (*env)->ReleaseStringUTFChars(env, jname, name);
379 if (!host) { /* invalid name */
380 jxbt_throw_host_not_found(env, name);
384 if (!host->data) { /* native host not associated yet with java host */
386 /* instanciate a new java host */
387 jhost = jhost_new_instance(env);
390 jxbt_throw_jni(env, "java host instantiation failed");
394 /* get a global reference to the newly created host */
395 jhost = jhost_ref(env, jhost);
398 jxbt_throw_jni(env, "new global ref allocation failed");
402 /* bind the java host and the native host */
403 jhost_bind(jhost, host, env);
405 /* the native host data field is set with the global reference to the
406 * java host returned by this function
408 host->data = (void *) jhost;
411 /* return the global reference to the java host instance */
412 return (jobject) host->data;
415 JNIEXPORT jstring JNICALL
416 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
419 m_host_t host = jhost_get_native(env, jhost);
422 jxbt_throw_notbound(env, "host", jhost);
426 return (*env)->NewStringUTF(env, host->name);
429 JNIEXPORT jint JNICALL
430 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
432 return (jint) MSG_get_host_number();
435 JNIEXPORT jobject JNICALL
436 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
440 m_host_t host = MSG_host_self();
443 /* the native host not yet associated with the java host instance */
445 /* instanciate a new java host instance */
446 jhost = jhost_new_instance(env);
449 jxbt_throw_jni(env, "java host instantiation failed");
453 /* get a global reference to the newly created host */
454 jhost = jhost_ref(env, jhost);
457 jxbt_throw_jni(env, "global ref allocation failed");
461 /* Bind & store it */
462 jhost_bind(jhost, host, env);
463 host->data = (void *) jhost;
465 jhost = (jobject) host->data;
471 JNIEXPORT jdouble JNICALL
472 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
475 m_host_t host = jhost_get_native(env, jhost);
478 jxbt_throw_notbound(env, "host", jhost);
482 return (jdouble) MSG_get_host_speed(host);
485 JNIEXPORT jint JNICALL
486 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
489 m_host_t host = jhost_get_native(env, jhost);
492 jxbt_throw_notbound(env, "host", jhost);
496 return (jint) MSG_get_host_msgload(host);
500 JNIEXPORT jboolean JNICALL
501 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
504 m_host_t host = jhost_get_native(env, jhost);
507 jxbt_throw_notbound(env, "host", jhost);
511 return (jboolean) MSG_host_is_avail(host);
515 /***************************************************************************************
516 * The MSG task connected functions implementation. *
517 ***************************************************************************************/
519 JNIEXPORT void JNICALL
520 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
521 jstring jname, jdouble jcomputeDuration,
522 jdouble jmessageSize)
524 m_task_t task; /* the native task to create */
525 const char *name; /* the name of the task */
527 if (jcomputeDuration < 0) {
528 jxbt_throw_illegal(env,
529 bprintf("Task ComputeDuration (%f) cannot be negative",
530 (double) jcomputeDuration));
534 if (jmessageSize < 0) {
535 jxbt_throw_illegal(env,
536 bprintf("Task MessageSize (%f) cannot be negative",
537 (double) jmessageSize));
542 jxbt_throw_null(env, xbt_strdup("Task name cannot be null"));
546 /* get the C string from the java string */
547 name = (*env)->GetStringUTFChars(env, jname, 0);
549 /* create the task */
551 MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
554 (*env)->ReleaseStringUTFChars(env, jname, name);
556 /* bind & store the task */
557 jtask_bind(jtask, task, env);
559 /* allocate a new global reference to the java task instance */
560 task->data = (void *) jtask_new_global_ref(jtask, env);
563 jxbt_throw_jni(env, "global ref allocation failed");
567 JNIEXPORT void JNICALL
568 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
569 jobject jtask, jstring jname,
572 jcomputeDurations_arg,
573 jdoubleArray jmessageSizes_arg)
576 m_task_t task; /* the native parallel task to create */
577 const char *name; /* the name of the task */
580 double *computeDurations;
581 double *messageSizes;
582 jdouble *jcomputeDurations;
583 jdouble *jmessageSizes;
589 if (!jcomputeDurations_arg) {
592 ("Parallel task compute durations cannot be null"));
596 if (!jmessageSizes_arg) {
598 xbt_strdup("Parallel task message sizes cannot be null"));
603 jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
607 host_count = (int) (*env)->GetArrayLength(env, jhosts);
610 hosts = xbt_new0(m_host_t, host_count);
611 computeDurations = xbt_new0(double, host_count);
612 messageSizes = xbt_new0(double, host_count * host_count);
615 (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
616 jmessageSizes = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
618 for (index = 0; index < host_count; index++) {
619 jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
620 hosts[index] = jhost_get_native(env, jhost);
621 computeDurations[index] = jcomputeDurations[index];
623 for (index = 0; index < host_count * host_count; index++) {
624 messageSizes[index] = jmessageSizes[index];
627 (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
628 jcomputeDurations, 0);
629 (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
633 /* get the C string from the java string */
634 name = (*env)->GetStringUTFChars(env, jname, 0);
637 MSG_parallel_task_create(name, host_count, hosts, computeDurations,
640 (*env)->ReleaseStringUTFChars(env, jname, name);
642 /* associate the java task object and the native task */
643 jtask_bind(jtask, task, env);
645 task->data = (void *) jtask;
648 jxbt_throw_jni(env, "global ref allocation failed");
651 JNIEXPORT jobject JNICALL
652 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
657 m_task_t task = jtask_to_native_task(jtask, env);
660 jxbt_throw_notbound(env, "task", jtask);
664 process = MSG_task_get_sender(task);
665 return (jobject) native_to_java_process(process);
668 JNIEXPORT jobject JNICALL
669 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
673 m_task_t task = jtask_to_native_task(jtask, env);
676 jxbt_throw_notbound(env, "task", jtask);
680 host = MSG_task_get_source(task);
683 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
687 return (jobject) host->data;
691 JNIEXPORT jstring JNICALL
692 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
695 m_task_t task = jtask_to_native_task(jtask, env);
698 jxbt_throw_notbound(env, "task", jtask);
702 return (*env)->NewStringUTF(env, task->name);
705 JNIEXPORT void JNICALL
706 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
708 m_task_t ptask = jtask_to_native_task(jtask, env);
711 jxbt_throw_notbound(env, "task", jtask);
715 if (MSG_OK != MSG_task_cancel(ptask))
716 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
719 JNIEXPORT jdouble JNICALL
720 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
723 m_task_t ptask = jtask_to_native_task(jtask, env);
726 jxbt_throw_notbound(env, "task", jtask);
729 return (jdouble) MSG_task_get_compute_duration(ptask);
732 JNIEXPORT jdouble JNICALL
733 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env, jclass cls,
736 m_task_t ptask = jtask_to_native_task(jtask, env);
739 jxbt_throw_notbound(env, "task", jtask);
742 return (jdouble) MSG_task_get_remaining_computation(ptask);
745 JNIEXPORT void JNICALL
746 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
747 jobject jtask, jdouble priority)
749 m_task_t task = jtask_to_native_task(jtask, env);
752 jxbt_throw_notbound(env, "task", jtask);
755 MSG_task_set_priority(task, (double) priority);
758 JNIEXPORT void JNICALL
759 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
763 /* get the native task */
764 m_task_t task = jtask_to_native_task(jtask_arg, env);
768 jxbt_throw_notbound(env, "task", task);
771 jtask = (jobject) task->data;
773 if (MSG_OK != MSG_task_destroy(task))
774 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
776 /* delete the global reference to the java task object */
777 jtask_delete_global_ref(jtask, env);
780 JNIEXPORT void JNICALL
781 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
784 m_task_t task = jtask_to_native_task(jtask, env);
787 jxbt_throw_notbound(env, "task", jtask);
791 if (MSG_OK != MSG_task_execute(task))
792 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
795 /***************************************************************************************
796 * The Task reception functions *
797 ***************************************************************************************/
799 JNIEXPORT jobject JNICALL
800 Java_simgrid_msg_MsgNative_taskGet(JNIEnv * env, jclass cls,
801 jint chan_id, jdouble jtimeout,
804 m_task_t task = NULL;
805 m_host_t host = NULL;
808 host = jhost_get_native(env, jhost);
810 jxbt_throw_notbound(env, "host", jhost);
816 MSG_task_get_ext(&task, (int) chan_id, (double) jtimeout, host)) {
817 jxbt_throw_native(env, xbt_strdup("MSG_task_get_ext() failed"));
821 return (jobject) task->data;
825 JNIEXPORT jboolean JNICALL
826 Java_simgrid_msg_MsgNative_taskProbe(JNIEnv * env, jclass cls, jint chan_id)
828 return (jboolean) MSG_task_Iprobe(chan_id);
831 JNIEXPORT jobject JNICALL
832 Java_simgrid_msg_MsgNative_taskGetCommunicatingProcess(JNIEnv * env,
836 int pid = MSG_task_probe_from(chan_id);
838 return Java_simgrid_msg_MsgNative_processFromPID(env, cls, (jint) pid);
843 JNIEXPORT jint JNICALL
844 Java_simgrid_msg_MsgNative_taskProbeHost(JNIEnv * env, jclass cls,
845 jobject jhost, jint chan_id)
847 m_host_t host = jhost_get_native(env, jhost);
850 jxbt_throw_notbound(env, "host", jhost);
854 return (jint) MSG_task_probe_from_host(chan_id, host);
858 /***************************************************************************************
859 * The Task reception functions *
860 ***************************************************************************************/
863 JNIEXPORT void JNICALL
864 Java_simgrid_msg_MsgNative_hostPut(JNIEnv * env, jclass cls,
865 jobject jhost, jint chan_id, jobject jtask,
868 m_task_t task = jtask_to_native_task(jtask, env);
869 m_host_t host = jhost_get_native(env, jhost);
872 jxbt_throw_notbound(env, "host", jhost);
876 jxbt_throw_notbound(env, "task", jtask);
881 MSG_task_put_with_timeout(task, host, (int) chan_id, (double) jtimeout))
882 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
887 JNIEXPORT void JNICALL
888 Java_simgrid_msg_MsgNative_hostPutBounded(JNIEnv * env, jclass cls,
889 jobject jhost, jint chan_id,
890 jobject jtask, jdouble jmaxRate)
892 m_task_t task = jtask_to_native_task(jtask, env);
893 m_host_t host = jhost_get_native(env, jhost);
896 jxbt_throw_notbound(env, "host", jhost);
900 jxbt_throw_notbound(env, "task", jtask);
905 MSG_task_put_bounded(task, host, (int) chan_id, (double) jmaxRate))
906 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
909 JNIEXPORT jint JNICALL
910 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
912 return (jint) MSG_get_errno();
915 JNIEXPORT jdouble JNICALL
916 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
918 return (jdouble) MSG_get_clock();
922 JNIEXPORT void JNICALL
923 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
933 argc = (int) (*env)->GetArrayLength(env, jargs);
937 argv = xbt_new0(char *, argc);
939 argv[0] = strdup("java");
941 for (index = 0; index < argc - 1; index++) {
942 jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
944 tmp = (*env)->GetStringUTFChars(env, jval, 0);
946 argv[index + 1] = strdup(tmp);
948 (*env)->ReleaseStringUTFChars(env, jval, tmp);
951 MSG_global_init(&argc, argv);
952 MSG_set_channel_number(10); /* FIXME: this should not be fixed statically */
954 for (index = 0; index < argc; index++)
959 (*env)->GetJavaVM(env, &__java_vm);
963 JNIEXPORT void JNICALL
964 JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
966 xbt_fifo_item_t item = NULL;
967 m_host_t host = NULL;
971 if (MSG_OK != MSG_main())
972 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
975 ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
977 DEBUG0("Clean java world");
978 /* Cleanup java hosts */
979 xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
980 jhost = (jobject) host->data;
983 jhost_unref(env, jhost);
986 DEBUG0("Clean native world");
987 /* cleanup native stuff */
988 if (MSG_OK != MSG_clean())
989 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
993 JNIEXPORT jint JNICALL
994 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
997 return (jint) MSG_process_killall((int) jresetPID);
1000 JNIEXPORT void JNICALL
1001 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
1002 jstring jplatformFile)
1005 const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
1007 MSG_create_environment(platformFile);
1009 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
1012 JNIEXPORT void JNICALL
1013 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
1017 m_process_t process = jprocess_to_native_process(jprocess, env);
1020 jxbt_throw_notbound(env, "process", jprocess);
1024 xbt_context_stop(0);
1027 JNIEXPORT void JNICALL
1028 Java_simgrid_msg_Msg_pajeOutput(JNIEnv * env, jclass cls, jstring jpajeFile)
1030 const char *pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
1032 MSG_paje_output(pajeFile);
1034 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
1038 JNIEXPORT void JNICALL
1039 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
1041 const char *s = (*env)->GetStringUTFChars(env, js, 0);
1043 (*env)->ReleaseStringUTFChars(env, js, s);
1046 JNIEXPORT jobjectArray JNICALL
1047 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
1050 jobjectArray jtable;
1055 int count = xbt_fifo_size(msg_global->host);
1056 m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
1058 jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
1064 jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
1067 jxbt_throw_jni(env, "Hosts table allocation failed");
1071 for (index = 0; index < count; index++) {
1072 host = table[index];
1073 jhost = (jobject) (host->data);
1076 jname = (*env)->NewStringUTF(env, host->name);
1078 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
1079 /* FIXME: leak of jname ? */
1082 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
1089 JNIEXPORT void JNICALL
1090 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
1093 char *errmsg = NULL;
1096 /* get the C string from the java string */
1097 const char *name = (*env)->GetStringUTFChars(env, jname, 0);
1100 xbt_context_select_factory(name);
1102 errmsg = xbt_strdup(e.msg);
1106 (*env)->ReleaseStringUTFChars(env, jname, name);
1109 char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
1111 jxbt_throw_native(env, thrown);
1115 JNIEXPORT void JNICALL
1116 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
1117 jstring jalias, jobject jtask,
1122 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1124 m_task_t task = jtask_to_native_task(jtask, env);
1128 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1129 jxbt_throw_notbound(env, "task", jtask);
1133 rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
1135 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1138 jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
1142 JNIEXPORT void JNICALL
1143 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1144 jstring jalias, jobject jtask,
1147 m_task_t task = jtask_to_native_task(jtask, env);
1152 jxbt_throw_notbound(env, "task", jtask);
1156 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1158 rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1160 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1163 jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1166 JNIEXPORT jobject JNICALL
1167 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1168 jstring jalias, jdouble jtimeout,
1172 m_task_t task = NULL;
1173 m_host_t host = NULL;
1177 host = jhost_get_native(env, jhost);
1180 jxbt_throw_notbound(env, "host", jhost);
1185 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1187 rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1189 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1192 jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1196 return (jobject) task->data;
1199 JNIEXPORT jboolean JNICALL
1200 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1207 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1209 rv = MSG_task_listen(alias);
1211 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1213 return (jboolean) rv;
1216 JNIEXPORT jint JNICALL
1217 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1218 jstring jalias, jobject jhost)
1224 m_host_t host = jhost_get_native(env, jhost);
1227 jxbt_throw_notbound(env, "host", jhost);
1231 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1233 rv = MSG_task_listen_from_host(alias, host);
1235 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1240 JNIEXPORT jint JNICALL
1241 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1246 const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1248 rv = MSG_task_listen_from(alias);
1250 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1255 JNIEXPORT void JNICALL
1256 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1257 jstring jdeploymentFile)
1260 const char *deploymentFile =
1261 (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1263 surf_parse_reset_parser();
1265 surfxml_add_callback(STag_surfxml_process_cb_list,
1266 japplication_handler_on_begin_process);
1268 surfxml_add_callback(ETag_surfxml_argument_cb_list,
1269 japplication_handler_on_process_arg);
1271 surfxml_add_callback(STag_surfxml_prop_cb_list,
1272 japplication_handler_on_property);
1274 surfxml_add_callback(ETag_surfxml_process_cb_list,
1275 japplication_handler_on_end_process);
1277 surf_parse_open(deploymentFile);
1279 japplication_handler_on_start_document();
1282 jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
1286 japplication_handler_on_end_document();
1288 (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);