Logo AND Algorithmique Numérique Distribuée

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