Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b44b39ff43d18b8d05e24e68341358562cd6784f
[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 /*Hack: let msg load directly the right factory */
807 typedef void (*SIMIX_ctx_factory_initializer_t)(smx_context_factory_t *);
808 extern SIMIX_ctx_factory_initializer_t factory_initializer_to_use;
809
810 JNIEXPORT void JNICALL
811 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
812 {
813   char **argv = NULL;
814   int index;
815   int argc = 0;
816   jstring jval;
817   const char *tmp;
818
819   factory_initializer_to_use = SIMIX_ctx_java_factory_init;
820
821   if (jargs)
822     argc = (int) (*env)->GetArrayLength(env, jargs);
823
824   argc++;
825   argv = xbt_new0(char *, argc);
826   argv[0] = strdup("java");
827
828   for (index = 0; index < argc - 1; index++) {
829     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
830     tmp = (*env)->GetStringUTFChars(env, jval, 0);
831     argv[index + 1] = strdup(tmp);
832     (*env)->ReleaseStringUTFChars(env, jval, tmp);
833   }
834
835   MSG_global_init(&argc, argv);
836
837   for (index = 0; index < argc; index++)
838     free(argv[index]);
839
840   free(argv);
841
842   (*env)->GetJavaVM(env, &__java_vm);
843 }
844
845 JNIEXPORT void JNICALL
846     JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
847 {
848   MSG_error_t rv;
849   int index;                    //xbt_fifo_item_t item = NULL;
850   m_host_t *hosts;
851   jobject jhost;
852
853   /* Run everything */
854   rv = MSG_main();
855   jxbt_check_res("MSG_main()", rv, MSG_OK,
856                  bprintf
857                  ("unexpected error : MSG_main() failed .. please report this bug "));
858
859   DEBUG0
860       ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
861
862   DEBUG0("Clean java world");
863   /* Cleanup java hosts */
864   hosts = MSG_get_host_table();
865   for (index = 0; index < MSG_get_host_number() - 1; index++) {
866     jhost = (jobject) hosts[index]->data;
867     if (jhost)
868       jhost_unref(env, jhost);
869
870   }
871
872   DEBUG0("Clean native world");
873   /* cleanup native stuff */
874   rv = MSG_OK != MSG_clean();
875   jxbt_check_res("MSG_clean()", rv, MSG_OK,
876                  bprintf
877                  ("unexpected error : MSG_clean() failed .. please report this bug "));
878
879 }
880
881 JNIEXPORT jint JNICALL
882 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
883                                           jint jresetPID)
884 {
885   return (jint) MSG_process_killall((int) jresetPID);
886 }
887
888 JNIEXPORT void JNICALL
889 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
890                                        jstring jplatformFile)
891 {
892
893   const char *platformFile =
894       (*env)->GetStringUTFChars(env, jplatformFile, 0);
895
896   MSG_create_environment(platformFile);
897
898   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
899 }
900
901 JNIEXPORT void JNICALL
902 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
903                                        jobject jprocess)
904 {
905
906   m_process_t process = jprocess_to_native_process(jprocess, env);
907
908   if (!process) {
909     jxbt_throw_notbound(env, "process", jprocess);
910     return;
911   }
912
913   SIMIX_context_stop(SIMIX_process_self()->context);
914 }
915
916 JNIEXPORT void JNICALL
917 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
918 {
919   const char *s = (*env)->GetStringUTFChars(env, js, 0);
920   INFO1("%s", s);
921   (*env)->ReleaseStringUTFChars(env, js, s);
922 }
923
924 JNIEXPORT jobjectArray JNICALL
925 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
926 {
927   int index;
928   jobjectArray jtable;
929   jobject jhost;
930   jstring jname;
931   m_host_t host;
932
933   int count = MSG_get_host_number();
934   m_host_t *table = MSG_get_host_table();
935
936   jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
937
938   if (!cls) {
939     return NULL;
940   }
941
942   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
943
944   if (!jtable) {
945     jxbt_throw_jni(env, "Hosts table allocation failed");
946     return NULL;
947   }
948
949   for (index = 0; index < count; index++) {
950     host = table[index];
951     jhost = (jobject) (MSG_host_get_data(host));
952
953     if (!jhost) {
954       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
955
956       jhost =
957           Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
958       /* FIXME: leak of jname ? */
959     }
960
961     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
962   }
963
964   return jtable;
965 }
966
967 JNIEXPORT void JNICALL
968 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
969                                     jstring jalias, jobject jtask,
970                                     jdouble jtimeout)
971 {
972
973   MSG_error_t rv;
974   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
975
976   m_task_t task = jtask_to_native_task(jtask, env);
977
978
979   if (!task) {
980     (*env)->ReleaseStringUTFChars(env, jalias, alias);
981     jxbt_throw_notbound(env, "task", jtask);
982     return;
983   }
984
985   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
986   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
987   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
988
989   (*env)->ReleaseStringUTFChars(env, jalias, alias);
990
991   jxbt_check_res("MSG_task_send_with_timeout()", rv,
992                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
993                  bprintf("while sending task %s to mailbox %s",
994                          MSG_task_get_name(task), alias));
995 }
996
997 JNIEXPORT void JNICALL
998 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
999                                            jstring jalias, jobject jtask,
1000                                            jdouble jmaxRate)
1001 {
1002   m_task_t task = jtask_to_native_task(jtask, env);
1003   MSG_error_t rv;
1004   const char *alias;
1005
1006   if (!task) {
1007     jxbt_throw_notbound(env, "task", jtask);
1008     return;
1009   }
1010
1011   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1012
1013   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
1014   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
1015   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1016
1017   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1018
1019   jxbt_check_res("MSG_task_send_bounded()", rv,
1020                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1021                  bprintf
1022                  ("while sending task %s to mailbox %s with max rate %f",
1023                   MSG_task_get_name(task), alias, (double) jmaxRate));
1024
1025 }
1026
1027 JNIEXPORT jobject JNICALL
1028 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1029                                        jstring jalias, jdouble jtimeout,
1030                                        jobject jhost)
1031 {
1032   MSG_error_t rv;
1033   m_task_t task = NULL;
1034   m_host_t host = NULL;
1035   jobject jtask_global, jtask_local;
1036   const char *alias;
1037
1038   if (jhost) {
1039     host = jhost_get_native(env, jhost);
1040
1041     if (!host) {
1042       jxbt_throw_notbound(env, "host", jhost);
1043       return NULL;
1044     }
1045   }
1046
1047   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1048
1049   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1050   jtask_global = MSG_task_get_data(task);
1051
1052   /* Convert the global ref into a local ref so that the JVM can free the stuff */
1053   jtask_local = (*env)->NewLocalRef(env, jtask_global);
1054   (*env)->DeleteGlobalRef(env, jtask_global);
1055   MSG_task_set_data(task, NULL);
1056
1057   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1058
1059   jxbt_check_res("MSG_task_receive_ext()", rv,
1060                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1061                  bprintf("while receiving from mailbox %s", alias));
1062
1063   return (jobject) jtask_local;
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,
1086                                               jobject jhost)
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   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1098
1099   rv = MSG_task_listen_from_host(alias, host);
1100
1101   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1102
1103   return (jint) rv;
1104 }
1105
1106 JNIEXPORT jint JNICALL
1107 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1108                                           jstring jalias)
1109 {
1110
1111   int rv;
1112   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1113
1114   rv = MSG_task_listen_from(alias);
1115
1116   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1117
1118   return (jint) rv;
1119 }
1120
1121 JNIEXPORT void JNICALL
1122 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1123                                        jstring jdeploymentFile)
1124 {
1125
1126   const char *deploymentFile =
1127       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1128
1129   surf_parse_reset_parser();
1130
1131   surfxml_add_callback(STag_surfxml_process_cb_list,
1132                        japplication_handler_on_begin_process);
1133
1134   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1135                        japplication_handler_on_process_arg);
1136
1137   surfxml_add_callback(STag_surfxml_prop_cb_list,
1138                        japplication_handler_on_property);
1139
1140   surfxml_add_callback(ETag_surfxml_process_cb_list,
1141                        japplication_handler_on_end_process);
1142
1143   surf_parse_open(deploymentFile);
1144
1145   japplication_handler_on_start_document();
1146
1147   if (surf_parse())
1148     jxbt_throw_jni(env, "surf_parse() failed");
1149
1150   surf_parse_close();
1151
1152   japplication_handler_on_end_document();
1153
1154   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1155 }