Logo AND Algorithmique Numérique Distribuée

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