1 /* Functions related to the java comm instances */
3 /* Copyright (c) 2012-2013. The SimGrid Team.
4 * All rights reserved. */
6 #include "jxbt_utilities.h"
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
12 static jfieldID jcomm_field_Comm_bind;
13 static jfieldID jcomm_field_Comm_finished;
14 static jfieldID jcomm_field_Comm_receiving;
15 static jfieldID jtask_field_Comm_task;
16 static jfieldID jcomm_field_Comm_taskBind;
18 void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
19 msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
20 //test if we are receiving or sending a task.
21 jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
22 if (jreceiving == JNI_TRUE) {
23 //bind the task object.
24 msg_task_t task = MSG_comm_get_task(comm);
25 xbt_assert(task != NULL, "Task is NULL");
26 jobject jtask_global = MSG_task_get_data(task);
27 //case where the data has already been retrieved
28 if (jtask_global == NULL) {
32 //Make sure the data will be correctly gc.
33 jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
34 (*env)->DeleteGlobalRef(env, jtask_global);
36 (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
38 MSG_task_set_data(task, NULL);
43 JNIEXPORT void JNICALL
44 Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
45 jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
46 if (!jfield_class_Comm) {
47 jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
50 jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
51 jcomm_field_Comm_taskBind = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
52 jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
53 jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
54 jcomm_field_Comm_finished = jxbt_get_jfield(env, jfield_class_Comm, "finished", "Z");
55 if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task || !jcomm_field_Comm_finished) {
56 jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
60 JNIEXPORT void JNICALL
61 Java_org_simgrid_msg_Comm_destroy(JNIEnv *env, jobject jcomm) {
63 msg_task_t *task_received;
65 task_received = (msg_task_t*) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
66 xbt_free(task_received);
68 comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
69 MSG_comm_destroy(comm);
72 JNIEXPORT jboolean JNICALL
73 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
75 comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
77 jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
78 if (finished == JNI_TRUE) {
83 jxbt_throw_native(env,bprintf("comm is null"));
87 if (MSG_comm_test(comm)) {
88 msg_error_t status = MSG_comm_get_status(comm);
89 if (status == MSG_OK) {
90 jcomm_bind_task(env,jcomm);
93 //send the correct exception
94 jmsg_throw_status(env,status);
99 JNIEXPORT void JNICALL
100 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
101 msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
103 jxbt_throw_native(env,bprintf("comm is null"));
107 jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
108 if (finished == JNI_TRUE) {
113 status = MSG_comm_wait(comm,(double)timeout);
114 (*env)->SetBooleanField(env, jcomm, jcomm_field_Comm_finished, JNI_TRUE);
115 if (status == MSG_OK) {
116 jcomm_bind_task(env,jcomm);
120 jmsg_throw_status(env,status);