Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'with_java'
[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   xbt_ex_t e;
86   TRY {
87     if (MSG_comm_test(comm)) {
88       msg_error_t status = MSG_comm_get_status(comm);
89       if (status == MSG_OK) {
90         jcomm_bind_task(env,jcomm);
91         return JNI_TRUE;
92       }
93       else {
94         //send the correct exception
95         jmsg_throw_status(env,status);
96         return JNI_FALSE;
97       }
98     }
99     else {
100       return JNI_FALSE;
101     }
102   }
103   CATCH(e) {
104     xbt_ex_free(e);
105   }
106
107   return JNI_FALSE;
108 }
109 JNIEXPORT void JNICALL
110 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
111   msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
112   if (!comm) {
113     jxbt_throw_native(env,bprintf("comm is null"));
114     return;
115   }
116
117   jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
118   if (finished == JNI_TRUE) {
119     return;
120   }
121
122   msg_error_t status;
123   status = MSG_comm_wait(comm,(double)timeout);
124   (*env)->SetBooleanField(env, jcomm, jcomm_field_Comm_finished, JNI_TRUE);
125   if (status == MSG_OK) {
126     jcomm_bind_task(env,jcomm);
127     return;
128   }
129   else {
130     jmsg_throw_status(env,status);
131   }
132
133 }