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 "jxbt_utilities.h"
25 #include "msg/mailbox.h"
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
29 static JavaVM * __java_vm = NULL;
32 native_to_java_process(m_process_t process);
40 get_current_thread_env(void) {
43 (*__java_vm)->AttachCurrentThread(__java_vm, (void **)&env, NULL);
49 native_to_java_process(m_process_t process)
51 return ((xbt_ctx_java_t)(process->simdata->s_process->simdata->context))->jprocess;
56 * The MSG process connected functions implementation.
59 JNIEXPORT void JNICALL
60 Java_simgrid_msg_MsgNative_processCreate(JNIEnv* env, jclass cls, 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;
68 DEBUG4("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
69 env,cls,jprocess_arg,jhost);
70 /* get the name of the java process */
71 jname = jprocess_get_name(jprocess_arg,env);
74 jxbt_throw_null(env,xbt_strdup("Internal error: Process name cannot be NULL"));
78 /* allocate the data of the simulation */
79 process = xbt_new0(s_m_process_t,1);
80 process->simdata = xbt_new0(s_simdata_process_t,1);
82 /* create a global java process instance */
83 jprocess = jprocess_new_global_ref(jprocess_arg,env);
86 free(process->simdata);
88 jxbt_throw_jni(env, "Can't get a global ref to the java process");
92 /* bind the java process instance to the native process */
93 jprocess_bind(jprocess,process,env);
95 /* build the C name of the process */
96 name = (*env)->GetStringUTFChars(env, jname, 0);
97 process->name = xbt_strdup(name);
98 (*env)->ReleaseStringUTFChars(env, jname, name);
100 process->simdata->m_host = jhost_get_native(env,jhost);
103 if( ! (process->simdata->m_host) ) { /* not binded */
104 free(process->simdata);
107 jxbt_throw_notbound(env,"host",jhost);
110 process->simdata->PID = msg_global->PID++;
112 /* create a new context */
113 DEBUG8("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
114 process->name,process->simdata->m_host->name,
115 process->simdata->PID,process,
116 process->simdata, process->simdata->m_host, process->simdata->m_host->simdata,
119 SIMIX_jprocess_create(process->name,
120 process->simdata->m_host->simdata->smx_host,
121 /*data*/ (void*)process,
124 &process->simdata->s_process);
127 DEBUG1("context created (s_process=%p)",process->simdata->s_process);
130 if (SIMIX_process_self()) { /* someone created me */
131 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
133 process->simdata->PPID = -1;
136 process->simdata->last_errno = MSG_OK;
138 /* add the process to the list of the processes of the simulation */
139 xbt_fifo_unshift(msg_global->process_list, process);
141 sprintf(alias,"%s:%s",(process->simdata->m_host->simdata->smx_host)->name,process->name);
143 mailbox = MSG_mailbox_new(alias);
144 MSG_mailbox_set_hostname(mailbox, process->simdata->m_host->simdata->smx_host->name);
149 JNIEXPORT void JNICALL
150 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv* env, jclass cls, jobject jprocess) {
151 m_process_t process = jprocess_to_native_process(jprocess,env);
154 jxbt_throw_notbound(env,"process",jprocess);
158 /* try to suspend the process */
159 if(MSG_OK != MSG_process_suspend(process))
160 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
163 JNIEXPORT void JNICALL
164 Java_simgrid_msg_MsgNative_processResume(JNIEnv* env, jclass cls, jobject jprocess) {
165 m_process_t process = jprocess_to_native_process(jprocess,env);
168 jxbt_throw_notbound(env,"process",jprocess);
172 /* try to resume the process */
173 if(MSG_OK != MSG_process_resume(process))
174 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
177 JNIEXPORT jboolean JNICALL
178 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv* env, jclass cls, jobject jprocess) {
179 m_process_t process = jprocess_to_native_process(jprocess,env);
182 jxbt_throw_notbound(env,"process",jprocess);
186 /* true is the process is suspended, false otherwise */
187 return (jboolean)MSG_process_is_suspended(process);
190 JNIEXPORT void JNICALL
191 Java_simgrid_msg_MsgNative_processKill(JNIEnv* env, jclass cls, jobject jprocess) {
192 /* get the native instances from the java ones */
193 m_process_t process = jprocess_to_native_process(jprocess,env);
196 jxbt_throw_notbound(env,"process",jprocess);
200 /* delete the global reference */
201 jprocess_delete_global_ref(native_to_java_process(process),env);
203 /* kill the native process (this wrapper is call by the destructor of the java
206 MSG_process_kill(process);
209 JNIEXPORT jobject JNICALL
210 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv* env, jclass cls, jobject jprocess) {
211 /* get the native instances from the java ones */
212 m_process_t process = jprocess_to_native_process(jprocess,env);
216 jxbt_throw_notbound(env,"process",jprocess);
220 host = MSG_process_get_host(process);
223 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
227 /* return the global reference to the java host instance */
228 return (jobject)host->data;
232 JNIEXPORT jobject JNICALL
233 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv* env, jclass cls, jint PID) {
234 m_process_t process = MSG_process_from_PID(PID);
237 jxbt_throw_process_not_found(env, bprintf("PID = %d",PID));
241 if(!native_to_java_process(process)) {
242 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
246 return (jobject)(native_to_java_process(process));
250 JNIEXPORT jint JNICALL
251 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv* env, jclass cls, jobject jprocess) {
252 m_process_t process = jprocess_to_native_process(jprocess,env);
255 jxbt_throw_notbound(env,"process",jprocess);
259 return (jint)MSG_process_get_PID(process);
263 JNIEXPORT jint JNICALL
264 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv* env, jclass cls, jobject jprocess) {
265 m_process_t process = jprocess_to_native_process(jprocess,env);
268 jxbt_throw_notbound(env,"process",jprocess);
272 return (jint)MSG_process_get_PPID(process);
275 JNIEXPORT jobject JNICALL
276 Java_simgrid_msg_MsgNative_processSelf(JNIEnv* env, jclass cls) {
277 m_process_t process = MSG_process_self();
281 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
285 jprocess = native_to_java_process(process);
288 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
294 JNIEXPORT jint JNICALL
295 Java_simgrid_msg_MsgNative_processSelfPID(JNIEnv* env, jclass cls) {
296 return (jint)MSG_process_self_PID();
300 JNIEXPORT jint JNICALL
301 Java_simgrid_msg_MsgNative_processSelfPPID(JNIEnv* env, jclass cls) {
302 return (jint)MSG_process_self_PPID();
305 JNIEXPORT void JNICALL
306 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv* env, jclass cls, jobject jhost){
307 m_host_t host = jhost_get_native(env,jhost);
310 jxbt_throw_notbound(env,"host",jhost);
314 /* try to change the host of the process */
315 if(MSG_OK != MSG_process_change_host(host))
316 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
319 JNIEXPORT void JNICALL
320 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv* env, jclass cls,jdouble seconds) {
321 if(MSG_OK != MSG_process_sleep((double)seconds))
322 jxbt_throw_native(env,
323 bprintf("MSG_process_change_host(%f) failed", (double)seconds));
327 /***************************************************************************************
328 * The MSG host connected functions implementation. *
329 ***************************************************************************************/
331 JNIEXPORT jobject JNICALL
332 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv* env, jclass cls, jstring jname) {
333 m_host_t host; /* native host */
334 jobject jhost; /* global reference to the java host instance returned */
336 /* get the C string from the java string*/
337 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
339 /* get the host by name (the hosts are created during the grid resolution) */
340 host = MSG_get_host_by_name(name);
341 DEBUG2("MSG gave %p as native host (simdata=%p)",host,host->simdata);
343 (*env)->ReleaseStringUTFChars(env, jname, name);
345 if(!host) {/* invalid name */
346 jxbt_throw_host_not_found(env,name);
350 if(!host->data) { /* native host not associated yet with java host */
352 /* instanciate a new java host */
353 jhost = jhost_new_instance(env);
356 jxbt_throw_jni(env,"java host instantiation failed");
360 /* get a global reference to the newly created host */
361 jhost = jhost_ref(env,jhost);
364 jxbt_throw_jni(env,"new global ref allocation failed");
368 /* bind the java host and the native host */
369 jhost_bind(jhost,host,env);
371 /* the native host data field is set with the global reference to the
372 * java host returned by this function
374 host->data = (void*)jhost;
377 /* return the global reference to the java host instance */
378 return (jobject)host->data;
381 JNIEXPORT jstring JNICALL
382 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv* env, jclass cls, jobject jhost) {
383 m_host_t host = jhost_get_native(env,jhost);
386 jxbt_throw_notbound(env,"host",jhost);
390 return (*env)->NewStringUTF(env,host->name);
393 JNIEXPORT jint JNICALL
394 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv* env, jclass cls) {
395 return (jint)MSG_get_host_number();
398 JNIEXPORT jobject JNICALL
399 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv* env, jclass cls) {
402 m_host_t host = MSG_host_self();
405 /* the native host not yet associated with the java host instance */
407 /* instanciate a new java host instance */
408 jhost = jhost_new_instance(env);
411 jxbt_throw_jni(env,"java host instantiation failed");
415 /* get a global reference to the newly created host */
416 jhost = jhost_ref(env,jhost);
419 jxbt_throw_jni(env,"global ref allocation failed");
423 /* Bind & store it */
424 jhost_bind(jhost,host,env);
425 host->data = (void*)jhost;
427 jhost = (jobject)host->data;
433 JNIEXPORT jdouble JNICALL
434 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv* env, jclass cls, jobject jhost) {
435 m_host_t host = jhost_get_native(env,jhost);
438 jxbt_throw_notbound(env,"host",jhost);
442 return (jdouble)MSG_get_host_speed(host);
445 JNIEXPORT jint JNICALL
446 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv* env, jclass cls, jobject jhost) {
447 m_host_t host = jhost_get_native(env,jhost);
450 jxbt_throw_notbound(env,"host",jhost);
454 return (jint)MSG_get_host_msgload(host);
458 JNIEXPORT jboolean JNICALL
459 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv* env, jclass cls, jobject jhost) {
460 m_host_t host = jhost_get_native(env,jhost);
463 jxbt_throw_notbound(env,"host",jhost);
467 return (jboolean)MSG_host_is_avail(host);
471 /***************************************************************************************
472 * The MSG task connected functions implementation. *
473 ***************************************************************************************/
475 JNIEXPORT void JNICALL
476 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
477 jdouble jcomputeDuration, jdouble jmessageSize) {
478 m_task_t task; /* the native task to create */
479 const char* name; /* the name of the task */
481 if(jcomputeDuration < 0) {
482 jxbt_throw_illegal(env,bprintf("Task ComputeDuration (%f) cannot be negative",
483 (double)jcomputeDuration));
487 if(jmessageSize < 0) {
488 jxbt_throw_illegal(env,bprintf("Task MessageSize (%f) cannot be negative",
489 (double)jmessageSize));
494 jxbt_throw_null(env,xbt_strdup("Task name cannot be null"));
498 /* get the C string from the java string*/
499 name = (*env)->GetStringUTFChars(env, jname, 0);
501 /* create the task */
502 task = MSG_task_create(name,(double)jcomputeDuration,(double)jmessageSize,NULL);
504 (*env)->ReleaseStringUTFChars(env, jname, name);
506 /* bind & store the task */
507 jtask_bind(jtask,task,env);
509 /* allocate a new global reference to the java task instance */
510 task->data = (void*)jtask_new_global_ref(jtask,env);
513 jxbt_throw_jni(env,"global ref allocation failed");
517 JNIEXPORT void JNICALL
518 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
519 jobjectArray jhosts,jdoubleArray jcomputeDurations_arg, jdoubleArray jmessageSizes_arg) {
521 m_task_t task; /* the native parallel task to create */
522 const char* name; /* the name of the task */
525 double* computeDurations;
526 double* messageSizes;
527 jdouble *jcomputeDurations;
528 jdouble *jmessageSizes;
534 if (!jcomputeDurations_arg) {
535 jxbt_throw_null(env,xbt_strdup("Parallel task compute durations cannot be null"));
539 if(!jmessageSizes_arg){
540 jxbt_throw_null(env,xbt_strdup("Parallel task message sizes cannot be null"));
545 jxbt_throw_null(env,xbt_strdup("Parallel task name cannot be null"));
549 host_count = (int)(*env)->GetArrayLength(env,jhosts);
552 hosts = xbt_new0(m_host_t,host_count);
553 computeDurations = xbt_new0(double,host_count);
554 messageSizes = xbt_new0(double,host_count*host_count);
556 jcomputeDurations = (*env)->GetDoubleArrayElements(env,jcomputeDurations_arg, 0);
557 jmessageSizes = (*env)->GetDoubleArrayElements(env,jmessageSizes_arg, 0);
559 for(index = 0; index < host_count; index++) {
560 jhost = (*env)->GetObjectArrayElement(env,jhosts,index);
561 hosts[index] = jhost_get_native(env,jhost);
562 computeDurations[index] = jcomputeDurations[index];
564 for(index = 0; index < host_count*host_count; index++) {
565 messageSizes[index] = jmessageSizes[index];
568 (*env)->ReleaseDoubleArrayElements(env,jcomputeDurations_arg,jcomputeDurations,0);
569 (*env)->ReleaseDoubleArrayElements(env,jmessageSizes_arg,jmessageSizes,0);
572 /* get the C string from the java string*/
573 name = (*env)->GetStringUTFChars(env, jname, 0);
575 task = MSG_parallel_task_create(name,host_count,hosts,computeDurations,messageSizes,NULL);
577 (*env)->ReleaseStringUTFChars(env, jname, name);
579 /* associate the java task object and the native task */
580 jtask_bind(jtask,task,env);
582 task->data = (void*)jtask;
585 jxbt_throw_jni(env,"global ref allocation failed");
588 JNIEXPORT jobject JNICALL
589 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
592 m_task_t task = jtask_to_native_task(jtask,env);
595 jxbt_throw_notbound(env,"task",jtask);
599 process = MSG_task_get_sender(task);
600 return (jobject)native_to_java_process(process);
603 JNIEXPORT jobject JNICALL
604 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
606 m_task_t task = jtask_to_native_task(jtask,env);
609 jxbt_throw_notbound(env,"task",jtask);
613 host = MSG_task_get_source(task);
616 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
620 return (jobject)host->data;
624 JNIEXPORT jstring JNICALL
625 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv* env, jclass cls, jobject jtask) {
626 m_task_t task = jtask_to_native_task(jtask,env);
629 jxbt_throw_notbound(env,"task",jtask);
633 return (*env)->NewStringUTF(env,task->name);
636 JNIEXPORT void JNICALL
637 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv* env, jclass cls, jobject jtask) {
638 m_task_t ptask = jtask_to_native_task(jtask,env);
641 jxbt_throw_notbound(env,"task",jtask);
645 if(MSG_OK != MSG_task_cancel(ptask))
646 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
649 JNIEXPORT jdouble JNICALL
650 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
651 m_task_t ptask = jtask_to_native_task(jtask,env);
654 jxbt_throw_notbound(env,"task",jtask);
657 return (jdouble)MSG_task_get_compute_duration(ptask);
660 JNIEXPORT jdouble JNICALL
661 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
662 m_task_t ptask = jtask_to_native_task(jtask,env);
665 jxbt_throw_notbound(env,"task",jtask);
668 return (jdouble)MSG_task_get_remaining_computation(ptask);
671 JNIEXPORT void JNICALL
672 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv* env, jclass cls, jobject jtask, jdouble priority) {
673 m_task_t task = jtask_to_native_task(jtask,env);
676 jxbt_throw_notbound(env,"task",jtask);
679 MSG_task_set_priority(task,(double)priority);
682 JNIEXPORT void JNICALL
683 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv* env, jclass cls, jobject jtask_arg) {
685 /* get the native task */
686 m_task_t task = jtask_to_native_task(jtask_arg,env);
690 jxbt_throw_notbound(env,"task",task);
693 jtask = (jobject)task->data;
695 if(MSG_OK != MSG_task_destroy(task))
696 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
698 /* delete the global reference to the java task object */
699 jtask_delete_global_ref(jtask,env);
702 JNIEXPORT void JNICALL
703 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv* env, jclass cls, jobject jtask) {
704 m_task_t task = jtask_to_native_task(jtask,env);
707 jxbt_throw_notbound(env,"task",jtask);
711 if(MSG_OK != MSG_task_execute(task))
712 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
715 /***************************************************************************************
716 * The Task reception functions *
717 ***************************************************************************************/
719 JNIEXPORT jobject JNICALL
720 Java_simgrid_msg_MsgNative_taskGet(JNIEnv* env, jclass cls,
721 jint chan_id, jdouble jtimeout, jobject jhost) {
722 m_task_t task = NULL;
723 m_host_t host = NULL;
726 host = jhost_get_native(env,jhost);
728 jxbt_throw_notbound(env,"host",jhost);
733 if (MSG_OK != MSG_task_get_ext(&task,(int)chan_id,(double)jtimeout,host)) {
734 jxbt_throw_native(env, xbt_strdup("MSG_task_get_ext() failed"));
738 return (jobject)task->data;
742 JNIEXPORT jboolean JNICALL
743 Java_simgrid_msg_MsgNative_taskProbe(JNIEnv* env, jclass cls, jint chan_id) {
744 return (jboolean)MSG_task_Iprobe(chan_id);
747 JNIEXPORT jobject JNICALL
748 Java_simgrid_msg_MsgNative_taskGetCommunicatingProcess(JNIEnv* env, jclass cls, jint chan_id) {
749 int pid = MSG_task_probe_from(chan_id);
751 return Java_simgrid_msg_MsgNative_processFromPID(env,cls,(jint)pid);
756 JNIEXPORT jint JNICALL
757 Java_simgrid_msg_MsgNative_taskProbeHost(JNIEnv* env, jclass cls,
758 jobject jhost, jint chan_id) {
759 m_host_t host = jhost_get_native(env,jhost);
762 jxbt_throw_notbound(env,"host",jhost);
766 return (jint)MSG_task_probe_from_host(chan_id,host);
770 /***************************************************************************************
771 * The Task reception functions *
772 ***************************************************************************************/
775 JNIEXPORT void JNICALL
776 Java_simgrid_msg_MsgNative_hostPut(JNIEnv* env, jclass cls,
777 jobject jhost, jint chan_id, jobject jtask,
779 m_task_t task = jtask_to_native_task(jtask,env);
780 m_host_t host = jhost_get_native(env,jhost);
783 jxbt_throw_notbound(env,"host",jhost);
787 jxbt_throw_notbound(env,"task",jtask);
791 if(MSG_OK != MSG_task_put_with_timeout(task,host,(int)chan_id,(double)jtimeout))
792 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
797 JNIEXPORT void JNICALL
798 Java_simgrid_msg_MsgNative_hostPutBounded(JNIEnv* env, jclass cls,
799 jobject jhost, jint chan_id, jobject jtask,
801 m_task_t task = jtask_to_native_task(jtask,env);
802 m_host_t host = jhost_get_native(env,jhost);
805 jxbt_throw_notbound(env,"host",jhost);
809 jxbt_throw_notbound(env,"task",jtask);
813 if(MSG_OK != MSG_task_put_bounded(task,host,(int)chan_id,(double)jmaxRate))
814 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
817 JNIEXPORT jint JNICALL
818 Java_simgrid_msg_Msg_getErrCode(JNIEnv* env, jclass cls) {
819 return (jint)MSG_get_errno();
822 JNIEXPORT jdouble JNICALL
823 Java_simgrid_msg_Msg_getClock(JNIEnv* env, jclass cls) {
824 return (jdouble)MSG_get_clock();
828 JNIEXPORT void JNICALL
829 Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
838 argc = (int)(*env)->GetArrayLength(env,jargs);
842 argv = xbt_new0(char*, argc);
844 argv[0] = strdup("java");
846 for(index = 0; index < argc -1; index++) {
847 jval = (jstring)(*env)->GetObjectArrayElement(env,jargs,index);
849 tmp = (*env)->GetStringUTFChars(env, jval, 0);
851 argv[index +1] = strdup(tmp);
853 (*env)->ReleaseStringUTFChars(env, jval, tmp);
856 MSG_global_init(&argc,argv);
857 MSG_set_channel_number(10); /* FIXME: this should not be fixed statically */
859 for(index = 0; index < argc; index++)
864 (*env)->GetJavaVM(env,&__java_vm);
868 JNIEXPORT void JNICALL
869 JNICALL Java_simgrid_msg_Msg_run(JNIEnv* env, jclass cls) {
870 xbt_fifo_item_t item = NULL;
871 m_host_t host = NULL;
875 if(MSG_OK != MSG_main())
876 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
878 DEBUG0("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
880 DEBUG0("Clean java world");
881 /* Cleanup java hosts */
882 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
883 jhost = (jobject)host->data;
886 jhost_unref(env,jhost);
889 DEBUG0("Clean native world");
890 /* cleanup native stuff */
891 if(MSG_OK != MSG_clean())
892 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
896 JNIEXPORT jint JNICALL
897 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv* env, jclass cls, jint jresetPID) {
898 return (jint)MSG_process_killall((int)jresetPID);
901 JNIEXPORT void JNICALL
902 Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatformFile) {
904 const char* platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
906 MSG_create_environment(platformFile);
908 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
911 JNIEXPORT void JNICALL
912 Java_simgrid_msg_MsgNative_processExit(JNIEnv* env, jclass cls, jobject jprocess) {
914 m_process_t process = jprocess_to_native_process(jprocess,env);
917 jxbt_throw_notbound(env,"process",jprocess);
924 JNIEXPORT void JNICALL
925 Java_simgrid_msg_Msg_pajeOutput(JNIEnv* env, jclass cls, jstring jpajeFile) {
926 const char*pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
928 MSG_paje_output(pajeFile);
930 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
934 JNIEXPORT void JNICALL
935 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js) {
936 const char* s = (*env)->GetStringUTFChars(env,js,0);
938 (*env)->ReleaseStringUTFChars(env, js, s);
941 JNIEXPORT jobjectArray JNICALL
942 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg) {
949 int count = xbt_fifo_size(msg_global->host);
950 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
952 jclass cls = jxbt_get_class(env,"simgrid/msg/Host");
958 jtable = (*env)->NewObjectArray(env,(jsize)count,cls,NULL);
961 jxbt_throw_jni(env,"Hosts table allocation failed");
965 for(index = 0; index < count; index++) {
967 jhost = (jobject)(host->data);
970 jname = (*env)->NewStringUTF(env,host->name);
972 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env,cls_arg,jname);
973 /* FIXME: leak of jname ? */
976 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
983 JNIEXPORT void JNICALL
984 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,jstring jname)
989 /* get the C string from the java string*/
990 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
993 xbt_context_select_factory(name);
995 errmsg = xbt_strdup(e.msg);
999 (*env)->ReleaseStringUTFChars(env, jname, name);
1002 char *thrown = bprintf("xbt_select_context_factory() failed: %s",errmsg);
1004 jxbt_throw_native(env, thrown);
1008 JNIEXPORT void JNICALL
1009 Java_simgrid_msg_MsgNative_taskSend(JNIEnv* env, jclass cls,
1010 jstring jalias, jobject jtask,
1014 const char* alias = (*env)->GetStringUTFChars(env, jalias, 0);
1016 m_task_t task = jtask_to_native_task(jtask,env);
1020 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1021 jxbt_throw_notbound(env,"task",jtask);
1025 rv = MSG_task_send_with_timeout(task,alias,(double)jtimeout);
1027 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1030 jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
1034 JNIEXPORT void JNICALL
1035 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv* env, jclass cls,
1036 jstring jalias, jobject jtask,
1038 m_task_t task = jtask_to_native_task(jtask,env);
1043 jxbt_throw_notbound(env,"task",jtask);
1047 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1049 rv = MSG_task_send_bounded(task,alias,(double)jmaxRate);
1051 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1054 jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1057 JNIEXPORT jobject JNICALL
1058 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv* env, jclass cls,
1059 jstring jalias, jdouble jtimeout, jobject jhost) {
1061 m_task_t task = NULL;
1062 m_host_t host = NULL;
1066 host = jhost_get_native(env,jhost);
1069 jxbt_throw_notbound(env,"host",jhost);
1074 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1076 rv = MSG_task_receive_ext(&task,alias,(double)jtimeout,host);
1078 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1082 jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1086 return (jobject)task->data;
1089 JNIEXPORT jboolean JNICALL
1090 Java_simgrid_msg_MsgNative_taskListen(JNIEnv* env, jclass cls, jstring jalias) {
1095 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1097 rv = MSG_task_listen(alias);
1099 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1101 return (jboolean)rv;
1104 JNIEXPORT jint JNICALL
1105 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv* env, jclass cls, jstring jalias, jobject jhost) {
1110 m_host_t host = jhost_get_native(env,jhost);
1113 jxbt_throw_notbound(env,"host",jhost);
1117 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1119 rv = MSG_task_listen_from_host(alias,host);
1121 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1126 JNIEXPORT jint JNICALL
1127 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv* env, jclass cls, jstring jalias) {
1130 const char* alias = (*env)->GetStringUTFChars(env, jalias, 0);
1132 rv = MSG_task_listen_from(alias);
1134 (*env)->ReleaseStringUTFChars(env, jalias, alias);