Logo AND Algorithmique Numérique Distribuée

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