Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ac07bfba31b6da054d8b5bd04712f359465f5e0b
[simgrid.git] / src / jmsg.c
1 /* Java Wrappers to the MSG API.                                            */
2
3 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
4  * All rights 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 #include <msg/msg.h>
10 #include <simgrid/simix.h>
11 #include <surf/surfxml_parse.h>
12
13
14 #include "smx_context_java.h"
15
16 #include "jmsg_process.h"
17
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
25 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
26 #ifndef JNIEXPORT
27 #define JNIEXPORT
28 #endif
29 #ifndef JNICALL
30 #define JNICALL
31 #endif
32 /* end of eclipse-mandated pimple */
33
34 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
35
36 static JavaVM *__java_vm = NULL;
37
38
39 JavaVM *get_java_VM(void)
40 {
41   return __java_vm;
42 }
43
44 JNIEnv *get_current_thread_env(void)
45 {
46   JNIEnv *env;
47
48   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
49
50   return env;
51 }
52
53 /*
54  * The MSG process connected functions implementation.                                 
55  */
56
57 JNIEXPORT void JNICALL
58 Java_org_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
59                                          jobject jprocess_arg,
60                                          jobject jhostname)
61 {
62      
63    
64   jobject jprocess;             /* the global reference to the java process instance    */
65   jstring jname;                /* the name of the java process instance                */
66   const char *name;             /* the C name of the process                            */
67   const char *hostname;
68   m_process_t process;          /* the native process to create                         */
69   m_host_t host;                /* Where that process lives */
70    
71   hostname = (*env)->GetStringUTFChars(env, jhostname, 0);
72
73   XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,host=%s)",
74          env, cls, jprocess_arg, hostname);
75    
76    
77   /* get the name of the java process */
78   jname = jprocess_get_name(jprocess_arg, env);
79   if (!jname) {
80     jxbt_throw_null(env,
81             xbt_strdup("Internal error: Process name cannot be NULL"));
82     return;
83   }
84
85   /* bind/retrieve the msg host */
86   host = MSG_get_host_by_name(hostname);
87
88   if (!(host)) {    /* not binded */
89     jxbt_throw_host_not_found(env, hostname);
90     return;
91   }
92
93   /* create a global java process instance */
94   jprocess = jprocess_new_global_ref(jprocess_arg, env);
95   if (!jprocess) {
96     jxbt_throw_jni(env, "Can't get a global ref to the java process");
97     return;
98   }
99
100   /* build the C name of the process */
101   name = (*env)->GetStringUTFChars(env, jname, 0);
102   name = xbt_strdup(name);
103   
104   /* Actually build the MSG process */
105   process = MSG_process_create_with_environment(name,
106                                                 (xbt_main_func_t) jprocess,
107                                                 /*data*/ NULL,
108                                                 host,
109                                                 /* kill_time */-1,
110                                                 /*argc, argv, properties*/
111                                                 0,NULL,NULL);
112      
113   MSG_process_set_data(process,&process);
114    
115   /* release our reference to the process name (variable name becomes invalid) */
116   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
117   //(*env)->ReleaseStringUTFChars(env, jname, name);
118   (*env)->ReleaseStringUTFChars(env, jhostname, hostname);
119    
120   /* bind the java process instance to the native process */
121   jprocess_bind(jprocess, process, env);
122
123 }
124
125 JNIEXPORT void JNICALL
126 Java_org_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
127                                           jobject jprocess)
128 {
129   m_process_t process = jprocess_to_native_process(jprocess, env);
130
131   if (!process) {
132     jxbt_throw_notbound(env, "process", jprocess);
133     return;
134   }
135
136   /* try to suspend the process */
137   MSG_error_t rv = MSG_process_suspend(process);
138
139   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
140                  bprintf("unexpected error , please report this bug"));
141
142 }
143
144 JNIEXPORT void JNICALL
145 Java_org_simgrid_msg_Process_simulatedSleep(JNIEnv * env, jobject jprocess,
146                                            jdouble jseconds) {
147           m_process_t process = jprocess_to_native_process(jprocess, env);
148
149           if (!process) {
150             jxbt_throw_notbound(env, "process", jprocess);
151             return;
152           }
153           MSG_error_t rv = MSG_process_sleep((double)jseconds);
154
155           jxbt_check_res("MSG_process_sleep()", rv, MSG_OK,
156                          bprintf("unexpected error , please report this bug"));
157 }
158
159
160 JNIEXPORT void JNICALL
161 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
162                                          jobject jprocess)
163 {
164   m_process_t process = jprocess_to_native_process(jprocess, env);
165
166   if (!process) {
167     jxbt_throw_notbound(env, "process", jprocess);
168     return;
169   }
170
171   /* try to resume the process */
172   MSG_error_t rv = MSG_process_resume(process);
173
174   jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
175                  bprintf("unexpected error , please report this bug"));
176 }
177
178 JNIEXPORT jboolean JNICALL
179 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
180                                               jobject jprocess)
181 {
182   m_process_t process = jprocess_to_native_process(jprocess, env);
183
184   if (!process) {
185     jxbt_throw_notbound(env, "process", jprocess);
186     return 0;
187   }
188
189   /* true is the process is suspended, false otherwise */
190   return (jboolean) MSG_process_is_suspended(process);
191 }
192
193 JNIEXPORT void JNICALL
194 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
195                                        jobject jprocess)
196 {
197   /* get the native instances from the java ones */
198   m_process_t process = jprocess_to_native_process(jprocess, env);
199
200   if (!process) {
201     jxbt_throw_notbound(env, "process", jprocess);
202     return;
203   }
204
205   /* kill the native process (this wrapper is call by the destructor of the java 
206    * process instance)
207    */
208   MSG_process_kill(process);
209 }
210
211 JNIEXPORT jobject JNICALL
212 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
213                                           jobject jprocess)
214 {
215   /* get the native instances from the java ones */
216   m_process_t process = jprocess_to_native_process(jprocess, env);
217   m_host_t host;
218
219   if (!process) {
220     jxbt_throw_notbound(env, "process", jprocess);
221     return NULL;
222   }
223
224   host = MSG_process_get_host(process);
225   jobject res = (jobject)MSG_host_get_data(host);
226
227   if (!res) {
228           XBT_INFO("Binding error for host %s ",MSG_host_get_name(host));
229           jxbt_throw_jni(env, bprintf("Binding error for host %s ",MSG_host_get_name(host)));
230           return NULL;
231   }
232
233   /* return the global reference to the java host instance */
234   return res;
235
236 }
237
238 JNIEXPORT jobject JNICALL
239 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
240                                           jint PID)
241 {
242   m_process_t process = MSG_process_from_PID(PID);
243
244   if (!process) {
245     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
246     return NULL;
247   }
248
249   if (!native_to_java_process(process)) {
250     jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
251     return NULL;
252   }
253
254   return (jobject) (native_to_java_process(process));
255 }
256
257
258 JNIEXPORT jint JNICALL
259 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
260                                          jobject jprocess)
261 {
262   m_process_t process = jprocess_to_native_process(jprocess, env);
263
264   if (!process) {
265     jxbt_throw_notbound(env, "process", jprocess);
266     return 0;
267   }
268
269   return (jint) MSG_process_get_PID(process);
270 }
271
272
273 JNIEXPORT jint JNICALL
274 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
275                                           jobject jprocess)
276 {
277   m_process_t process = jprocess_to_native_process(jprocess, env);
278
279   if (!process) {
280     jxbt_throw_notbound(env, "process", jprocess);
281     return 0;
282   }
283
284   return (jint) MSG_process_get_PPID(process);
285 }
286
287 JNIEXPORT jobject JNICALL
288 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
289 {
290   m_process_t process = MSG_process_self();
291   jobject jprocess;
292
293   if (!process) {
294     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
295     return NULL;
296   }
297
298   jprocess = native_to_java_process(process);
299
300   if (!jprocess)
301     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
302
303   return jprocess;
304 }
305
306 JNIEXPORT void JNICALL
307 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
308                                              jobject jprocess, jobject jhost)
309 {
310   m_process_t process = jprocess_to_native_process(jprocess, env);
311
312   if (!process) {
313     jxbt_throw_notbound(env, "process", jprocess);
314     return;
315   }
316
317   m_host_t host = jhost_get_native(env, jhost);
318
319   if (!host) {
320     jxbt_throw_notbound(env, "host", jhost);
321     return;
322   }
323
324   /* try to change the host of the process */
325   MSG_error_t rv = MSG_process_migrate(process, host);
326   jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
327                  bprintf("unexpected error , please report this bug"));
328
329 }
330
331 JNIEXPORT void JNICALL
332 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
333                                           jdouble seconds)
334 {
335   MSG_error_t rv = MSG_process_sleep((double) seconds);
336
337   jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
338                  bprintf("while process was waiting for %f seconds",
339                          (double) seconds));
340
341 }
342
343
344 /***************************************************************************************
345  * The MSG host connected functions implementation.                                    *
346  ***************************************************************************************/
347
348 JNIEXPORT jobject JNICALL
349 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
350                                          jstring jname)
351 {
352   m_host_t host;                /* native host                                          */
353   jobject jhost;                /* global reference to the java host instance returned  */
354
355   /* get the C string from the java string */
356   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
357   XBT_DEBUG("Looking for host '%s'",name);
358   /* get the host by name       (the hosts are created during the grid resolution) */
359   host = MSG_get_host_by_name(name);
360   XBT_DEBUG("MSG gave %p as native host", host);
361
362   if (!host) {                  /* invalid name */
363     jxbt_throw_host_not_found(env, name);
364     (*env)->ReleaseStringUTFChars(env, jname, name);
365     return NULL;
366   }
367   (*env)->ReleaseStringUTFChars(env, jname, name);
368
369   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
370
371     /* Instantiate a new java host */
372     jhost = jhost_new_instance(env);
373
374     if (!jhost) {
375       jxbt_throw_jni(env, "java host instantiation failed");
376       return NULL;
377     }
378
379     /* get a global reference to the newly created host */
380     jhost = jhost_ref(env, jhost);
381
382     if (!jhost) {
383       jxbt_throw_jni(env, "new global ref allocation failed");
384       return NULL;
385     }
386
387     /* bind the java host and the native host */
388     jhost_bind(jhost, host, env);
389
390     /* the native host data field is set with the global reference to the 
391      * java host returned by this function 
392      */
393     MSG_host_set_data(host, (void *) jhost);
394   }
395
396   /* return the global reference to the java host instance */
397   return (jobject) MSG_host_get_data(host);
398 }
399
400 JNIEXPORT jstring JNICALL
401 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
402                                        jobject jhost)
403 {
404   m_host_t host = jhost_get_native(env, jhost);
405   const char* name;
406
407   if (!host) {
408     jxbt_throw_notbound(env, "host", jhost);
409     return NULL;
410   }
411
412   name = MSG_host_get_name(host);
413   if (!name)
414           xbt_die("This host has no name...");
415
416   return (*env)->NewStringUTF(env, name);
417 }
418
419 JNIEXPORT jint JNICALL
420 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
421 {
422   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
423   int nb_host = xbt_dynar_length(hosts);
424   xbt_dynar_free(&hosts);
425   return (jint) nb_host;
426 }
427
428 JNIEXPORT jobject JNICALL
429 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
430 {
431   jobject jhost;
432
433   m_host_t host = MSG_host_self();
434
435   if (!MSG_host_get_data(host)) {
436     /* the native host not yet associated with the java host instance */
437
438     /* instanciate a new java host instance */
439     jhost = jhost_new_instance(env);
440
441     if (!jhost) {
442       jxbt_throw_jni(env, "java host instantiation failed");
443       return NULL;
444     }
445
446     /* get a global reference to the newly created host */
447     jhost = jhost_ref(env, jhost);
448
449     if (!jhost) {
450       jxbt_throw_jni(env, "global ref allocation failed");
451       return NULL;
452     }
453
454     /* Bind & store it */
455     jhost_bind(jhost, host, env);
456     MSG_host_set_data(host, (void *) jhost);
457   } else {
458     jhost = (jobject) MSG_host_get_data(host);
459   }
460
461   return jhost;
462 }
463
464 JNIEXPORT jdouble JNICALL
465 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
466                                         jobject jhost)
467 {
468   m_host_t host = jhost_get_native(env, jhost);
469
470   if (!host) {
471     jxbt_throw_notbound(env, "host", jhost);
472     return -1;
473   }
474
475   return (jdouble) MSG_get_host_speed(host);
476 }
477
478 JNIEXPORT jint JNICALL
479 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
480                                        jobject jhost)
481 {
482   m_host_t host = jhost_get_native(env, jhost);
483
484   if (!host) {
485     jxbt_throw_notbound(env, "host", jhost);
486     return -1;
487   }
488
489   return (jint) MSG_get_host_msgload(host);
490 }
491
492
493 JNIEXPORT jboolean JNICALL
494 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
495                                        jobject jhost)
496 {
497   m_host_t host = jhost_get_native(env, jhost);
498
499   if (!host) {
500     jxbt_throw_notbound(env, "host", jhost);
501     return 0;
502   }
503
504   return (jboolean) MSG_host_is_avail(host);
505 }
506
507
508 /***************************************************************************************
509  * The MSG task connected functions implementation.                                    *
510  ***************************************************************************************/
511
512 JNIEXPORT jobject JNICALL
513 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
514                                          jobject jtask)
515 {
516   m_host_t host;
517   m_task_t task = jtask_to_native_task(jtask, env);
518
519   if (!task) {
520     jxbt_throw_notbound(env, "task", jtask);
521     return NULL;
522   }
523
524   host = MSG_task_get_source(task);
525
526   if (!MSG_host_get_data(host)) {
527     jxbt_throw_jni(env, "MSG_task_get_source() failed");
528     return NULL;
529   }
530
531   return (jobject) MSG_host_get_data(host);
532 }
533
534 JNIEXPORT void JNICALL
535 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
536                                       jobject jtask)
537 {
538   m_task_t ptask = jtask_to_native_task(jtask, env);
539
540   if (!ptask) {
541     jxbt_throw_notbound(env, "task", jtask);
542     return;
543   }
544
545   MSG_error_t rv = MSG_task_cancel(ptask);
546
547   jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
548                  bprintf("unexpected error , please report this bug"));
549 }
550
551 JNIEXPORT jdouble JNICALL
552 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
553                                                   jobject jtask)
554 {
555   m_task_t ptask = jtask_to_native_task(jtask, env);
556
557   if (!ptask) {
558     jxbt_throw_notbound(env, "task", jtask);
559     return -1;
560   }
561   return (jdouble) MSG_task_get_compute_duration(ptask);
562 }
563
564 JNIEXPORT jdouble JNICALL
565 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
566                                                     jclass cls,
567                                                     jobject jtask)
568 {
569   m_task_t ptask = jtask_to_native_task(jtask, env);
570
571   if (!ptask) {
572     jxbt_throw_notbound(env, "task", jtask);
573     return -1;
574   }
575   return (jdouble) MSG_task_get_remaining_computation(ptask);
576 }
577
578 JNIEXPORT void JNICALL
579 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
580                                            jobject jtask, jdouble priority)
581 {
582   m_task_t task = jtask_to_native_task(jtask, env);
583
584   if (!task) {
585     jxbt_throw_notbound(env, "task", jtask);
586     return;
587   }
588   MSG_task_set_priority(task, (double) priority);
589 }
590
591 JNIEXPORT void JNICALL
592 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
593                                        jobject jtask)
594 {
595   m_task_t task = jtask_to_native_task(jtask, env);
596
597   if (!task) {
598     jxbt_throw_notbound(env, "task", jtask);
599     return;
600   }
601
602   MSG_error_t rv = MSG_task_execute(task);
603
604   jxbt_check_res("MSG_task_execute()", rv,
605                  MSG_HOST_FAILURE | MSG_TASK_CANCELED,
606                  bprintf("while executing task %s",
607                          MSG_task_get_name(task)));
608 }
609
610 /***************************************************************************************
611  * Unsortable functions                                                        *
612  ***************************************************************************************/
613
614 JNIEXPORT jdouble JNICALL
615 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
616 {
617   return (jdouble) MSG_get_clock();
618 }
619
620 JNIEXPORT void JNICALL
621 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
622 {
623   char **argv = NULL;
624   int index;
625   int argc = 0;
626   jstring jval;
627   const char *tmp;
628
629   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
630
631   if (jargs)
632     argc = (int) (*env)->GetArrayLength(env, jargs);
633
634   argc++;
635   argv = xbt_new(char *, argc + 1);
636   argv[0] = strdup("java");
637
638   for (index = 0; index < argc - 1; index++) {
639     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
640     tmp = (*env)->GetStringUTFChars(env, jval, 0);
641     argv[index + 1] = strdup(tmp);
642     (*env)->ReleaseStringUTFChars(env, jval, tmp);
643   }
644   argv[argc] = NULL;
645
646   MSG_global_init(&argc, argv);
647
648   for (index = 0; index < argc; index++)
649     free(argv[index]);
650
651   free(argv);
652
653   (*env)->GetJavaVM(env, &__java_vm);
654 }
655
656 JNIEXPORT void JNICALL
657     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
658 {
659   MSG_error_t rv;
660   int index;
661   xbt_dynar_t hosts;
662   jobject jhost;
663
664   /* Run everything */
665   XBT_INFO("Ready to run MSG_MAIN");
666   rv = MSG_main();
667   XBT_INFO("Done running MSG_MAIN");
668   jxbt_check_res("MSG_main()", rv, MSG_OK,
669                  bprintf
670                  ("unexpected error : MSG_main() failed .. please report this bug "));
671
672   XBT_INFO("MSG_main finished");
673
674   XBT_INFO("Clean java world");
675   /* Cleanup java hosts */
676   hosts = MSG_hosts_as_dynar();
677   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
678     jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
679     if (jhost)
680       jhost_unref(env, jhost);
681
682   }
683   xbt_dynar_free(&hosts);
684   XBT_INFO("Clean native world");
685 }
686 JNIEXPORT void JNICALL
687     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
688 {
689   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
690   MSG_error_t rv = MSG_OK != MSG_clean();
691   jxbt_check_res("MSG_clean()", rv, MSG_OK,
692                  bprintf
693                  ("unexpected error : MSG_clean() failed .. please report this bug "));
694 }
695    
696 JNIEXPORT jint JNICALL
697 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
698                                           jint jresetPID)
699 {
700   return (jint) MSG_process_killall((int) jresetPID);
701 }
702
703 JNIEXPORT void JNICALL
704 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
705                                        jstring jplatformFile)
706 {
707
708   const char *platformFile =
709       (*env)->GetStringUTFChars(env, jplatformFile, 0);
710
711   MSG_create_environment(platformFile);
712
713   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
714 }
715
716 JNIEXPORT void JNICALL
717 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
718                                        jobject jprocess)
719 {
720
721   m_process_t process = jprocess_to_native_process(jprocess, env);
722
723   if (!process) {
724     jxbt_throw_notbound(env, "process", jprocess);
725     return;
726   }
727
728   smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
729 }
730
731 JNIEXPORT void JNICALL
732 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
733 {
734   const char *s = (*env)->GetStringUTFChars(env, js, 0);
735   XBT_INFO("%s", s);
736   (*env)->ReleaseStringUTFChars(env, js, s);
737 }
738
739 JNIEXPORT jobjectArray JNICALL
740 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
741 {
742   int index;
743   jobjectArray jtable;
744   jobject jhost;
745   jstring jname;
746   m_host_t host;
747
748   xbt_dynar_t table =  MSG_hosts_as_dynar();
749   int count = xbt_dynar_length(table);
750
751   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
752
753   if (!cls) {
754     return NULL;
755   }
756
757   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
758
759   if (!jtable) {
760     jxbt_throw_jni(env, "Hosts table allocation failed");
761     return NULL;
762   }
763
764   for (index = 0; index < count; index++) {
765     host = xbt_dynar_get_as(table,index,m_host_t);
766     jhost = (jobject) (MSG_host_get_data(host));
767
768     if (!jhost) {
769       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
770
771       jhost =
772           Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
773       /* FIXME: leak of jname ? */
774     }
775
776     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
777   }
778   xbt_dynar_free(&table);
779   return jtable;
780 }
781
782 JNIEXPORT void JNICALL
783 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
784                                     jstring jalias, jobject jtask,
785                                     jdouble jtimeout)
786 {
787
788   MSG_error_t rv;
789   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
790
791   m_task_t task = jtask_to_native_task(jtask, env);
792
793
794   if (!task) {
795     (*env)->ReleaseStringUTFChars(env, jalias, alias);
796     jxbt_throw_notbound(env, "task", jtask);
797     return;
798   }
799
800   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
801   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
802   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
803
804   (*env)->ReleaseStringUTFChars(env, jalias, alias);
805
806   jxbt_check_res("MSG_task_send_with_timeout()", rv,
807                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
808                  bprintf("while sending task %s to mailbox %s",
809                          MSG_task_get_name(task), alias));
810 }
811
812 static void msg_task_cancel_on_failed_dsend(void*t) {
813         m_task_t task = t;
814         JNIEnv *env =get_current_thread_env();
815         jobject jtask_global = MSG_task_get_data(task);
816
817         /* Destroy the global ref so that the JVM can free the stuff */
818         (*env)->DeleteGlobalRef(env, jtask_global);
819         MSG_task_set_data(task, NULL);
820         MSG_task_destroy(task);
821 }
822
823
824 JNIEXPORT void JNICALL
825 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
826                                            jstring jalias, jobject jtask,
827                                            jdouble jmaxRate)
828 {
829   m_task_t task = jtask_to_native_task(jtask, env);
830   MSG_error_t rv;
831   const char *alias;
832
833   if (!task) {
834     jxbt_throw_notbound(env, "task", jtask);
835     return;
836   }
837
838   alias = (*env)->GetStringUTFChars(env, jalias, 0);
839
840   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
841   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
842   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
843
844   (*env)->ReleaseStringUTFChars(env, jalias, alias);
845
846   jxbt_check_res("MSG_task_send_bounded()", rv,
847                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
848                  bprintf
849                  ("while sending task %s to mailbox %s with max rate %f",
850                   MSG_task_get_name(task), alias, (double) jmaxRate));
851
852 }
853
854 JNIEXPORT jobject JNICALL
855 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
856                                        jstring jalias, jdouble jtimeout,
857                                        jobject jhost)
858 {
859   MSG_error_t rv;
860   m_task_t task = NULL;
861   m_host_t host = NULL;
862   jobject jtask_global, jtask_local;
863   const char *alias;
864
865   if (jhost) {
866     host = jhost_get_native(env, jhost);
867
868     if (!host) {
869       jxbt_throw_notbound(env, "host", jhost);
870       return NULL;
871     }
872   }
873
874   alias = (*env)->GetStringUTFChars(env, jalias, 0);
875
876   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
877   if (rv != MSG_OK) {
878         switch (rv) {
879                 case MSG_TIMEOUT:
880                         jxbt_throw_time_out_failure(env,NULL);
881                 break;
882                 case MSG_TRANSFER_FAILURE:
883                         jxbt_throw_transfer_failure(env,NULL);
884                 break;
885                 case MSG_HOST_FAILURE:
886                         jxbt_throw_host_failure(env,NULL);
887                 break;
888                 default:
889                         jxbt_throw_native(env,bprintf("receive failed"));
890         }
891         return NULL;
892   }
893   jtask_global = MSG_task_get_data(task);
894
895   /* Convert the global ref into a local ref so that the JVM can free the stuff */
896   jtask_local = (*env)->NewLocalRef(env, jtask_global);
897   (*env)->DeleteGlobalRef(env, jtask_global);
898   MSG_task_set_data(task, NULL);
899
900   (*env)->ReleaseStringUTFChars(env, jalias, alias);
901
902   jxbt_check_res("MSG_task_receive_ext()", rv,
903                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
904                  bprintf("while receiving from mailbox %s", alias));
905
906   return (jobject) jtask_local;
907 }
908
909 JNIEXPORT jboolean JNICALL
910 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
911                                       jstring jalias)
912 {
913
914   const char *alias;
915   int rv;
916
917   alias = (*env)->GetStringUTFChars(env, jalias, 0);
918
919   rv = MSG_task_listen(alias);
920
921   (*env)->ReleaseStringUTFChars(env, jalias, alias);
922
923   return (jboolean) rv;
924 }
925
926 JNIEXPORT jint JNICALL
927 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
928                                               jstring jalias,
929                                               jobject jhost)
930 {
931   int rv;
932   const char *alias;
933
934   m_host_t host = jhost_get_native(env, jhost);
935
936   if (!host) {
937     jxbt_throw_notbound(env, "host", jhost);
938     return -1;
939   }
940   alias = (*env)->GetStringUTFChars(env, jalias, 0);
941
942   rv = MSG_task_listen_from_host(alias, host);
943
944   (*env)->ReleaseStringUTFChars(env, jalias, alias);
945
946   return (jint) rv;
947 }
948
949 JNIEXPORT jint JNICALL
950 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
951                                           jstring jalias)
952 {
953
954   int rv;
955   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
956
957   rv = MSG_task_listen_from(alias);
958
959   (*env)->ReleaseStringUTFChars(env, jalias, alias);
960
961   return (jint) rv;
962 }
963
964 JNIEXPORT void JNICALL
965 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
966                                        jstring jdeploymentFile)
967 {
968
969   const char *deploymentFile =
970       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
971
972   surf_parse_reset_callbacks();
973
974   surfxml_add_callback(STag_surfxml_process_cb_list,
975                        japplication_handler_on_begin_process);
976
977   surfxml_add_callback(ETag_surfxml_argument_cb_list,
978                        japplication_handler_on_process_arg);
979
980   surfxml_add_callback(STag_surfxml_prop_cb_list,
981                        japplication_handler_on_property);
982
983   surfxml_add_callback(ETag_surfxml_process_cb_list,
984                        japplication_handler_on_end_process);
985
986   surf_parse_open(deploymentFile);
987
988   japplication_handler_on_start_document();
989
990   if (surf_parse())
991     jxbt_throw_jni(env, "surf_parse() failed");
992
993   surf_parse_close();
994
995   japplication_handler_on_end_document();
996
997   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
998 }