Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / src / bindings / java / jmsg_comm.c
1 /* Functions related to the java comm instances                                                                                                                                 */
2
3 /* Copyright (c) 2012. The SimGrid Team. All rights reserved.                   */
4 #include "jmsg_comm.h"
5 #include "jxbt_utilities.h"
6 #include "jmsg.h"
7
8 #include <msg/msg.h>
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
10
11 static jfieldID jcomm_field_Comm_bind;
12 static jfieldID jcomm_field_Comm_finished;
13 static jfieldID jcomm_field_Comm_receiving;
14 static jfieldID jtask_field_Comm_task;
15 static jfieldID jcomm_field_Comm_taskBind;
16
17 void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
18   msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
19   //test if we are receiving or sending a task.
20   jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
21   if (jreceiving == JNI_TRUE) {
22     //bind the task object.
23     msg_task_t task = MSG_comm_get_task(comm);
24     xbt_assert(task != NULL, "Task is NULL");
25     jobject jtask_global = MSG_task_get_data(task);
26     //case where the data has already been retrieved
27     if (jtask_global == NULL) {
28       return;
29     }
30
31     //Make sure the data will be correctly gc.
32     jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
33     (*env)->DeleteGlobalRef(env, jtask_global);
34
35     (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
36
37     MSG_task_set_data(task, NULL);
38   }
39
40 }
41
42 JNIEXPORT void JNICALL
43 Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
44   jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
45   if (!jfield_class_Comm) {
46     jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
47     return;
48   }
49   jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
50   jcomm_field_Comm_taskBind  = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
51   jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
52   jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
53   jcomm_field_Comm_finished = jxbt_get_jfield(env, jfield_class_Comm, "finished", "Z");
54   if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task || !jcomm_field_Comm_finished) {
55     jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
56   }
57 }
58
59 JNIEXPORT void JNICALL
60 Java_org_simgrid_msg_Comm_destroy(JNIEnv *env, jobject jcomm) {
61   msg_comm_t comm;
62   msg_task_t *task_received;
63
64   task_received = (msg_task_t*)  (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
65   xbt_free(task_received);
66
67   comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
68   MSG_comm_destroy(comm);
69 }
70
71 JNIEXPORT jboolean JNICALL
72 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
73   msg_comm_t comm;
74   comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
75
76   jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
77   if (finished == JNI_TRUE) {
78     return JNI_TRUE;
79   }
80
81   if (!comm) {
82     jxbt_throw_native(env,bprintf("comm is null"));
83     return JNI_FALSE;
84   }
85
86   if (MSG_comm_test(comm)) {
87     msg_error_t status = MSG_comm_get_status(comm);
88     if (status == MSG_OK) {
89       jcomm_bind_task(env,jcomm);
90       return JNI_TRUE;
91     } else {
92       //send the correct exception
93       jmsg_throw_status(env,status);
94     }
95   }
96   return JNI_FALSE;
97 }
98 JNIEXPORT void JNICALL
99 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
100   msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
101   if (!comm) {
102     jxbt_throw_native(env,bprintf("comm is null"));
103     return;
104   }
105
106   jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
107   if (finished == JNI_TRUE) {
108     return;
109   }
110
111   msg_error_t status;
112   status = MSG_comm_wait(comm,(double)timeout);
113   (*env)->SetBooleanField(env, jcomm, jcomm_field_Comm_finished, JNI_TRUE);
114   if (status == MSG_OK) {
115     jcomm_bind_task(env,jcomm);
116     return;
117   }
118   else {
119     jmsg_throw_status(env,status);
120   }
121
122 }