Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
adapt to the lastest (and probably short-lived) API change in MSG
[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 #include "smx_context_java.h"
14
15 #include "jmsg_process.h"
16 #include "jmsg_host.h"
17 #include "jmsg_task.h"
18 #include "jmsg_application_handler.h"
19 #include "jxbt_utilities.h"
20
21 #include "jmsg.h"
22
23 /* Shut up some errors in eclipse online compiler. I wish such a pimple wouldn't be needed */
24 #ifndef JNIEXPORT
25 #define JNIEXPORT
26 #endif
27 #ifndef JNICALL
28 #define JNICALL
29 #endif
30 /* end of eclipse-mandated pimple */
31
32 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
33
34 static JavaVM *__java_vm = NULL;
35
36 static jobject native_to_java_process(m_process_t process);
37
38 JavaVM *get_java_VM(void)
39 {
40   return __java_vm;
41 }
42
43 JNIEnv *get_current_thread_env(void)
44 {
45   JNIEnv *env;
46
47   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
48
49   return env;
50 }
51
52 static jobject native_to_java_process(m_process_t process)
53 {
54   return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
55 }
56
57 /*
58  * The MSG process connected functions implementation.                                 
59  */
60
61 JNIEXPORT void JNICALL
62 Java_org_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
63                                          jobject jprocess_arg,
64                                          jobject jhost)
65 {
66      
67    
68   jobject jprocess;             /* the global reference to the java process instance    */
69   jstring jname;                /* the name of the java process instance                */
70   const char *name;             /* the C name of the process                            */
71   m_process_t process;          /* the native process to create                         */
72   m_host_t host;                /* Where that process lives */
73    
74   XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
75          env, cls, jprocess_arg, jhost);
76    
77    
78   /* get the name of the java process */
79   jname = jprocess_get_name(jprocess_arg, env);
80   if (!jname) {
81     jxbt_throw_null(env,
82             xbt_strdup("Internal error: Process name cannot be NULL"));
83     return;
84   }
85
86   /* bind/retrieve the msg host */
87   host = jhost_get_native(env, jhost);
88
89   if (!(host)) {    /* not binded */
90     jxbt_throw_notbound(env, "host", jhost);
91     return;
92   }
93
94   /* create a global java process instance */
95   jprocess = jprocess_new_global_ref(jprocess_arg, env);
96   if (!jprocess) {
97     jxbt_throw_jni(env, "Can't get a global ref to the java process");
98     return;
99   }
100
101   /* build the C name of the process */
102   name = (*env)->GetStringUTFChars(env, jname, 0);
103   name = xbt_strdup(name);
104   
105   /* Actually build the MSG process */
106   process = MSG_process_create_with_environment(name,
107                                                 (xbt_main_func_t) jprocess,
108                                                 /*data*/ NULL,
109                                                 host,
110                                                 /*kill_time*/0.,
111                                                 /*argc, argv, properties*/
112                                                 0,NULL,NULL);
113      
114   MSG_process_set_data(process,&process);
115    
116   /* release our reference to the process name (variable name becomes invalid) */
117   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
118   //(*env)->ReleaseStringUTFChars(env, jname, name);
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
226   if (!MSG_host_get_data(host)) {
227     jxbt_throw_jni(env, "MSG_process_get_host() failed");
228     return NULL;
229   }
230
231   /* return the global reference to the java host instance */
232   return (jobject) MSG_host_get_data(host);
233
234 }
235
236 JNIEXPORT jobject JNICALL
237 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
238                                           jint PID)
239 {
240   m_process_t process = MSG_process_from_PID(PID);
241
242   if (!process) {
243     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
244     return NULL;
245   }
246
247   if (!native_to_java_process(process)) {
248     jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
249     return NULL;
250   }
251
252   return (jobject) (native_to_java_process(process));
253 }
254
255
256 JNIEXPORT jint JNICALL
257 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
258                                          jobject jprocess)
259 {
260   m_process_t process = jprocess_to_native_process(jprocess, env);
261
262   if (!process) {
263     jxbt_throw_notbound(env, "process", jprocess);
264     return 0;
265   }
266
267   return (jint) MSG_process_get_PID(process);
268 }
269
270
271 JNIEXPORT jint JNICALL
272 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
273                                           jobject jprocess)
274 {
275   m_process_t process = jprocess_to_native_process(jprocess, env);
276
277   if (!process) {
278     jxbt_throw_notbound(env, "process", jprocess);
279     return 0;
280   }
281
282   return (jint) MSG_process_get_PPID(process);
283 }
284
285 JNIEXPORT jobject JNICALL
286 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
287 {
288   m_process_t process = MSG_process_self();
289   jobject jprocess;
290
291   if (!process) {
292     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
293     return NULL;
294   }
295
296   jprocess = native_to_java_process(process);
297
298   if (!jprocess)
299     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
300
301   return jprocess;
302 }
303
304 JNIEXPORT void JNICALL
305 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
306                                              jobject jprocess, jobject jhost)
307 {
308   m_process_t process = jprocess_to_native_process(jprocess, env);
309
310   if (!process) {
311     jxbt_throw_notbound(env, "process", jprocess);
312     return;
313   }
314
315   m_host_t host = jhost_get_native(env, jhost);
316
317   if (!host) {
318     jxbt_throw_notbound(env, "host", jhost);
319     return;
320   }
321
322   /* try to change the host of the process */
323   MSG_error_t rv = MSG_process_migrate(process, host);
324   jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
325                  bprintf("unexpected error , please report this bug"));
326
327 }
328
329 JNIEXPORT void JNICALL
330 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
331                                           jdouble seconds)
332 {
333   MSG_error_t rv = MSG_process_sleep((double) seconds);
334
335   jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
336                  bprintf("while process was waiting for %f seconds",
337                          (double) seconds));
338
339 }
340
341
342 /***************************************************************************************
343  * The MSG host connected functions implementation.                                    *
344  ***************************************************************************************/
345
346 JNIEXPORT jobject JNICALL
347 Java_org_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   XBT_DEBUG("Looking for host '%s'",name);
356   /* get the host by name       (the hosts are created during the grid resolution) */
357   host = MSG_get_host_by_name(name);
358   XBT_DEBUG("MSG gave %p as native host (smx_host=%p)", host,host? host->smx_host:NULL);
359
360   if (!host) {                  /* invalid name */
361     jxbt_throw_host_not_found(env, name);
362     (*env)->ReleaseStringUTFChars(env, jname, name);
363     return NULL;
364   }
365   (*env)->ReleaseStringUTFChars(env, jname, name);
366
367   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
368
369     /* Instantiate 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     MSG_host_set_data(host, (void *) jhost);
392   }
393
394   /* return the global reference to the java host instance */
395   return (jobject) MSG_host_get_data(host);
396 }
397
398 JNIEXPORT jstring JNICALL
399 Java_org_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, MSG_host_get_name(host));
410 }
411
412 JNIEXPORT jint JNICALL
413 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
414 {
415   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
416   int nb_host = xbt_dynar_length(hosts);
417   xbt_dynar_free(&hosts);
418   return (jint) nb_host;
419 }
420
421 JNIEXPORT jobject JNICALL
422 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
423 {
424   jobject jhost;
425
426   m_host_t host = MSG_host_self();
427
428   if (!MSG_host_get_data(host)) {
429     /* the native host not yet associated with the java host instance */
430
431     /* instanciate a new java host instance */
432     jhost = jhost_new_instance(env);
433
434     if (!jhost) {
435       jxbt_throw_jni(env, "java host instantiation failed");
436       return NULL;
437     }
438
439     /* get a global reference to the newly created host */
440     jhost = jhost_ref(env, jhost);
441
442     if (!jhost) {
443       jxbt_throw_jni(env, "global ref allocation failed");
444       return NULL;
445     }
446
447     /* Bind & store it */
448     jhost_bind(jhost, host, env);
449     MSG_host_set_data(host, (void *) jhost);
450   } else {
451     jhost = (jobject) MSG_host_get_data(host);
452   }
453
454   return jhost;
455 }
456
457 JNIEXPORT jdouble JNICALL
458 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
459                                         jobject jhost)
460 {
461   m_host_t host = jhost_get_native(env, jhost);
462
463   if (!host) {
464     jxbt_throw_notbound(env, "host", jhost);
465     return -1;
466   }
467
468   return (jdouble) MSG_get_host_speed(host);
469 }
470
471 JNIEXPORT jint JNICALL
472 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
473                                        jobject jhost)
474 {
475   m_host_t host = jhost_get_native(env, jhost);
476
477   if (!host) {
478     jxbt_throw_notbound(env, "host", jhost);
479     return -1;
480   }
481
482   return (jint) MSG_get_host_msgload(host);
483 }
484
485
486 JNIEXPORT jboolean JNICALL
487 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
488                                        jobject jhost)
489 {
490   m_host_t host = jhost_get_native(env, jhost);
491
492   if (!host) {
493     jxbt_throw_notbound(env, "host", jhost);
494     return 0;
495   }
496
497   return (jboolean) MSG_host_is_avail(host);
498 }
499
500
501 /***************************************************************************************
502  * The MSG task connected functions implementation.                                    *
503  ***************************************************************************************/
504
505 JNIEXPORT void JNICALL
506 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
507                                       jobject jtask, jstring jname,
508                                       jdouble jcomputeDuration,
509                                       jdouble jmessageSize)
510 {
511   m_task_t task;                /* the native task to create                            */
512   const char *name = NULL;      /* the name of the task                                 */
513
514   if (jcomputeDuration < 0) {
515     jxbt_throw_illegal(env,
516                        bprintf
517                        ("Task ComputeDuration (%f) cannot be negative",
518                         (double) jcomputeDuration));
519     return;
520   }
521
522   if (jmessageSize < 0) {
523     jxbt_throw_illegal(env,
524                        bprintf("Task MessageSize (%f) cannot be negative",
525                                (double) jmessageSize));
526     return;
527   }
528
529   if (jname) {
530     /* get the C string from the java string */
531     name = (*env)->GetStringUTFChars(env, jname, 0);
532   }
533
534
535   /* create the task */
536   task =
537       MSG_task_create(name, (double) jcomputeDuration,
538                       (double) jmessageSize, NULL);
539
540   if (jname)
541     (*env)->ReleaseStringUTFChars(env, jname, name);
542
543   /* bind & store the task */
544   jtask_bind(jtask, task, env);
545   MSG_task_set_data(task, jtask);
546 }
547
548 JNIEXPORT void JNICALL
549 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
550                                                jobject jtask,
551                                                jstring jname,
552                                                jobjectArray jhosts,
553                                                jdoubleArray
554                                                jcomputeDurations_arg,
555                                                jdoubleArray
556                                                jmessageSizes_arg)
557 {
558
559   m_task_t task;                /* the native parallel task to create           */
560   const char *name;             /* the name of the task                         */
561   int host_count;
562   m_host_t *hosts;
563   double *computeDurations;
564   double *messageSizes;
565   jdouble *jcomputeDurations;
566   jdouble *jmessageSizes;
567
568   jobject jhost;
569   int index;
570
571
572   if (!jcomputeDurations_arg) {
573     jxbt_throw_null(env,
574                     xbt_strdup
575                     ("Parallel task compute durations cannot be null"));
576     return;
577   }
578
579   if (!jmessageSizes_arg) {
580     jxbt_throw_null(env,
581                     xbt_strdup
582                     ("Parallel task message sizes cannot be null"));
583     return;
584   }
585
586   if (!jname) {
587     jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
588     return;
589   }
590
591   host_count = (int) (*env)->GetArrayLength(env, jhosts);
592
593
594   hosts = xbt_new0(m_host_t, host_count);
595   computeDurations = xbt_new0(double, host_count);
596   messageSizes = xbt_new0(double, host_count * host_count);
597
598   jcomputeDurations =
599       (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
600   jmessageSizes =
601       (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
602
603   for (index = 0; index < host_count; index++) {
604     jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
605     hosts[index] = jhost_get_native(env, jhost);
606     computeDurations[index] = jcomputeDurations[index];
607   }
608   for (index = 0; index < host_count * host_count; index++) {
609     messageSizes[index] = jmessageSizes[index];
610   }
611
612   (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
613                                      jcomputeDurations, 0);
614   (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
615                                      0);
616
617
618   /* get the C string from the java string */
619   name = (*env)->GetStringUTFChars(env, jname, 0);
620
621   task =
622       MSG_parallel_task_create(name, host_count, hosts, computeDurations,
623                                messageSizes, NULL);
624
625   (*env)->ReleaseStringUTFChars(env, jname, name);
626
627   /* associate the java task object and the native task */
628   jtask_bind(jtask, task, env);
629
630   MSG_task_set_data(task, (void *) jtask);
631
632   if (!MSG_task_get_data(task))
633     jxbt_throw_jni(env, "global ref allocation failed");
634 }
635
636 JNIEXPORT jobject JNICALL
637 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
638                                          jobject jtask)
639 {
640   m_process_t process;
641
642   m_task_t task = jtask_to_native_task(jtask, env);
643
644   if (!task) {
645     jxbt_throw_notbound(env, "task", jtask);
646     return NULL;
647   }
648
649   process = MSG_task_get_sender(task);
650   return (jobject) native_to_java_process(process);
651 }
652
653 JNIEXPORT jobject JNICALL
654 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
655                                          jobject jtask)
656 {
657   m_host_t host;
658   m_task_t task = jtask_to_native_task(jtask, env);
659
660   if (!task) {
661     jxbt_throw_notbound(env, "task", jtask);
662     return NULL;
663   }
664
665   host = MSG_task_get_source(task);
666
667   if (!MSG_host_get_data(host)) {
668     jxbt_throw_jni(env, "MSG_task_get_source() failed");
669     return NULL;
670   }
671
672   return (jobject) MSG_host_get_data(host);
673 }
674
675
676 JNIEXPORT jstring JNICALL
677 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
678                                        jobject jtask)
679 {
680   m_task_t task = jtask_to_native_task(jtask, env);
681
682   if (!task) {
683     jxbt_throw_notbound(env, "task", jtask);
684     return NULL;
685   }
686
687   return (*env)->NewStringUTF(env, MSG_task_get_name(task));
688 }
689
690 JNIEXPORT void JNICALL
691 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
692                                       jobject jtask)
693 {
694   m_task_t ptask = jtask_to_native_task(jtask, env);
695
696   if (!ptask) {
697     jxbt_throw_notbound(env, "task", jtask);
698     return;
699   }
700
701   MSG_error_t rv = MSG_task_cancel(ptask);
702
703   jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
704                  bprintf("unexpected error , please report this bug"));
705 }
706
707 JNIEXPORT jdouble JNICALL
708 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
709                                                   jobject jtask)
710 {
711   m_task_t ptask = jtask_to_native_task(jtask, env);
712
713   if (!ptask) {
714     jxbt_throw_notbound(env, "task", jtask);
715     return -1;
716   }
717   return (jdouble) MSG_task_get_compute_duration(ptask);
718 }
719
720 JNIEXPORT jdouble JNICALL
721 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
722                                                     jclass cls,
723                                                     jobject jtask)
724 {
725   m_task_t ptask = jtask_to_native_task(jtask, env);
726
727   if (!ptask) {
728     jxbt_throw_notbound(env, "task", jtask);
729     return -1;
730   }
731   return (jdouble) MSG_task_get_remaining_computation(ptask);
732 }
733
734 JNIEXPORT void JNICALL
735 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
736                                            jobject jtask, jdouble priority)
737 {
738   m_task_t task = jtask_to_native_task(jtask, env);
739
740   if (!task) {
741     jxbt_throw_notbound(env, "task", jtask);
742     return;
743   }
744   MSG_task_set_priority(task, (double) priority);
745 }
746
747 JNIEXPORT void JNICALL
748 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
749                                        jobject jtask_arg)
750 {
751
752   /* get the native task */
753   m_task_t task = jtask_to_native_task(jtask_arg, env);
754
755   if (!task) {
756     jxbt_throw_notbound(env, "task", task);
757     return;
758   }
759
760   MSG_error_t rv = MSG_task_destroy(task);
761
762   jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
763                  bprintf("unexpected error , please report this bug"));
764 }
765
766 JNIEXPORT void JNICALL
767 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
768                                        jobject jtask)
769 {
770   m_task_t task = jtask_to_native_task(jtask, env);
771
772   if (!task) {
773     jxbt_throw_notbound(env, "task", jtask);
774     return;
775   }
776
777   MSG_error_t rv = MSG_task_execute(task);
778
779   jxbt_check_res("MSG_task_execute()", rv,
780                  MSG_HOST_FAILURE | MSG_TASK_CANCELED,
781                  bprintf("while executing task %s",
782                          MSG_task_get_name(task)));
783 }
784
785 /***************************************************************************************
786  * Unsortable functions                                                        *
787  ***************************************************************************************/
788
789 JNIEXPORT jdouble JNICALL
790 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
791 {
792   return (jdouble) MSG_get_clock();
793 }
794
795 JNIEXPORT void JNICALL
796 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
797 {
798   char **argv = NULL;
799   int index;
800   int argc = 0;
801   jstring jval;
802   const char *tmp;
803
804   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
805
806   if (jargs)
807     argc = (int) (*env)->GetArrayLength(env, jargs);
808
809   argc++;
810   argv = xbt_new(char *, argc + 1);
811   argv[0] = strdup("java");
812
813   for (index = 0; index < argc - 1; index++) {
814     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
815     tmp = (*env)->GetStringUTFChars(env, jval, 0);
816     argv[index + 1] = strdup(tmp);
817     (*env)->ReleaseStringUTFChars(env, jval, tmp);
818   }
819   argv[argc] = NULL;
820
821   MSG_global_init(&argc, argv);
822
823   for (index = 0; index < argc; index++)
824     free(argv[index]);
825
826   free(argv);
827
828   (*env)->GetJavaVM(env, &__java_vm);
829 }
830
831 JNIEXPORT void JNICALL
832     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
833 {
834   MSG_error_t rv;
835   int index;
836   xbt_dynar_t hosts;
837   jobject jhost;
838
839   /* Run everything */
840   XBT_INFO("Ready to run MSG_MAIN");
841   rv = MSG_main();
842   XBT_INFO("Done running MSG_MAIN");
843   jxbt_check_res("MSG_main()", rv, MSG_OK,
844                  bprintf
845                  ("unexpected error : MSG_main() failed .. please report this bug "));
846
847   XBT_INFO("MSG_main finished");
848
849   XBT_INFO("Clean java world");
850   /* Cleanup java hosts */
851   hosts = MSG_hosts_as_dynar();
852   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
853     jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,m_host_t));
854     if (jhost)
855       jhost_unref(env, jhost);
856
857   }
858   xbt_dynar_free(&hosts);
859   XBT_INFO("Clean native world");
860 }
861 JNIEXPORT void JNICALL
862     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
863 {
864   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
865   MSG_error_t rv = MSG_OK != MSG_clean();
866   jxbt_check_res("MSG_clean()", rv, MSG_OK,
867                  bprintf
868                  ("unexpected error : MSG_clean() failed .. please report this bug "));
869 }
870    
871 JNIEXPORT jint JNICALL
872 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
873                                           jint jresetPID)
874 {
875   return (jint) MSG_process_killall((int) jresetPID);
876 }
877
878 JNIEXPORT void JNICALL
879 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
880                                        jstring jplatformFile)
881 {
882
883   const char *platformFile =
884       (*env)->GetStringUTFChars(env, jplatformFile, 0);
885
886   MSG_create_environment(platformFile);
887
888   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
889 }
890
891 JNIEXPORT void JNICALL
892 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
893                                        jobject jprocess)
894 {
895
896   m_process_t process = jprocess_to_native_process(jprocess, env);
897
898   if (!process) {
899     jxbt_throw_notbound(env, "process", jprocess);
900     return;
901   }
902
903   smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
904 }
905
906 JNIEXPORT void JNICALL
907 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
908 {
909   const char *s = (*env)->GetStringUTFChars(env, js, 0);
910   XBT_INFO("%s", s);
911   (*env)->ReleaseStringUTFChars(env, js, s);
912 }
913
914 JNIEXPORT jobjectArray JNICALL
915 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
916 {
917   int index;
918   jobjectArray jtable;
919   jobject jhost;
920   jstring jname;
921   m_host_t host;
922
923   xbt_dynar_t table =  MSG_hosts_as_dynar();
924   int count = xbt_dynar_length(table);
925
926   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
927
928   if (!cls) {
929     return NULL;
930   }
931
932   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
933
934   if (!jtable) {
935     jxbt_throw_jni(env, "Hosts table allocation failed");
936     return NULL;
937   }
938
939   for (index = 0; index < count; index++) {
940     host = xbt_dynar_get_as(table,index,m_host_t);
941     jhost = (jobject) (MSG_host_get_data(host));
942
943     if (!jhost) {
944       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
945
946       jhost =
947           Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
948       /* FIXME: leak of jname ? */
949     }
950
951     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
952   }
953   xbt_dynar_free(&table);
954   return jtable;
955 }
956
957 JNIEXPORT void JNICALL
958 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
959                                     jstring jalias, jobject jtask,
960                                     jdouble jtimeout)
961 {
962
963   MSG_error_t rv;
964   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
965
966   m_task_t task = jtask_to_native_task(jtask, env);
967
968
969   if (!task) {
970     (*env)->ReleaseStringUTFChars(env, jalias, alias);
971     jxbt_throw_notbound(env, "task", jtask);
972     return;
973   }
974
975   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
976   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
977   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
978
979   (*env)->ReleaseStringUTFChars(env, jalias, alias);
980
981   jxbt_check_res("MSG_task_send_with_timeout()", rv,
982                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
983                  bprintf("while sending task %s to mailbox %s",
984                          MSG_task_get_name(task), alias));
985 }
986
987 static void msg_task_cancel_on_failed_dsend(void*t) {
988         m_task_t task = t;
989         JNIEnv *env =get_current_thread_env();
990         jobject jtask_global = MSG_task_get_data(task);
991
992         /* Destroy the global ref so that the JVM can free the stuff */
993         (*env)->DeleteGlobalRef(env, jtask_global);
994         MSG_task_set_data(task, NULL);
995         MSG_task_destroy(task);
996 }
997
998 JNIEXPORT void JNICALL
999 Java_org_simgrid_msg_MsgNative_taskDSend(JNIEnv * env, jclass cls,
1000                                     jstring jalias, jobject jtask)
1001 {
1002
1003   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1004
1005   m_task_t task = jtask_to_native_task(jtask, env);
1006
1007
1008   if (!task) {
1009     (*env)->ReleaseStringUTFChars(env, jalias, alias);
1010     jxbt_throw_notbound(env, "task", jtask);
1011     return;
1012   }
1013
1014   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1015   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1016   MSG_task_dsend(task, alias, msg_task_cancel_on_failed_dsend);
1017
1018   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1019 }
1020
1021
1022 JNIEXPORT void JNICALL
1023 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1024                                            jstring jalias, jobject jtask,
1025                                            jdouble jmaxRate)
1026 {
1027   m_task_t task = jtask_to_native_task(jtask, env);
1028   MSG_error_t rv;
1029   const char *alias;
1030
1031   if (!task) {
1032     jxbt_throw_notbound(env, "task", jtask);
1033     return;
1034   }
1035
1036   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1037
1038   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1039   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1040   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1041
1042   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1043
1044   jxbt_check_res("MSG_task_send_bounded()", rv,
1045                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1046                  bprintf
1047                  ("while sending task %s to mailbox %s with max rate %f",
1048                   MSG_task_get_name(task), alias, (double) jmaxRate));
1049
1050 }
1051
1052 JNIEXPORT jobject JNICALL
1053 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1054                                        jstring jalias, jdouble jtimeout,
1055                                        jobject jhost)
1056 {
1057   MSG_error_t rv;
1058   m_task_t task = NULL;
1059   m_host_t host = NULL;
1060   jobject jtask_global, jtask_local;
1061   const char *alias;
1062
1063   if (jhost) {
1064     host = jhost_get_native(env, jhost);
1065
1066     if (!host) {
1067       jxbt_throw_notbound(env, "host", jhost);
1068       return NULL;
1069     }
1070   }
1071
1072   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1073
1074   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1075   if (rv != MSG_OK) {
1076         switch (rv) {
1077                 case MSG_TIMEOUT:
1078                         jxbt_throw_time_out_failure(env,NULL);
1079                 break;
1080                 case MSG_TRANSFER_FAILURE:
1081                         jxbt_throw_transfer_failure(env,NULL);
1082                 break;
1083                 case MSG_HOST_FAILURE:
1084                         jxbt_throw_host_failure(env,NULL);
1085                 break;
1086                 default:
1087                         jxbt_throw_native(env,bprintf("receive failed"));
1088         }
1089         return NULL;
1090   }
1091   jtask_global = MSG_task_get_data(task);
1092
1093   /* Convert the global ref into a local ref so that the JVM can free the stuff */
1094   jtask_local = (*env)->NewLocalRef(env, jtask_global);
1095   (*env)->DeleteGlobalRef(env, jtask_global);
1096   MSG_task_set_data(task, NULL);
1097
1098   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1099
1100   jxbt_check_res("MSG_task_receive_ext()", rv,
1101                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1102                  bprintf("while receiving from mailbox %s", alias));
1103
1104   return (jobject) jtask_local;
1105 }
1106
1107 JNIEXPORT jboolean JNICALL
1108 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1109                                       jstring jalias)
1110 {
1111
1112   const char *alias;
1113   int rv;
1114
1115   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1116
1117   rv = MSG_task_listen(alias);
1118
1119   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1120
1121   return (jboolean) rv;
1122 }
1123
1124 JNIEXPORT jint JNICALL
1125 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1126                                               jstring jalias,
1127                                               jobject jhost)
1128 {
1129   int rv;
1130   const char *alias;
1131
1132   m_host_t host = jhost_get_native(env, jhost);
1133
1134   if (!host) {
1135     jxbt_throw_notbound(env, "host", jhost);
1136     return -1;
1137   }
1138   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1139
1140   rv = MSG_task_listen_from_host(alias, host);
1141
1142   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1143
1144   return (jint) rv;
1145 }
1146
1147 JNIEXPORT jint JNICALL
1148 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1149                                           jstring jalias)
1150 {
1151
1152   int rv;
1153   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1154
1155   rv = MSG_task_listen_from(alias);
1156
1157   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1158
1159   return (jint) rv;
1160 }
1161
1162 JNIEXPORT void JNICALL
1163 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1164                                        jstring jdeploymentFile)
1165 {
1166
1167   const char *deploymentFile =
1168       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1169
1170   surf_parse_reset_callbacks();
1171
1172   surfxml_add_callback(STag_surfxml_process_cb_list,
1173                        japplication_handler_on_begin_process);
1174
1175   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1176                        japplication_handler_on_process_arg);
1177
1178   surfxml_add_callback(STag_surfxml_prop_cb_list,
1179                        japplication_handler_on_property);
1180
1181   surfxml_add_callback(ETag_surfxml_process_cb_list,
1182                        japplication_handler_on_end_process);
1183
1184   surf_parse_open(deploymentFile);
1185
1186   japplication_handler_on_start_document();
1187
1188   if (surf_parse())
1189     jxbt_throw_jni(env, "surf_parse() failed");
1190
1191   surf_parse_close();
1192
1193   japplication_handler_on_end_document();
1194
1195   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1196 }