Logo AND Algorithmique Numérique Distribuée

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