Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Allow task name to be null
[simgrid.git] / src / java / jmsg.c
1 /*
2  * $Id$
3  *
4  * Copyright 2006,2007 Martin Quinson, Malek Cherier All right 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  * This contains the implementation of the wrapper functions used to interface
10  * the java object with the native functions of the MSG API.
11  */
12 #include "msg/msg.h"
13 #include "msg/private.h"
14 #include "simix/private.h"
15 #include "simix/smx_context_java.h"
16
17 #include "jmsg_process.h"
18 #include "jmsg_host.h"
19 #include "jmsg_task.h"
20 #include "jmsg_application_handler.h"
21 #include "jxbt_utilities.h"
22
23
24 #include "jmsg.h"
25
26 #include "msg/mailbox.h"
27
28 #include "surf/surfxml_parse.h"
29
30
31 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
32
33 static JavaVM *__java_vm = NULL;
34
35 static jobject native_to_java_process(m_process_t process);
36
37 JavaVM *get_java_VM(void)
38 {
39   return __java_vm;
40 }
41
42 JNIEnv *get_current_thread_env(void)
43 {
44   JNIEnv *env;
45
46   (*__java_vm)->AttachCurrentThread(__java_vm, (void **) &env, NULL);
47
48   return env;
49 }
50
51 static jobject native_to_java_process(m_process_t process)
52 {
53   return ((smx_ctx_java_t)
54           (process->simdata->s_process->context))->jprocess;
55 }
56
57
58 /*
59  * The MSG process connected functions implementation.                                 
60  */
61
62 JNIEXPORT void JNICALL
63 Java_simgrid_msg_MsgNative_processCreate(JNIEnv * env, jclass cls,
64                                          jobject jprocess_arg, jobject jhost)
65 {
66   jobject jprocess;             /* the global reference to the java process instance    */
67   jstring jname;                /* the name of the java process instance                */
68   const char *name;             /* the C name of the process                            */
69   m_process_t process;          /* the native process to create                         */
70   char alias[MAX_ALIAS_NAME + 1] = { 0 };
71   msg_mailbox_t mailbox;
72
73   DEBUG4
74     ("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
75      env, cls, jprocess_arg, jhost);
76   /* get the name of the java process */
77   jname = jprocess_get_name(jprocess_arg, env);
78
79   if (!jname) {
80     jxbt_throw_null(env,
81                     xbt_strdup
82                     ("Internal error: Process name cannot be NULL"));
83     return;
84   }
85
86   /* allocate the data of the simulation */
87   process = xbt_new0(s_m_process_t, 1);
88   process->simdata = xbt_new0(s_simdata_process_t, 1);
89
90   /* create a global java process instance */
91   jprocess = jprocess_new_global_ref(jprocess_arg, env);
92
93   if (!jprocess) {
94     free(process->simdata);
95     free(process);
96     jxbt_throw_jni(env, "Can't get a global ref to the java process");
97     return;
98   }
99
100   /* bind the java process instance to the native process */
101   jprocess_bind(jprocess, process, env);
102
103   /* build the C name of the process */
104   name = (*env)->GetStringUTFChars(env, jname, 0);
105   process->name = xbt_strdup(name);
106   (*env)->ReleaseStringUTFChars(env, jname, name);
107
108   process->simdata->m_host = jhost_get_native(env, jhost);
109
110
111   if (!(process->simdata->m_host)) {    /* not binded */
112     free(process->simdata);
113     free(process->data);
114     free(process);
115     jxbt_throw_notbound(env, "host", jhost);
116     return;
117   }
118   process->simdata->PID = msg_global->PID++;
119
120   /* create a new context */
121   DEBUG8
122     ("fill in process %s/%s (pid=%d) %p (sd=%p, host=%p, host->sd=%p); env=%p",
123      process->name, process->simdata->m_host->name, process->simdata->PID,
124      process, process->simdata, process->simdata->m_host,
125      process->simdata->m_host->simdata, env);
126
127   process->simdata->s_process = 
128     SIMIX_process_create(process->name, (xbt_main_func_t)jprocess, 
129                          /*data */ (void *) process,
130                          process->simdata->m_host->simdata->smx_host->name, 
131                          0, NULL, NULL);
132     
133   DEBUG1("context created (s_process=%p)", process->simdata->s_process);
134
135
136   if (SIMIX_process_self()) {   /* someone created me */
137     process->simdata->PPID = MSG_process_get_PID(SIMIX_process_self()->data);
138   } else {
139     process->simdata->PPID = -1;
140   }
141
142   process->simdata->last_errno = MSG_OK;
143
144   /* add the process to the list of the processes of the simulation */
145   xbt_fifo_unshift(msg_global->process_list, process);
146
147   sprintf(alias, "%s:%s", (process->simdata->m_host->simdata->smx_host)->name,
148           process->name);
149
150   mailbox = MSG_mailbox_new(alias);
151   
152 }
153
154 JNIEXPORT void JNICALL
155 Java_simgrid_msg_MsgNative_processSuspend(JNIEnv * env, jclass cls,
156                                           jobject jprocess)
157 {
158   m_process_t process = jprocess_to_native_process(jprocess, env);
159
160   if (!process) {
161     jxbt_throw_notbound(env, "process", jprocess);
162     return;
163   }
164
165   /* try to suspend the process */
166   if (MSG_OK != MSG_process_suspend(process))
167     jxbt_throw_native(env, xbt_strdup("MSG_process_suspend() failed"));
168 }
169
170 JNIEXPORT void JNICALL
171 Java_simgrid_msg_MsgNative_processResume(JNIEnv * env, jclass cls,
172                                          jobject jprocess)
173 {
174   m_process_t process = jprocess_to_native_process(jprocess, env);
175
176   if (!process) {
177     jxbt_throw_notbound(env, "process", jprocess);
178     return;
179   }
180
181   /* try to resume the process */
182   if (MSG_OK != MSG_process_resume(process))
183     jxbt_throw_native(env, xbt_strdup("MSG_process_resume() failed"));
184 }
185
186 JNIEXPORT jboolean JNICALL
187 Java_simgrid_msg_MsgNative_processIsSuspended(JNIEnv * env, jclass cls,
188                                               jobject jprocess)
189 {
190   m_process_t process = jprocess_to_native_process(jprocess, env);
191
192   if (!process) {
193     jxbt_throw_notbound(env, "process", jprocess);
194     return 0;
195   }
196
197   /* true is the process is suspended, false otherwise */
198   return (jboolean) MSG_process_is_suspended(process);
199 }
200
201 JNIEXPORT void JNICALL
202 Java_simgrid_msg_MsgNative_processKill(JNIEnv * env, jclass cls,
203                                        jobject jprocess)
204 {
205   /* get the native instances from the java ones */
206   m_process_t process = jprocess_to_native_process(jprocess, env);
207
208   if (!process) {
209     jxbt_throw_notbound(env, "process", jprocess);
210     return;
211   }
212
213   /* delete the global reference */
214   jprocess_delete_global_ref(native_to_java_process(process), env);
215
216   /* kill the native process (this wrapper is call by the destructor of the java 
217    * process instance)
218    */
219   MSG_process_kill(process);
220 }
221
222 JNIEXPORT jobject JNICALL
223 Java_simgrid_msg_MsgNative_processGetHost(JNIEnv * env, jclass cls,
224                                           jobject jprocess)
225 {
226   /* get the native instances from the java ones */
227   m_process_t process = jprocess_to_native_process(jprocess, env);
228   m_host_t host;
229
230   if (!process) {
231     jxbt_throw_notbound(env, "process", jprocess);
232     return NULL;
233   }
234
235   host = MSG_process_get_host(process);
236
237   if (!host->data) {
238     jxbt_throw_native(env, xbt_strdup("MSG_process_get_host() failed"));
239     return NULL;
240   }
241
242   /* return the global reference to the java host instance */
243   return (jobject) host->data;
244
245 }
246
247 JNIEXPORT jobject JNICALL
248 Java_simgrid_msg_MsgNative_processFromPID(JNIEnv * env, jclass cls, jint PID)
249 {
250   m_process_t process = MSG_process_from_PID(PID);
251
252   if (!process) {
253     jxbt_throw_process_not_found(env, bprintf("PID = %d", PID));
254     return NULL;
255   }
256
257   if (!native_to_java_process(process)) {
258     jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
259     return NULL;
260   }
261
262   return (jobject) (native_to_java_process(process));
263 }
264
265
266 JNIEXPORT jint JNICALL
267 Java_simgrid_msg_MsgNative_processGetPID(JNIEnv * env, jclass cls,
268                                          jobject jprocess)
269 {
270   m_process_t process = jprocess_to_native_process(jprocess, env);
271
272   if (!process) {
273     jxbt_throw_notbound(env, "process", jprocess);
274     return 0;
275   }
276
277   return (jint) MSG_process_get_PID(process);
278 }
279
280
281 JNIEXPORT jint JNICALL
282 Java_simgrid_msg_MsgNative_processGetPPID(JNIEnv * env, jclass cls,
283                                           jobject jprocess)
284 {
285   m_process_t process = jprocess_to_native_process(jprocess, env);
286
287   if (!process) {
288     jxbt_throw_notbound(env, "process", jprocess);
289     return 0;
290   }
291
292   return (jint) MSG_process_get_PPID(process);
293 }
294
295 JNIEXPORT jobject JNICALL
296 Java_simgrid_msg_MsgNative_processSelf(JNIEnv * env, jclass cls)
297 {
298   m_process_t process = MSG_process_self();
299   jobject jprocess;
300
301   if (!process) {
302     jxbt_throw_native(env, xbt_strdup("MSG_process_self() failed"));
303     return NULL;
304   }
305
306   jprocess = native_to_java_process(process);
307
308   if (!jprocess)
309     jxbt_throw_native(env, xbt_strdup("SIMIX_process_get_jprocess() failed"));
310
311   return jprocess;
312 }
313
314
315 JNIEXPORT jint JNICALL
316 Java_simgrid_msg_MsgNative_processSelfPID(JNIEnv * env, jclass cls)
317 {
318   return (jint) MSG_process_self_PID();
319 }
320
321
322 JNIEXPORT jint JNICALL
323 Java_simgrid_msg_MsgNative_processSelfPPID(JNIEnv * env, jclass cls)
324 {
325   return (jint) MSG_process_self_PPID();
326 }
327
328 JNIEXPORT void JNICALL
329 Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv * env, jclass cls,
330                                              jobject jhost)
331 {
332   m_host_t host = jhost_get_native(env, jhost);
333
334   if (!host) {
335     jxbt_throw_notbound(env, "host", jhost);
336     return;
337   }
338
339   /* try to change the host of the process */
340   if (MSG_OK != MSG_process_change_host(host))
341     jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
342 }
343
344 JNIEXPORT void JNICALL
345 Java_simgrid_msg_MsgNative_processWaitFor(JNIEnv * env, jclass cls,
346                                           jdouble seconds)
347 {
348   if (MSG_OK != MSG_process_sleep((double) seconds))
349     jxbt_throw_native(env,
350                       bprintf("MSG_process_change_host(%f) failed",
351                               (double) seconds));
352 }
353
354
355 /***************************************************************************************
356  * The MSG host connected functions implementation.                                    *
357  ***************************************************************************************/
358
359 JNIEXPORT jobject JNICALL
360 Java_simgrid_msg_MsgNative_hostGetByName(JNIEnv * env, jclass cls,
361                                          jstring jname)
362 {
363   m_host_t host;                /* native host                                          */
364   jobject jhost;                /* global reference to the java host instance returned  */
365
366   /* get the C string from the java string */
367   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
368
369   /* get the host by name       (the hosts are created during the grid resolution) */
370   host = MSG_get_host_by_name(name);
371   DEBUG2("MSG gave %p as native host (simdata=%p)", host, host->simdata);
372
373   (*env)->ReleaseStringUTFChars(env, jname, name);
374
375   if (!host) {                  /* invalid name */
376     jxbt_throw_host_not_found(env, name);
377     return NULL;
378   }
379
380   if (!host->data) {            /* native host not associated yet with java host */
381
382     /* instanciate a new java host */
383     jhost = jhost_new_instance(env);
384
385     if (!jhost) {
386       jxbt_throw_jni(env, "java host instantiation failed");
387       return NULL;
388     }
389
390     /* get a global reference to the newly created host */
391     jhost = jhost_ref(env, jhost);
392
393     if (!jhost) {
394       jxbt_throw_jni(env, "new global ref allocation failed");
395       return NULL;
396     }
397
398     /* bind the java host and the native host */
399     jhost_bind(jhost, host, env);
400
401     /* the native host data field is set with the global reference to the 
402      * java host returned by this function 
403      */
404     host->data = (void *) jhost;
405   }
406
407   /* return the global reference to the java host instance */
408   return (jobject) host->data;
409 }
410
411 JNIEXPORT jstring JNICALL
412 Java_simgrid_msg_MsgNative_hostGetName(JNIEnv * env, jclass cls,
413                                        jobject jhost)
414 {
415   m_host_t host = jhost_get_native(env, jhost);
416
417   if (!host) {
418     jxbt_throw_notbound(env, "host", jhost);
419     return NULL;
420   }
421
422   return (*env)->NewStringUTF(env, host->name);
423 }
424
425 JNIEXPORT jint JNICALL
426 Java_simgrid_msg_MsgNative_hostGetNumber(JNIEnv * env, jclass cls)
427 {
428   return (jint) MSG_get_host_number();
429 }
430
431 JNIEXPORT jobject JNICALL
432 Java_simgrid_msg_MsgNative_hostSelf(JNIEnv * env, jclass cls)
433 {
434   jobject jhost;
435
436   m_host_t host = MSG_host_self();
437
438   if (!host->data) {
439     /* the native host not yet associated with the java host instance */
440
441     /* instanciate a new java host instance */
442     jhost = jhost_new_instance(env);
443
444     if (!jhost) {
445       jxbt_throw_jni(env, "java host instantiation failed");
446       return NULL;
447     }
448
449     /* get a global reference to the newly created host */
450     jhost = jhost_ref(env, jhost);
451
452     if (!jhost) {
453       jxbt_throw_jni(env, "global ref allocation failed");
454       return NULL;
455     }
456
457     /* Bind & store it */
458     jhost_bind(jhost, host, env);
459     host->data = (void *) jhost;
460   } else {
461     jhost = (jobject) host->data;
462   }
463
464   return jhost;
465 }
466
467 JNIEXPORT jdouble JNICALL
468 Java_simgrid_msg_MsgNative_hostGetSpeed(JNIEnv * env, jclass cls,
469                                         jobject jhost)
470 {
471   m_host_t host = jhost_get_native(env, jhost);
472
473   if (!host) {
474     jxbt_throw_notbound(env, "host", jhost);
475     return -1;
476   }
477
478   return (jdouble) MSG_get_host_speed(host);
479 }
480
481 JNIEXPORT jint JNICALL
482 Java_simgrid_msg_MsgNative_hostGetLoad(JNIEnv * env, jclass cls,
483                                        jobject jhost)
484 {
485   m_host_t host = jhost_get_native(env, jhost);
486
487   if (!host) {
488     jxbt_throw_notbound(env, "host", jhost);
489     return -1;
490   }
491
492   return (jint) MSG_get_host_msgload(host);
493 }
494
495
496 JNIEXPORT jboolean JNICALL
497 Java_simgrid_msg_MsgNative_hostIsAvail(JNIEnv * env, jclass cls,
498                                        jobject jhost)
499 {
500   m_host_t host = jhost_get_native(env, jhost);
501
502   if (!host) {
503     jxbt_throw_notbound(env, "host", jhost);
504     return 0;
505   }
506
507   return (jboolean) MSG_host_is_avail(host);
508 }
509
510
511 /***************************************************************************************
512  * The MSG task connected functions implementation.                                    *
513  ***************************************************************************************/
514
515 JNIEXPORT void JNICALL
516 Java_simgrid_msg_MsgNative_taskCreate(JNIEnv * env, jclass cls, jobject jtask,
517                                       jstring jname, jdouble jcomputeDuration,
518                                       jdouble jmessageSize)
519 {
520   m_task_t task;                /* the native task to create                            */
521   const char *name=NULL;        /* the name of the task                                 */
522
523   if (jcomputeDuration < 0) {
524     jxbt_throw_illegal(env,
525                        bprintf("Task ComputeDuration (%f) cannot be negative",
526                                (double) jcomputeDuration));
527     return;
528   }
529
530   if (jmessageSize < 0) {
531     jxbt_throw_illegal(env,
532                        bprintf("Task MessageSize (%f) cannot be negative",
533                                (double) jmessageSize));
534     return;
535   }
536
537   if (jname) {
538     /* get the C string from the java string */
539     name = (*env)->GetStringUTFChars(env, jname, 0);
540   }
541
542
543   /* create the task */
544   task =
545     MSG_task_create(name, (double) jcomputeDuration, (double) jmessageSize,
546                     NULL);
547
548   if (jname)
549     (*env)->ReleaseStringUTFChars(env, jname, name);
550
551   /* bind & store the task */
552   jtask_bind(jtask, task, env);
553
554   /* allocate a new global reference to the java task instance */
555   task->data = (void *) jtask_new_global_ref(jtask, env);
556
557   if (!task->data)
558     jxbt_throw_jni(env, "global ref allocation failed");
559
560 }
561
562 JNIEXPORT void JNICALL
563 Java_simgrid_msg_MsgNative_parallel_taskCreate(JNIEnv * env, jclass cls,
564                                                jobject jtask, jstring jname,
565                                                jobjectArray jhosts,
566                                                jdoubleArray
567                                                jcomputeDurations_arg,
568                                                jdoubleArray jmessageSizes_arg)
569 {
570
571   m_task_t task;                /* the native parallel task to create           */
572   const char *name;             /* the name of the task                         */
573   int host_count;
574   m_host_t *hosts;
575   double *computeDurations;
576   double *messageSizes;
577   jdouble *jcomputeDurations;
578   jdouble *jmessageSizes;
579
580   jobject jhost;
581   int index;
582
583
584   if (!jcomputeDurations_arg) {
585     jxbt_throw_null(env,
586                     xbt_strdup
587                     ("Parallel task compute durations cannot be null"));
588     return;
589   }
590
591   if (!jmessageSizes_arg) {
592     jxbt_throw_null(env,
593                     xbt_strdup("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 = (*env)->GetDoubleArrayElements(env, jmessageSizes_arg, 0);
612
613   for (index = 0; index < host_count; index++) {
614     jhost = (*env)->GetObjectArrayElement(env, jhosts, index);
615     hosts[index] = jhost_get_native(env, jhost);
616     computeDurations[index] = jcomputeDurations[index];
617   }
618   for (index = 0; index < host_count * host_count; index++) {
619     messageSizes[index] = jmessageSizes[index];
620   }
621
622   (*env)->ReleaseDoubleArrayElements(env, jcomputeDurations_arg,
623                                      jcomputeDurations, 0);
624   (*env)->ReleaseDoubleArrayElements(env, jmessageSizes_arg, jmessageSizes,
625                                      0);
626
627
628   /* get the C string from the java string */
629   name = (*env)->GetStringUTFChars(env, jname, 0);
630
631   task =
632     MSG_parallel_task_create(name, host_count, hosts, computeDurations,
633                              messageSizes, NULL);
634
635   (*env)->ReleaseStringUTFChars(env, jname, name);
636
637   /* associate the java task object and the native task */
638   jtask_bind(jtask, task, env);
639
640   task->data = (void *) jtask;
641
642   if (!task->data)
643     jxbt_throw_jni(env, "global ref allocation failed");
644 }
645
646 JNIEXPORT jobject JNICALL
647 Java_simgrid_msg_MsgNative_taskGetSender(JNIEnv * env, jclass cls,
648                                          jobject jtask)
649 {
650   m_process_t process;
651
652   m_task_t task = jtask_to_native_task(jtask, env);
653
654   if (!task) {
655     jxbt_throw_notbound(env, "task", jtask);
656     return NULL;
657   }
658
659   process = MSG_task_get_sender(task);
660   return (jobject) native_to_java_process(process);
661 }
662
663 JNIEXPORT jobject JNICALL
664 Java_simgrid_msg_MsgNative_taskGetSource(JNIEnv * env, jclass cls,
665                                          jobject jtask)
666 {
667   m_host_t host;
668   m_task_t task = jtask_to_native_task(jtask, env);
669
670   if (!task) {
671     jxbt_throw_notbound(env, "task", jtask);
672     return NULL;
673   }
674
675   host = MSG_task_get_source(task);
676
677   if (!host->data) {
678     jxbt_throw_native(env, xbt_strdup("MSG_task_get_source() failed"));
679     return NULL;
680   }
681
682   return (jobject) host->data;
683 }
684
685
686 JNIEXPORT jstring JNICALL
687 Java_simgrid_msg_MsgNative_taskGetName(JNIEnv * env, jclass cls,
688                                        jobject jtask)
689 {
690   m_task_t task = jtask_to_native_task(jtask, env);
691
692   if (!task) {
693     jxbt_throw_notbound(env, "task", jtask);
694     return NULL;
695   }
696
697   return (*env)->NewStringUTF(env, task->name);
698 }
699
700 JNIEXPORT void JNICALL
701 Java_simgrid_msg_MsgNative_taskCancel(JNIEnv * env, jclass cls, jobject jtask)
702 {
703   m_task_t ptask = jtask_to_native_task(jtask, env);
704
705   if (!ptask) {
706     jxbt_throw_notbound(env, "task", jtask);
707     return;
708   }
709
710   if (MSG_OK != MSG_task_cancel(ptask))
711     jxbt_throw_native(env, xbt_strdup("MSG_task_cancel() failed"));
712 }
713
714 JNIEXPORT jdouble JNICALL
715 Java_simgrid_msg_MsgNative_taskGetComputeDuration(JNIEnv * env, jclass cls,
716                                                   jobject jtask)
717 {
718   m_task_t ptask = jtask_to_native_task(jtask, env);
719
720   if (!ptask) {
721     jxbt_throw_notbound(env, "task", jtask);
722     return -1;
723   }
724   return (jdouble) MSG_task_get_compute_duration(ptask);
725 }
726
727 JNIEXPORT jdouble JNICALL
728 Java_simgrid_msg_MsgNative_taskGetRemainingDuration(JNIEnv * env, jclass cls,
729                                                     jobject jtask)
730 {
731   m_task_t ptask = jtask_to_native_task(jtask, env);
732
733   if (!ptask) {
734     jxbt_throw_notbound(env, "task", jtask);
735     return -1;
736   }
737   return (jdouble) MSG_task_get_remaining_computation(ptask);
738 }
739
740 JNIEXPORT void JNICALL
741 Java_simgrid_msg_MsgNative_taskSetPriority(JNIEnv * env, jclass cls,
742                                            jobject jtask, jdouble priority)
743 {
744   m_task_t task = jtask_to_native_task(jtask, env);
745
746   if (!task) {
747     jxbt_throw_notbound(env, "task", jtask);
748     return;
749   }
750   MSG_task_set_priority(task, (double) priority);
751 }
752
753 JNIEXPORT void JNICALL
754 Java_simgrid_msg_MsgNative_taskDestroy(JNIEnv * env, jclass cls,
755                                        jobject jtask_arg)
756 {
757
758   /* get the native task */
759   m_task_t task = jtask_to_native_task(jtask_arg, env);
760   jobject jtask;
761
762   if (!task) {
763     jxbt_throw_notbound(env, "task", task);
764     return;
765   }
766   jtask = (jobject) task->data;
767
768   if (MSG_OK != MSG_task_destroy(task))
769     jxbt_throw_native(env, xbt_strdup("MSG_task_destroy() failed"));
770
771   /* delete the global reference to the java task object */
772   jtask_delete_global_ref(jtask, env);
773 }
774
775 JNIEXPORT void JNICALL
776 Java_simgrid_msg_MsgNative_taskExecute(JNIEnv * env, jclass cls,
777                                        jobject jtask)
778 {
779   m_task_t task = jtask_to_native_task(jtask, env);
780
781   if (!task) {
782     jxbt_throw_notbound(env, "task", jtask);
783     return;
784   }
785
786   if (MSG_OK != MSG_task_execute(task))
787     jxbt_throw_native(env, xbt_strdup("MSG_task_execute() failed"));
788 }
789
790 /***************************************************************************************
791  * Unsortable functions                                                        *
792  ***************************************************************************************/
793
794
795 JNIEXPORT jint JNICALL
796 Java_simgrid_msg_Msg_getErrCode(JNIEnv * env, jclass cls)
797 {
798   return (jint) MSG_get_errno();
799 }
800
801 JNIEXPORT jdouble JNICALL
802 Java_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
803 {
804   return (jdouble) MSG_get_clock();
805 }
806
807
808 JNIEXPORT void JNICALL
809 Java_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs) {
810   char **argv = NULL;
811   int index;
812   int argc = 0;
813   jstring jval;
814   const char *tmp;
815
816   if (jargs)
817     argc = (int) (*env)->GetArrayLength(env, jargs);
818
819   argc++;
820   argv = xbt_new0(char *, argc);
821   argv[0] = strdup("java");
822
823   for (index = 0; index < argc - 1; index++) {
824     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
825     tmp = (*env)->GetStringUTFChars(env, jval, 0);
826     argv[index + 1] = strdup(tmp);
827     (*env)->ReleaseStringUTFChars(env, jval, tmp);
828   }
829
830   MSG_global_init(&argc, argv);
831   SIMIX_context_select_factory("java");
832
833   for (index = 0; index < argc; index++)
834     free(argv[index]);
835
836   free(argv);
837
838   (*env)->GetJavaVM(env, &__java_vm);
839 }
840
841 JNIEXPORT void JNICALL
842   JNICALL Java_simgrid_msg_Msg_run(JNIEnv * env, jclass cls) {
843   xbt_fifo_item_t item = NULL;
844   m_host_t host = NULL;
845   jobject jhost;
846
847   /* Run everything */
848   if (MSG_OK != MSG_main()) {
849     jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
850   }
851   DEBUG0
852     ("MSG_main finished. Bail out before cleanup since there is a bug in this part.");
853
854   DEBUG0("Clean java world");
855   /* Cleanup java hosts */
856   xbt_fifo_foreach(msg_global->host, item, host, m_host_t) {
857     jhost = (jobject) host->data;
858
859     if (jhost)
860       jhost_unref(env, jhost);
861   }
862
863   DEBUG0("Clean native world");
864   /* cleanup native stuff */
865   if (MSG_OK != MSG_clean()){
866     jxbt_throw_native(env, xbt_strdup("MSG_main() failed"));
867   }
868 }
869
870 JNIEXPORT jint JNICALL
871 Java_simgrid_msg_MsgNative_processKillAll(JNIEnv * env, jclass cls,
872                                           jint jresetPID)
873 {
874   return (jint) MSG_process_killall((int) jresetPID);
875 }
876
877 JNIEXPORT void JNICALL
878 Java_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
879                                        jstring jplatformFile)
880 {
881
882   const char *platformFile = (*env)->GetStringUTFChars(env, jplatformFile, 0);
883
884   MSG_create_environment(platformFile);
885
886   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
887 }
888
889 JNIEXPORT void JNICALL
890 Java_simgrid_msg_MsgNative_processExit(JNIEnv * env, jclass cls,
891                                        jobject jprocess)
892 {
893
894   m_process_t process = jprocess_to_native_process(jprocess, env);
895
896   if (!process) {
897     jxbt_throw_notbound(env, "process", jprocess);
898     return;
899   }
900
901   SIMIX_context_stop(SIMIX_process_self()->context);
902 }
903
904 JNIEXPORT void JNICALL
905 Java_simgrid_msg_Msg_info(JNIEnv * env, jclass cls, jstring js)
906 {
907   const char *s = (*env)->GetStringUTFChars(env, js, 0);
908   INFO1("%s", s);
909   (*env)->ReleaseStringUTFChars(env, js, s);
910 }
911
912 JNIEXPORT jobjectArray JNICALL
913 Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg)
914 {
915   int index;
916   jobjectArray jtable;
917   jobject jhost;
918   jstring jname;
919   m_host_t host;
920
921   int count = xbt_fifo_size(msg_global->host);
922   m_host_t *table = (m_host_t *) xbt_fifo_to_array(msg_global->host);
923
924   jclass cls = jxbt_get_class(env, "simgrid/msg/Host");
925
926   if (!cls) {
927     return NULL;
928   }
929
930   jtable = (*env)->NewObjectArray(env, (jsize) count, cls, NULL);
931
932   if (!jtable) {
933     jxbt_throw_jni(env, "Hosts table allocation failed");
934     return NULL;
935   }
936
937   for (index = 0; index < count; index++) {
938     host = table[index];
939     jhost = (jobject) (host->data);
940
941     if (!jhost) {
942       jname = (*env)->NewStringUTF(env, host->name);
943
944       jhost = Java_simgrid_msg_MsgNative_hostGetByName(env, cls_arg, jname);
945       /* FIXME: leak of jname ? */
946     }
947
948     (*env)->SetObjectArrayElement(env, jtable, index, jhost);
949   }
950
951   return jtable;
952 }
953
954
955 JNIEXPORT void JNICALL
956 Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,
957                                                 jstring jname)
958 {
959   char *errmsg = NULL;
960   xbt_ex_t e;
961
962   /* get the C string from the java string */
963   const char *name = (*env)->GetStringUTFChars(env, jname, 0);
964
965   TRY {
966     SIMIX_context_select_factory(name);
967   } CATCH(e) {
968     errmsg = xbt_strdup(e.msg);
969     xbt_ex_free(e);
970   }
971
972   (*env)->ReleaseStringUTFChars(env, jname, name);
973
974   if (errmsg) {
975     char *thrown = bprintf("xbt_select_context_factory() failed: %s", errmsg);
976     free(errmsg);
977     jxbt_throw_native(env, thrown);
978   }
979 }
980
981 JNIEXPORT void JNICALL
982 Java_simgrid_msg_MsgNative_taskSend(JNIEnv * env, jclass cls,
983                                     jstring jalias, jobject jtask,
984                                     jdouble jtimeout)
985 {
986
987   MSG_error_t rv;
988   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
989
990   m_task_t task = jtask_to_native_task(jtask, env);
991
992
993   if (!task) {
994     (*env)->ReleaseStringUTFChars(env, jalias, alias);
995     jxbt_throw_notbound(env, "task", jtask);
996     return;
997   }
998
999   rv = MSG_task_send_with_timeout(task, alias, (double) jtimeout);
1000
1001   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1002
1003   if (MSG_OK != rv)
1004     jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
1005
1006 }
1007
1008 JNIEXPORT void JNICALL
1009 Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv * env, jclass cls,
1010                                            jstring jalias, jobject jtask,
1011                                            jdouble jmaxRate)
1012 {
1013   m_task_t task = jtask_to_native_task(jtask, env);
1014   MSG_error_t rv;
1015   const char *alias;
1016
1017   if (!task) {
1018     jxbt_throw_notbound(env, "task", jtask);
1019     return;
1020   }
1021
1022   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1023
1024   rv = MSG_task_send_bounded(task, alias, (double) jmaxRate);
1025
1026   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1027
1028   if (MSG_OK != rv)
1029     jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
1030 }
1031
1032 JNIEXPORT jobject JNICALL
1033 Java_simgrid_msg_MsgNative_taskReceive(JNIEnv * env, jclass cls,
1034                                        jstring jalias, jdouble jtimeout,
1035                                        jobject jhost)
1036 {
1037   MSG_error_t rv;
1038   m_task_t task = NULL;
1039   m_host_t host = NULL;
1040   const char *alias;
1041
1042   if (jhost) {
1043     host = jhost_get_native(env, jhost);
1044
1045     if (!host) {
1046       jxbt_throw_notbound(env, "host", jhost);
1047       return NULL;
1048     }
1049   }
1050
1051   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1052
1053   rv = MSG_task_receive_ext(&task, alias, (double) jtimeout, host);
1054
1055   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1056
1057   if (MSG_OK != rv) {
1058     jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
1059     return NULL;
1060   }
1061
1062   return (jobject) task->data;
1063 }
1064
1065 JNIEXPORT jboolean JNICALL
1066 Java_simgrid_msg_MsgNative_taskListen(JNIEnv * env, jclass cls,
1067                                       jstring jalias)
1068 {
1069
1070   const char *alias;
1071   int rv;
1072
1073   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1074
1075   rv = MSG_task_listen(alias);
1076
1077   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1078
1079   return (jboolean) rv;
1080 }
1081
1082 JNIEXPORT jint JNICALL
1083 Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv * env, jclass cls,
1084                                               jstring jalias, jobject jhost)
1085 {
1086
1087   int rv;
1088   const char *alias;
1089
1090   m_host_t host = jhost_get_native(env, jhost);
1091
1092   if (!host) {
1093     jxbt_throw_notbound(env, "host", jhost);
1094     return -1;
1095   }
1096
1097   alias = (*env)->GetStringUTFChars(env, jalias, 0);
1098
1099   rv = MSG_task_listen_from_host(alias, host);
1100
1101   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1102
1103   return (jint) rv;
1104 }
1105
1106 JNIEXPORT jint JNICALL
1107 Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv * env, jclass cls,
1108                                           jstring jalias)
1109 {
1110
1111   int rv;
1112   const char *alias = (*env)->GetStringUTFChars(env, jalias, 0);
1113
1114   rv = MSG_task_listen_from(alias);
1115
1116   (*env)->ReleaseStringUTFChars(env, jalias, alias);
1117
1118   return (jint) rv;
1119 }
1120
1121 JNIEXPORT void JNICALL
1122 Java_simgrid_msg_Msg_deployApplication(JNIEnv * env, jclass cls,
1123                                        jstring jdeploymentFile)
1124 {
1125
1126   const char *deploymentFile =
1127     (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
1128
1129   surf_parse_reset_parser();
1130
1131   surfxml_add_callback(STag_surfxml_process_cb_list,
1132                        japplication_handler_on_begin_process);
1133
1134   surfxml_add_callback(ETag_surfxml_argument_cb_list,
1135                        japplication_handler_on_process_arg);
1136
1137   surfxml_add_callback(STag_surfxml_prop_cb_list,
1138                        japplication_handler_on_property);
1139
1140   surfxml_add_callback(ETag_surfxml_process_cb_list,
1141                        japplication_handler_on_end_process);
1142
1143   surf_parse_open(deploymentFile);
1144
1145   japplication_handler_on_start_document();
1146
1147   if (surf_parse())
1148     jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
1149
1150   surf_parse_close();
1151
1152   japplication_handler_on_end_document();
1153
1154   (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
1155 }