Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
various cleanups to the java bindings
[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
315 JNIEXPORT jint JNICALL
316 Java_simgrid_msg_MsgNative_processSelfPID(JNIEnv * env, jclass cls)
317 {
318   return (jint) MSG_process_self_PID();
319 }
320
321
322 JNIEXPORT jint JNICALL
323 Java_simgrid_msg_MsgNative_processSelfPPID(JNIEnv * env, jclass cls)
324 {
325   return (jint) MSG_process_self_PPID();
326 }
327
328 JNIEXPORT void JNICALL
329 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
330                                              jobject jhost)
331 {
332   m_host_t host = jhost_get_native(env, jhost);
333
334   if (!host) {
335     jxbt_throw_notbound(env, "host", jhost);
336     return;
337   }
338
339   /* try to change the host of the process */
340   if (MSG_OK != MSG_process_change_host(host))
341     jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
342 }
343
344 JNIEXPORT void JNICALL
345 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
346                                           jdouble seconds)
347 {
348   if (MSG_OK != MSG_process_sleep((double) seconds))
349     jxbt_throw_native(env,
350                       bprintf("MSG_process_change_host(%f) failed",
351                               (double) seconds));
352 }
353
354
355 /***************************************************************************************
356  * The MSG host connected functions implementation.                                    *
357  ***************************************************************************************/
358
359 JNIEXPORT jobject JNICALL
360 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
361                                          jstring jname)
362 {
363   m_host_t host;                /* native host                                          */
364   jobject jhost;                /* global reference to the java host instance returned  */
365
366   /* get the C string from the java string */
367   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
368
369   /* get the host by name       (the hosts are created during the grid resolution) */
370   host = MSG_get_host_by_name(name);
371   DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
372
373   (*env)->ReleaseStringUTFChars(env, jname, name);
374
375   if (!host) {                  /* invalid name */
376     jxbt_throw_host_not_found(env, name);
377     return NULL;
378   }
379
380   if (!host->data) {            /* native host not associated yet with java host */
381
382     /* instanciate a new java host */
383     jhost = jhost_new_instance(env);
384
385     if (!jhost) {
386       jxbt_throw_jni(env, "java host instantiation failed");
387       return NULL;
388     }
389
390     /* get a global reference to the newly created host */
391     jhost = jhost_ref(env, jhost);
392
393     if (!jhost) {
394       jxbt_throw_jni(env, "new global ref allocation failed");
395       return NULL;
396     }
397
398     /* bind the java host and the native host */
399     jhost_bind(jhost, host, env);
400
401     /* the native host data field is set with the global reference to the 
402      * java host returned by this function 
403      */
404     host->data = (void *) jhost;
405   }
406
407   /* return the global reference to the java host instance */
408   return (jobject) host->data;
409 }
410
411 JNIEXPORT jstring JNICALL
412 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
413                                        jobject jhost)
414 {
415   m_host_t host = jhost_get_native(env, jhost);
416
417   if (!host) {
418     jxbt_throw_notbound(env, "host", jhost);
419     return NULL;
420   }
421
422   return (*env)->NewStringUTF(env, host->name);
423 }
424
425 JNIEXPORT jint JNICALL
426 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
427 {
428   return (jint) MSG_get_host_number();
429 }
430
431 JNIEXPORT jobject JNICALL
432 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
433 {
434   jobject jhost;
435
436   m_host_t host = MSG_host_self();
437
438   if (!host->data) {
439     /* the native host not yet associated with the java host instance */
440
441     /* instanciate a new java host instance */
442     jhost = jhost_new_instance(env);
443
444     if (!jhost) {
445       jxbt_throw_jni(env, "java host instantiation failed");
446       return NULL;
447     }
448
449     /* get a global reference to the newly created host */
450     jhost = jhost_ref(env, jhost);
451
452     if (!jhost) {
453       jxbt_throw_jni(env, "global ref allocation failed");
454       return NULL;
455     }
456
457     /* Bind & store it */
458     jhost_bind(jhost, host, env);
459     host->data = (void *) jhost;
460   } else {
461     jhost = (jobject) host->data;
462   }
463
464   return jhost;
465 }
466
467 JNIEXPORT jdouble JNICALL
468 Java_simgrid_msg_MsgNative_hostGetSpeed(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 (jdouble) MSG_get_host_speed(host);
479 }
480
481 JNIEXPORT jint JNICALL
482 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
483                                        jobject jhost)
484 {
485   m_host_t host = jhost_get_native(env, jhost);
486
487   if (!host) {
488     jxbt_throw_notbound(env, "host", jhost);
489     return -1;
490   }
491
492   return (jint) MSG_get_host_msgload(host);
493 }
494
495
496 JNIEXPORT jboolean JNICALL
497 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
498                                        jobject jhost)
499 {
500   m_host_t host = jhost_get_native(env, jhost);
501
502   if (!host) {
503     jxbt_throw_notbound(env, "host", jhost);
504     return 0;
505   }
506
507   return (jboolean) MSG_host_is_avail(host);
508 }
509
510
511 /***************************************************************************************
512  * The MSG task connected functions implementation.                                    *
513  ***************************************************************************************/
514
515 JNIEXPORT void JNICALL
516 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
517                                       jstring jname, jdouble jcomputeDuration,
518                                       jdouble jmessageSize)
519 {
520   m_task_t task;                /* the native task to create                            */
521   const char *name;             /* the name of the task                                 */
522
523   if (jcomputeDuration < 0) {
524     jxbt_throw_illegal(env,
525                        bprintf("Task ComputeDuration (%f) cannot be negative",
526                                (double) jcomputeDuration));
527     return;
528   }
529
530   if (jmessageSize < 0) {
531     jxbt_throw_illegal(env,
532                        bprintf("Task MessageSize (%f) cannot be negative",
533                                (double) jmessageSize));
534     return;
535   }
536
537   if (!jname) {
538     jxbt_throw_null(env, xbt_strdup("Task name cannot be null"));
539     return;
540   }
541
542   /* get the C string from the java string */
543   name = (*env)->GetStringUTFChars(env, jname, 0);
544
545   /* create the task */
546   task =
547     MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
548                     NULL);
549
550   (*env)->ReleaseStringUTFChars(env, jname, name);
551
552   /* bind & store the task */
553   jtask_bind(jtask, task, env);
554
555   /* allocate a new global reference to the java task instance */
556   task->data = (void *) jtask_new_global_ref(jtask, env);
557
558   if (!task->data)
559     jxbt_throw_jni(env, "global ref allocation failed");
560
561 }
562
563 JNIEXPORT void JNICALL
564 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
565                                                jobject jtask, jstring jname,
566                                                jobjectArray jhosts,
567                                                jdoubleArray
568                                                jcomputeDurations_arg,
569                                                jdoubleArray jmessageSizes_arg)
570 {
571
572   m_task_t task;                /* the native parallel task to create           */
573   const char *name;             /* the name of the task                         */
574   int host_count;
575   m_host_t *hosts;
576   double *computeDurations;
577   double *messageSizes;
578   jdouble *jcomputeDurations;
579   jdouble *jmessageSizes;
580
581   jobject jhost;
582   int index;
583
584
585   if (!jcomputeDurations_arg) {
586     jxbt_throw_null(env,
587                     xbt_strdup
588                     ("Parallel task compute durations cannot be null"));
589     return;
590   }
591
592   if (!jmessageSizes_arg) {
593     jxbt_throw_null(env,
594                     xbt_strdup("Parallel task message sizes cannot be null"));
595     return;
596   }
597
598   if (!jname) {
599     jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
600     return;
601   }
602
603   host_count = (int) (*env)->GetArrayLength(env, jhosts);
604
605
606   hosts = xbt_new0(m_host_t, host_count);
607   computeDurations = xbt_new0(double, host_count);
608   messageSizes = xbt_new0(double, host_count * host_count);
609
610   jcomputeDurations =
611     (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
612   jmessageSizes = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
613
614   for (index = 0; index < host_count; index++) {
615     jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
616     hosts[index] = jhost_get_native(env, jhost);
617     computeDurations[index] = jcomputeDurations[index];
618   }
619   for (index = 0; index < host_count * host_count; index++) {
620     messageSizes[index] = jmessageSizes[index];
621   }
622
623   (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
624                                      jcomputeDurations, 0);
625   (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
626                                      0);
627
628
629   /* get the C string from the java string */
630   name = (*env)->GetStringUTFChars(env, jname, 0);
631
632   task =
633     MSG_parallel_task_create(name, host_count, hosts, computeDurations,
634                              messageSizes, NULL);
635
636   (*env)->ReleaseStringUTFChars(env, jname, name);
637
638   /* associate the java task object and the native task */
639   jtask_bind(jtask, task, env);
640
641   task->data = (void *) jtask;
642
643   if (!task->data)
644     jxbt_throw_jni(env, "global ref allocation failed");
645 }
646
647 JNIEXPORT jobject JNICALL
648 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
649                                          jobject jtask)
650 {
651   m_process_t process;
652
653   m_task_t task = jtask_to_native_task(jtask, env);
654
655   if (!task) {
656     jxbt_throw_notbound(env, "task", jtask);
657     return NULL;
658   }
659
660   process = MSG_task_get_sender(task);
661   return (jobject) native_to_java_process(process);
662 }
663
664 JNIEXPORT jobject JNICALL
665 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
666                                          jobject jtask)
667 {
668   m_host_t host;
669   m_task_t task = jtask_to_native_task(jtask, env);
670
671   if (!task) {
672     jxbt_throw_notbound(env, "task", jtask);
673     return NULL;
674   }
675
676   host = MSG_task_get_source(task);
677
678   if (!host->data) {
679     jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
680     return NULL;
681   }
682
683   return (jobject) host->data;
684 }
685
686
687 JNIEXPORT jstring JNICALL
688 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
689                                        jobject jtask)
690 {
691   m_task_t task = jtask_to_native_task(jtask, env);
692
693   if (!task) {
694     jxbt_throw_notbound(env, "task", jtask);
695     return NULL;
696   }
697
698   return (*env)->NewStringUTF(env, task->name);
699 }
700
701 JNIEXPORT void JNICALL
702 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, 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;
709   }
710
711   if (MSG_OK != MSG_task_cancel(ptask))
712     jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
713 }
714
715 JNIEXPORT jdouble JNICALL
716 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
717                                                   jobject jtask)
718 {
719   m_task_t ptask = jtask_to_native_task(jtask, env);
720
721   if (!ptask) {
722     jxbt_throw_notbound(env, "task", jtask);
723     return -1;
724   }
725   return (jdouble) MSG_task_get_compute_duration(ptask);
726 }
727
728 JNIEXPORT jdouble JNICALL
729 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env, jclass cls,
730                                                     jobject jtask)
731 {
732   m_task_t ptask = jtask_to_native_task(jtask, env);
733
734   if (!ptask) {
735     jxbt_throw_notbound(env, "task", jtask);
736     return -1;
737   }
738   return (jdouble) MSG_task_get_remaining_computation(ptask);
739 }
740
741 JNIEXPORT void JNICALL
742 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
743                                            jobject jtask, jdouble priority)
744 {
745   m_task_t task = jtask_to_native_task(jtask, env);
746
747   if (!task) {
748     jxbt_throw_notbound(env, "task", jtask);
749     return;
750   }
751   MSG_task_set_priority(task, (double) priority);
752 }
753
754 JNIEXPORT void JNICALL
755 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
756                                        jobject jtask_arg)
757 {
758
759   /* get the native task */
760   m_task_t task = jtask_to_native_task(jtask_arg, env);
761   jobject jtask;
762
763   if (!task) {
764     jxbt_throw_notbound(env, "task", task);
765     return;
766   }
767   jtask = (jobject) task->data;
768
769   if (MSG_OK != MSG_task_destroy(task))
770     jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
771
772   /* delete the global reference to the java task object */
773   jtask_delete_global_ref(jtask, env);
774 }
775
776 JNIEXPORT void JNICALL
777 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
778                                        jobject jtask)
779 {
780   m_task_t task = jtask_to_native_task(jtask, env);
781
782   if (!task) {
783     jxbt_throw_notbound(env, "task", jtask);
784     return;
785   }
786
787   if (MSG_OK != MSG_task_execute(task))
788     jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
789 }
790
791 /***************************************************************************************
792  * Unsortable functions                                                        *
793  ***************************************************************************************/
794
795
796 JNIEXPORT jint JNICALL
797 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
798 {
799   return (jint) MSG_get_errno();
800 }
801
802 JNIEXPORT jdouble JNICALL
803 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
804 {
805   return (jdouble) MSG_get_clock();
806 }
807
808
809 JNIEXPORT void JNICALL
810 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) {
811   char **argv = NULL;
812   int index;
813   int argc = 0;
814   jstring jval;
815   const char *tmp;
816
817   if (jargs)
818     argc = (int) (*env)->GetArrayLength(env, jargs);
819
820   argc++;
821   argv = xbt_new0(char *, argc);
822   argv[0] = strdup("java");
823
824   for (index = 0; index < argc - 1; index++) {
825     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
826     tmp = (*env)->GetStringUTFChars(env, jval, 0);
827     argv[index + 1] = strdup(tmp);
828     (*env)->ReleaseStringUTFChars(env, jval, tmp);
829   }
830
831   MSG_global_init(&argc, argv);
832   SIMIX_context_select_factory("java");
833
834   for (index = 0; index < argc; index++)
835     free(argv[index]);
836
837   free(argv);
838
839   (*env)->GetJavaVM(env, &__java_vm);
840 }
841
842 JNIEXPORT void JNICALL
843   JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) {
844   xbt_fifo_item_t item = NULL;
845   m_host_t host = NULL;
846   jobject jhost;
847
848   /* Run everything */
849   if (MSG_OK != MSG_main()) {
850     jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
851   }
852   DEBUG0
853     ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
854
855   DEBUG0("Clean java world");
856   /* Cleanup java hosts */
857   xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
858     jhost = (jobject) host->data;
859
860     if (jhost)
861       jhost_unref(env, jhost);
862   }
863
864   DEBUG0("Clean native world");
865   /* cleanup native stuff */
866   if (MSG_OK != MSG_clean()){
867     jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
868   }
869 }
870
871 JNIEXPORT jint JNICALL
872 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
873                                           jint jresetPID)
874 {
875   return (jint) MSG_process_killall((int) jresetPID);
876 }
877
878 JNIEXPORT void JNICALL
879 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
880                                        jstring jplatformFile)
881 {
882
883   const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
884
885   MSG_create_environment(platformFile);
886
887   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
888 }
889
890 JNIEXPORT void JNICALL
891 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
892                                        jobject jprocess)
893 {
894
895   m_process_t process = jprocess_to_native_process(jprocess, env);
896
897   if (!process) {
898     jxbt_throw_notbound(env, "process", jprocess);
899     return;
900   }
901
902   SIMIX_context_stop(SIMIX_process_self()->context);
903 }
904
905 JNIEXPORT void JNICALL
906 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
907 {
908   const char *s = (*env)->GetStringUTFChars(env, js, 0);
909   INFO1("%s", s);
910   (*env)->ReleaseStringUTFChars(env, js, s);
911 }
912
913 JNIEXPORT jobjectArray JNICALL
914 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
915 {
916   int index;
917   jobjectArray jtable;
918   jobject jhost;
919   jstring jname;
920   m_host_t host;
921
922   int count = xbt_fifo_size(msg_global->host);
923   m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
924
925   jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
926
927   if (!cls) {
928     return NULL;
929   }
930
931   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
932
933   if (!jtable) {
934     jxbt_throw_jni(env, "Hosts table allocation failed");
935     return NULL;
936   }
937
938   for (index = 0; index < count; index++) {
939     host = table[index];
940     jhost = (jobject) (host->data);
941
942     if (!jhost) {
943       jname = (*env)->NewStringUTF(env, host->name);
944
945       jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
946       /* FIXME: leak of jname ? */
947     }
948
949     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
950   }
951
952   return jtable;
953 }
954
955
956 JNIEXPORT void JNICALL
957 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
958                                                 jstring jname)
959 {
960   char *errmsg = NULL;
961   xbt_ex_t e;
962
963   /* get the C string from the java string */
964   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
965
966   TRY {
967     SIMIX_context_select_factory(name);
968   } CATCH(e) {
969     errmsg = xbt_strdup(e.msg);
970     xbt_ex_free(e);
971   }
972
973   (*env)->ReleaseStringUTFChars(env, jname, name);
974
975   if (errmsg) {
976     char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
977     free(errmsg);
978     jxbt_throw_native(env, thrown);
979   }
980 }
981
982 JNIEXPORT void JNICALL
983 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
984                                     jstring jalias, jobject jtask,
985                                     jdouble jtimeout)
986 {
987
988   MSG_error_t rv;
989   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
990
991   m_task_t task = jtask_to_native_task(jtask, env);
992
993
994   if (!task) {
995     (*env)->ReleaseStringUTFChars(env, jalias, alias);
996     jxbt_throw_notbound(env, "task", jtask);
997     return;
998   }
999
1000   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
1001
1002   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1003
1004   if (MSG_OK != rv)
1005     jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
1006
1007 }
1008
1009 JNIEXPORT void JNICALL
1010 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1011                                            jstring jalias, jobject jtask,
1012                                            jdouble jmaxRate)
1013 {
1014   m_task_t task = jtask_to_native_task(jtask, env);
1015   MSG_error_t rv;
1016   const char *alias;
1017
1018   if (!task) {
1019     jxbt_throw_notbound(env, "task", jtask);
1020     return;
1021   }
1022
1023   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1024
1025   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1026
1027   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1028
1029   if (MSG_OK != rv)
1030     jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1031 }
1032
1033 JNIEXPORT jobject JNICALL
1034 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1035                                        jstring jalias, jdouble jtimeout,
1036                                        jobject jhost)
1037 {
1038   MSG_error_t rv;
1039   m_task_t task = NULL;
1040   m_host_t host = NULL;
1041   const char *alias;
1042
1043   if (jhost) {
1044     host = jhost_get_native(env, jhost);
1045
1046     if (!host) {
1047       jxbt_throw_notbound(env, "host", jhost);
1048       return NULL;
1049     }
1050   }
1051
1052   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1053
1054   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1055
1056   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1057
1058   if (MSG_OK != rv) {
1059     jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1060     return NULL;
1061   }
1062
1063   return (jobject) task->data;
1064 }
1065
1066 JNIEXPORT jboolean JNICALL
1067 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1068                                       jstring jalias)
1069 {
1070
1071   const char *alias;
1072   int rv;
1073
1074   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1075
1076   rv = MSG_task_listen(alias);
1077
1078   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1079
1080   return (jboolean) rv;
1081 }
1082
1083 JNIEXPORT jint JNICALL
1084 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1085                                               jstring jalias, jobject jhost)
1086 {
1087
1088   int rv;
1089   const char *alias;
1090
1091   m_host_t host = jhost_get_native(env, jhost);
1092
1093   if (!host) {
1094     jxbt_throw_notbound(env, "host", jhost);
1095     return -1;
1096   }
1097
1098   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1099
1100   rv = MSG_task_listen_from_host(alias, host);
1101
1102   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1103
1104   return (jint) rv;
1105 }
1106
1107 JNIEXPORT jint JNICALL
1108 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1109                                           jstring jalias)
1110 {
1111
1112   int rv;
1113   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1114
1115   rv = MSG_task_listen_from(alias);
1116
1117   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1118
1119   return (jint) rv;
1120 }
1121
1122 JNIEXPORT void JNICALL
1123 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1124                                        jstring jdeploymentFile)
1125 {
1126
1127   const char *deploymentFile =
1128     (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1129
1130   surf_parse_reset_parser();
1131
1132   surfxml_add_callback(STag_surfxml_process_cb_list,
1133                        japplication_handler_on_begin_process);
1134
1135   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1136                        japplication_handler_on_process_arg);
1137
1138   surfxml_add_callback(STag_surfxml_prop_cb_list,
1139                        japplication_handler_on_property);
1140
1141   surfxml_add_callback(ETag_surfxml_process_cb_list,
1142                        japplication_handler_on_end_process);
1143
1144   surf_parse_open(deploymentFile);
1145
1146   japplication_handler_on_start_document();
1147
1148   if (surf_parse())
1149     jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
1150
1151   surf_parse_close();
1152
1153   japplication_handler_on_end_document();
1154
1155   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1156 }