Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add setlocale in jmsg.c, to have traces not depending on the locale of the user
[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_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
62         jcomm_field_Comm_taskBind  = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "taskBind", "J");
63         jcomm_field_Comm_receiving = jxbt_get_sfield(env, "org/simgrid/msg/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         if (MSG_comm_test(comm)) {
92                 MSG_error_t status = MSG_comm_get_status(comm);
93                 if (status == MSG_OK) {
94                         jcomm_bind_task(env,jcomm);
95                         return JNI_TRUE;
96                 }
97                 else {
98                         //send the correct exception
99                         jcomm_throw(env,status);
100                         return JNI_FALSE;
101                 }
102         }
103         else {
104                 return JNI_FALSE;
105         }
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         MSG_error_t status = MSG_comm_wait(comm,(double)timeout);
116         if (status == MSG_OK) {
117                 jcomm_bind_task(env,jcomm);
118                 return;
119         }
120         else {
121                 jcomm_throw(env,status);
122         }
123
124
125 }