Logo AND Algorithmique Numérique Distribuée

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