Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
8b57c1c8d75b92b17e181071b96679ab66d21453
[simgrid.git] / src / java / jmsg.c
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier All right reserved. 
5  *
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.
8  *
9  * This contains the implementation of the wrapper functions used to interface
10  * the java object with the native functions of the MSG API.
11  */
12 #include "msg/msg.h"
13 #include "msg/private.h"
14 #include "simix/private.h"
15 #include "simix/smx_context_java.h"
16
17 #include "jmsg_process.h"
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jmsg_application_handler.h"
21 #include "jxbt_utilities.h"
22
23 #include "jmsg.h"
24 #include "msg/mailbox.h"
25 #include "surf/surfxml_parse.h"
26
27 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
28
29 static JavaVM *__java_vm = NULL;
30
31 static jobject native_to_java_process(m_process_t process);
32
33 JavaVM *get_java_VM(void)
34 {
35   return __java_vm;
36 }
37
38 JNIEnv *get_current_thread_env(void)
39 {
40   JNIEnv *env;
41
42   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
43
44   return env;
45 }
46
47 static jobject native_to_java_process(m_process_t process)
48 {
49   return ((smx_ctx_java_t)
50           (process->simdata->s_process->context))->jprocess;
51 }
52
53 /*
54  * The MSG process connected functions implementation.                                 
55  */
56
57 JNIEXPORT void JNICALL
58 Java_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
59                                          jobject jprocess_arg, jobject jhost)
60 {
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;
67
68   DEBUG4
69     ("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);
73
74   if (!jname) {
75     jxbt_throw_null(env,
76                     xbt_strdup
77                     ("Internal error: Process name cannot be NULL"));
78     return;
79   }
80
81   /* allocate the data of the simulation */
82   process = xbt_new0(s_m_process_t, 1);
83   process->simdata = xbt_new0(s_simdata_process_t, 1);
84
85   /* create a global java process instance */
86   jprocess = jprocess_new_global_ref(jprocess_arg, env);
87
88   if (!jprocess) {
89     free(process->simdata);
90     free(process);
91     jxbt_throw_jni(env, "Can't get a global ref to the java process");
92     return;
93   }
94
95   /* bind the java process instance to the native process */
96   jprocess_bind(jprocess, process, env);
97
98   /* build the C name of the process */
99   name = (*env)->GetStringUTFChars(env, jname, 0);
100   process->name = xbt_strdup(name);
101   (*env)->ReleaseStringUTFChars(env, jname, name);
102
103   process->simdata->m_host = jhost_get_native(env, jhost);
104
105
106   if (!(process->simdata->m_host)) {    /* not binded */
107     free(process->simdata);
108     free(process->data);
109     free(process);
110     jxbt_throw_notbound(env, "host", jhost);
111     return;
112   }
113   process->simdata->PID = msg_global->PID++;
114
115   /* create a new context */
116   DEBUG8
117     ("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
118      process->name, process->simdata->m_host->name, process->simdata->PID,
119      process, process->simdata, process->simdata->m_host,
120      process->simdata->m_host->simdata, env);
121
122   process->simdata->s_process = 
123     SIMIX_process_create(process->name, (xbt_main_func_t)jprocess, 
124                          /*data */ (void *) process,
125                          process->simdata->m_host->simdata->smx_host->name, 
126                          0, NULL, NULL);
127     
128   DEBUG1("context created (s_process=%p)", process->simdata->s_process);
129
130
131   if (SIMIX_process_self()) {   /* someone created me */
132     process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
133   } else {
134     process->simdata->PPID = -1;
135   }
136
137   process->simdata->last_errno = MSG_OK;
138
139   /* add the process to the list of the processes of the simulation */
140   xbt_fifo_unshift(msg_global->process_list, process);
141
142   sprintf(alias, "%s:%s", (process->simdata->m_host->simdata->smx_host)->name,
143           process->name);
144
145   mailbox = MSG_mailbox_new(alias);
146   
147 }
148
149 JNIEXPORT void JNICALL
150 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
151                                           jobject jprocess)
152 {
153   m_process_t process = jprocess_to_native_process(jprocess, env);
154
155   if (!process) {
156     jxbt_throw_notbound(env, "process", jprocess);
157     return;
158   }
159
160   /* try to suspend the process */
161   MSG_error_t rv = MSG_process_suspend(process);
162   
163   jxbt_check_res("MSG_process_suspend()",rv,MSG_OK,
164     bprintf("unexpected error , please report this bug"));
165   
166 }
167
168 JNIEXPORT void JNICALL
169 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
170                                          jobject jprocess)
171 {
172   m_process_t process = jprocess_to_native_process(jprocess, env);
173
174   if (!process) {
175     jxbt_throw_notbound(env,"process", jprocess);
176     return;
177   }
178
179   /* try to resume the process */
180     MSG_error_t rv = MSG_process_resume(process);
181     
182     jxbt_check_res("MSG_process_resume()",rv,MSG_OK,
183     bprintf("unexpected error , please report this bug"));
184 }
185
186 JNIEXPORT jboolean JNICALL
187 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
188                                               jobject jprocess)
189 {
190   m_process_t process = jprocess_to_native_process(jprocess, env);
191
192   if (!process) {
193     jxbt_throw_notbound(env, "process", jprocess);
194     return 0;
195   }
196
197   /* true is the process is suspended, false otherwise */
198   return (jboolean) MSG_process_is_suspended(process);
199 }
200
201 JNIEXPORT void JNICALL
202 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
203                                        jobject jprocess)
204 {
205   /* get the native instances from the java ones */
206   m_process_t process = jprocess_to_native_process(jprocess, env);
207
208   if (!process) {
209     jxbt_throw_notbound(env, "process", jprocess);
210     return;
211   }
212
213   /* delete the global reference */
214   jprocess_delete_global_ref(native_to_java_process(process), env);
215
216   /* kill the native process (this wrapper is call by the destructor of the java 
217    * process instance)
218    */
219   MSG_process_kill(process);
220 }
221
222 JNIEXPORT jobject JNICALL
223 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
224                                           jobject jprocess)
225 {
226   /* get the native instances from the java ones */
227   m_process_t process = jprocess_to_native_process(jprocess, env);
228   m_host_t host;
229
230   if (!process) {
231     jxbt_throw_notbound(env, "process", jprocess);
232     return NULL;
233   }
234
235   host = MSG_process_get_host(process);
236
237   if (!host->data) {
238     jxbt_throw_jni(env, "MSG_process_get_host() failed");
239     return NULL;
240   }
241
242   /* return the global reference to the java host instance */
243   return (jobject) host->data;
244
245 }
246
247 JNIEXPORT jobject JNICALL
248 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
249 {
250   m_process_t process = MSG_process_from_PID(PID);
251
252   if (!process) {
253     jxbt_throw_process_not_found(env, bprintf("PID = %d", PID));
254     return NULL;
255   }
256
257   if (!native_to_java_process(process)) {
258     jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
259     return NULL;
260   }
261
262   return (jobject) (native_to_java_process(process));
263 }
264
265
266 JNIEXPORT jint JNICALL
267 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
268                                          jobject jprocess)
269 {
270   m_process_t process = jprocess_to_native_process(jprocess, env);
271
272   if (!process) {
273     jxbt_throw_notbound(env, "process", jprocess);
274     return 0;
275   }
276
277   return (jint) MSG_process_get_PID(process);
278 }
279
280
281 JNIEXPORT jint JNICALL
282 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
283                                           jobject jprocess)
284 {
285   m_process_t process = jprocess_to_native_process(jprocess, env);
286
287   if (!process) {
288     jxbt_throw_notbound(env, "process", jprocess);
289     return 0;
290   }
291
292   return (jint) MSG_process_get_PPID(process);
293 }
294
295 JNIEXPORT jobject JNICALL
296 Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
297 {
298   m_process_t process = MSG_process_self();
299   jobject jprocess;
300
301   if (!process) {
302     jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
303     return NULL;
304   }
305
306   jprocess = native_to_java_process(process);
307
308   if (!jprocess)
309     jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
310
311   return jprocess;
312 }
313
314 JNIEXPORT void JNICALL
315 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
316                                              jobject jhost)
317 {
318   m_host_t host = jhost_get_native(env, jhost);
319
320   if (!host) {
321     jxbt_throw_notbound(env, "host", jhost);
322     return;
323   }
324
325   /* try to change the host of the process */
326   if (MSG_OK != MSG_process_change_host(host))
327     jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
328 }
329
330 JNIEXPORT void JNICALL
331 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
332                                           jdouble seconds)
333 {
334     MSG_error_t rv= MSG_process_sleep((double) seconds);
335     
336     jxbt_check_res("MSG_process_sleep()",rv, MSG_HOST_FAILURE,
337     bprintf("while process was waiting for %f seconds",(double)seconds));
338     
339 }
340
341
342 /***************************************************************************************
343  * The MSG host connected functions implementation.                                    *
344  ***************************************************************************************/
345
346 JNIEXPORT jobject JNICALL
347 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
348                                          jstring jname)
349 {
350   m_host_t host;                /* native host                                          */
351   jobject jhost;                /* global reference to the java host instance returned  */
352
353   /* get the C string from the java string */
354   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
355
356   /* get the host by name       (the hosts are created during the grid resolution) */
357   host = MSG_get_host_by_name(name);
358   DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
359
360   (*env)->ReleaseStringUTFChars(env, jname, name);
361
362   if (!host) {                  /* invalid name */
363     jxbt_throw_host_not_found(env, name);
364     return NULL;
365   }
366
367   if (!host->data) {            /* native host not associated yet with java host */
368
369     /* instanciate a new java host */
370     jhost = jhost_new_instance(env);
371
372     if (!jhost) {
373       jxbt_throw_jni(env, "java host instantiation failed");
374       return NULL;
375     }
376
377     /* get a global reference to the newly created host */
378     jhost = jhost_ref(env, jhost);
379
380     if (!jhost) {
381       jxbt_throw_jni(env, "new global ref allocation failed");
382       return NULL;
383     }
384
385     /* bind the java host and the native host */
386     jhost_bind(jhost, host, env);
387
388     /* the native host data field is set with the global reference to the 
389      * java host returned by this function 
390      */
391     host->data = (void *) jhost;
392   }
393
394   /* return the global reference to the java host instance */
395   return (jobject) host->data;
396 }
397
398 JNIEXPORT jstring JNICALL
399 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
400                                        jobject jhost)
401 {
402   m_host_t host = jhost_get_native(env, jhost);
403
404   if (!host) {
405     jxbt_throw_notbound(env, "host", jhost);
406     return NULL;
407   }
408
409   return (*env)->NewStringUTF(env, host->name);
410 }
411
412 JNIEXPORT jint JNICALL
413 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
414 {
415   return (jint) MSG_get_host_number();
416 }
417
418 JNIEXPORT jobject JNICALL
419 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
420 {
421   jobject jhost;
422
423   m_host_t host = MSG_host_self();
424
425   if (!host->data) {
426     /* the native host not yet associated with the java host instance */
427
428     /* instanciate a new java host instance */
429     jhost = jhost_new_instance(env);
430
431     if (!jhost) {
432       jxbt_throw_jni(env, "java host instantiation failed");
433       return NULL;
434     }
435
436     /* get a global reference to the newly created host */
437     jhost = jhost_ref(env, jhost);
438
439     if (!jhost) {
440       jxbt_throw_jni(env, "global ref allocation failed");
441       return NULL;
442     }
443
444     /* Bind & store it */
445     jhost_bind(jhost, host, env);
446     host->data = (void *) jhost;
447   } else {
448     jhost = (jobject) host->data;
449   }
450
451   return jhost;
452 }
453
454 JNIEXPORT jdouble JNICALL
455 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
456                                         jobject jhost)
457 {
458   m_host_t host = jhost_get_native(env, jhost);
459
460   if (!host) {
461     jxbt_throw_notbound(env, "host", jhost);
462     return -1;
463   }
464
465   return (jdouble) MSG_get_host_speed(host);
466 }
467
468 JNIEXPORT jint JNICALL
469 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
470                                        jobject jhost)
471 {
472   m_host_t host = jhost_get_native(env, jhost);
473
474   if (!host) {
475     jxbt_throw_notbound(env, "host", jhost);
476     return -1;
477   }
478
479   return (jint) MSG_get_host_msgload(host);
480 }
481
482
483 JNIEXPORT jboolean JNICALL
484 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
485                                        jobject jhost)
486 {
487   m_host_t host = jhost_get_native(env, jhost);
488
489   if (!host) {
490     jxbt_throw_notbound(env, "host", jhost);
491     return 0;
492   }
493
494   return (jboolean) MSG_host_is_avail(host);
495 }
496
497
498 /***************************************************************************************
499  * The MSG task connected functions implementation.                                    *
500  ***************************************************************************************/
501
502 JNIEXPORT void JNICALL
503 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
504                                       jstring jname, jdouble jcomputeDuration,
505                                       jdouble jmessageSize)
506 {
507   m_task_t task;                /* the native task to create                            */
508   const char *name=NULL;        /* the name of the task                                 */
509
510   if (jcomputeDuration < 0) {
511     jxbt_throw_illegal(env,
512                        bprintf("Task ComputeDuration (%f) cannot be negative",
513                                (double) jcomputeDuration));
514     return;
515   }
516
517   if (jmessageSize < 0) {
518     jxbt_throw_illegal(env,
519                        bprintf("Task MessageSize (%f) cannot be negative",
520                                (double) jmessageSize));
521     return;
522   }
523
524   if (jname) {
525     /* get the C string from the java string */
526     name = (*env)->GetStringUTFChars(env, jname, 0);
527   }
528
529
530   /* create the task */
531   task =
532     MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
533                     NULL);
534
535   if (jname)
536     (*env)->ReleaseStringUTFChars(env, jname, name);
537
538   /* bind & store the task */
539   jtask_bind(jtask, task, env);
540
541   /* allocate a new global reference to the java task instance */
542   task->data = (void *) jtask_new_global_ref(jtask, env);
543
544   if (!task->data)
545     jxbt_throw_jni(env, "global ref allocation failed");
546
547 }
548
549 JNIEXPORT void JNICALL
550 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
551                                                jobject jtask, jstring jname,
552                                                jobjectArray jhosts,
553                                                jdoubleArray
554                                                jcomputeDurations_arg,
555                                                jdoubleArray jmessageSizes_arg)
556 {
557
558   m_task_t task;                /* the native parallel task to create           */
559   const char *name;             /* the name of the task                         */
560   int host_count;
561   m_host_t *hosts;
562   double *computeDurations;
563   double *messageSizes;
564   jdouble *jcomputeDurations;
565   jdouble *jmessageSizes;
566
567   jobject jhost;
568   int index;
569
570
571   if (!jcomputeDurations_arg) {
572     jxbt_throw_null(env,
573                     xbt_strdup
574                     ("Parallel task compute durations cannot be null"));
575     return;
576   }
577
578   if (!jmessageSizes_arg) {
579     jxbt_throw_null(env,
580                     xbt_strdup("Parallel task message sizes cannot be null"));
581     return;
582   }
583
584   if (!jname) {
585     jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
586     return;
587   }
588
589   host_count = (int) (*env)->GetArrayLength(env, jhosts);
590
591
592   hosts = xbt_new0(m_host_t, host_count);
593   computeDurations = xbt_new0(double, host_count);
594   messageSizes = xbt_new0(double, host_count * host_count);
595
596   jcomputeDurations =
597     (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
598   jmessageSizes = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
599
600   for (index = 0; index < host_count; index++) {
601     jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
602     hosts[index] = jhost_get_native(env, jhost);
603     computeDurations[index] = jcomputeDurations[index];
604   }
605   for (index = 0; index < host_count * host_count; index++) {
606     messageSizes[index] = jmessageSizes[index];
607   }
608
609   (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
610                                      jcomputeDurations, 0);
611   (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
612                                      0);
613
614
615   /* get the C string from the java string */
616   name = (*env)->GetStringUTFChars(env, jname, 0);
617
618   task =
619     MSG_parallel_task_create(name, host_count, hosts, computeDurations,
620                              messageSizes, NULL);
621
622   (*env)->ReleaseStringUTFChars(env, jname, name);
623
624   /* associate the java task object and the native task */
625   jtask_bind(jtask, task, env);
626
627   task->data = (void *) jtask;
628
629   if (!task->data)
630     jxbt_throw_jni(env, "global ref allocation failed");
631 }
632
633 JNIEXPORT jobject JNICALL
634 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
635                                          jobject jtask)
636 {
637   m_process_t process;
638
639   m_task_t task = jtask_to_native_task(jtask, env);
640
641   if (!task) {
642     jxbt_throw_notbound(env, "task", jtask);
643     return NULL;
644   }
645
646   process = MSG_task_get_sender(task);
647   return (jobject) native_to_java_process(process);
648 }
649
650 JNIEXPORT jobject JNICALL
651 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
652                                          jobject jtask)
653 {
654   m_host_t host;
655   m_task_t task = jtask_to_native_task(jtask, env);
656
657   if (!task) {
658     jxbt_throw_notbound(env, "task", jtask);
659     return NULL;
660   }
661
662   host = MSG_task_get_source(task);
663
664   if (!host->data) {
665     jxbt_throw_jni(env, "MSG_task_get_source() failed");
666     return NULL;
667   }
668
669   return (jobject) host->data;
670 }
671
672
673 JNIEXPORT jstring JNICALL
674 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
675                                        jobject jtask)
676 {
677   m_task_t task = jtask_to_native_task(jtask, env);
678
679   if (!task) {
680     jxbt_throw_notbound(env, "task", jtask);
681     return NULL;
682   }
683
684   return (*env)->NewStringUTF(env, task->name);
685 }
686
687 JNIEXPORT void JNICALL
688 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
689 {
690   m_task_t ptask = jtask_to_native_task(jtask, env);
691
692   if (!ptask) {
693     jxbt_throw_notbound(env, "task", jtask);
694     return;
695   }
696
697   MSG_error_t rv = MSG_task_cancel(ptask);
698   
699     jxbt_check_res("MSG_task_cancel()",rv,MSG_OK,
700     bprintf("unexpected error , please report this bug"));
701 }
702
703 JNIEXPORT jdouble JNICALL
704 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
705                                                   jobject jtask)
706 {
707   m_task_t ptask = jtask_to_native_task(jtask, env);
708
709   if (!ptask) {
710     jxbt_throw_notbound(env, "task", jtask);
711     return -1;
712   }
713   return (jdouble) MSG_task_get_compute_duration(ptask);
714 }
715
716 JNIEXPORT jdouble JNICALL
717 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env, jclass cls,
718                                                     jobject jtask)
719 {
720   m_task_t ptask = jtask_to_native_task(jtask, env);
721
722   if (!ptask) {
723     jxbt_throw_notbound(env, "task", jtask);
724     return -1;
725   }
726   return (jdouble) MSG_task_get_remaining_computation(ptask);
727 }
728
729 JNIEXPORT void JNICALL
730 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
731                                            jobject jtask, jdouble priority)
732 {
733   m_task_t task = jtask_to_native_task(jtask, env);
734
735   if (!task) {
736     jxbt_throw_notbound(env, "task", jtask);
737     return;
738   }
739   MSG_task_set_priority(task, (double) priority);
740 }
741
742 JNIEXPORT void JNICALL
743 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
744                                        jobject jtask_arg)
745 {
746
747   /* get the native task */
748   m_task_t task = jtask_to_native_task(jtask_arg, env);
749   jobject jtask;
750
751   if (!task) {
752     jxbt_throw_notbound(env, "task", task);
753     return;
754   }
755   jtask = (jobject) task->data;
756     
757   MSG_error_t rv = MSG_task_destroy(task);
758     
759   jxbt_check_res("MSG_task_destroy()",rv,MSG_OK,
760     bprintf("unexpected error , please report this bug"));
761
762
763   /* delete the global reference to the java task object */
764   jtask_delete_global_ref(jtask, env);
765 }
766
767 JNIEXPORT void JNICALL
768 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
769                                        jobject jtask)
770 {
771   m_task_t task = jtask_to_native_task(jtask, env);
772
773   if (!task) {
774     jxbt_throw_notbound(env, "task", jtask);
775     return;
776   }
777
778   MSG_error_t rv = MSG_task_execute(task);
779   
780     jxbt_check_res("MSG_task_execute()",rv,MSG_HOST_FAILURE|MSG_TASK_CANCELLED,
781     bprintf("while executing task %s", MSG_task_get_name(task)));
782 }
783
784 /***************************************************************************************
785  * Unsortable functions                                                        *
786  ***************************************************************************************/
787
788
789 JNIEXPORT jint JNICALL
790 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
791 {
792   return (jint) MSG_get_errno();
793 }
794
795 JNIEXPORT jdouble JNICALL
796 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
797 {
798   return (jdouble) MSG_get_clock();
799 }
800
801
802 JNIEXPORT void JNICALL
803 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) {
804   char **argv = NULL;
805   int index;
806   int argc = 0;
807   jstring jval;
808   const char *tmp;
809
810   if (jargs)
811     argc = (int) (*env)->GetArrayLength(env, jargs);
812
813   argc++;
814   argv = xbt_new0(char *, argc);
815   argv[0] = strdup("java");
816
817   for (index = 0; index < argc - 1; index++) {
818     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
819     tmp = (*env)->GetStringUTFChars(env, jval, 0);
820     argv[index + 1] = strdup(tmp);
821     (*env)->ReleaseStringUTFChars(env, jval, tmp);
822   }
823
824   MSG_global_init(&argc, argv);
825   SIMIX_context_select_factory("java");
826
827   for (index = 0; index < argc; index++)
828     free(argv[index]);
829
830   free(argv);
831
832   (*env)->GetJavaVM(env, &__java_vm);
833 }
834
835 JNIEXPORT void JNICALL
836   JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) {
837   xbt_fifo_item_t item = NULL;
838   m_host_t host = NULL;
839   jobject jhost;
840
841   /* Run everything */
842   if (MSG_OK != MSG_main()) {
843     jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
844   }
845   DEBUG0
846     ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
847
848   DEBUG0("Clean java world");
849   /* Cleanup java hosts */
850   xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
851     jhost = (jobject) host->data;
852
853     if (jhost)
854       jhost_unref(env, jhost);
855   }
856
857   DEBUG0("Clean native world");
858   /* cleanup native stuff */
859   if (MSG_OK != MSG_clean()){
860     jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
861   }
862 }
863
864 JNIEXPORT jint JNICALL
865 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
866                                           jint jresetPID)
867 {
868   return (jint) MSG_process_killall((int) jresetPID);
869 }
870
871 JNIEXPORT void JNICALL
872 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
873                                        jstring jplatformFile)
874 {
875
876   const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
877
878   MSG_create_environment(platformFile);
879
880   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
881 }
882
883 JNIEXPORT void JNICALL
884 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
885                                        jobject jprocess)
886 {
887
888   m_process_t process = jprocess_to_native_process(jprocess, env);
889
890   if (!process) {
891     jxbt_throw_notbound(env, "process", jprocess);
892     return;
893   }
894
895   SIMIX_context_stop(SIMIX_process_self()->context);
896 }
897
898 JNIEXPORT void JNICALL
899 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
900 {
901   const char *s = (*env)->GetStringUTFChars(env, js, 0);
902   INFO1("%s", s);
903   (*env)->ReleaseStringUTFChars(env, js, s);
904 }
905
906 JNIEXPORT jobjectArray JNICALL
907 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
908 {
909   int index;
910   jobjectArray jtable;
911   jobject jhost;
912   jstring jname;
913   m_host_t host;
914
915   int count = xbt_fifo_size(msg_global->host);
916   m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
917
918   jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
919
920   if (!cls) {
921     return NULL;
922   }
923
924   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
925
926   if (!jtable) {
927     jxbt_throw_jni(env, "Hosts table allocation failed");
928     return NULL;
929   }
930
931   for (index = 0; index < count; index++) {
932     host = table[index];
933     jhost = (jobject) (host->data);
934
935     if (!jhost) {
936       jname = (*env)->NewStringUTF(env, host->name);
937
938       jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
939       /* FIXME: leak of jname ? */
940     }
941
942     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
943   }
944
945   return jtable;
946 }
947
948
949 JNIEXPORT void JNICALL
950 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
951                                                 jstring jname)
952 {
953   char *errmsg = NULL;
954   xbt_ex_t e;
955
956   /* get the C string from the java string */
957   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
958
959   TRY {
960     SIMIX_context_select_factory(name);
961   } CATCH(e) {
962     errmsg = xbt_strdup(e.msg);
963     xbt_ex_free(e);
964   }
965
966   (*env)->ReleaseStringUTFChars(env, jname, name);
967
968   if (errmsg) {
969     char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
970     free(errmsg);
971     jxbt_throw_native(env, thrown);
972   }
973 }
974
975 JNIEXPORT void JNICALL
976 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
977                                     jstring jalias, jobject jtask,
978                                     jdouble jtimeout)
979 {
980
981   MSG_error_t rv;
982   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
983
984   m_task_t task = jtask_to_native_task(jtask, env);
985
986
987   if (!task) {
988     (*env)->ReleaseStringUTFChars(env, jalias, alias);
989     jxbt_throw_notbound(env, "task", jtask);
990     return;
991   }
992
993   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
994
995   (*env)->ReleaseStringUTFChars(env, jalias, alias);
996
997   jxbt_check_res("MSG_task_send_with_timeout()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT_FAILURE,
998     bprintf("while sending task %s to mailbox %s", MSG_task_get_name(task),alias));
999 }
1000
1001 JNIEXPORT void JNICALL
1002 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1003                                            jstring jalias, jobject jtask,
1004                                            jdouble jmaxRate)
1005 {
1006   m_task_t task = jtask_to_native_task(jtask, env);
1007   MSG_error_t rv;
1008   const char *alias;
1009
1010   if (!task) {
1011     jxbt_throw_notbound(env, "task", jtask);
1012     return;
1013   }
1014
1015   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1016
1017   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1018
1019   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1020
1021   jxbt_check_res("MSG_task_send_bounded()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT_FAILURE,
1022     bprintf("while sending task %s to mailbox %s with max rate %f", MSG_task_get_name(task),alias,(double)jmaxRate));
1023     
1024 }
1025
1026 JNIEXPORT jobject JNICALL
1027 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1028                                        jstring jalias, jdouble jtimeout,
1029                                        jobject jhost)
1030 {
1031   MSG_error_t rv;
1032   m_task_t task = NULL;
1033   m_host_t host = NULL;
1034   const char *alias;
1035
1036   if (jhost) {
1037     host = jhost_get_native(env, jhost);
1038
1039     if (!host) {
1040       jxbt_throw_notbound(env, "host", jhost);
1041       return NULL;
1042     }
1043   }
1044
1045   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1046
1047   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1048
1049   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1050
1051   jxbt_check_res("MSG_task_receive_ext()",rv, MSG_HOST_FAILURE|MSG_TRANSFER_FAILURE|MSG_TIMEOUT_FAILURE,
1052     bprintf("while receiving from mailbox %s",alias));
1053
1054   return (jobject) task->data;
1055 }
1056
1057 JNIEXPORT jboolean JNICALL
1058 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1059                                       jstring jalias)
1060 {
1061
1062   const char *alias;
1063   int rv;
1064
1065   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1066
1067   rv = MSG_task_listen(alias);
1068
1069   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1070
1071   return (jboolean) rv;
1072 }
1073
1074 JNIEXPORT jint JNICALL
1075 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1076                                               jstring jalias, jobject jhost)
1077 {
1078   int rv;
1079   const char *alias;
1080
1081   m_host_t host = jhost_get_native(env, jhost);
1082
1083   if (!host) {
1084     jxbt_throw_notbound(env, "host", jhost);
1085     return -1;
1086   }
1087   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1088
1089   rv = MSG_task_listen_from_host(alias, host);
1090
1091   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1092
1093   return (jint) rv;
1094 }
1095
1096 JNIEXPORT jint JNICALL
1097 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1098                                           jstring jalias)
1099 {
1100
1101   int rv;
1102   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1103
1104   rv = MSG_task_listen_from(alias);
1105
1106   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1107
1108   return (jint) rv;
1109 }
1110
1111 JNIEXPORT void JNICALL
1112 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1113                                        jstring jdeploymentFile)
1114 {
1115
1116   const char *deploymentFile =
1117     (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1118
1119   surf_parse_reset_parser();
1120
1121   surfxml_add_callback(STag_surfxml_process_cb_list,
1122                        japplication_handler_on_begin_process);
1123
1124   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1125                        japplication_handler_on_process_arg);
1126
1127   surfxml_add_callback(STag_surfxml_prop_cb_list,
1128                        japplication_handler_on_property);
1129
1130   surfxml_add_callback(ETag_surfxml_process_cb_list,
1131                        japplication_handler_on_end_process);
1132
1133   surf_parse_open(deploymentFile);
1134
1135   japplication_handler_on_start_document();
1136
1137   if (surf_parse())
1138     jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
1139
1140   surf_parse_close();
1141
1142   japplication_handler_on_end_document();
1143
1144   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1145 }