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,
123 &process->simdata->s_process);
126 DEBUG1("context created (s_process=%p)",process->simdata->s_process);
129 if (SIMIX_process_self()) { /* someone created me */
130 process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
132 process->simdata->PPID = -1;
135 process->simdata->last_errno = MSG_OK;
137 /* add the process to the list of the processes of the simulation */
138 xbt_fifo_unshift(msg_global->process_list, process);
140 sprintf(alias,"%s:%s",(process->simdata->m_host->simdata->smx_host)->name,process->name);
142 mailbox = MSG_mailbox_new(alias);
143 MSG_mailbox_set_hostname(mailbox, process->simdata->m_host->simdata->smx_host->name);
148 JNIEXPORT void JNICALL
149 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv* env, jclass cls, jobject jprocess) {
150 m_process_t process = jprocess_to_native_process(jprocess,env);
153 jxbt_throw_notbound(env,"process",jprocess);
157 /* try to suspend the process */
158 if(MSG_OK != MSG_process_suspend(process))
159 jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
162 JNIEXPORT void JNICALL
163 Java_simgrid_msg_MsgNative_processResume(JNIEnv* env, jclass cls, jobject jprocess) {
164 m_process_t process = jprocess_to_native_process(jprocess,env);
167 jxbt_throw_notbound(env,"process",jprocess);
171 /* try to resume the process */
172 if(MSG_OK != MSG_process_resume(process))
173 jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
176 JNIEXPORT jboolean JNICALL
177 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv* env, jclass cls, jobject jprocess) {
178 m_process_t process = jprocess_to_native_process(jprocess,env);
181 jxbt_throw_notbound(env,"process",jprocess);
185 /* true is the process is suspended, false otherwise */
186 return (jboolean)MSG_process_is_suspended(process);
189 JNIEXPORT void JNICALL
190 Java_simgrid_msg_MsgNative_processKill(JNIEnv* env, jclass cls, jobject jprocess) {
191 /* get the native instances from the java ones */
192 m_process_t process = jprocess_to_native_process(jprocess,env);
195 jxbt_throw_notbound(env,"process",jprocess);
199 /* delete the global reference */
200 jprocess_delete_global_ref(native_to_java_process(process),env);
202 /* kill the native process (this wrapper is call by the destructor of the java
205 MSG_process_kill(process);
208 JNIEXPORT jobject JNICALL
209 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv* env, jclass cls, jobject jprocess) {
210 /* get the native instances from the java ones */
211 m_process_t process = jprocess_to_native_process(jprocess,env);
215 jxbt_throw_notbound(env,"process",jprocess);
219 host = MSG_process_get_host(process);
222 jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
226 /* return the global reference to the java host instance */
227 return (jobject)host->data;
231 JNIEXPORT jobject JNICALL
232 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv* env, jclass cls, jint PID) {
233 m_process_t process = MSG_process_from_PID(PID);
236 jxbt_throw_process_not_found(env, bprintf("PID = %d",PID));
240 if(!native_to_java_process(process)) {
241 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
245 return (jobject)(native_to_java_process(process));
249 JNIEXPORT jint JNICALL
250 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv* env, jclass cls, jobject jprocess) {
251 m_process_t process = jprocess_to_native_process(jprocess,env);
254 jxbt_throw_notbound(env,"process",jprocess);
258 return (jint)MSG_process_get_PID(process);
262 JNIEXPORT jint JNICALL
263 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv* env, jclass cls, jobject jprocess) {
264 m_process_t process = jprocess_to_native_process(jprocess,env);
267 jxbt_throw_notbound(env,"process",jprocess);
271 return (jint)MSG_process_get_PPID(process);
274 JNIEXPORT jobject JNICALL
275 Java_simgrid_msg_MsgNative_processSelf(JNIEnv* env, jclass cls) {
276 m_process_t process = MSG_process_self();
280 jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
284 jprocess = native_to_java_process(process);
287 jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
293 JNIEXPORT jint JNICALL
294 Java_simgrid_msg_MsgNative_processSelfPID(JNIEnv* env, jclass cls) {
295 return (jint)MSG_process_self_PID();
299 JNIEXPORT jint JNICALL
300 Java_simgrid_msg_MsgNative_processSelfPPID(JNIEnv* env, jclass cls) {
301 return (jint)MSG_process_self_PPID();
304 JNIEXPORT void JNICALL
305 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv* env, jclass cls, jobject jhost){
306 m_host_t host = jhost_get_native(env,jhost);
309 jxbt_throw_notbound(env,"host",jhost);
313 /* try to change the host of the process */
314 if(MSG_OK != MSG_process_change_host(host))
315 jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
318 JNIEXPORT void JNICALL
319 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv* env, jclass cls,jdouble seconds) {
320 if(MSG_OK != MSG_process_sleep((double)seconds))
321 jxbt_throw_native(env,
322 bprintf("MSG_process_change_host(%f) failed", (double)seconds));
326 /***************************************************************************************
327 * The MSG host connected functions implementation. *
328 ***************************************************************************************/
330 JNIEXPORT jobject JNICALL
331 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv* env, jclass cls, jstring jname) {
332 m_host_t host; /* native host */
333 jobject jhost; /* global reference to the java host instance returned */
335 /* get the C string from the java string*/
336 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
338 /* get the host by name (the hosts are created during the grid resolution) */
339 host = MSG_get_host_by_name(name);
340 DEBUG2("MSG gave %p as native host (simdata=%p)",host,host->simdata);
342 (*env)->ReleaseStringUTFChars(env, jname, name);
344 if(!host) {/* invalid name */
345 jxbt_throw_host_not_found(env,name);
349 if(!host->data) { /* native host not associated yet with java host */
351 /* instanciate a new java host */
352 jhost = jhost_new_instance(env);
355 jxbt_throw_jni(env,"java host instantiation failed");
359 /* get a global reference to the newly created host */
360 jhost = jhost_ref(env,jhost);
363 jxbt_throw_jni(env,"new global ref allocation failed");
367 /* bind the java host and the native host */
368 jhost_bind(jhost,host,env);
370 /* the native host data field is set with the global reference to the
371 * java host returned by this function
373 host->data = (void*)jhost;
376 /* return the global reference to the java host instance */
377 return (jobject)host->data;
380 JNIEXPORT jstring JNICALL
381 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv* env, jclass cls, jobject jhost) {
382 m_host_t host = jhost_get_native(env,jhost);
385 jxbt_throw_notbound(env,"host",jhost);
389 return (*env)->NewStringUTF(env,host->name);
392 JNIEXPORT jint JNICALL
393 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv* env, jclass cls) {
394 return (jint)MSG_get_host_number();
397 JNIEXPORT jobject JNICALL
398 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv* env, jclass cls) {
401 m_host_t host = MSG_host_self();
404 /* the native host not yet associated with the java host instance */
406 /* instanciate a new java host instance */
407 jhost = jhost_new_instance(env);
410 jxbt_throw_jni(env,"java host instantiation failed");
414 /* get a global reference to the newly created host */
415 jhost = jhost_ref(env,jhost);
418 jxbt_throw_jni(env,"global ref allocation failed");
422 /* Bind & store it */
423 jhost_bind(jhost,host,env);
424 host->data = (void*)jhost;
426 jhost = (jobject)host->data;
432 JNIEXPORT jdouble JNICALL
433 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv* env, jclass cls, jobject jhost) {
434 m_host_t host = jhost_get_native(env,jhost);
437 jxbt_throw_notbound(env,"host",jhost);
441 return (jdouble)MSG_get_host_speed(host);
444 JNIEXPORT jint JNICALL
445 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv* env, jclass cls, jobject jhost) {
446 m_host_t host = jhost_get_native(env,jhost);
449 jxbt_throw_notbound(env,"host",jhost);
453 return (jint)MSG_get_host_msgload(host);
457 JNIEXPORT jboolean JNICALL
458 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv* env, jclass cls, jobject jhost) {
459 m_host_t host = jhost_get_native(env,jhost);
462 jxbt_throw_notbound(env,"host",jhost);
466 return (jboolean)MSG_host_is_avail(host);
470 /***************************************************************************************
471 * The MSG task connected functions implementation. *
472 ***************************************************************************************/
474 JNIEXPORT void JNICALL
475 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
476 jdouble jcomputeDuration, jdouble jmessageSize) {
477 m_task_t task; /* the native task to create */
478 const char* name; /* the name of the task */
480 if(jcomputeDuration < 0) {
481 jxbt_throw_illegal(env,bprintf("Task ComputeDuration (%f) cannot be negative",
482 (double)jcomputeDuration));
486 if(jmessageSize < 0) {
487 jxbt_throw_illegal(env,bprintf("Task MessageSize (%f) cannot be negative",
488 (double)jmessageSize));
493 jxbt_throw_null(env,xbt_strdup("Task name cannot be null"));
497 /* get the C string from the java string*/
498 name = (*env)->GetStringUTFChars(env, jname, 0);
500 /* create the task */
501 task = MSG_task_create(name,(double)jcomputeDuration,(double)jmessageSize,NULL);
503 (*env)->ReleaseStringUTFChars(env, jname, name);
505 /* bind & store the task */
506 jtask_bind(jtask,task,env);
508 /* allocate a new global reference to the java task instance */
509 task->data = (void*)jtask_new_global_ref(jtask,env);
512 jxbt_throw_jni(env,"global ref allocation failed");
516 JNIEXPORT void JNICALL
517 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv* env, jclass cls, jobject jtask, jstring jname,
518 jobjectArray jhosts,jdoubleArray jcomputeDurations_arg, jdoubleArray jmessageSizes_arg) {
520 m_task_t task; /* the native parallel task to create */
521 const char* name; /* the name of the task */
524 double* computeDurations;
525 double* messageSizes;
526 jdouble *jcomputeDurations;
527 jdouble *jmessageSizes;
533 if (!jcomputeDurations_arg) {
534 jxbt_throw_null(env,xbt_strdup("Parallel task compute durations cannot be null"));
538 if(!jmessageSizes_arg){
539 jxbt_throw_null(env,xbt_strdup("Parallel task message sizes cannot be null"));
544 jxbt_throw_null(env,xbt_strdup("Parallel task name cannot be null"));
548 host_count = (int)(*env)->GetArrayLength(env,jhosts);
551 hosts = xbt_new0(m_host_t,host_count);
552 computeDurations = xbt_new0(double,host_count);
553 messageSizes = xbt_new0(double,host_count*host_count);
555 jcomputeDurations = (*env)->GetDoubleArrayElements(env,jcomputeDurations_arg, 0);
556 jmessageSizes = (*env)->GetDoubleArrayElements(env,jmessageSizes_arg, 0);
558 for(index = 0; index < host_count; index++) {
559 jhost = (*env)->GetObjectArrayElement(env,jhosts,index);
560 hosts[index] = jhost_get_native(env,jhost);
561 computeDurations[index] = jcomputeDurations[index];
563 for(index = 0; index < host_count*host_count; index++) {
564 messageSizes[index] = jmessageSizes[index];
567 (*env)->ReleaseDoubleArrayElements(env,jcomputeDurations_arg,jcomputeDurations,0);
568 (*env)->ReleaseDoubleArrayElements(env,jmessageSizes_arg,jmessageSizes,0);
571 /* get the C string from the java string*/
572 name = (*env)->GetStringUTFChars(env, jname, 0);
574 task = MSG_parallel_task_create(name,host_count,hosts,computeDurations,messageSizes,NULL);
576 (*env)->ReleaseStringUTFChars(env, jname, name);
578 /* associate the java task object and the native task */
579 jtask_bind(jtask,task,env);
581 task->data = (void*)jtask;
584 jxbt_throw_jni(env,"global ref allocation failed");
587 JNIEXPORT jobject JNICALL
588 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv* env , jclass cls , jobject jtask) {
591 m_task_t task = jtask_to_native_task(jtask,env);
594 jxbt_throw_notbound(env,"task",jtask);
598 process = MSG_task_get_sender(task);
599 return (jobject)native_to_java_process(process);
602 JNIEXPORT jobject JNICALL
603 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv* env , jclass cls, jobject jtask) {
605 m_task_t task = jtask_to_native_task(jtask,env);
608 jxbt_throw_notbound(env,"task",jtask);
612 host = MSG_task_get_source(task);
615 jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
619 return (jobject)host->data;
623 JNIEXPORT jstring JNICALL
624 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv* env, jclass cls, jobject jtask) {
625 m_task_t task = jtask_to_native_task(jtask,env);
628 jxbt_throw_notbound(env,"task",jtask);
632 return (*env)->NewStringUTF(env,task->name);
635 JNIEXPORT void JNICALL
636 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv* env, jclass cls, jobject jtask) {
637 m_task_t ptask = jtask_to_native_task(jtask,env);
640 jxbt_throw_notbound(env,"task",jtask);
644 if(MSG_OK != MSG_task_cancel(ptask))
645 jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
648 JNIEXPORT jdouble JNICALL
649 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv* env, jclass cls, jobject jtask) {
650 m_task_t ptask = jtask_to_native_task(jtask,env);
653 jxbt_throw_notbound(env,"task",jtask);
656 return (jdouble)MSG_task_get_compute_duration(ptask);
659 JNIEXPORT jdouble JNICALL
660 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv* env, jclass cls, jobject jtask) {
661 m_task_t ptask = jtask_to_native_task(jtask,env);
664 jxbt_throw_notbound(env,"task",jtask);
667 return (jdouble)MSG_task_get_remaining_computation(ptask);
670 JNIEXPORT void JNICALL
671 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv* env, jclass cls, jobject jtask, jdouble priority) {
672 m_task_t task = jtask_to_native_task(jtask,env);
675 jxbt_throw_notbound(env,"task",jtask);
678 MSG_task_set_priority(task,(double)priority);
681 JNIEXPORT void JNICALL
682 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv* env, jclass cls, jobject jtask_arg) {
684 /* get the native task */
685 m_task_t task = jtask_to_native_task(jtask_arg,env);
689 jxbt_throw_notbound(env,"task",task);
692 jtask = (jobject)task->data;
694 if(MSG_OK != MSG_task_destroy(task))
695 jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
697 /* delete the global reference to the java task object */
698 jtask_delete_global_ref(jtask,env);
701 JNIEXPORT void JNICALL
702 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv* env, jclass cls, jobject jtask) {
703 m_task_t task = jtask_to_native_task(jtask,env);
706 jxbt_throw_notbound(env,"task",jtask);
710 if(MSG_OK != MSG_task_execute(task))
711 jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
714 /***************************************************************************************
715 * The Task reception functions *
716 ***************************************************************************************/
718 JNIEXPORT jobject JNICALL
719 Java_simgrid_msg_MsgNative_taskGet(JNIEnv* env, jclass cls,
720 jint chan_id, jdouble jtimeout, jobject jhost) {
721 m_task_t task = NULL;
722 m_host_t host = NULL;
725 host = jhost_get_native(env,jhost);
727 jxbt_throw_notbound(env,"host",jhost);
732 if (MSG_OK != MSG_task_get_ext(&task,(int)chan_id,(double)jtimeout,host)) {
733 jxbt_throw_native(env, xbt_strdup("MSG_task_get_ext() failed"));
737 return (jobject)task->data;
741 JNIEXPORT jboolean JNICALL
742 Java_simgrid_msg_MsgNative_taskProbe(JNIEnv* env, jclass cls, jint chan_id) {
743 return (jboolean)MSG_task_Iprobe(chan_id);
746 JNIEXPORT jobject JNICALL
747 Java_simgrid_msg_MsgNative_taskGetCommunicatingProcess(JNIEnv* env, jclass cls, jint chan_id) {
748 int pid = MSG_task_probe_from(chan_id);
750 return Java_simgrid_msg_MsgNative_processFromPID(env,cls,(jint)pid);
755 JNIEXPORT jint JNICALL
756 Java_simgrid_msg_MsgNative_taskProbeHost(JNIEnv* env, jclass cls,
757 jobject jhost, jint chan_id) {
758 m_host_t host = jhost_get_native(env,jhost);
761 jxbt_throw_notbound(env,"host",jhost);
765 return (jint)MSG_task_probe_from_host(chan_id,host);
769 /***************************************************************************************
770 * The Task reception functions *
771 ***************************************************************************************/
774 JNIEXPORT void JNICALL
775 Java_simgrid_msg_MsgNative_hostPut(JNIEnv* env, jclass cls,
776 jobject jhost, jint chan_id, jobject jtask,
778 m_task_t task = jtask_to_native_task(jtask,env);
779 m_host_t host = jhost_get_native(env,jhost);
782 jxbt_throw_notbound(env,"host",jhost);
786 jxbt_throw_notbound(env,"task",jtask);
790 if(MSG_OK != MSG_task_put_with_timeout(task,host,(int)chan_id,(double)jtimeout))
791 jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
796 JNIEXPORT void JNICALL
797 Java_simgrid_msg_MsgNative_hostPutBounded(JNIEnv* env, jclass cls,
798 jobject jhost, jint chan_id, jobject jtask,
800 m_task_t task = jtask_to_native_task(jtask,env);
801 m_host_t host = jhost_get_native(env,jhost);
804 jxbt_throw_notbound(env,"host",jhost);
808 jxbt_throw_notbound(env,"task",jtask);
812 if(MSG_OK != MSG_task_put_bounded(task,host,(int)chan_id,(double)jmaxRate))
813 jxbt_throw_native(env, xbt_strdup("MSG_task_put_bounded() failed"));
816 JNIEXPORT jint JNICALL
817 Java_simgrid_msg_Msg_getErrCode(JNIEnv* env, jclass cls) {
818 return (jint)MSG_get_errno();
821 JNIEXPORT jdouble JNICALL
822 Java_simgrid_msg_Msg_getClock(JNIEnv* env, jclass cls) {
823 return (jdouble)MSG_get_clock();
827 JNIEXPORT void JNICALL
828 Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
837 argc = (int)(*env)->GetArrayLength(env,jargs);
841 argv = xbt_new0(char*, argc);
843 argv[0] = strdup("java");
845 for(index = 0; index < argc -1; index++) {
846 jval = (jstring)(*env)->GetObjectArrayElement(env,jargs,index);
848 tmp = (*env)->GetStringUTFChars(env, jval, 0);
850 argv[index +1] = strdup(tmp);
852 (*env)->ReleaseStringUTFChars(env, jval, tmp);
855 MSG_global_init(&argc,argv);
856 MSG_set_channel_number(10); /* FIXME: this should not be fixed statically */
858 for(index = 0; index < argc; index++)
863 (*env)->GetJavaVM(env,&__java_vm);
867 JNIEXPORT void JNICALL
868 JNICALL Java_simgrid_msg_Msg_run(JNIEnv* env, jclass cls) {
869 xbt_fifo_item_t item = NULL;
870 m_host_t host = NULL;
874 if(MSG_OK != MSG_main())
875 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
877 DEBUG0("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
879 DEBUG0("Clean java world");
880 /* Cleanup java hosts */
881 xbt_fifo_foreach(msg_global->host,item,host,m_host_t) {
882 jhost = (jobject)host->data;
885 jhost_unref(env,jhost);
888 DEBUG0("Clean native world");
889 /* cleanup native stuff */
890 if(MSG_OK != MSG_clean())
891 jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
895 JNIEXPORT jint JNICALL
896 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv* env, jclass cls, jint jresetPID) {
897 return (jint)MSG_process_killall((int)jresetPID);
900 JNIEXPORT void JNICALL
901 Java_simgrid_msg_Msg_createEnvironment(JNIEnv* env, jclass cls,jstring jplatformFile) {
903 const char* platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
905 MSG_create_environment(platformFile);
907 (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
910 JNIEXPORT void JNICALL
911 Java_simgrid_msg_MsgNative_processExit(JNIEnv* env, jclass cls, jobject jprocess) {
913 m_process_t process = jprocess_to_native_process(jprocess,env);
916 jxbt_throw_notbound(env,"process",jprocess);
923 JNIEXPORT void JNICALL
924 Java_simgrid_msg_Msg_pajeOutput(JNIEnv* env, jclass cls, jstring jpajeFile) {
925 const char*pajeFile = (*env)->GetStringUTFChars(env, jpajeFile, 0);
927 MSG_paje_output(pajeFile);
929 (*env)->ReleaseStringUTFChars(env, jpajeFile, pajeFile);
933 JNIEXPORT void JNICALL
934 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js) {
935 const char* s = (*env)->GetStringUTFChars(env,js,0);
937 (*env)->ReleaseStringUTFChars(env, js, s);
940 JNIEXPORT jobjectArray JNICALL
941 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg) {
948 int count = xbt_fifo_size(msg_global->host);
949 m_host_t* table = (m_host_t *)xbt_fifo_to_array(msg_global->host);
951 jclass cls = jxbt_get_class(env,"simgrid/msg/Host");
957 jtable = (*env)->NewObjectArray(env,(jsize)count,cls,NULL);
960 jxbt_throw_jni(env,"Hosts table allocation failed");
964 for(index = 0; index < count; index++) {
966 jhost = (jobject)(host->data);
969 jname = (*env)->NewStringUTF(env,host->name);
971 jhost = Java_simgrid_msg_MsgNative_hostGetByName(env,cls_arg,jname);
972 /* FIXME: leak of jname ? */
975 (*env)->SetObjectArrayElement(env, jtable, index, jhost);
982 JNIEXPORT void JNICALL
983 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,jstring jname)
988 /* get the C string from the java string*/
989 const char* name = (*env)->GetStringUTFChars(env, jname, 0);
992 xbt_context_select_factory(name);
994 errmsg = xbt_strdup(e.msg);
998 (*env)->ReleaseStringUTFChars(env, jname, name);
1001 char *thrown = bprintf("xbt_select_context_factory() failed: %s",errmsg);
1003 jxbt_throw_native(env, thrown);
1007 JNIEXPORT void JNICALL
1008 Java_simgrid_msg_MsgNative_taskSend(JNIEnv* env, jclass cls,
1009 jstring jalias, jobject jtask,
1013 const char* alias = (*env)->GetStringUTFChars(env, jalias, 0);
1015 m_task_t task = jtask_to_native_task(jtask,env);
1019 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1020 jxbt_throw_notbound(env,"task",jtask);
1024 rv = MSG_task_send_with_timeout(task,alias,(double)jtimeout);
1026 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1029 jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
1033 JNIEXPORT void JNICALL
1034 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv* env, jclass cls,
1035 jstring jalias, jobject jtask,
1037 m_task_t task = jtask_to_native_task(jtask,env);
1042 jxbt_throw_notbound(env,"task",jtask);
1046 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1048 rv = MSG_task_send_bounded(task,alias,(double)jmaxRate);
1050 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1053 jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1056 JNIEXPORT jobject JNICALL
1057 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv* env, jclass cls,
1058 jstring jalias, jdouble jtimeout, jobject jhost) {
1060 m_task_t task = NULL;
1061 m_host_t host = NULL;
1065 host = jhost_get_native(env,jhost);
1068 jxbt_throw_notbound(env,"host",jhost);
1073 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1075 rv = MSG_task_receive_ext(&task,alias,(double)jtimeout,host);
1077 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1081 jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1085 return (jobject)task->data;
1088 JNIEXPORT jboolean JNICALL
1089 Java_simgrid_msg_MsgNative_taskListen(JNIEnv* env, jclass cls, jstring jalias) {
1094 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1096 rv = MSG_task_listen(alias);
1098 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1100 return (jboolean)rv;
1103 JNIEXPORT jint JNICALL
1104 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv* env, jclass cls, jstring jalias, jobject jhost) {
1109 m_host_t host = jhost_get_native(env,jhost);
1112 jxbt_throw_notbound(env,"host",jhost);
1116 alias = (*env)->GetStringUTFChars(env, jalias, 0);
1118 rv = MSG_task_listen_from_host(alias,host);
1120 (*env)->ReleaseStringUTFChars(env, jalias, alias);
1125 JNIEXPORT jint JNICALL
1126 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv* env, jclass cls, jstring jalias) {
1129 const char* alias = (*env)->GetStringUTFChars(env, jalias, 0);
1131 rv = MSG_task_listen_from(alias);
1133 (*env)->ReleaseStringUTFChars(env, jalias, alias);