Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge scm.gforge.inria.fr:/gitroot/simgrid/simgrid-java
[simgrid.git] / src / 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                 m_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         m_task_t *task_received;
63
64         task_received = (m_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         TRY {
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                         }
92                         else {
93                                 //send the correct exception
94                                 jmsg_throw_status(env,status);
95                                 return JNI_FALSE;
96                         }
97                 }
98                 else {
99                         return JNI_FALSE;
100                 }
101         }
102         CATCH_ANONYMOUS {
103
104         }
105         return JNI_FALSE;
106 }
107 JNIEXPORT void JNICALL
108 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
109         msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
110         if (!comm) {
111                 jxbt_throw_native(env,bprintf("comm is null"));
112                 return;
113         }
114
115         jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
116         if (finished == JNI_TRUE) {
117                 return;
118         }
119
120         MSG_error_t status;
121         TRY {
122                 status = MSG_comm_wait(comm,(double)timeout);
123         }
124         CATCH_ANONYMOUS {
125                 return;
126         }
127         (*env)->SetBooleanField(env, jcomm, jcomm_field_Comm_finished, JNI_TRUE);
128         if (status == MSG_OK) {
129                 jcomm_bind_task(env,jcomm);
130                 return;
131         }
132         else {
133                 jmsg_throw_status(env,status);
134         }
135
136
137 }