Logo AND Algorithmique Numérique Distribuée

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