Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add isend in Task
[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 JNIEXPORT void JNICALL
10 Java_org_simgrid_msg_Comm_unbind(JNIEnv *env, jobject jcomm) {
11         jfieldID id = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
12         jfieldID id_task = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bindTask", "J");
13         msg_comm_t comm;
14         m_task_t *task_received;
15         if (!id || !id_task)
16                 return;
17
18         task_received = (m_task_t*)  (long) (*env)->GetLongField(env, jcomm, id_task);
19         if (task_received != NULL) {
20                 xbt_free(task_received);
21         }
22
23         comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, id);
24         MSG_comm_destroy(comm);
25 }
26
27 JNIEXPORT jboolean JNICALL
28 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
29         msg_comm_t comm;
30
31         jfieldID idComm = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
32         jclass jclass = jxbt_get_class(env,"org/simgrid/msg/Comm");
33         jfieldID idTask = jxbt_get_jfield(env, jclass, "task", "Lorg/simgrid/msg/Task;");
34         jfieldID id_receiving = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "receiving", "Z");
35
36
37         if (!idComm || !idTask || !id_receiving) {
38                 jxbt_throw_native(env,bprintf("idTask or idComm is null"));
39                 return JNI_FALSE;
40         }
41
42         comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, idComm);
43         if (!comm) {
44                 jxbt_throw_native(env,bprintf("comm is null"));
45                 return JNI_FALSE;
46         }
47         if (MSG_comm_test(comm)) {
48                 MSG_error_t status = MSG_comm_get_status(comm);
49
50                 if (status == MSG_OK) {
51                         //test if we are receiving or sending a task.
52                         jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, id_receiving);
53                         if (jreceiving == JNI_TRUE) {
54                                 //bind the task object.
55                                 m_task_t task = MSG_comm_get_task(comm);
56                                 xbt_assert(task != NULL, "Task is NULL");
57                                 jobject jtask_global = MSG_task_get_data(task);
58                                 //case where the data has already been retrieved
59                                 if (jtask_global == NULL)
60                                 {
61                                         return JNI_TRUE;
62                                 }
63
64                                 //Make sure the data will be correctly gc.
65                                 jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
66                                 (*env)->DeleteGlobalRef(env, jtask_global);
67
68                                 (*env)->SetObjectField(env, jcomm, idTask, jtask_local);
69
70                                 MSG_task_set_data(task, NULL);
71                         }
72                         return JNI_TRUE;
73                 }
74                 else {
75                         //send the correct exception
76                         switch (status) {
77                                 case MSG_TIMEOUT:
78                                         jxbt_throw_time_out_failure(env,NULL);
79                                 break;
80                                 case MSG_TRANSFER_FAILURE:
81                                         jxbt_throw_transfer_failure(env,NULL);
82                                 break;
83                                 case MSG_HOST_FAILURE:
84                                         jxbt_throw_host_failure(env,NULL);
85                                 break;
86                                 default:
87                                         jxbt_throw_native(env,bprintf("receive failed"));
88                         }
89                         return JNI_FALSE;
90                 }
91         }
92         else {
93                 return JNI_FALSE;
94         }
95 }