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_jcontext.h"
16 #include "xbt/xbt_context_factory.h"
18 #include "jmsg_process.h"
19 #include "jmsg_host.h"
20 #include "jmsg_task.h"
21 #include "jmsg_channel.h"
22 #include "jxbt_utilities.h"
26 #include "msg/mailbox.h"
28 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
30 static JavaVM * __java_vm = NULL;
33 native_to_java_process(m_process_t process);
41 get_current_thread_env(void) {
44 (*__java_vm)->AttachCurrentThread(__java_vm, (void **)&env, NULL);
50 native_to_java_process(m_process_t process)
52 return ((xbt_jcontext_t)(process->simdata->s_process->simdata->context))->jprocess;
57 * The MSG process connected functions implementation.
60 JNIEXPORT void JNICALL
61 Java_simgrid_msg_MsgNative_processCreate(JNIEnv* env, jclass cls, jobject jprocess_arg, jobject jhost) {
62 jobject jprocess; /* the global reference to the java process instance */
63 jstring jname; /* the name of the java process instance */
64 const char* name; /* the C name of the process */
65 m_process_t process; /* the native process to create */
66 char alias[MAX_ALIAS_NAME + 1] = {0};
67 msg_mailbox_t mailbox;
69 DEBUG4("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);
75 jxbt_throw_null(env,xbt_strdup("Internal error: Process name cannot be NULL"));
79 /* allocate the data of the simulation */
80 process = xbt_new0(s_m_process_t,1);
81 process->simdata = xbt_new0(s_simdata_process_t,1);
83 /* create a global java process instance */
84 jprocess = jprocess_new_global_ref(jprocess_arg,env);
87 free(process->simdata);
89 jxbt_throw_jni(env, "Can't get a global ref to the java process");
93 /* bind the java process instance to the native process */
94 jprocess_bind(jprocess,process,env);
96 /* build the C name of the process */
97 name = (*env)->GetStringUTFChars(env, jname, 0);
98 process->name = xbt_strdup(name);
99 (*env)->ReleaseStringUTFChars(env, jname, name);
101 process->simdata->m_host = jhost_get_native(env,jhost);
104 if( ! (process->simdata->m_host) ) { /* not binded */
105 free(process->simdata);
108 jxbt_throw_notbound(env,"host",jhost);
111 process->simdata->PID = msg_global->PID++;
113 /* create a new context */
114 DEBUG8("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
115 process->name,process->simdata->m_host->name,
116 process->simdata->PID,process,
117 process->simdata, process->simdata->m_host, process->simdata->m_host->simdata,
120 SIMIX_jprocess_create(process->name,
121 process->simdata->m_host->simdata->smx_host,
122 /*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 jprocess, jobject jhost){
307 m_host_t host = jhost_get_native(env,jhost);
308 m_process_t process = jprocess_to_native_process(jprocess,env);
311 jxbt_throw_notbound(env,"process",jprocess);
316 jxbt_throw_notbound(env,"host",jhost);
320 /* try to change the host of the process */
321 if(MSG_OK != MSG_process_change_host(process,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,jdouble seconds) {
327 if(MSG_OK != MSG_process_sleep((double)seconds))
328 jxbt_throw_native(env,
329 bprintf("MSG_process_change_host(%f) failed", (double)seconds));
333 /***************************************************************************************
334 * The MSG host connected functions implementation. *
335 ***************************************************************************************/
337 JNIEXPORT jobject JNICALL
338 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv* env, jclass cls, jstring jname) {
339 m_host_t host; /* native host */
340 jobject jhost; /* global reference to the java host instance returned */
342 /* get the C string from the java string*/
343 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
345 /* get the host by name (the hosts are created during the grid resolution) */
346 host = MSG_get_host_by_name(name);
347 DEBUG2("MSG gave %p as native host (simdata=%p)",host,host->simdata);
349 (*env)->ReleaseStringUTFChars(env, jname, name);
351 if(!host) {/* invalid name */
352 jxbt_throw_host_not_found(env,name);
356 if(!host->data) { /* native host not associated yet with java host */
358 /* instanciate a new java host */
359 jhost = jhost_new_instance(env);
362 jxbt_throw_jni(env,"java host instantiation failed");
366 /* get a global reference to the newly created host */
367 jhost = jhost_ref(env,jhost);
370 jxbt_throw_jni(env,"new global ref allocation failed");
374 /* bind the java host and the native host */
375 jhost_bind(jhost,host,env);
377 /* the native host data field is set with the global reference to the
378 * java host returned by this function
380 host->data = (void*)jhost;
383 /* return the global reference to the java host instance */
384 return (jobject)host->data;
387 JNIEXPORT jstring JNICALL
388 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv* env, jclass cls, jobject jhost) {
389 m_host_t host = jhost_get_native(env,jhost);
392 jxbt_throw_notbound(env,"host",jhost);
396 return (*env)->NewStringUTF(env,host->name);
399 JNIEXPORT jint JNICALL
400 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv* env, jclass cls) {
401 return (jint)MSG_get_host_number();
404 JNIEXPORT jobject JNICALL
405 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv* env, jclass cls) {
408 m_host_t host = MSG_host_self();
411 /* the native host not yet associated with the java host instance */
413 /* instanciate a new java host instance */
414 jhost = jhost_new_instance(env);
417 jxbt_throw_jni(env,"java host instantiation failed");
421 /* get a global reference to the newly created host */
422 jhost = jhost_ref(env,jhost);
425 jxbt_throw_jni(env,"global ref allocation failed");
429 /* Bind & store it */
430 jhost_bind(jhost,host,env);
431 host->data = (void*)jhost;
433 jhost = (jobject)host->data;
439 JNIEXPORT jdouble JNICALL
440 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv* env, jclass cls, jobject jhost) {
441 m_host_t host = jhost_get_native(env,jhost);
444 jxbt_throw_notbound(env,"host",jhost);
448 return (jdouble)MSG_get_host_speed(host);
451 JNIEXPORT jint JNICALL
452 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv* env, jclass cls, jobject jhost) {
453 m_host_t host = jhost_get_native(env,jhost);
456 jxbt_throw_notbound(env,"host",jhost);
460 return (jint)MSG_get_host_msgload(host);
464 JNIEXPORT jboolean JNICALL
465 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv* env, jclass cls, jobject jhost) {
466 m_host_t host = jhost_get_native(env,jhost);
469 jxbt_throw_notbound(env,"host",jhost);
473 return (jboolean)MSG_host_is_avail(host);
477 /***************************************************************************************
478 * The MSG task connected functions implementation. *
479 ***************************************************************************************/
481 JNIEXPORT void JNICALL
482 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
483 jdouble jcomputeDuration, jdouble jmessageSize) {
484 m_task_t task; /* the native task to create */
485 const char* name; /* the name of the task */
487 if(jcomputeDuration < 0) {
488 jxbt_throw_illegal(env,bprintf("Task ComputeDuration (%f) cannot be negative",
489 (double)jcomputeDuration));
493 if(jmessageSize < 0) {
494 jxbt_throw_illegal(env,bprintf("Task MessageSize (%f) cannot be negative",
495 (double)jmessageSize));
500 jxbt_throw_null(env,xbt_strdup("Task name cannot be null"));
504 /* get the C string from the java string*/
505 name = (*env)->GetStringUTFChars(env, jname, 0);
507 /* create the task */
508 task = MSG_task_create(name,(double)jcomputeDuration,(double)jmessageSize,NULL);
510 (*env)->ReleaseStringUTFChars(env, jname, name);
512 /* bind & store the task */
513 jtask_bind(jtask,task,env);
515 /* allocate a new global reference to the java task instance */
516 task->data = (void*)jtask_new_global_ref(jtask,env);
519 jxbt_throw_jni(env,"global ref allocation failed");
523 JNIEXPORT void JNICALL
524 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
525 jobjectArray jhosts,jdoubleArray jcomputeDurations_arg, jdoubleArray jmessageSizes_arg) {
527 m_task_t task; /* the native parallel task to create */
528 const char* name; /* the name of the task */
531 double* computeDurations;
532 double* messageSizes;
533 jdouble *jcomputeDurations;
534 jdouble *jmessageSizes;
540 if (!jcomputeDurations_arg) {
541 jxbt_throw_null(env,xbt_strdup("Parallel task compute durations cannot be null"));
545 if(!jmessageSizes_arg){
546 jxbt_throw_null(env,xbt_strdup("Parallel task message sizes cannot be null"));
551 jxbt_throw_null(env,xbt_strdup("Parallel task name cannot be null"));
555 host_count = (int)(*env)->GetArrayLength(env,jhosts);
558 hosts = xbt_new0(m_host_t,host_count);
559 computeDurations = xbt_new0(double,host_count);
560 messageSizes = xbt_new0(double,host_count*host_count);
562 jcomputeDurations = (*env)->GetDoubleArrayElements(env,jcomputeDurations_arg, 0);
563 jmessageSizes = (*env)->GetDoubleArrayElements(env,jmessageSizes_arg, 0);
565 for(index = 0; index < host_count; index++) {
566 jhost = (*env)->GetObjectArrayElement(env,jhosts,index);
567 hosts[index] = jhost_get_native(env,jhost);
568 computeDurations[index] = jcomputeDurations[index];
570 for(index = 0; index < host_count*host_count; index++) {
571 messageSizes[index] = jmessageSizes[index];
574 (*env)->ReleaseDoubleArrayElements(env,jcomputeDurations_arg,jcomputeDurations,0);
575 (*env)->ReleaseDoubleArrayElements(env,jmessageSizes_arg,jmessageSizes,0);
578 /* get the C string from the java string*/
579 name = (*env)->GetStringUTFChars(env, jname, 0);
581 task = MSG_parallel_task_create(name,host_count,hosts,computeDurations,messageSizes,NULL);
583 (*env)->ReleaseStringUTFChars(env, jname, name);
585 /* associate the java task object and the native task */
586 jtask_bind(jtask,task,env);
588 task->data = (void*)jtask;
591 jxbt_throw_jni(env,"global ref allocation failed");
594 JNIEXPORT jobject JNICALL
595 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
598 m_task_t task = jtask_to_native_task(jtask,env);
601 jxbt_throw_notbound(env,"task",jtask);
605 process = MSG_task_get_sender(task);
606 return (jobject)native_to_java_process(process);
609 JNIEXPORT jobject JNICALL
610 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
612 m_task_t task = jtask_to_native_task(jtask,env);
615 jxbt_throw_notbound(env,"task",jtask);
619 host = MSG_task_get_source(task);
622 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
626 return (jobject)host->data;
630 JNIEXPORT jstring JNICALL
631 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv* env, jclass cls, jobject jtask) {
632 m_task_t task = jtask_to_native_task(jtask,env);
635 jxbt_throw_notbound(env,"task",jtask);
639 return (*env)->NewStringUTF(env,task->name);
642 JNIEXPORT void JNICALL
643 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv* env, jclass cls, jobject jtask) {
644 m_task_t ptask = jtask_to_native_task(jtask,env);
647 jxbt_throw_notbound(env,"task",jtask);
651 if(MSG_OK != MSG_task_cancel(ptask))
652 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
655 JNIEXPORT jdouble JNICALL
656 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
657 m_task_t ptask = jtask_to_native_task(jtask,env);
660 jxbt_throw_notbound(env,"task",jtask);
663 return (jdouble)MSG_task_get_compute_duration(ptask);
666 JNIEXPORT jdouble JNICALL
667 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
668 m_task_t ptask = jtask_to_native_task(jtask,env);
671 jxbt_throw_notbound(env,"task",jtask);
674 return (jdouble)MSG_task_get_remaining_computation(ptask);
677 JNIEXPORT void JNICALL
678 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv* env, jclass cls, jobject jtask, jdouble priority) {
679 m_task_t task = jtask_to_native_task(jtask,env);
682 jxbt_throw_notbound(env,"task",jtask);
685 MSG_task_set_priority(task,(double)priority);
688 JNIEXPORT void JNICALL
689 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv* env, jclass cls, jobject jtask_arg) {
691 /* get the native task */
692 m_task_t task = jtask_to_native_task(jtask_arg,env);
696 jxbt_throw_notbound(env,"task",task);
699 jtask = (jobject)task->data;
701 if(MSG_OK != MSG_task_destroy(task))
702 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
704 /* delete the global reference to the java task object */
705 jtask_delete_global_ref(jtask,env);
708 JNIEXPORT void JNICALL
709 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv* env, jclass cls, jobject jtask) {
710 m_task_t task = jtask_to_native_task(jtask,env);
713 jxbt_throw_notbound(env,"task",jtask);
717 if(MSG_OK != MSG_task_execute(task))
718 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
721 /***************************************************************************************
722 * The Task reception functions *
723 ***************************************************************************************/
725 JNIEXPORT jobject JNICALL
726 Java_simgrid_msg_MsgNative_taskGet(JNIEnv* env, jclass cls,
727 jint chan_id, jdouble jtimeout, jobject jhost) {
728 m_task_t task = NULL;
729 m_host_t host = NULL;
732 host = jhost_get_native(env,jhost);
734 jxbt_throw_notbound(env,"host",jhost);
739 if (MSG_OK != MSG_task_get_ext(&task,(int)chan_id,(double)jtimeout,host)) {
740 jxbt_throw_native(env, xbt_strdup("MSG_task_get_ext() failed"));
744 return (jobject)task->data;
748 JNIEXPORT jboolean JNICALL
749 Java_simgrid_msg_MsgNative_taskProbe(JNIEnv* env, jclass cls, jint chan_id) {
750 return (jboolean)MSG_task_Iprobe(chan_id);
753 JNIEXPORT jobject JNICALL
754 Java_simgrid_msg_MsgNative_taskGetCommunicatingProcess(JNIEnv* env, jclass cls, jint chan_id) {
755 int pid = MSG_task_probe_from(chan_id);
757 return Java_simgrid_msg_MsgNative_processFromPID(env,cls,(jint)pid);
762 JNIEXPORT jint JNICALL
763 Java_simgrid_msg_MsgNative_taskProbeHost(JNIEnv* env, jclass cls,
764 jobject jhost, jint chan_id) {
765 m_host_t host = jhost_get_native(env,jhost);
768 jxbt_throw_notbound(env,"host",jhost);
772 return (jint)MSG_task_probe_from_host(chan_id,host);
776 /***************************************************************************************
777 * The Task reception functions *
778 ***************************************************************************************/
781 JNIEXPORT void JNICALL
782 Java_simgrid_msg_MsgNative_hostPut(JNIEnv* env, jclass cls,
783 jobject jhost, jint chan_id, jobject jtask,
785 m_task_t task = jtask_to_native_task(jtask,env);
786 m_host_t host = jhost_get_native(env,jhost);
789 jxbt_throw_notbound(env,"host",jhost);
793 jxbt_throw_notbound(env,"task",jtask);
797 if(MSG_OK != MSG_task_put_with_timeout(task,host,(int)chan_id,(double)jtimeout))
798 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
803 JNIEXPORT void JNICALL
804 Java_simgrid_msg_MsgNative_hostPutBounded(JNIEnv* env, jclass cls,
805 jobject jhost, jint chan_id, jobject jtask,
807 m_task_t task = jtask_to_native_task(jtask,env);
808 m_host_t host = jhost_get_native(env,jhost);
811 jxbt_throw_notbound(env,"host",jhost);
815 jxbt_throw_notbound(env,"task",jtask);
819 if(MSG_OK != MSG_task_put_bounded(task,host,(int)chan_id,(double)jmaxRate))
820 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
823 JNIEXPORT jint JNICALL
824 Java_simgrid_msg_Msg_getErrCode(JNIEnv* env, jclass cls) {
825 return (jint)MSG_get_errno();
828 JNIEXPORT jdouble JNICALL
829 Java_simgrid_msg_Msg_getClock(JNIEnv* env, jclass cls) {
830 return (jdouble)MSG_get_clock();
834 JNIEXPORT void JNICALL
835 Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
844 argc = (int)(*env)->GetArrayLength(env,jargs);
848 argv = xbt_new0(char*,1);
850 argv[0] = strdup("java");
852 for(index = 0; index < argc -1; index++) {
853 jval = (jstring)(*env)->GetObjectArrayElement(env,jargs,index);
855 tmp = (*env)->GetStringUTFChars(env, jval, 0);
857 argv[index +1] = strdup(tmp);
859 (*env)->ReleaseStringUTFChars(env, jval, tmp);
862 MSG_global_init(&argc,argv);
863 MSG_set_channel_number(10); /* FIXME: this should not be fixed statically */
865 for(index = 0; index < argc; index++)
870 (*env)->GetJavaVM(env,&__java_vm);
874 JNIEXPORT void JNICALL
875 JNICALL Java_simgrid_msg_Msg_run(JNIEnv* env, jclass cls) {
876 xbt_fifo_item_t item = NULL;
877 m_host_t host = NULL;
881 if(MSG_OK != MSG_main())
882 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
884 DEBUG0("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
886 DEBUG0("Clean java world");
887 /* Cleanup java hosts */
888 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
889 jhost = (jobject)host->data;
892 jhost_unref(env,jhost);
895 DEBUG0("Clean native world");
896 /* cleanup native stuff */
897 if(MSG_OK != MSG_clean())
898 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
902 JNIEXPORT jint JNICALL
903 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv* env, jclass cls, jint jresetPID) {
904 return (jint)MSG_process_killall((int)jresetPID);
907 JNIEXPORT void JNICALL
908 Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatformFile) {
910 const char* platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
912 MSG_create_environment(platformFile);
914 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
917 JNIEXPORT void JNICALL
918 Java_simgrid_msg_MsgNative_processExit(JNIEnv* env, jclass cls, jobject jprocess) {
920 m_process_t process = jprocess_to_native_process(jprocess,env);
923 jxbt_throw_notbound(env,"process",jprocess);
930 JNIEXPORT void JNICALL
931 Java_simgrid_msg_Msg_pajeOutput(JNIEnv* env, jclass cls, jstring jpajeFile) {
932 const char*pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
934 MSG_paje_output(pajeFile);
936 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
940 JNIEXPORT void JNICALL
941 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js) {
942 const char* s = (*env)->GetStringUTFChars(env,js,0);
944 (*env)->ReleaseStringUTFChars(env, js, s);
947 JNIEXPORT jobjectArray JNICALL
948 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg) {
955 int count = xbt_fifo_size(msg_global->host);
956 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
958 jclass cls = jxbt_get_class(env,"simgrid/msg/Host");
964 jtable = (*env)->NewObjectArray(env,(jsize)count,cls,NULL);
967 jxbt_throw_jni(env,"Hosts table allocation failed");
971 for(index = 0; index < count; index++) {
973 jhost = (jobject)(host->data);
976 jname = (*env)->NewStringUTF(env,host->name);
978 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env,cls_arg,jname);
979 /* FIXME: leak of jname ? */
982 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
989 JNIEXPORT void JNICALL
990 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,jstring jname)
994 /* get the C string from the java string*/
995 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
997 rv = xbt_context_select_factory(name);
999 (*env)->ReleaseStringUTFChars(env, jname, name);
1002 jxbt_throw_native(env, xbt_strdup("xbt_select_context_factory() failed"));
1005 JNIEXPORT void JNICALL
1006 Java_simgrid_msg_MsgNative_taskSend(JNIEnv* env, jclass cls,
1007 jstring jalias, jobject jtask,
1011 const char* alias = (*env)->GetStringUTFChars(env, jalias, 0);
1013 m_task_t task = jtask_to_native_task(jtask,env);
1017 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1018 jxbt_throw_notbound(env,"task",jtask);
1022 rv = MSG_task_send_with_timeout(task,alias,(double)jtimeout);
1024 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1027 jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
1031 JNIEXPORT void JNICALL
1032 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv* env, jclass cls,
1033 jstring jalias, jobject jtask,
1035 m_task_t task = jtask_to_native_task(jtask,env);
1040 jxbt_throw_notbound(env,"task",jtask);
1044 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1046 rv = MSG_task_send_bounded(task,alias,(double)jmaxRate);
1048 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1051 jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1054 JNIEXPORT jobject JNICALL
1055 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv* env, jclass cls,
1056 jstring jalias, jdouble jtimeout, jobject jhost) {
1058 m_task_t task = NULL;
1059 m_host_t host = NULL;
1063 host = jhost_get_native(env,jhost);
1066 jxbt_throw_notbound(env,"host",jhost);
1071 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1073 rv = MSG_task_receive_ext(&task,alias,(double)jtimeout,host);
1075 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1079 jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1083 return (jobject)task->data;
1086 JNIEXPORT jboolean JNICALL
1087 Java_simgrid_msg_MsgNative_taskListen(JNIEnv* env, jclass cls, jstring jalias) {
1092 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1094 rv = MSG_task_listen(alias);
1096 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1098 return (jboolean)rv;
1101 JNIEXPORT jint JNICALL
1102 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv* env, jclass cls, jstring jalias, jobject jhost) {
1107 m_host_t host = jhost_get_native(env,jhost);
1110 jxbt_throw_notbound(env,"host",jhost);
1114 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1116 rv = MSG_task_listen_from_host(alias,host);
1118 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1123 JNIEXPORT jint JNICALL
1124 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv* env, jclass cls, jstring jalias) {
1127 const char* alias = (*env)->GetStringUTFChars(env, jalias, 0);
1129 rv = MSG_task_listen_from(alias);
1131 (*env)->ReleaseStringUTFChars(env, jalias, alias);