Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
17a714ecdb202760bdae2bd7904e0afb172d30ea
[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
16         if (!id || !id_task)
17                 return;
18
19         task_received = (m_task_t*)  (long) (*env)->GetLongField(env, jcomm, id_task);
20         xbt_free(task_received);
21
22         comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, id);
23         MSG_comm_destroy(comm);
24 }
25
26 JNIEXPORT jboolean JNICALL
27 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
28         msg_comm_t comm;
29
30         jfieldID idComm = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
31         jclass jclass = jxbt_get_class(env,"org/simgrid/msg/Comm");
32         jfieldID idTask = jxbt_get_jfield(env, jclass, "task", "Lorg/simgrid/msg/Task;");
33         if (!idComm || !idTask) {
34                 jxbt_throw_native(env,bprintf("idTask or idComm is null"));
35                 return JNI_FALSE;
36         }
37
38         comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, idComm);
39         if (!comm) {
40                 jxbt_throw_native(env,bprintf("comm is null"));
41                 return JNI_FALSE;
42         }
43         if (MSG_comm_test(comm)) {
44                 MSG_error_t status = MSG_comm_get_status(comm);
45
46                 if (status == MSG_OK) {
47                         //bind the task object.
48                         m_task_t task = MSG_comm_get_task(comm);
49                         jobject jtask_global = MSG_task_get_data(task);
50                         jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
51
52                         (*env)->DeleteGlobalRef(env, jtask_global);
53
54                         (*env)->SetObjectField(env, jcomm, idTask, jtask_local);
55
56                         MSG_task_set_data(task, NULL);
57
58                         return JNI_TRUE;
59                 }
60                 else {
61                         //send the correct exception
62                         switch (status) {
63                                 case MSG_TIMEOUT:
64                                         jxbt_throw_time_out_failure(env,NULL);
65                                 break;
66                                 case MSG_TRANSFER_FAILURE:
67                                         jxbt_throw_transfer_failure(env,NULL);
68                                 break;
69                                 case MSG_HOST_FAILURE:
70                                         jxbt_throw_host_failure(env,NULL);
71                                 break;
72                                 default:
73                                         jxbt_throw_native(env,bprintf("receive failed"));
74                         }
75                         return JNI_FALSE;
76                 }
77         }
78         else {
79                 return JNI_FALSE;
80         }
81 }