Logo AND Algorithmique Numérique Distribuée

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