Logo AND Algorithmique Numérique Distribuée

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