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 jtask_field_Comm_task;
12 static jfieldID jcomm_field_Comm_bind;
13 static jfieldID jcomm_field_Comm_taskBind;
14 static jfieldID jcomm_field_Comm_receiving;
16 void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
17 msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
18 //test if we are receiving or sending a task.
19 jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
20 if (jreceiving == JNI_TRUE) {
21 //bind the task object.
22 m_task_t task = MSG_comm_get_task(comm);
23 xbt_assert(task != NULL, "Task is NULL");
24 jobject jtask_global = MSG_task_get_data(task);
25 //case where the data has already been retrieved
26 if (jtask_global == NULL) {
30 //Make sure the data will be correctly gc.
31 jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
32 (*env)->DeleteGlobalRef(env, jtask_global);
34 (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
36 MSG_task_set_data(task, NULL);
41 JNIEXPORT void JNICALL
42 Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
43 jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
44 if (!jfield_class_Comm) {
45 jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
48 jcomm_field_Comm_bind = jxbt_get_jfield(env, jfield_class_Comm, "bind", "J");
49 jcomm_field_Comm_taskBind = jxbt_get_jfield(env, jfield_class_Comm, "taskBind", "J");
50 jcomm_field_Comm_receiving = jxbt_get_jfield(env, jfield_class_Comm, "receiving", "Z");
51 jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
52 if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task) {
53 jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
57 JNIEXPORT void JNICALL
58 Java_org_simgrid_msg_Comm_destroy(JNIEnv *env, jobject jcomm) {
60 m_task_t *task_received;
62 task_received = (m_task_t*) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
63 xbt_free(task_received);
65 comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
66 MSG_comm_destroy(comm);
69 JNIEXPORT jboolean JNICALL
70 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
72 comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
75 jxbt_throw_native(env,bprintf("comm is null"));
79 if (MSG_comm_test(comm)) {
80 MSG_error_t status = MSG_comm_get_status(comm);
81 if (status == MSG_OK) {
82 jcomm_bind_task(env,jcomm);
86 //send the correct exception
87 jmsg_throw_status(env,status);
100 JNIEXPORT void JNICALL
101 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
102 msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
104 jxbt_throw_native(env,bprintf("comm is null"));
109 status = MSG_comm_wait(comm,(double)timeout);
114 if (status == MSG_OK) {
115 jcomm_bind_task(env,jcomm);
119 jmsg_throw_status(env,status);