Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Process kills now seems to work. May need some additional checking ...
[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   return (jint) MSG_get_host_number();
390 }
391
392 JNIEXPORT jobject JNICALL
393 Java_org_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
394 {
395   jobject jhost;
396
397   m_host_t host = MSG_host_self();
398
399   if (!MSG_host_get_data(host)) {
400     /* the native host not yet associated with the java host instance */
401
402     /* instanciate a new java host instance */
403     jhost = jhost_new_instance(env);
404
405     if (!jhost) {
406       jxbt_throw_jni(env, "java host instantiation failed");
407       return NULL;
408     }
409
410     /* get a global reference to the newly created host */
411     jhost = jhost_ref(env, jhost);
412
413     if (!jhost) {
414       jxbt_throw_jni(env, "global ref allocation failed");
415       return NULL;
416     }
417
418     /* Bind & store it */
419     jhost_bind(jhost, host, env);
420     MSG_host_set_data(host, (void *) jhost);
421   } else {
422     jhost = (jobject) MSG_host_get_data(host);
423   }
424
425   return jhost;
426 }
427
428 JNIEXPORT jdouble JNICALL
429 Java_org_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
430                                         jobject jhost)
431 {
432   m_host_t host = jhost_get_native(env, jhost);
433
434   if (!host) {
435     jxbt_throw_notbound(env, "host", jhost);
436     return -1;
437   }
438
439   return (jdouble) MSG_get_host_speed(host);
440 }
441
442 JNIEXPORT jint JNICALL
443 Java_org_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
444                                        jobject jhost)
445 {
446   m_host_t host = jhost_get_native(env, jhost);
447
448   if (!host) {
449     jxbt_throw_notbound(env, "host", jhost);
450     return -1;
451   }
452
453   return (jint) MSG_get_host_msgload(host);
454 }
455
456
457 JNIEXPORT jboolean JNICALL
458 Java_org_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
459                                        jobject jhost)
460 {
461   m_host_t host = jhost_get_native(env, jhost);
462
463   if (!host) {
464     jxbt_throw_notbound(env, "host", jhost);
465     return 0;
466   }
467
468   return (jboolean) MSG_host_is_avail(host);
469 }
470
471
472 /***************************************************************************************
473  * The MSG task connected functions implementation.                                    *
474  ***************************************************************************************/
475
476 JNIEXPORT void JNICALL
477 Java_org_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls,
478                                       jobject jtask, jstring jname,
479                                       jdouble jcomputeDuration,
480                                       jdouble jmessageSize)
481 {
482   m_task_t task;                /* the native task to create                            */
483   const char *name = NULL;      /* the name of the task                                 */
484
485   if (jcomputeDuration < 0) {
486     jxbt_throw_illegal(env,
487                        bprintf
488                        ("Task ComputeDuration (%f) cannot be negative",
489                         (double) jcomputeDuration));
490     return;
491   }
492
493   if (jmessageSize < 0) {
494     jxbt_throw_illegal(env,
495                        bprintf("Task MessageSize (%f) cannot be negative",
496                                (double) jmessageSize));
497     return;
498   }
499
500   if (jname) {
501     /* get the C string from the java string */
502     name = (*env)->GetStringUTFChars(env, jname, 0);
503   }
504
505
506   /* create the task */
507   task =
508       MSG_task_create(name, (double) jcomputeDuration,
509                       (double) jmessageSize, NULL);
510
511   if (jname)
512     (*env)->ReleaseStringUTFChars(env, jname, name);
513
514   /* bind & store the task */
515   jtask_bind(jtask, task, env);
516   MSG_task_set_data(task, jtask);
517 }
518
519 JNIEXPORT void JNICALL
520 Java_org_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
521                                                jobject jtask,
522                                                jstring jname,
523                                                jobjectArray jhosts,
524                                                jdoubleArray
525                                                jcomputeDurations_arg,
526                                                jdoubleArray
527                                                jmessageSizes_arg)
528 {
529
530   m_task_t task;                /* the native parallel task to create           */
531   const char *name;             /* the name of the task                         */
532   int host_count;
533   m_host_t *hosts;
534   double *computeDurations;
535   double *messageSizes;
536   jdouble *jcomputeDurations;
537   jdouble *jmessageSizes;
538
539   jobject jhost;
540   int index;
541
542
543   if (!jcomputeDurations_arg) {
544     jxbt_throw_null(env,
545                     xbt_strdup
546                     ("Parallel task compute durations cannot be null"));
547     return;
548   }
549
550   if (!jmessageSizes_arg) {
551     jxbt_throw_null(env,
552                     xbt_strdup
553                     ("Parallel task message sizes cannot be null"));
554     return;
555   }
556
557   if (!jname) {
558     jxbt_throw_null(env, xbt_strdup("Parallel task name cannot be null"));
559     return;
560   }
561
562   host_count = (int) (*env)->GetArrayLength(env, jhosts);
563
564
565   hosts = xbt_new0(m_host_t, host_count);
566   computeDurations = xbt_new0(double, host_count);
567   messageSizes = xbt_new0(double, host_count * host_count);
568
569   jcomputeDurations =
570       (*env)->GetDoubleArrayElements(env, jcomputeDurations_arg, 0);
571   jmessageSizes =
572       (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
573
574   for (index = 0; index < host_count; index++) {
575     jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
576     hosts[index] = jhost_get_native(env, jhost);
577     computeDurations[index] = jcomputeDurations[index];
578   }
579   for (index = 0; index < host_count * host_count; index++) {
580     messageSizes[index] = jmessageSizes[index];
581   }
582
583   (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
584                                      jcomputeDurations, 0);
585   (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
586                                      0);
587
588
589   /* get the C string from the java string */
590   name = (*env)->GetStringUTFChars(env, jname, 0);
591
592   task =
593       MSG_parallel_task_create(name, host_count, hosts, computeDurations,
594                                messageSizes, NULL);
595
596   (*env)->ReleaseStringUTFChars(env, jname, name);
597
598   /* associate the java task object and the native task */
599   jtask_bind(jtask, task, env);
600
601   MSG_task_set_data(task, (void *) jtask);
602
603   if (!MSG_task_get_data(task))
604     jxbt_throw_jni(env, "global ref allocation failed");
605 }
606
607 JNIEXPORT jobject JNICALL
608 Java_org_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
609                                          jobject jtask)
610 {
611   m_process_t process;
612
613   m_task_t task = jtask_to_native_task(jtask, env);
614
615   if (!task) {
616     jxbt_throw_notbound(env, "task", jtask);
617     return NULL;
618   }
619
620   process = MSG_task_get_sender(task);
621   return (jobject) native_to_java_process(process);
622 }
623
624 JNIEXPORT jobject JNICALL
625 Java_org_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
626                                          jobject jtask)
627 {
628   m_host_t host;
629   m_task_t task = jtask_to_native_task(jtask, env);
630
631   if (!task) {
632     jxbt_throw_notbound(env, "task", jtask);
633     return NULL;
634   }
635
636   host = MSG_task_get_source(task);
637
638   if (!MSG_host_get_data(host)) {
639     jxbt_throw_jni(env, "MSG_task_get_source() failed");
640     return NULL;
641   }
642
643   return (jobject) MSG_host_get_data(host);
644 }
645
646
647 JNIEXPORT jstring JNICALL
648 Java_org_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
649                                        jobject jtask)
650 {
651   m_task_t task = jtask_to_native_task(jtask, env);
652
653   if (!task) {
654     jxbt_throw_notbound(env, "task", jtask);
655     return NULL;
656   }
657
658   return (*env)->NewStringUTF(env, MSG_task_get_name(task));
659 }
660
661 JNIEXPORT void JNICALL
662 Java_org_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls,
663                                       jobject jtask)
664 {
665   m_task_t ptask = jtask_to_native_task(jtask, env);
666
667   if (!ptask) {
668     jxbt_throw_notbound(env, "task", jtask);
669     return;
670   }
671
672   MSG_error_t rv = MSG_task_cancel(ptask);
673
674   jxbt_check_res("MSG_task_cancel()", rv, MSG_OK,
675                  bprintf("unexpected error , please report this bug"));
676 }
677
678 JNIEXPORT jdouble JNICALL
679 Java_org_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
680                                                   jobject jtask)
681 {
682   m_task_t ptask = jtask_to_native_task(jtask, env);
683
684   if (!ptask) {
685     jxbt_throw_notbound(env, "task", jtask);
686     return -1;
687   }
688   return (jdouble) MSG_task_get_compute_duration(ptask);
689 }
690
691 JNIEXPORT jdouble JNICALL
692 Java_org_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env,
693                                                     jclass cls,
694                                                     jobject jtask)
695 {
696   m_task_t ptask = jtask_to_native_task(jtask, env);
697
698   if (!ptask) {
699     jxbt_throw_notbound(env, "task", jtask);
700     return -1;
701   }
702   return (jdouble) MSG_task_get_remaining_computation(ptask);
703 }
704
705 JNIEXPORT void JNICALL
706 Java_org_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
707                                            jobject jtask, jdouble priority)
708 {
709   m_task_t task = jtask_to_native_task(jtask, env);
710
711   if (!task) {
712     jxbt_throw_notbound(env, "task", jtask);
713     return;
714   }
715   MSG_task_set_priority(task, (double) priority);
716 }
717
718 JNIEXPORT void JNICALL
719 Java_org_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
720                                        jobject jtask_arg)
721 {
722
723   /* get the native task */
724   m_task_t task = jtask_to_native_task(jtask_arg, env);
725
726   if (!task) {
727     jxbt_throw_notbound(env, "task", task);
728     return;
729   }
730
731   MSG_error_t rv = MSG_task_destroy(task);
732
733   jxbt_check_res("MSG_task_destroy()", rv, MSG_OK,
734                  bprintf("unexpected error , please report this bug"));
735 }
736
737 JNIEXPORT void JNICALL
738 Java_org_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
739                                        jobject jtask)
740 {
741   m_task_t task = jtask_to_native_task(jtask, env);
742
743   if (!task) {
744     jxbt_throw_notbound(env, "task", jtask);
745     return;
746   }
747
748   MSG_error_t rv = MSG_task_execute(task);
749
750   jxbt_check_res("MSG_task_execute()", rv,
751                  MSG_HOST_FAILURE | MSG_TASK_CANCELED,
752                  bprintf("while executing task %s",
753                          MSG_task_get_name(task)));
754 }
755
756 /***************************************************************************************
757  * Unsortable functions                                                        *
758  ***************************************************************************************/
759
760 JNIEXPORT jdouble JNICALL
761 Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
762 {
763   return (jdouble) MSG_get_clock();
764 }
765
766 JNIEXPORT void JNICALL
767 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
768 {
769   char **argv = NULL;
770   int index;
771   int argc = 0;
772   jstring jval;
773   const char *tmp;
774
775   smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
776
777   if (jargs)
778     argc = (int) (*env)->GetArrayLength(env, jargs);
779
780   argc++;
781   argv = xbt_new0(char *, argc);
782   argv[0] = strdup("java");
783
784   for (index = 0; index < argc - 1; index++) {
785     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
786     tmp = (*env)->GetStringUTFChars(env, jval, 0);
787     argv[index + 1] = strdup(tmp);
788     (*env)->ReleaseStringUTFChars(env, jval, tmp);
789   }
790
791   MSG_global_init(&argc, argv);
792
793   for (index = 0; index < argc; index++)
794     free(argv[index]);
795
796   free(argv);
797
798   (*env)->GetJavaVM(env, &__java_vm);
799 }
800
801 JNIEXPORT void JNICALL
802     JNICALL Java_org_simgrid_msg_Msg_run(JNIEnv * env, jclass cls)
803 {
804   MSG_error_t rv;
805   int index;
806   m_host_t *hosts;
807   jobject jhost;
808
809   /* Run everything */
810   XBT_INFO("Ready to run MSG_MAIN");
811   rv = MSG_main();
812   XBT_INFO("Done running MSG_MAIN");
813   jxbt_check_res("MSG_main()", rv, MSG_OK,
814                  bprintf
815                  ("unexpected error : MSG_main() failed .. please report this bug "));
816
817   XBT_INFO("MSG_main finished");
818
819   XBT_INFO("Clean java world");
820   /* Cleanup java hosts */
821   hosts = MSG_get_host_table();
822   for (index = 0; index < MSG_get_host_number() - 1; index++) {
823     jhost = (jobject) hosts[index]->data;
824     if (jhost)
825       jhost_unref(env, jhost);
826
827   }
828
829   XBT_INFO("Clean native world");
830 }
831 JNIEXPORT void JNICALL
832     JNICALL Java_org_simgrid_msg_Msg_clean(JNIEnv * env, jclass cls)
833 {
834   /* cleanup native stuff. Calling it is ... useless since leaking memory at the end of the simulation is a non-issue */
835   MSG_error_t rv = MSG_OK != MSG_clean();
836   jxbt_check_res("MSG_clean()", rv, MSG_OK,
837                  bprintf
838                  ("unexpected error : MSG_clean() failed .. please report this bug "));
839 }
840    
841 JNIEXPORT jint JNICALL
842 Java_org_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
843                                           jint jresetPID)
844 {
845   return (jint) MSG_process_killall((int) jresetPID);
846 }
847
848 JNIEXPORT void JNICALL
849 Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
850                                        jstring jplatformFile)
851 {
852
853   const char *platformFile =
854       (*env)->GetStringUTFChars(env, jplatformFile, 0);
855
856   MSG_create_environment(platformFile);
857
858   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
859 }
860
861 JNIEXPORT void JNICALL
862 Java_org_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
863                                        jobject jprocess)
864 {
865
866   m_process_t process = jprocess_to_native_process(jprocess, env);
867
868   if (!process) {
869     jxbt_throw_notbound(env, "process", jprocess);
870     return;
871   }
872
873   smx_ctx_java_stop(MSG_process_get_smx_ctx(process));
874 }
875
876 JNIEXPORT void JNICALL
877 Java_org_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
878 {
879   const char *s = (*env)->GetStringUTFChars(env, js, 0);
880   XBT_INFO("%s", s);
881   (*env)->ReleaseStringUTFChars(env, js, s);
882 }
883
884 JNIEXPORT jobjectArray JNICALL
885 Java_org_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
886 {
887   int index;
888   jobjectArray jtable;
889   jobject jhost;
890   jstring jname;
891   m_host_t host;
892
893   int count = MSG_get_host_number();
894   m_host_t *table = MSG_get_host_table();
895
896   jclass cls = jxbt_get_class(env, "org/simgrid/msg/Host");
897
898   if (!cls) {
899     return NULL;
900   }
901
902   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
903
904   if (!jtable) {
905     jxbt_throw_jni(env, "Hosts table allocation failed");
906     return NULL;
907   }
908
909   for (index = 0; index < count; index++) {
910     host = table[index];
911     jhost = (jobject) (MSG_host_get_data(host));
912
913     if (!jhost) {
914       jname = (*env)->NewStringUTF(env, MSG_host_get_name(host));
915
916       jhost =
917           Java_org_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
918       /* FIXME: leak of jname ? */
919     }
920
921     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
922   }
923
924   return jtable;
925 }
926
927 JNIEXPORT void JNICALL
928 Java_org_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
929                                     jstring jalias, jobject jtask,
930                                     jdouble jtimeout)
931 {
932
933   MSG_error_t rv;
934   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
935
936   m_task_t task = jtask_to_native_task(jtask, env);
937
938
939   if (!task) {
940     (*env)->ReleaseStringUTFChars(env, jalias, alias);
941     jxbt_throw_notbound(env, "task", jtask);
942     return;
943   }
944
945   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
946   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
947   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
948
949   (*env)->ReleaseStringUTFChars(env, jalias, alias);
950
951   jxbt_check_res("MSG_task_send_with_timeout()", rv,
952                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
953                  bprintf("while sending task %s to mailbox %s",
954                          MSG_task_get_name(task), alias));
955 }
956
957 JNIEXPORT void JNICALL
958 Java_org_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
959                                            jstring jalias, jobject jtask,
960                                            jdouble jmaxRate)
961 {
962   m_task_t task = jtask_to_native_task(jtask, env);
963   MSG_error_t rv;
964   const char *alias;
965
966   if (!task) {
967     jxbt_throw_notbound(env, "task", jtask);
968     return;
969   }
970
971   alias = (*env)->GetStringUTFChars(env, jalias, 0);
972
973   /* Pass a global ref to the Jtask into the Ctask so that the receiver can use it */
974   MSG_task_set_data(task, (void *) (*env)->NewGlobalRef(env, jtask));
975   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
976
977   (*env)->ReleaseStringUTFChars(env, jalias, alias);
978
979   jxbt_check_res("MSG_task_send_bounded()", rv,
980                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
981                  bprintf
982                  ("while sending task %s to mailbox %s with max rate %f",
983                   MSG_task_get_name(task), alias, (double) jmaxRate));
984
985 }
986
987 JNIEXPORT jobject JNICALL
988 Java_org_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
989                                        jstring jalias, jdouble jtimeout,
990                                        jobject jhost)
991 {
992   MSG_error_t rv;
993   m_task_t task = NULL;
994   m_host_t host = NULL;
995   jobject jtask_global, jtask_local;
996   const char *alias;
997
998   if (jhost) {
999     host = jhost_get_native(env, jhost);
1000
1001     if (!host) {
1002       jxbt_throw_notbound(env, "host", jhost);
1003       return NULL;
1004     }
1005   }
1006
1007   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1008
1009   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1010   jtask_global = MSG_task_get_data(task);
1011
1012   /* Convert the global ref into a local ref so that the JVM can free the stuff */
1013   jtask_local = (*env)->NewLocalRef(env, jtask_global);
1014   (*env)->DeleteGlobalRef(env, jtask_global);
1015   MSG_task_set_data(task, NULL);
1016
1017   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1018
1019   jxbt_check_res("MSG_task_receive_ext()", rv,
1020                  MSG_HOST_FAILURE | MSG_TRANSFER_FAILURE | MSG_TIMEOUT,
1021                  bprintf("while receiving from mailbox %s", alias));
1022
1023   return (jobject) jtask_local;
1024 }
1025
1026 JNIEXPORT jboolean JNICALL
1027 Java_org_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1028                                       jstring jalias)
1029 {
1030
1031   const char *alias;
1032   int rv;
1033
1034   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1035
1036   rv = MSG_task_listen(alias);
1037
1038   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1039
1040   return (jboolean) rv;
1041 }
1042
1043 JNIEXPORT jint JNICALL
1044 Java_org_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1045                                               jstring jalias,
1046                                               jobject jhost)
1047 {
1048   int rv;
1049   const char *alias;
1050
1051   m_host_t host = jhost_get_native(env, jhost);
1052
1053   if (!host) {
1054     jxbt_throw_notbound(env, "host", jhost);
1055     return -1;
1056   }
1057   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1058
1059   rv = MSG_task_listen_from_host(alias, host);
1060
1061   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1062
1063   return (jint) rv;
1064 }
1065
1066 JNIEXPORT jint JNICALL
1067 Java_org_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1068                                           jstring jalias)
1069 {
1070
1071   int rv;
1072   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1073
1074   rv = MSG_task_listen_from(alias);
1075
1076   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1077
1078   return (jint) rv;
1079 }
1080
1081 JNIEXPORT void JNICALL
1082 Java_org_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1083                                        jstring jdeploymentFile)
1084 {
1085
1086   const char *deploymentFile =
1087       (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1088
1089   surf_parse_reset_callbacks();
1090
1091   surfxml_add_callback(STag_surfxml_process_cb_list,
1092                        japplication_handler_on_begin_process);
1093
1094   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1095                        japplication_handler_on_process_arg);
1096
1097   surfxml_add_callback(STag_surfxml_prop_cb_list,
1098                        japplication_handler_on_property);
1099
1100   surfxml_add_callback(ETag_surfxml_process_cb_list,
1101                        japplication_handler_on_end_process);
1102
1103   surf_parse_open(deploymentFile);
1104
1105   japplication_handler_on_start_document();
1106
1107   if (surf_parse())
1108     jxbt_throw_jni(env, "surf_parse() failed");
1109
1110   surf_parse_close();
1111
1112   japplication_handler_on_end_document();
1113
1114   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1115 }