Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add some parenthesis around a truth value, as suggested by eclipse
[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   /* kill the native process (this wrapper is call by the destructor of the java 
180    * process instance)
181    */
182   MSG_process_kill(process);
183 }
184
185 JNIEXPORT jobject JNICALL
186 Java_org_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
187                                           jobject jprocess)
188 {
189   /* get the native instances from the java ones */
190   m_process_t process = jprocess_to_native_process(jprocess, env);
191   m_host_t host;
192
193   if (!process) {
194     jxbt_throw_notbound(env, "process", jprocess);
195     return NULL;
196   }
197
198   host = MSG_process_get_host(process);
199
200   if (!MSG_host_get_data(host)) {
201     jxbt_throw_jni(env, "MSG_process_get_host() failed");
202     return NULL;
203   }
204
205   /* return the global reference to the java host instance */
206   return (jobject) MSG_host_get_data(host);
207
208 }
209
210 JNIEXPORT jobject JNICALL
211 Java_org_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls,
212                                           jint PID)
213 {
214   m_process_t process = MSG_process_from_PID(PID);
215
216   if (!process) {
217     jxbt_throw_process_not_found(env, bprintf("PID = %d",(int) PID));
218     return NULL;
219   }
220
221   if (!native_to_java_process(process)) {
222     jxbt_throw_jni(env, "SIMIX_process_get_jprocess() failed");
223     return NULL;
224   }
225
226   return (jobject) (native_to_java_process(process));
227 }
228
229
230 JNIEXPORT jint JNICALL
231 Java_org_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
232                                          jobject jprocess)
233 {
234   m_process_t process = jprocess_to_native_process(jprocess, env);
235
236   if (!process) {
237     jxbt_throw_notbound(env, "process", jprocess);
238     return 0;
239   }
240
241   return (jint) MSG_process_get_PID(process);
242 }
243
244
245 JNIEXPORT jint JNICALL
246 Java_org_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
247                                           jobject jprocess)
248 {
249   m_process_t process = jprocess_to_native_process(jprocess, env);
250
251   if (!process) {
252     jxbt_throw_notbound(env, "process", jprocess);
253     return 0;
254   }
255
256   return (jint) MSG_process_get_PPID(process);
257 }
258
259 JNIEXPORT jobject JNICALL
260 Java_org_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
261 {
262   m_process_t process = MSG_process_self();
263   jobject jprocess;
264
265   if (!process) {
266     jxbt_throw_jni(env, xbt_strdup("MSG_process_self() failed"));
267     return NULL;
268   }
269
270   jprocess = native_to_java_process(process);
271
272   if (!jprocess)
273     jxbt_throw_jni(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
274
275   return jprocess;
276 }
277
278 JNIEXPORT void JNICALL
279 Java_org_simgrid_msg_MsgNative_processMigrate(JNIEnv * env, jclass cls,
280                                              jobject jprocess, jobject jhost)
281 {
282   m_process_t process = jprocess_to_native_process(jprocess, env);
283
284   if (!process) {
285     jxbt_throw_notbound(env, "process", jprocess);
286     return;
287   }
288
289   m_host_t host = jhost_get_native(env, jhost);
290
291   if (!host) {
292     jxbt_throw_notbound(env, "host", jhost);
293     return;
294   }
295
296   /* try to change the host of the process */
297   MSG_error_t rv = MSG_process_migrate(process, host);
298   jxbt_check_res("MSG_process_migrate()", rv, MSG_OK,
299                  bprintf("unexpected error , please report this bug"));
300
301 }
302
303 JNIEXPORT void JNICALL
304 Java_org_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
305                                           jdouble seconds)
306 {
307   MSG_error_t rv = MSG_process_sleep((double) seconds);
308
309   jxbt_check_res("MSG_process_sleep()", rv, MSG_HOST_FAILURE,
310                  bprintf("while process was waiting for %f seconds",
311                          (double) seconds));
312
313 }
314
315
316 /***************************************************************************************
317  * The MSG host connected functions implementation.                                    *
318  ***************************************************************************************/
319
320 JNIEXPORT jobject JNICALL
321 Java_org_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
322                                          jstring jname)
323 {
324   m_host_t host;                /* native host                                          */
325   jobject jhost;                /* global reference to the java host instance returned  */
326
327   /* get the C string from the java string */
328   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
329   XBT_DEBUG("Looking for host '%s'",name);
330   /* get the host by name       (the hosts are created during the grid resolution) */
331   host = MSG_get_host_by_name(name);
332   XBT_DEBUG("MSG gave %p as native host (simdata=%p)", host,host? host->simdata:NULL);
333
334   if (!host) {                  /* invalid name */
335     jxbt_throw_host_not_found(env, name);
336     (*env)->ReleaseStringUTFChars(env, jname, name);
337     return NULL;
338   }
339   (*env)->ReleaseStringUTFChars(env, jname, name);
340
341   if (!MSG_host_get_data(host)) {       /* native host not associated yet with java host */
342
343     /* instanciate a new java host */
344     jhost = jhost_new_instance(env);
345
346     if (!jhost) {
347       jxbt_throw_jni(env, "java host instantiation failed");
348       return NULL;
349     }
350
351     /* get a global reference to the newly created host */
352     jhost = jhost_ref(env, jhost);
353
354     if (!jhost) {
355       jxbt_throw_jni(env, "new global ref allocation failed");
356       return NULL;
357     }
358
359     /* bind the java host and the native host */
360     jhost_bind(jhost, host, env);
361
362     /* the native host data field is set with the global reference to the 
363      * java host returned by this function 
364      */
365     MSG_host_set_data(host, (void *) jhost);
366   }
367
368   /* return the global reference to the java host instance */
369   return (jobject) MSG_host_get_data(host);
370 }
371
372 JNIEXPORT jstring JNICALL
373 Java_org_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
374                                        jobject jhost)
375 {
376   m_host_t host = jhost_get_native(env, jhost);
377
378   if (!host) {
379     jxbt_throw_notbound(env, "host", jhost);
380     return NULL;
381   }
382
383   return (*env)->NewStringUTF(env, MSG_host_get_name(host));
384 }
385
386 JNIEXPORT jint JNICALL
387 Java_org_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
388 {
389   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
390   int nb_host = xbt_dynar_length(hosts);
391   xbt_dynar_free(&hosts);
392   return (jint) nb_host;
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
729   if (!task) {
730     jxbt_throw_notbound(env, "task", task);
731     return;
732   }
733
734   MSG_error_t rv = MSG_task_destroy(task);
735
736   jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
737                  bprintf("unexpected error , please report this bug"));
738 }
739
740 JNIEXPORT void JNICALL
741 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
742                                        jobject jtask)
743 {
744   m_task_t task = jtask_to_native_task(jtask, env);
745
746   if (!task) {
747     jxbt_throw_notbound(env, "task", jtask);
748     return;
749   }
750
751   MSG_error_t rv = MSG_task_execute(task);
752
753   jxbt_check_res("MSG_task_execute()", rv,
754                  MSG_HOST_FAILURE | MSG_TASK_CANCELED,
755                  bprintf("while executing task %s",
756                          MSG_task_get_name(task)));
757 }
758
759 /***************************************************************************************
760  * Unsortable functions                                                        *
761  ***************************************************************************************/
762
763 JNIEXPORT jdouble JNICALL
764 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
765 {
766   return (jdouble) MSG_get_clock();
767 }
768
769 JNIEXPORT void JNICALL
770 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
771 {
772   char **argv = NULL;
773   int index;
774   int argc = 0;
775   jstring jval;
776   const char *tmp;
777
778   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
779
780   if (jargs)
781     argc = (int) (*env)->GetArrayLength(env, jargs);
782
783   argc++;
784   argv = xbt_new(char *, argc + 1);
785   argv[0] = strdup("java");
786
787   for (index = 0; index < argc - 1; index++) {
788     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
789     tmp = (*env)->GetStringUTFChars(env, jval, 0);
790     argv[index + 1] = strdup(tmp);
791     (*env)->ReleaseStringUTFChars(env, jval, tmp);
792   }
793   argv[argc] = NULL;
794
795   MSG_global_init(&argc, argv);
796
797   for (index = 0; index < argc; index++)
798     free(argv[index]);
799
800   free(argv);
801
802   (*env)->GetJavaVM(env, &__java_vm);
803 }
804
805 JNIEXPORT void JNICALL
806     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
807 {
808   MSG_error_t rv;
809   int index;
810   xbt_dynar_t hosts;
811   jobject jhost;
812
813   /* Run everything */
814   XBT_INFO("Ready to run MSG_MAIN");
815   rv = MSG_main();
816   XBT_INFO("Done running MSG_MAIN");
817   jxbt_check_res("MSG_main()", rv, MSG_OK,
818                  bprintf
819                  ("unexpected error : MSG_main() failed .. please report this bug "));
820
821   XBT_INFO("MSG_main finished");
822
823   XBT_INFO("Clean java world");
824   /* Cleanup java hosts */
825   hosts = MSG_hosts_as_dynar();
826   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
827     jhost = (jobject) xbt_dynar_get_as(hosts,index,m_host_t)->data;
828     if (jhost)
829       jhost_unref(env, jhost);
830
831   }
832   xbt_dynar_free(&hosts);
833   XBT_INFO("Clean native world");
834 }
835 JNIEXPORT void JNICALL
836     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
837 {
838   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
839   MSG_error_t rv = MSG_OK != MSG_clean();
840   jxbt_check_res("MSG_clean()", rv, MSG_OK,
841                  bprintf
842                  ("unexpected error : MSG_clean() failed .. please report this bug "));
843 }
844    
845 JNIEXPORT jint JNICALL
846 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
847                                           jint jresetPID)
848 {
849   return (jint) MSG_process_killall((int) jresetPID);
850 }
851
852 JNIEXPORT void JNICALL
853 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
854                                        jstring jplatformFile)
855 {
856
857   const char *platformFile =
858       (*env)->GetStringUTFChars(env, jplatformFile, 0);
859
860   MSG_create_environment(platformFile);
861
862   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
863 }
864
865 JNIEXPORT void JNICALL
866 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
867                                        jobject jprocess)
868 {
869
870   m_process_t process = jprocess_to_native_process(jprocess, env);
871
872   if (!process) {
873     jxbt_throw_notbound(env, "process", jprocess);
874     return;
875   }
876
877   smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
878 }
879
880 JNIEXPORT void JNICALL
881 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
882 {
883   const char *s = (*env)->GetStringUTFChars(env, js, 0);
884   XBT_INFO("%s", s);
885   (*env)->ReleaseStringUTFChars(env, js, s);
886 }
887
888 JNIEXPORT jobjectArray JNICALL
889 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
890 {
891   int index;
892   jobjectArray jtable;
893   jobject jhost;
894   jstring jname;
895   m_host_t host;
896
897   xbt_dynar_t table =  MSG_hosts_as_dynar();
898   int count = xbt_dynar_length(table);
899
900   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
901
902   if (!cls) {
903     return NULL;
904   }
905
906   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
907
908   if (!jtable) {
909     jxbt_throw_jni(env, "Hosts table allocation failed");
910     return NULL;
911   }
912
913   for (index = 0; index < count; index++) {
914     host = xbt_dynar_get_as(table,index,m_host_t);
915     jhost = (jobject) (MSG_host_get_data(host));
916
917     if (!jhost) {
918       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
919
920       jhost =
921           Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
922       /* FIXME: leak of jname ? */
923     }
924
925     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
926   }
927   xbt_dynar_free(&table);
928   return jtable;
929 }
930
931 JNIEXPORT void JNICALL
932 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
933                                     jstring jalias, jobject jtask,
934                                     jdouble jtimeout)
935 {
936
937   MSG_error_t rv;
938   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
939
940   m_task_t task = jtask_to_native_task(jtask, env);
941
942
943   if (!task) {
944     (*env)->ReleaseStringUTFChars(env, jalias, alias);
945     jxbt_throw_notbound(env, "task", jtask);
946     return;
947   }
948
949   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
950   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
951   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
952
953   (*env)->ReleaseStringUTFChars(env, jalias, alias);
954
955   jxbt_check_res("MSG_task_send_with_timeout()", rv,
956                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
957                  bprintf("while sending task %s to mailbox %s",
958                          MSG_task_get_name(task), alias));
959 }
960
961 JNIEXPORT void JNICALL
962 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
963                                            jstring jalias, jobject jtask,
964                                            jdouble jmaxRate)
965 {
966   m_task_t task = jtask_to_native_task(jtask, env);
967   MSG_error_t rv;
968   const char *alias;
969
970   if (!task) {
971     jxbt_throw_notbound(env, "task", jtask);
972     return;
973   }
974
975   alias = (*env)->GetStringUTFChars(env, jalias, 0);
976
977   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
978   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
979   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
980
981   (*env)->ReleaseStringUTFChars(env, jalias, alias);
982
983   jxbt_check_res("MSG_task_send_bounded()", rv,
984                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
985                  bprintf
986                  ("while sending task %s to mailbox %s with max rate %f",
987                   MSG_task_get_name(task), alias, (double) jmaxRate));
988
989 }
990
991 JNIEXPORT jobject JNICALL
992 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
993                                        jstring jalias, jdouble jtimeout,
994                                        jobject jhost)
995 {
996   MSG_error_t rv;
997   m_task_t task = NULL;
998   m_host_t host = NULL;
999   jobject jtask_global, jtask_local;
1000   const char *alias;
1001
1002   if (jhost) {
1003     host = jhost_get_native(env, jhost);
1004
1005     if (!host) {
1006       jxbt_throw_notbound(env, "host", jhost);
1007       return NULL;
1008     }
1009   }
1010
1011   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1012
1013   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1014   jtask_global = MSG_task_get_data(task);
1015
1016   /* Convert the global ref into a local ref so that the JVM can free the stuff */
1017   jtask_local = (*env)->NewLocalRef(env, jtask_global);
1018   (*env)->DeleteGlobalRef(env, jtask_global);
1019   MSG_task_set_data(task, NULL);
1020
1021   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1022
1023   jxbt_check_res("MSG_task_receive_ext()", rv,
1024                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1025                  bprintf("while receiving from mailbox %s", alias));
1026
1027   return (jobject) jtask_local;
1028 }
1029
1030 JNIEXPORT jboolean JNICALL
1031 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1032                                       jstring jalias)
1033 {
1034
1035   const char *alias;
1036   int rv;
1037
1038   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1039
1040   rv = MSG_task_listen(alias);
1041
1042   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1043
1044   return (jboolean) rv;
1045 }
1046
1047 JNIEXPORT jint JNICALL
1048 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1049                                               jstring jalias,
1050                                               jobject jhost)
1051 {
1052   int rv;
1053   const char *alias;
1054
1055   m_host_t host = jhost_get_native(env, jhost);
1056
1057   if (!host) {
1058     jxbt_throw_notbound(env, "host", jhost);
1059     return -1;
1060   }
1061   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1062
1063   rv = MSG_task_listen_from_host(alias, host);
1064
1065   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1066
1067   return (jint) rv;
1068 }
1069
1070 JNIEXPORT jint JNICALL
1071 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1072                                           jstring jalias)
1073 {
1074
1075   int rv;
1076   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1077
1078   rv = MSG_task_listen_from(alias);
1079
1080   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1081
1082   return (jint) rv;
1083 }
1084
1085 JNIEXPORT void JNICALL
1086 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1087                                        jstring jdeploymentFile)
1088 {
1089
1090   const char *deploymentFile =
1091       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1092
1093   surf_parse_reset_callbacks();
1094
1095   surfxml_add_callback(STag_surfxml_process_cb_list,
1096                        japplication_handler_on_begin_process);
1097
1098   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1099                        japplication_handler_on_process_arg);
1100
1101   surfxml_add_callback(STag_surfxml_prop_cb_list,
1102                        japplication_handler_on_property);
1103
1104   surfxml_add_callback(ETag_surfxml_process_cb_list,
1105                        japplication_handler_on_end_process);
1106
1107   surf_parse_open(deploymentFile);
1108
1109   japplication_handler_on_start_document();
1110
1111   if (surf_parse())
1112     jxbt_throw_jni(env, "surf_parse() failed");
1113
1114   surf_parse_close();
1115
1116   japplication_handler_on_end_document();
1117
1118   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1119 }