Logo AND Algorithmique Numérique Distribuée

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