Logo AND Algorithmique Numérique Distribuée

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