Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
First series of changes to lua-bindings in order to
[simgrid.git] / src / bindings / java / jmsg_comm.c
1 /* Functions related to the java comm instances                             */
2
3 /* Copyright (c) 2012-2015. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "jmsg_comm.h"
10 #include "jxbt_utilities.h"
11 #include "jmsg.h"
12
13 #include <simgrid/msg.h>
14 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
15
16 static jfieldID jcomm_field_Comm_bind;
17 static jfieldID jcomm_field_Comm_finished;
18 static jfieldID jcomm_field_Comm_receiving;
19 static jfieldID jtask_field_Comm_task;
20 static jfieldID jcomm_field_Comm_taskBind;
21
22 void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
23   msg_comm_t comm = (msg_comm_t) (uintptr_t) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
24   //test if we are receiving or sending a task.
25   jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
26   if (jreceiving == JNI_TRUE) {
27     //bind the task object.
28     msg_task_t task = MSG_comm_get_task(comm);
29     xbt_assert(task != NULL, "Task is NULL");
30     jobject jtask_global = MSG_task_get_data(task);
31     //case where the data has already been retrieved
32     if (jtask_global == NULL) {
33       return;
34     }
35
36     //Make sure the data will be correctly gc.
37     jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
38     (*env)->DeleteGlobalRef(env, jtask_global);
39
40     (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
41
42     MSG_task_set_data(task, NULL);
43   }
44
45 }
46
47 JNIEXPORT void JNICALL
48 Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
49   jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
50   if (!jfield_class_Comm) {
51     jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
52     return;
53   }
54   jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
55   jcomm_field_Comm_taskBind  = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
56   jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
57   jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
58   jcomm_field_Comm_finished = jxbt_get_jfield(env, jfield_class_Comm, "finished", "Z");
59   if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task || !jcomm_field_Comm_finished) {
60     jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
61   }
62 }
63
64 JNIEXPORT void JNICALL
65 Java_org_simgrid_msg_Comm_nativeFinalize(JNIEnv *env, jobject jcomm) {
66   msg_comm_t comm;
67   msg_task_t *task_received;
68
69   task_received = (msg_task_t*)  (uintptr_t) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
70   xbt_free(task_received);
71
72   comm = (msg_comm_t) (uintptr_t) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
73   MSG_comm_destroy(comm);
74 }
75
76 JNIEXPORT jboolean JNICALL
77 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
78   msg_comm_t comm;
79   comm = (msg_comm_t) (uintptr_t) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
80
81   jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
82   if (finished == JNI_TRUE) {
83     return JNI_TRUE;
84   }
85
86   if (!comm) {
87     jxbt_throw_native(env,bprintf("comm is null"));
88     return JNI_FALSE;
89   }
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     } else {
97       //send the correct exception
98       jmsg_throw_status(env,status);
99     }
100   }
101   return JNI_FALSE;
102 }
103 JNIEXPORT void JNICALL
104 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
105   msg_comm_t comm = (msg_comm_t) (uintptr_t) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
106   if (!comm) {
107     jxbt_throw_native(env,bprintf("comm is null"));
108     return;
109   }
110
111   jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
112   if (finished == JNI_TRUE) {
113     return;
114   }
115
116   msg_error_t status;
117   status = MSG_comm_wait(comm,(double)timeout);
118   (*env)->SetBooleanField(env, jcomm, jcomm_field_Comm_finished, JNI_TRUE);
119   if (status == MSG_OK) {
120     jcomm_bind_task(env,jcomm);
121     return;
122   }
123   else {
124     jmsg_throw_status(env,status);
125   }
126
127 }