1 /* Functions related to the java comm instances */
3 /* Copyright (c) 2012. The SimGrid Team. All rights reserved. */
5 #include "jxbt_utilities.h"
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
11 static jfieldID jcomm_field_Comm_bind;
12 static jfieldID jcomm_field_Comm_finished;
13 static jfieldID jcomm_field_Comm_receiving;
14 static jfieldID jtask_field_Comm_task;
15 static jfieldID jcomm_field_Comm_taskBind;
17 void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
18 msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
19 //test if we are receiving or sending a task.
20 jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
21 if (jreceiving == JNI_TRUE) {
22 //bind the task object.
23 msg_task_t task = MSG_comm_get_task(comm);
24 xbt_assert(task != NULL, "Task is NULL");
25 jobject jtask_global = MSG_task_get_data(task);
26 //case where the data has already been retrieved
27 if (jtask_global == NULL) {
31 //Make sure the data will be correctly gc.
32 jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
33 (*env)->DeleteGlobalRef(env, jtask_global);
35 (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
37 MSG_task_set_data(task, NULL);
42 JNIEXPORT void JNICALL
43 Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
44 jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
45 if (!jfield_class_Comm) {
46 jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
49 jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
50 jcomm_field_Comm_taskBind = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
51 jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
52 jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
53 jcomm_field_Comm_finished = jxbt_get_jfield(env, jfield_class_Comm, "finished", "Z");
54 if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task || !jcomm_field_Comm_finished) {
55 jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
59 JNIEXPORT void JNICALL
60 Java_org_simgrid_msg_Comm_destroy(JNIEnv *env, jobject jcomm) {
62 msg_task_t *task_received;
64 task_received = (msg_task_t*) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
65 xbt_free(task_received);
67 comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
68 MSG_comm_destroy(comm);
71 JNIEXPORT jboolean JNICALL
72 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
74 comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
76 jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
77 if (finished == JNI_TRUE) {
82 jxbt_throw_native(env,bprintf("comm is null"));
86 if (MSG_comm_test(comm)) {
87 msg_error_t status = MSG_comm_get_status(comm);
88 if (status == MSG_OK) {
89 jcomm_bind_task(env,jcomm);
92 //send the correct exception
93 jmsg_throw_status(env,status);
98 JNIEXPORT void JNICALL
99 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
100 msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
102 jxbt_throw_native(env,bprintf("comm is null"));
106 jboolean finished = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_finished);
107 if (finished == JNI_TRUE) {
112 status = MSG_comm_wait(comm,(double)timeout);
113 (*env)->SetBooleanField(env, jcomm, jcomm_field_Comm_finished, JNI_TRUE);
114 if (status == MSG_OK) {
115 jcomm_bind_task(env,jcomm);
119 jmsg_throw_status(env,status);