Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ae5dd0a6f249651dc7535485159d584614f78364
[simgrid.git] / src / 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 <simix/context.h>
11 #include <surf/surfxml_parse.h>
12
13 #include "smx_context_java.h"
14
15 #include "jmsg_process.h"
16 #include "jmsg_host.h"
17 #include "jmsg_task.h"
18 #include "jmsg_application_handler.h"
19 #include "jxbt_utilities.h"
20
21 #include "jmsg.h"
22
23 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
24
25 static JavaVM *__java_vm = NULL;
26
27 static jobject native_to_java_process(m_process_t process);
28
29 JavaVM *get_java_VM(void)
30 {
31   return __java_vm;
32 }
33
34 JNIEnv *get_current_thread_env(void)
35 {
36   JNIEnv *env;
37
38   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
39
40   return env;
41 }
42
43 static jobject native_to_java_process(m_process_t process)
44 {
45   return ((smx_ctx_java_t)MSG_process_get_smx_ctx(process))->jprocess;
46 }
47
48 /*
49  * The MSG process connected functions implementation.                                 
50  */
51
52 JNIEXPORT void JNICALL
53 Java_org_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
54                                          jobject jprocess_arg,
55                                          jobject jhost)
56 {
57      
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   m_host_t host;                /* Where that process lives */
64    
65   XBT_DEBUG("Java_org_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
66          env, cls, jprocess_arg, jhost);
67    
68    
69   /* get the name of the java process */
70   jname = jprocess_get_name(jprocess_arg, env);
71   if (!jname) {
72     jxbt_throw_null(env,
73             xbt_strdup("Internal error: Process name cannot be NULL"));
74     return;
75   }
76
77   /* bind/retrieve the msg host */
78   host = jhost_get_native(env, jhost);
79
80   if (!(host)) {    /* not binded */
81     jxbt_throw_notbound(env, "host", jhost);
82     return;
83   }
84
85   /* create a global java process instance */
86   jprocess = jprocess_new_global_ref(jprocess_arg, env);
87   if (!jprocess) {
88     jxbt_throw_jni(env, "Can't get a global ref to the java process");
89     return;
90   }
91
92   /* build the C name of the process */
93   name = (*env)->GetStringUTFChars(env, jname, 0);
94   name = xbt_strdup(name);
95   
96   /* Actually build the MSG process */
97   process = MSG_process_create_with_environment(name,
98                                                 (xbt_main_func_t) jprocess,
99                                                 /*data*/ NULL,
100                                                 host,
101                                                 /*argc, argv, properties*/
102                                                 0,NULL,NULL);
103      
104   MSG_process_set_data(process,&process);
105    
106   /* release our reference to the process name (variable name becomes invalid) */
107   //FIXME : This line should be uncommented but with mac it doesn't work. BIG WARNING
108   //(*env)->ReleaseStringUTFChars(env, jname, name);
109    
110   /* bind the java process instance to the native process */
111   jprocess_bind(jprocess, process, env);
112
113 }
114
115 JNIEXPORT void JNICALL
116 Java_org_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
117                                           jobject jprocess)
118 {
119   m_process_t process = jprocess_to_native_process(jprocess, env);
120
121   if (!process) {
122     jxbt_throw_notbound(env, "process", jprocess);
123     return;
124   }
125
126   /* try to suspend the process */
127   MSG_error_t rv = MSG_process_suspend(process);
128
129   jxbt_check_res("MSG_process_suspend()", rv, MSG_OK,
130                  bprintf("unexpected error , please report this bug"));
131
132 }
133
134 JNIEXPORT void JNICALL
135 Java_org_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
136                                          jobject jprocess)
137 {
138   m_process_t process = jprocess_to_native_process(jprocess, env);
139
140   if (!process) {
141     jxbt_throw_notbound(env, "process", jprocess);
142     return;
143   }
144
145   /* try to resume the process */
146   MSG_error_t rv = MSG_process_resume(process);
147
148   jxbt_check_res("MSG_process_resume()", rv, MSG_OK,
149                  bprintf("unexpected error , please report this bug"));
150 }
151
152 JNIEXPORT jboolean JNICALL
153 Java_org_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
154                                               jobject jprocess)
155 {
156   m_process_t process = jprocess_to_native_process(jprocess, env);
157
158   if (!process) {
159     jxbt_throw_notbound(env, "process", jprocess);
160     return 0;
161   }
162
163   /* true is the process is suspended, false otherwise */
164   return (jboolean) MSG_process_is_suspended(process);
165 }
166
167 JNIEXPORT void JNICALL
168 Java_org_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
169                                        jobject jprocess)
170 {
171   /* get the native instances from the java ones */
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   /* delete the global reference */
180   jprocess_delete_global_ref(native_to_java_process(process), env);
181
182   /* kill the native process (this wrapper is call by the destructor of the java 
183    * process instance)
184    */
185   MSG_process_kill(process);
186 }
187
188 JNIEXPORT jobject JNICALL
189 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
190                                           jobject jprocess)
191 {
192   /* get the native instances from the java ones */
193   m_process_t process = jprocess_to_native_process(jprocess, env);
194   m_host_t host;
195
196   if (!process) {
197     jxbt_throw_notbound(env, "process", jprocess);
198     return NULL;
199   }
200
201   host = MSG_process_get_host(process);
202
203   if (!MSG_host_get_data(host)) {
204     jxbt_throw_jni(env, "MSG_process_get_host() failed");
205     return NULL;
206   }
207
208   /* return the global reference to the java host instance */
209   return (jobject) MSG_host_get_data(host);
210
211 }
212
213 JNIEXPORT jobject JNICALL
214 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
215                                           jint PID)
216 {
217   m_process_t process = MSG_process_from_PID(PID);
218
219   if (!process) {
220     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
221     return NULL;
222   }
223
224   if (!native_to_java_process(process)) {
225     jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
226     return NULL;
227   }
228
229   return (jobject) (native_to_java_process(process));
230 }
231
232
233 JNIEXPORT jint JNICALL
234 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
235                                          jobject jprocess)
236 {
237   m_process_t process = jprocess_to_native_process(jprocess, env);
238
239   if (!process) {
240     jxbt_throw_notbound(env, "process", jprocess);
241     return 0;
242   }
243
244   return (jint) MSG_process_get_PID(process);
245 }
246
247
248 JNIEXPORT jint JNICALL
249 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
250                                           jobject jprocess)
251 {
252   m_process_t process = jprocess_to_native_process(jprocess, env);
253
254   if (!process) {
255     jxbt_throw_notbound(env, "process", jprocess);
256     return 0;
257   }
258
259   return (jint) MSG_process_get_PPID(process);
260 }
261
262 JNIEXPORT jobject JNICALL
263 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
264 {
265   m_process_t process = MSG_process_self();
266   jobject jprocess;
267
268   if (!process) {
269     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
270     return NULL;
271   }
272
273   jprocess = native_to_java_process(process);
274
275   if (!jprocess)
276     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
277
278   return jprocess;
279 }
280
281 JNIEXPORT void JNICALL
282 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
283                                              jobject jprocess, jobject jhost)
284 {
285   m_process_t process = jprocess_to_native_process(jprocess, env);
286
287   if (!process) {
288     jxbt_throw_notbound(env, "process", jprocess);
289     return;
290   }
291
292   m_host_t host = jhost_get_native(env, jhost);
293
294   if (!host) {
295     jxbt_throw_notbound(env, "host", jhost);
296     return;
297   }
298
299   /* try to change the host of the process */
300   MSG_error_t rv = MSG_process_migrate(process, host);
301   jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
302                  bprintf("unexpected error , please report this bug"));
303
304 }
305
306 JNIEXPORT void JNICALL
307 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
308                                           jdouble seconds)
309 {
310   MSG_error_t rv = MSG_process_sleep((double) seconds);
311
312   jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
313                  bprintf("while process was waiting for %f seconds",
314                          (double) seconds));
315
316 }
317
318
319 /***************************************************************************************
320  * The MSG host connected functions implementation.                                    *
321  ***************************************************************************************/
322
323 JNIEXPORT jobject JNICALL
324 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
325                                          jstring jname)
326 {
327   m_host_t host;                /* native host                                          */
328   jobject jhost;                /* global reference to the java host instance returned  */
329
330   /* get the C string from the java string */
331   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
332   XBT_DEBUG("Looking for host '%s'",name);
333   /* get the host by name       (the hosts are created during the grid resolution) */
334   host = MSG_get_host_by_name(name);
335   XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
336
337   if (!host) {                  /* invalid name */
338     jxbt_throw_host_not_found(env, name);
339     (*env)->ReleaseStringUTFChars(env, jname, name);
340     return NULL;
341   }
342   (*env)->ReleaseStringUTFChars(env, jname, name);
343
344   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
345
346     /* instanciate a new java host */
347     jhost = jhost_new_instance(env);
348
349     if (!jhost) {
350       jxbt_throw_jni(env, "java host instantiation failed");
351       return NULL;
352     }
353
354     /* get a global reference to the newly created host */
355     jhost = jhost_ref(env, jhost);
356
357     if (!jhost) {
358       jxbt_throw_jni(env, "new global ref allocation failed");
359       return NULL;
360     }
361
362     /* bind the java host and the native host */
363     jhost_bind(jhost, host, env);
364
365     /* the native host data field is set with the global reference to the 
366      * java host returned by this function 
367      */
368     MSG_host_set_data(host, (void *) jhost);
369   }
370
371   /* return the global reference to the java host instance */
372   return (jobject) MSG_host_get_data(host);
373 }
374
375 JNIEXPORT jstring JNICALL
376 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
377                                        jobject jhost)
378 {
379   m_host_t host = jhost_get_native(env, jhost);
380
381   if (!host) {
382     jxbt_throw_notbound(env, "host", jhost);
383     return NULL;
384   }
385
386   return (*env)->NewStringUTF(env, MSG_host_get_name(host));
387 }
388
389 JNIEXPORT jint JNICALL
390 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
391 {
392   return (jint) MSG_get_host_number();
393 }
394
395 JNIEXPORT jobject JNICALL
396 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
397 {
398   jobject jhost;
399
400   m_host_t host = MSG_host_self();
401
402   if (!MSG_host_get_data(host)) {
403     /* the native host not yet associated with the java host instance */
404
405     /* instanciate a new java host instance */
406     jhost = jhost_new_instance(env);
407
408     if (!jhost) {
409       jxbt_throw_jni(env, "java host instantiation failed");
410       return NULL;
411     }
412
413     /* get a global reference to the newly created host */
414     jhost = jhost_ref(env, jhost);
415
416     if (!jhost) {
417       jxbt_throw_jni(env, "global ref allocation failed");
418       return NULL;
419     }
420
421     /* Bind & store it */
422     jhost_bind(jhost, host, env);
423     MSG_host_set_data(host, (void *) jhost);
424   } else {
425     jhost = (jobject) MSG_host_get_data(host);
426   }
427
428   return jhost;
429 }
430
431 JNIEXPORT jdouble JNICALL
432 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
433                                         jobject jhost)
434 {
435   m_host_t host = jhost_get_native(env, jhost);
436
437   if (!host) {
438     jxbt_throw_notbound(env, "host", jhost);
439     return -1;
440   }
441
442   return (jdouble) MSG_get_host_speed(host);
443 }
444
445 JNIEXPORT jint JNICALL
446 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
447                                        jobject jhost)
448 {
449   m_host_t host = jhost_get_native(env, jhost);
450
451   if (!host) {
452     jxbt_throw_notbound(env, "host", jhost);
453     return -1;
454   }
455
456   return (jint) MSG_get_host_msgload(host);
457 }
458
459
460 JNIEXPORT jboolean JNICALL
461 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
462                                        jobject jhost)
463 {
464   m_host_t host = jhost_get_native(env, jhost);
465
466   if (!host) {
467     jxbt_throw_notbound(env, "host", jhost);
468     return 0;
469   }
470
471   return (jboolean) MSG_host_is_avail(host);
472 }
473
474
475 /***************************************************************************************
476  * The MSG task connected functions implementation.                                    *
477  ***************************************************************************************/
478
479 JNIEXPORT void JNICALL
480 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
481                                       jobject jtask, jstring jname,
482                                       jdouble jcomputeDuration,
483                                       jdouble jmessageSize)
484 {
485   m_task_t task;                /* the native task to create                            */
486   const char *name = NULL;      /* the name of the task                                 */
487
488   if (jcomputeDuration < 0) {
489     jxbt_throw_illegal(env,
490                        bprintf
491                        ("Task ComputeDuration (%f) cannot be negative",
492                         (double) jcomputeDuration));
493     return;
494   }
495
496   if (jmessageSize < 0) {
497     jxbt_throw_illegal(env,
498                        bprintf("Task MessageSize (%f) cannot be negative",
499                                (double) jmessageSize));
500     return;
501   }
502
503   if (jname) {
504     /* get the C string from the java string */
505     name = (*env)->GetStringUTFChars(env, jname, 0);
506   }
507
508
509   /* create the task */
510   task =
511       MSG_task_create(name, (double) jcomputeDuration,
512                       (double) jmessageSize, NULL);
513
514   if (jname)
515     (*env)->ReleaseStringUTFChars(env, jname, name);
516
517   /* bind & store the task */
518   jtask_bind(jtask, task, env);
519   MSG_task_set_data(task, jtask);
520 }
521
522 JNIEXPORT void JNICALL
523 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
524                                                jobject jtask,
525                                                jstring jname,
526                                                jobjectArray jhosts,
527                                                jdoubleArray
528                                                jcomputeDurations_arg,
529                                                jdoubleArray
530                                                jmessageSizes_arg)
531 {
532
533   m_task_t task;                /* the native parallel task to create           */
534   const char *name;             /* the name of the task                         */
535   int host_count;
536   m_host_t *hosts;
537   double *computeDurations;
538   double *messageSizes;
539   jdouble *jcomputeDurations;
540   jdouble *jmessageSizes;
541
542   jobject jhost;
543   int index;
544
545
546   if (!jcomputeDurations_arg) {
547     jxbt_throw_null(env,
548                     xbt_strdup
549                     ("Parallel task compute durations cannot be null"));
550     return;
551   }
552
553   if (!jmessageSizes_arg) {
554     jxbt_throw_null(env,
555                     xbt_strdup
556                     ("Parallel task message sizes cannot be null"));
557     return;
558   }
559
560   if (!jname) {
561     jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
562     return;
563   }
564
565   host_count = (int) (*env)->GetArrayLength(env, jhosts);
566
567
568   hosts = xbt_new0(m_host_t, host_count);
569   computeDurations = xbt_new0(double, host_count);
570   messageSizes = xbt_new0(double, host_count * host_count);
571
572   jcomputeDurations =
573       (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
574   jmessageSizes =
575       (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
576
577   for (index = 0; index < host_count; index++) {
578     jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
579     hosts[index] = jhost_get_native(env, jhost);
580     computeDurations[index] = jcomputeDurations[index];
581   }
582   for (index = 0; index < host_count * host_count; index++) {
583     messageSizes[index] = jmessageSizes[index];
584   }
585
586   (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
587                                      jcomputeDurations, 0);
588   (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
589                                      0);
590
591
592   /* get the C string from the java string */
593   name = (*env)->GetStringUTFChars(env, jname, 0);
594
595   task =
596       MSG_parallel_task_create(name, host_count, hosts, computeDurations,
597                                messageSizes, NULL);
598
599   (*env)->ReleaseStringUTFChars(env, jname, name);
600
601   /* associate the java task object and the native task */
602   jtask_bind(jtask, task, env);
603
604   MSG_task_set_data(task, (void *) jtask);
605
606   if (!MSG_task_get_data(task))
607     jxbt_throw_jni(env, "global ref allocation failed");
608 }
609
610 JNIEXPORT jobject JNICALL
611 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
612                                          jobject jtask)
613 {
614   m_process_t process;
615
616   m_task_t task = jtask_to_native_task(jtask, env);
617
618   if (!task) {
619     jxbt_throw_notbound(env, "task", jtask);
620     return NULL;
621   }
622
623   process = MSG_task_get_sender(task);
624   return (jobject) native_to_java_process(process);
625 }
626
627 JNIEXPORT jobject JNICALL
628 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
629                                          jobject jtask)
630 {
631   m_host_t host;
632   m_task_t task = jtask_to_native_task(jtask, env);
633
634   if (!task) {
635     jxbt_throw_notbound(env, "task", jtask);
636     return NULL;
637   }
638
639   host = MSG_task_get_source(task);
640
641   if (!MSG_host_get_data(host)) {
642     jxbt_throw_jni(env, "MSG_task_get_source() failed");
643     return NULL;
644   }
645
646   return (jobject) MSG_host_get_data(host);
647 }
648
649
650 JNIEXPORT jstring JNICALL
651 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
652                                        jobject jtask)
653 {
654   m_task_t task = jtask_to_native_task(jtask, env);
655
656   if (!task) {
657     jxbt_throw_notbound(env, "task", jtask);
658     return NULL;
659   }
660
661   return (*env)->NewStringUTF(env, MSG_task_get_name(task));
662 }
663
664 JNIEXPORT void JNICALL
665 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
666                                       jobject jtask)
667 {
668   m_task_t ptask = jtask_to_native_task(jtask, env);
669
670   if (!ptask) {
671     jxbt_throw_notbound(env, "task", jtask);
672     return;
673   }
674
675   MSG_error_t rv = MSG_task_cancel(ptask);
676
677   jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
678                  bprintf("unexpected error , please report this bug"));
679 }
680
681 JNIEXPORT jdouble JNICALL
682 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
683                                                   jobject jtask)
684 {
685   m_task_t ptask = jtask_to_native_task(jtask, env);
686
687   if (!ptask) {
688     jxbt_throw_notbound(env, "task", jtask);
689     return -1;
690   }
691   return (jdouble) MSG_task_get_compute_duration(ptask);
692 }
693
694 JNIEXPORT jdouble JNICALL
695 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
696                                                     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_remaining_computation(ptask);
706 }
707
708 JNIEXPORT void JNICALL
709 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
710                                            jobject jtask, jdouble priority)
711 {
712   m_task_t task = jtask_to_native_task(jtask, env);
713
714   if (!task) {
715     jxbt_throw_notbound(env, "task", jtask);
716     return;
717   }
718   MSG_task_set_priority(task, (double) priority);
719 }
720
721 JNIEXPORT void JNICALL
722 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
723                                        jobject jtask_arg)
724 {
725
726   /* get the native task */
727   m_task_t task = jtask_to_native_task(jtask_arg, env);
728 //  jobject jtask;
729
730   if (!task) {
731     jxbt_throw_notbound(env, "task", task);
732     return;
733   }
734 //  jtask = (jobject) MSG_task_get_data(task);
735
736   MSG_error_t rv = MSG_task_destroy(task);
737
738   jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
739                  bprintf("unexpected error , please report this bug"));
740 }
741
742 JNIEXPORT void JNICALL
743 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
744                                        jobject jtask)
745 {
746   m_task_t task = jtask_to_native_task(jtask, env);
747
748   if (!task) {
749     jxbt_throw_notbound(env, "task", jtask);
750     return;
751   }
752
753   MSG_error_t rv = MSG_task_execute(task);
754
755   jxbt_check_res("MSG_task_execute()", rv,
756                  MSG_HOST_FAILURE | MSG_TASK_CANCELLED,
757                  bprintf("while executing task %s",
758                          MSG_task_get_name(task)));
759 }
760
761 /***************************************************************************************
762  * Unsortable functions                                                        *
763  ***************************************************************************************/
764
765
766 JNIEXPORT jint JNICALL
767 Java_org_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
768 {
769   return (jint) MSG_get_errno();
770 }
771
772 JNIEXPORT jdouble JNICALL
773 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
774 {
775   return (jdouble) MSG_get_clock();
776 }
777
778 JNIEXPORT void JNICALL
779 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
780 {
781   char **argv = NULL;
782   int index;
783   int argc = 0;
784   jstring jval;
785   const char *tmp;
786
787   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
788
789   if (jargs)
790     argc = (int) (*env)->GetArrayLength(env, jargs);
791
792   argc++;
793   argv = xbt_new0(char *, argc);
794   argv[0] = strdup("java");
795
796   for (index = 0; index < argc - 1; index++) {
797     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
798     tmp = (*env)->GetStringUTFChars(env, jval, 0);
799     argv[index + 1] = strdup(tmp);
800     //argv[index] = strdup(tmp);
801     (*env)->ReleaseStringUTFChars(env, jval, tmp);
802   }
803
804   MSG_global_init(&argc, argv);
805
806   for (index = 0; index < argc; index++)
807     free(argv[index]);
808
809   free(argv);
810
811   (*env)->GetJavaVM(env, &__java_vm);
812 }
813
814 JNIEXPORT void JNICALL
815     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
816 {
817   MSG_error_t rv;
818   int index;                    //xbt_fifo_item_t item = NULL;
819   m_host_t *hosts;
820   jobject jhost;
821
822   /* Run everything */
823   XBT_INFO("Ready to run MSG_MAIN");
824   rv = MSG_main();
825   XBT_INFO("Done running MSG_MAIN");
826   jxbt_check_res("MSG_main()", rv, MSG_OK,
827                  bprintf
828                  ("unexpected error : MSG_main() failed .. please report this bug "));
829
830   XBT_INFO("MSG_main finished");
831
832   XBT_INFO("Clean java world");
833   /* Cleanup java hosts */
834   hosts = MSG_get_host_table();
835   for (index = 0; index < MSG_get_host_number() - 1; index++) {
836     jhost = (jobject) hosts[index]->data;
837     if (jhost)
838       jhost_unref(env, jhost);
839
840   }
841
842   XBT_INFO("Clean native world");
843   /* cleanup native stuff */
844   rv = MSG_OK != MSG_clean();
845   jxbt_check_res("MSG_clean()", rv, MSG_OK,
846                  bprintf
847                  ("unexpected error : MSG_clean() failed .. please report this bug "));
848 }
849
850 JNIEXPORT jint JNICALL
851 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
852                                           jint jresetPID)
853 {
854   return (jint) MSG_process_killall((int) jresetPID);
855 }
856
857 JNIEXPORT void JNICALL
858 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
859                                        jstring jplatformFile)
860 {
861
862   const char *platformFile =
863       (*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_org_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   smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
883 }
884
885 JNIEXPORT void JNICALL
886 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
887 {
888   const char *s = (*env)->GetStringUTFChars(env, js, 0);
889   XBT_INFO("%s", s);
890   (*env)->ReleaseStringUTFChars(env, js, s);
891 }
892
893 JNIEXPORT jobjectArray JNICALL
894 Java_org_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 = MSG_get_host_number();
903   m_host_t *table = MSG_get_host_table();
904
905   jclass cls = jxbt_get_class(env, "org/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) (MSG_host_get_data(host));
921
922     if (!jhost) {
923       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
924
925       jhost =
926           Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
927       /* FIXME: leak of jname ? */
928     }
929
930     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
931   }
932
933   return jtable;
934 }
935
936 JNIEXPORT void JNICALL
937 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
938                                     jstring jalias, jobject jtask,
939                                     jdouble jtimeout)
940 {
941
942   MSG_error_t rv;
943   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
944
945   m_task_t task = jtask_to_native_task(jtask, env);
946
947
948   if (!task) {
949     (*env)->ReleaseStringUTFChars(env, jalias, alias);
950     jxbt_throw_notbound(env, "task", jtask);
951     return;
952   }
953
954   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
955   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
956   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
957
958   (*env)->ReleaseStringUTFChars(env, jalias, alias);
959
960   jxbt_check_res("MSG_task_send_with_timeout()", rv,
961                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
962                  bprintf("while sending task %s to mailbox %s",
963                          MSG_task_get_name(task), alias));
964 }
965
966 JNIEXPORT void JNICALL
967 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
968                                            jstring jalias, jobject jtask,
969                                            jdouble jmaxRate)
970 {
971   m_task_t task = jtask_to_native_task(jtask, env);
972   MSG_error_t rv;
973   const char *alias;
974
975   if (!task) {
976     jxbt_throw_notbound(env, "task", jtask);
977     return;
978   }
979
980   alias = (*env)->GetStringUTFChars(env, jalias, 0);
981
982   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
983   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
984   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
985
986   (*env)->ReleaseStringUTFChars(env, jalias, alias);
987
988   jxbt_check_res("MSG_task_send_bounded()", rv,
989                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
990                  bprintf
991                  ("while sending task %s to mailbox %s with max rate %f",
992                   MSG_task_get_name(task), alias, (double) jmaxRate));
993
994 }
995
996 JNIEXPORT jobject JNICALL
997 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
998                                        jstring jalias, jdouble jtimeout,
999                                        jobject jhost)
1000 {
1001   MSG_error_t rv;
1002   m_task_t task = NULL;
1003   m_host_t host = NULL;
1004   jobject jtask_global, jtask_local;
1005   const char *alias;
1006
1007   if (jhost) {
1008     host = jhost_get_native(env, jhost);
1009
1010     if (!host) {
1011       jxbt_throw_notbound(env, "host", jhost);
1012       return NULL;
1013     }
1014   }
1015
1016   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1017
1018   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1019   jtask_global = MSG_task_get_data(task);
1020
1021   /* Convert the global ref into a local ref so that the JVM can free the stuff */
1022   jtask_local = (*env)->NewLocalRef(env, jtask_global);
1023   (*env)->DeleteGlobalRef(env, jtask_global);
1024   MSG_task_set_data(task, NULL);
1025
1026   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1027
1028   jxbt_check_res("MSG_task_receive_ext()", rv,
1029                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1030                  bprintf("while receiving from mailbox %s", alias));
1031
1032   return (jobject) jtask_local;
1033 }
1034
1035 JNIEXPORT jboolean JNICALL
1036 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1037                                       jstring jalias)
1038 {
1039
1040   const char *alias;
1041   int rv;
1042
1043   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1044
1045   rv = MSG_task_listen(alias);
1046
1047   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1048
1049   return (jboolean) rv;
1050 }
1051
1052 JNIEXPORT jint JNICALL
1053 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1054                                               jstring jalias,
1055                                               jobject jhost)
1056 {
1057   int rv;
1058   const char *alias;
1059
1060   m_host_t host = jhost_get_native(env, jhost);
1061
1062   if (!host) {
1063     jxbt_throw_notbound(env, "host", jhost);
1064     return -1;
1065   }
1066   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1067
1068   rv = MSG_task_listen_from_host(alias, host);
1069
1070   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1071
1072   return (jint) rv;
1073 }
1074
1075 JNIEXPORT jint JNICALL
1076 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1077                                           jstring jalias)
1078 {
1079
1080   int rv;
1081   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1082
1083   rv = MSG_task_listen_from(alias);
1084
1085   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1086
1087   return (jint) rv;
1088 }
1089
1090 JNIEXPORT void JNICALL
1091 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1092                                        jstring jdeploymentFile)
1093 {
1094
1095   const char *deploymentFile =
1096       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1097
1098   surf_parse_reset_callbacks();
1099
1100   surfxml_add_callback(STag_surfxml_process_cb_list,
1101                        japplication_handler_on_begin_process);
1102
1103   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1104                        japplication_handler_on_process_arg);
1105
1106   surfxml_add_callback(STag_surfxml_prop_cb_list,
1107                        japplication_handler_on_property);
1108
1109   surfxml_add_callback(ETag_surfxml_process_cb_list,
1110                        japplication_handler_on_end_process);
1111
1112   surf_parse_open(deploymentFile);
1113
1114   japplication_handler_on_start_document();
1115
1116   if (surf_parse())
1117     jxbt_throw_jni(env, "surf_parse() failed");
1118
1119   surf_parse_close();
1120
1121   japplication_handler_on_end_document();
1122
1123   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1124 }