Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
look for msg.h instead of gras.h (cosmetics only: GRAS is soon to be deprecated)
[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   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
108   //(*env)->ReleaseStringUTFChars(env, jname, name);
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_org_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_org_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_org_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_org_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   /* kill the native process (this wrapper is call by the destructor of the java 
180    * process instance)
181    */
182   MSG_process_kill(process);
183 }
184
185 JNIEXPORT jobject JNICALL
186 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
187                                           jobject jprocess)
188 {
189   /* get the native instances from the java ones */
190   m_process_t process = jprocess_to_native_process(jprocess, env);
191   m_host_t host;
192
193   if (!process) {
194     jxbt_throw_notbound(env, "process", jprocess);
195     return NULL;
196   }
197
198   host = MSG_process_get_host(process);
199
200   if (!MSG_host_get_data(host)) {
201     jxbt_throw_jni(env, "MSG_process_get_host() failed");
202     return NULL;
203   }
204
205   /* return the global reference to the java host instance */
206   return (jobject) MSG_host_get_data(host);
207
208 }
209
210 JNIEXPORT jobject JNICALL
211 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
212                                           jint PID)
213 {
214   m_process_t process = MSG_process_from_PID(PID);
215
216   if (!process) {
217     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
218     return NULL;
219   }
220
221   if (!native_to_java_process(process)) {
222     jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
223     return NULL;
224   }
225
226   return (jobject) (native_to_java_process(process));
227 }
228
229
230 JNIEXPORT jint JNICALL
231 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
232                                          jobject jprocess)
233 {
234   m_process_t process = jprocess_to_native_process(jprocess, env);
235
236   if (!process) {
237     jxbt_throw_notbound(env, "process", jprocess);
238     return 0;
239   }
240
241   return (jint) MSG_process_get_PID(process);
242 }
243
244
245 JNIEXPORT jint JNICALL
246 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
247                                           jobject jprocess)
248 {
249   m_process_t process = jprocess_to_native_process(jprocess, env);
250
251   if (!process) {
252     jxbt_throw_notbound(env, "process", jprocess);
253     return 0;
254   }
255
256   return (jint) MSG_process_get_PPID(process);
257 }
258
259 JNIEXPORT jobject JNICALL
260 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
261 {
262   m_process_t process = MSG_process_self();
263   jobject jprocess;
264
265   if (!process) {
266     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
267     return NULL;
268   }
269
270   jprocess = native_to_java_process(process);
271
272   if (!jprocess)
273     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
274
275   return jprocess;
276 }
277
278 JNIEXPORT void JNICALL
279 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
280                                              jobject jprocess, jobject jhost)
281 {
282   m_process_t process = jprocess_to_native_process(jprocess, env);
283
284   if (!process) {
285     jxbt_throw_notbound(env, "process", jprocess);
286     return;
287   }
288
289   m_host_t host = jhost_get_native(env, jhost);
290
291   if (!host) {
292     jxbt_throw_notbound(env, "host", jhost);
293     return;
294   }
295
296   /* try to change the host of the process */
297   MSG_error_t rv = MSG_process_migrate(process, host);
298   jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
299                  bprintf("unexpected error , please report this bug"));
300
301 }
302
303 JNIEXPORT void JNICALL
304 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
305                                           jdouble seconds)
306 {
307   MSG_error_t rv = MSG_process_sleep((double) seconds);
308
309   jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
310                  bprintf("while process was waiting for %f seconds",
311                          (double) seconds));
312
313 }
314
315
316 /***************************************************************************************
317  * The MSG host connected functions implementation.                                    *
318  ***************************************************************************************/
319
320 JNIEXPORT jobject JNICALL
321 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
322                                          jstring jname)
323 {
324   m_host_t host;                /* native host                                          */
325   jobject jhost;                /* global reference to the java host instance returned  */
326
327   /* get the C string from the java string */
328   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
329   XBT_DEBUG("Looking for host '%s'",name);
330   /* get the host by name       (the hosts are created during the grid resolution) */
331   host = MSG_get_host_by_name(name);
332   XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
333
334   if (!host) {                  /* invalid name */
335     jxbt_throw_host_not_found(env, name);
336     (*env)->ReleaseStringUTFChars(env, jname, name);
337     return NULL;
338   }
339   (*env)->ReleaseStringUTFChars(env, jname, name);
340
341   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
342
343     /* instanciate a new java host */
344     jhost = jhost_new_instance(env);
345
346     if (!jhost) {
347       jxbt_throw_jni(env, "java host instantiation failed");
348       return NULL;
349     }
350
351     /* get a global reference to the newly created host */
352     jhost = jhost_ref(env, jhost);
353
354     if (!jhost) {
355       jxbt_throw_jni(env, "new global ref allocation failed");
356       return NULL;
357     }
358
359     /* bind the java host and the native host */
360     jhost_bind(jhost, host, env);
361
362     /* the native host data field is set with the global reference to the 
363      * java host returned by this function 
364      */
365     MSG_host_set_data(host, (void *) jhost);
366   }
367
368   /* return the global reference to the java host instance */
369   return (jobject) MSG_host_get_data(host);
370 }
371
372 JNIEXPORT jstring JNICALL
373 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
374                                        jobject jhost)
375 {
376   m_host_t host = jhost_get_native(env, jhost);
377
378   if (!host) {
379     jxbt_throw_notbound(env, "host", jhost);
380     return NULL;
381   }
382
383   return (*env)->NewStringUTF(env, MSG_host_get_name(host));
384 }
385
386 JNIEXPORT jint JNICALL
387 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
388 {
389   return (jint) MSG_get_host_number();
390 }
391
392 JNIEXPORT jobject JNICALL
393 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
394 {
395   jobject jhost;
396
397   m_host_t host = MSG_host_self();
398
399   if (!MSG_host_get_data(host)) {
400     /* the native host not yet associated with the java host instance */
401
402     /* instanciate a new java host instance */
403     jhost = jhost_new_instance(env);
404
405     if (!jhost) {
406       jxbt_throw_jni(env, "java host instantiation failed");
407       return NULL;
408     }
409
410     /* get a global reference to the newly created host */
411     jhost = jhost_ref(env, jhost);
412
413     if (!jhost) {
414       jxbt_throw_jni(env, "global ref allocation failed");
415       return NULL;
416     }
417
418     /* Bind & store it */
419     jhost_bind(jhost, host, env);
420     MSG_host_set_data(host, (void *) jhost);
421   } else {
422     jhost = (jobject) MSG_host_get_data(host);
423   }
424
425   return jhost;
426 }
427
428 JNIEXPORT jdouble JNICALL
429 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
430                                         jobject jhost)
431 {
432   m_host_t host = jhost_get_native(env, jhost);
433
434   if (!host) {
435     jxbt_throw_notbound(env, "host", jhost);
436     return -1;
437   }
438
439   return (jdouble) MSG_get_host_speed(host);
440 }
441
442 JNIEXPORT jint JNICALL
443 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
444                                        jobject jhost)
445 {
446   m_host_t host = jhost_get_native(env, jhost);
447
448   if (!host) {
449     jxbt_throw_notbound(env, "host", jhost);
450     return -1;
451   }
452
453   return (jint) MSG_get_host_msgload(host);
454 }
455
456
457 JNIEXPORT jboolean JNICALL
458 Java_org_simgrid_msg_MsgNative_hostIsAvail(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 0;
466   }
467
468   return (jboolean) MSG_host_is_avail(host);
469 }
470
471
472 /***************************************************************************************
473  * The MSG task connected functions implementation.                                    *
474  ***************************************************************************************/
475
476 JNIEXPORT void JNICALL
477 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
478                                       jobject jtask, jstring jname,
479                                       jdouble jcomputeDuration,
480                                       jdouble jmessageSize)
481 {
482   m_task_t task;                /* the native task to create                            */
483   const char *name = NULL;      /* the name of the task                                 */
484
485   if (jcomputeDuration < 0) {
486     jxbt_throw_illegal(env,
487                        bprintf
488                        ("Task ComputeDuration (%f) cannot be negative",
489                         (double) jcomputeDuration));
490     return;
491   }
492
493   if (jmessageSize < 0) {
494     jxbt_throw_illegal(env,
495                        bprintf("Task MessageSize (%f) cannot be negative",
496                                (double) jmessageSize));
497     return;
498   }
499
500   if (jname) {
501     /* get the C string from the java string */
502     name = (*env)->GetStringUTFChars(env, jname, 0);
503   }
504
505
506   /* create the task */
507   task =
508       MSG_task_create(name, (double) jcomputeDuration,
509                       (double) jmessageSize, NULL);
510
511   if (jname)
512     (*env)->ReleaseStringUTFChars(env, jname, name);
513
514   /* bind & store the task */
515   jtask_bind(jtask, task, env);
516   MSG_task_set_data(task, jtask);
517 }
518
519 JNIEXPORT void JNICALL
520 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
521                                                jobject jtask,
522                                                jstring jname,
523                                                jobjectArray jhosts,
524                                                jdoubleArray
525                                                jcomputeDurations_arg,
526                                                jdoubleArray
527                                                jmessageSizes_arg)
528 {
529
530   m_task_t task;                /* the native parallel task to create           */
531   const char *name;             /* the name of the task                         */
532   int host_count;
533   m_host_t *hosts;
534   double *computeDurations;
535   double *messageSizes;
536   jdouble *jcomputeDurations;
537   jdouble *jmessageSizes;
538
539   jobject jhost;
540   int index;
541
542
543   if (!jcomputeDurations_arg) {
544     jxbt_throw_null(env,
545                     xbt_strdup
546                     ("Parallel task compute durations cannot be null"));
547     return;
548   }
549
550   if (!jmessageSizes_arg) {
551     jxbt_throw_null(env,
552                     xbt_strdup
553                     ("Parallel task message sizes cannot be null"));
554     return;
555   }
556
557   if (!jname) {
558     jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
559     return;
560   }
561
562   host_count = (int) (*env)->GetArrayLength(env, jhosts);
563
564
565   hosts = xbt_new0(m_host_t, host_count);
566   computeDurations = xbt_new0(double, host_count);
567   messageSizes = xbt_new0(double, host_count * host_count);
568
569   jcomputeDurations =
570       (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
571   jmessageSizes =
572       (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
573
574   for (index = 0; index < host_count; index++) {
575     jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
576     hosts[index] = jhost_get_native(env, jhost);
577     computeDurations[index] = jcomputeDurations[index];
578   }
579   for (index = 0; index < host_count * host_count; index++) {
580     messageSizes[index] = jmessageSizes[index];
581   }
582
583   (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
584                                      jcomputeDurations, 0);
585   (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
586                                      0);
587
588
589   /* get the C string from the java string */
590   name = (*env)->GetStringUTFChars(env, jname, 0);
591
592   task =
593       MSG_parallel_task_create(name, host_count, hosts, computeDurations,
594                                messageSizes, NULL);
595
596   (*env)->ReleaseStringUTFChars(env, jname, name);
597
598   /* associate the java task object and the native task */
599   jtask_bind(jtask, task, env);
600
601   MSG_task_set_data(task, (void *) jtask);
602
603   if (!MSG_task_get_data(task))
604     jxbt_throw_jni(env, "global ref allocation failed");
605 }
606
607 JNIEXPORT jobject JNICALL
608 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
609                                          jobject jtask)
610 {
611   m_process_t process;
612
613   m_task_t task = jtask_to_native_task(jtask, env);
614
615   if (!task) {
616     jxbt_throw_notbound(env, "task", jtask);
617     return NULL;
618   }
619
620   process = MSG_task_get_sender(task);
621   return (jobject) native_to_java_process(process);
622 }
623
624 JNIEXPORT jobject JNICALL
625 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
626                                          jobject jtask)
627 {
628   m_host_t host;
629   m_task_t task = jtask_to_native_task(jtask, env);
630
631   if (!task) {
632     jxbt_throw_notbound(env, "task", jtask);
633     return NULL;
634   }
635
636   host = MSG_task_get_source(task);
637
638   if (!MSG_host_get_data(host)) {
639     jxbt_throw_jni(env, "MSG_task_get_source() failed");
640     return NULL;
641   }
642
643   return (jobject) MSG_host_get_data(host);
644 }
645
646
647 JNIEXPORT jstring JNICALL
648 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
649                                        jobject jtask)
650 {
651   m_task_t task = jtask_to_native_task(jtask, env);
652
653   if (!task) {
654     jxbt_throw_notbound(env, "task", jtask);
655     return NULL;
656   }
657
658   return (*env)->NewStringUTF(env, MSG_task_get_name(task));
659 }
660
661 JNIEXPORT void JNICALL
662 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
663                                       jobject jtask)
664 {
665   m_task_t ptask = jtask_to_native_task(jtask, env);
666
667   if (!ptask) {
668     jxbt_throw_notbound(env, "task", jtask);
669     return;
670   }
671
672   MSG_error_t rv = MSG_task_cancel(ptask);
673
674   jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
675                  bprintf("unexpected error , please report this bug"));
676 }
677
678 JNIEXPORT jdouble JNICALL
679 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
680                                                   jobject jtask)
681 {
682   m_task_t ptask = jtask_to_native_task(jtask, env);
683
684   if (!ptask) {
685     jxbt_throw_notbound(env, "task", jtask);
686     return -1;
687   }
688   return (jdouble) MSG_task_get_compute_duration(ptask);
689 }
690
691 JNIEXPORT jdouble JNICALL
692 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
693                                                     jclass cls,
694                                                     jobject jtask)
695 {
696   m_task_t ptask = jtask_to_native_task(jtask, env);
697
698   if (!ptask) {
699     jxbt_throw_notbound(env, "task", jtask);
700     return -1;
701   }
702   return (jdouble) MSG_task_get_remaining_computation(ptask);
703 }
704
705 JNIEXPORT void JNICALL
706 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
707                                            jobject jtask, jdouble priority)
708 {
709   m_task_t task = jtask_to_native_task(jtask, env);
710
711   if (!task) {
712     jxbt_throw_notbound(env, "task", jtask);
713     return;
714   }
715   MSG_task_set_priority(task, (double) priority);
716 }
717
718 JNIEXPORT void JNICALL
719 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
720                                        jobject jtask_arg)
721 {
722
723   /* get the native task */
724   m_task_t task = jtask_to_native_task(jtask_arg, env);
725
726   if (!task) {
727     jxbt_throw_notbound(env, "task", task);
728     return;
729   }
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_CANCELED,
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     (*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_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
810 {
811   MSG_error_t rv;
812   int index;
813   m_host_t *hosts;
814   jobject jhost;
815
816   /* Run everything */
817   XBT_INFO("Ready to run MSG_MAIN");
818   rv = MSG_main();
819   XBT_INFO("Done running MSG_MAIN");
820   jxbt_check_res("MSG_main()", rv, MSG_OK,
821                  bprintf
822                  ("unexpected error : MSG_main() failed .. please report this bug "));
823
824   XBT_INFO("MSG_main finished");
825
826   XBT_INFO("Clean java world");
827   /* Cleanup java hosts */
828   hosts = MSG_get_host_table();
829   for (index = 0; index < MSG_get_host_number() - 1; index++) {
830     jhost = (jobject) hosts[index]->data;
831     if (jhost)
832       jhost_unref(env, jhost);
833
834   }
835
836   XBT_INFO("Clean native world");
837 }
838 JNIEXPORT void JNICALL
839     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
840 {
841   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
842   MSG_error_t rv = MSG_OK != MSG_clean();
843   jxbt_check_res("MSG_clean()", rv, MSG_OK,
844                  bprintf
845                  ("unexpected error : MSG_clean() failed .. please report this bug "));
846 }
847    
848 JNIEXPORT jint JNICALL
849 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
850                                           jint jresetPID)
851 {
852   return (jint) MSG_process_killall((int) jresetPID);
853 }
854
855 JNIEXPORT void JNICALL
856 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
857                                        jstring jplatformFile)
858 {
859
860   const char *platformFile =
861       (*env)->GetStringUTFChars(env, jplatformFile, 0);
862
863   MSG_create_environment(platformFile);
864
865   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
866 }
867
868 JNIEXPORT void JNICALL
869 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
870                                        jobject jprocess)
871 {
872
873   m_process_t process = jprocess_to_native_process(jprocess, env);
874
875   if (!process) {
876     jxbt_throw_notbound(env, "process", jprocess);
877     return;
878   }
879
880   smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
881 }
882
883 JNIEXPORT void JNICALL
884 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
885 {
886   const char *s = (*env)->GetStringUTFChars(env, js, 0);
887   XBT_INFO("%s", s);
888   (*env)->ReleaseStringUTFChars(env, js, s);
889 }
890
891 JNIEXPORT jobjectArray JNICALL
892 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
893 {
894   int index;
895   jobjectArray jtable;
896   jobject jhost;
897   jstring jname;
898   m_host_t host;
899
900   int count = MSG_get_host_number();
901   m_host_t *table = MSG_get_host_table();
902
903   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
904
905   if (!cls) {
906     return NULL;
907   }
908
909   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
910
911   if (!jtable) {
912     jxbt_throw_jni(env, "Hosts table allocation failed");
913     return NULL;
914   }
915
916   for (index = 0; index < count; index++) {
917     host = table[index];
918     jhost = (jobject) (MSG_host_get_data(host));
919
920     if (!jhost) {
921       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
922
923       jhost =
924           Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
925       /* FIXME: leak of jname ? */
926     }
927
928     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
929   }
930
931   return jtable;
932 }
933
934 JNIEXPORT void JNICALL
935 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
936                                     jstring jalias, jobject jtask,
937                                     jdouble jtimeout)
938 {
939
940   MSG_error_t rv;
941   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
942
943   m_task_t task = jtask_to_native_task(jtask, env);
944
945
946   if (!task) {
947     (*env)->ReleaseStringUTFChars(env, jalias, alias);
948     jxbt_throw_notbound(env, "task", jtask);
949     return;
950   }
951
952   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
953   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
954   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
955
956   (*env)->ReleaseStringUTFChars(env, jalias, alias);
957
958   jxbt_check_res("MSG_task_send_with_timeout()", rv,
959                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
960                  bprintf("while sending task %s to mailbox %s",
961                          MSG_task_get_name(task), alias));
962 }
963
964 JNIEXPORT void JNICALL
965 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
966                                            jstring jalias, jobject jtask,
967                                            jdouble jmaxRate)
968 {
969   m_task_t task = jtask_to_native_task(jtask, env);
970   MSG_error_t rv;
971   const char *alias;
972
973   if (!task) {
974     jxbt_throw_notbound(env, "task", jtask);
975     return;
976   }
977
978   alias = (*env)->GetStringUTFChars(env, jalias, 0);
979
980   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
981   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
982   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
983
984   (*env)->ReleaseStringUTFChars(env, jalias, alias);
985
986   jxbt_check_res("MSG_task_send_bounded()", rv,
987                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
988                  bprintf
989                  ("while sending task %s to mailbox %s with max rate %f",
990                   MSG_task_get_name(task), alias, (double) jmaxRate));
991
992 }
993
994 JNIEXPORT jobject JNICALL
995 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
996                                        jstring jalias, jdouble jtimeout,
997                                        jobject jhost)
998 {
999   MSG_error_t rv;
1000   m_task_t task = NULL;
1001   m_host_t host = NULL;
1002   jobject jtask_global, jtask_local;
1003   const char *alias;
1004
1005   if (jhost) {
1006     host = jhost_get_native(env, jhost);
1007
1008     if (!host) {
1009       jxbt_throw_notbound(env, "host", jhost);
1010       return NULL;
1011     }
1012   }
1013
1014   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1015
1016   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1017   jtask_global = MSG_task_get_data(task);
1018
1019   /* Convert the global ref into a local ref so that the JVM can free the stuff */
1020   jtask_local = (*env)->NewLocalRef(env, jtask_global);
1021   (*env)->DeleteGlobalRef(env, jtask_global);
1022   MSG_task_set_data(task, NULL);
1023
1024   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1025
1026   jxbt_check_res("MSG_task_receive_ext()", rv,
1027                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1028                  bprintf("while receiving from mailbox %s", alias));
1029
1030   return (jobject) jtask_local;
1031 }
1032
1033 JNIEXPORT jboolean JNICALL
1034 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1035                                       jstring jalias)
1036 {
1037
1038   const char *alias;
1039   int rv;
1040
1041   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1042
1043   rv = MSG_task_listen(alias);
1044
1045   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1046
1047   return (jboolean) rv;
1048 }
1049
1050 JNIEXPORT jint JNICALL
1051 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1052                                               jstring jalias,
1053                                               jobject jhost)
1054 {
1055   int rv;
1056   const char *alias;
1057
1058   m_host_t host = jhost_get_native(env, jhost);
1059
1060   if (!host) {
1061     jxbt_throw_notbound(env, "host", jhost);
1062     return -1;
1063   }
1064   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1065
1066   rv = MSG_task_listen_from_host(alias, host);
1067
1068   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1069
1070   return (jint) rv;
1071 }
1072
1073 JNIEXPORT jint JNICALL
1074 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1075                                           jstring jalias)
1076 {
1077
1078   int rv;
1079   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1080
1081   rv = MSG_task_listen_from(alias);
1082
1083   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1084
1085   return (jint) rv;
1086 }
1087
1088 JNIEXPORT void JNICALL
1089 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1090                                        jstring jdeploymentFile)
1091 {
1092
1093   const char *deploymentFile =
1094       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1095
1096   surf_parse_reset_callbacks();
1097
1098   surfxml_add_callback(STag_surfxml_process_cb_list,
1099                        japplication_handler_on_begin_process);
1100
1101   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1102                        japplication_handler_on_process_arg);
1103
1104   surfxml_add_callback(STag_surfxml_prop_cb_list,
1105                        japplication_handler_on_property);
1106
1107   surfxml_add_callback(ETag_surfxml_process_cb_list,
1108                        japplication_handler_on_end_process);
1109
1110   surf_parse_open(deploymentFile);
1111
1112   japplication_handler_on_start_document();
1113
1114   if (surf_parse())
1115     jxbt_throw_jni(env, "surf_parse() failed");
1116
1117   surf_parse_close();
1118
1119   japplication_handler_on_end_document();
1120
1121   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1122 }