Logo AND Algorithmique Numérique Distribuée

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