Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
4a394cd3f405e2b648f2563f7f69f5b75ef88a92
[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 <msg/msg.h>
7 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
8
9 static jfieldID jtask_field_Comm_task;
10 static jfieldID jcomm_field_Comm_bind;
11 static jfieldID jcomm_field_Comm_taskBind;
12 static jfieldID jcomm_field_Comm_receiving;
13
14 void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
15         msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
16         //test if we are receiving or sending a task.
17         jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
18         if (jreceiving == JNI_TRUE) {
19                 //bind the task object.
20                 m_task_t task = MSG_comm_get_task(comm);
21                 xbt_assert(task != NULL, "Task is NULL");
22                 jobject jtask_global = MSG_task_get_data(task);
23                 //case where the data has already been retrieved
24                 if (jtask_global == NULL) {
25                         return;
26                 }
27
28                 //Make sure the data will be correctly gc.
29                 jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
30                 (*env)->DeleteGlobalRef(env, jtask_global);
31
32                 (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
33
34                 MSG_task_set_data(task, NULL);
35         }
36
37 }
38 void jcomm_throw(JNIEnv *env, MSG_error_t status) {
39         switch (status) {
40                 case MSG_TIMEOUT:
41                         jxbt_throw_time_out_failure(env,NULL);
42                 break;
43                 case MSG_TRANSFER_FAILURE:
44                         jxbt_throw_transfer_failure(env,NULL);
45                 break;
46                 case MSG_HOST_FAILURE:
47                         jxbt_throw_host_failure(env,NULL);
48                 break;
49                 default:
50                         jxbt_throw_native(env,bprintf("communication failed"));
51         }
52 }
53
54 JNIEXPORT void JNICALL
55 Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
56         jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
57         if (!jfield_class_Comm) {
58                 jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
59                 return;
60         }
61         jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
62         jcomm_field_Comm_taskBind  = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
63         jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
64         jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
65         if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task) {
66         jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
67         }
68 }
69
70 JNIEXPORT void JNICALL
71 Java_org_simgrid_msg_Comm_unbind(JNIEnv *env, jobject jcomm) {
72         msg_comm_t comm;
73         m_task_t *task_received;
74
75         task_received = (m_task_t*)  (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
76         xbt_free(task_received);
77
78         comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
79         MSG_comm_destroy(comm);
80 }
81
82 JNIEXPORT jboolean JNICALL
83 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
84         msg_comm_t comm;
85         comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
86
87         if (!comm) {
88                 jxbt_throw_native(env,bprintf("comm is null"));
89                 return JNI_FALSE;
90         }
91         TRY {
92                 if (MSG_comm_test(comm)) {
93                         MSG_error_t status = MSG_comm_get_status(comm);
94                         if (status == MSG_OK) {
95                                 jcomm_bind_task(env,jcomm);
96                                 return JNI_TRUE;
97                         }
98                         else {
99                                 //send the correct exception
100                                 jcomm_throw(env,status);
101                                 return JNI_FALSE;
102                         }
103                 }
104                 else {
105                         return JNI_FALSE;
106                 }
107         }
108         CATCH_ANONYMOUS {
109
110         }
111         return JNI_FALSE;
112 }
113 JNIEXPORT void JNICALL
114 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
115         msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
116         if (!comm) {
117                 jxbt_throw_native(env,bprintf("comm is null"));
118                 return;
119         }
120         MSG_error_t status;
121         TRY {
122                 status = MSG_comm_wait(comm,(double)timeout);
123         }
124         CATCH_ANONYMOUS {
125                 return;
126         }
127         if (status == MSG_OK) {
128                 jcomm_bind_task(env,jcomm);
129                 return;
130         }
131         else {
132                 jcomm_throw(env,status);
133         }
134
135
136 }