1 /* Functions related to the java comm instances */
3 /* Copyright (c) 2012. The SimGrid Team. All rights reserved. */
5 #include "jxbt_utilities.h"
7 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
9 static jfieldID jtask_field_Comm_task;
10 static jfieldID jcomm_field_Comm_bind;
11 static jfieldID jcomm_field_Comm_taskBind;
12 static jfieldID jcomm_field_Comm_receiving;
14 void jcomm_bind_task(JNIEnv *env, jobject jcomm) {
15 msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
16 //test if we are receiving or sending a task.
17 jboolean jreceiving = (*env)->GetBooleanField(env, jcomm, jcomm_field_Comm_receiving);
18 if (jreceiving == JNI_TRUE) {
19 //bind the task object.
20 m_task_t task = MSG_comm_get_task(comm);
21 xbt_assert(task != NULL, "Task is NULL");
22 jobject jtask_global = MSG_task_get_data(task);
23 //case where the data has already been retrieved
24 if (jtask_global == NULL) {
28 //Make sure the data will be correctly gc.
29 jobject jtask_local = (*env)->NewLocalRef(env, jtask_global);
30 (*env)->DeleteGlobalRef(env, jtask_global);
32 (*env)->SetObjectField(env, jcomm, jtask_field_Comm_task, jtask_local);
34 MSG_task_set_data(task, NULL);
38 void jcomm_throw(JNIEnv *env, MSG_error_t status) {
41 jxbt_throw_time_out_failure(env,NULL);
43 case MSG_TRANSFER_FAILURE:
44 jxbt_throw_transfer_failure(env,NULL);
46 case MSG_HOST_FAILURE:
47 jxbt_throw_host_failure(env,NULL);
50 jxbt_throw_native(env,bprintf("communication failed"));
54 JNIEXPORT void JNICALL
55 Java_org_simgrid_msg_Comm_nativeInit(JNIEnv *env, jclass cls) {
56 jclass jfield_class_Comm = (*env)->FindClass(env, "org/simgrid/msg/Comm");
57 if (!jfield_class_Comm) {
58 jxbt_throw_native(env,bprintf("Can't find the org/simgrid/msg/Comm class."));
61 jcomm_field_Comm_bind = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "bind", "J");
62 jcomm_field_Comm_taskBind = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "taskBind", "J");
63 jcomm_field_Comm_receiving = jxbt_get_sfield(env, "org/simgrid/msg/Comm", "receiving", "Z");
64 jtask_field_Comm_task = jxbt_get_jfield(env, jfield_class_Comm, "task", "Lorg/simgrid/msg/Task;");
65 if (!jcomm_field_Comm_bind || !jcomm_field_Comm_taskBind || !jcomm_field_Comm_receiving || !jtask_field_Comm_task) {
66 jxbt_throw_native(env,bprintf("Can't find some fields in Java class."));
70 JNIEXPORT void JNICALL
71 Java_org_simgrid_msg_Comm_unbind(JNIEnv *env, jobject jcomm) {
73 m_task_t *task_received;
75 task_received = (m_task_t*) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_taskBind);
76 xbt_free(task_received);
78 comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
79 MSG_comm_destroy(comm);
82 JNIEXPORT jboolean JNICALL
83 Java_org_simgrid_msg_Comm_test(JNIEnv *env, jobject jcomm) {
85 comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
88 jxbt_throw_native(env,bprintf("comm is null"));
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);
98 //send the correct exception
99 jcomm_throw(env,status);
107 JNIEXPORT void JNICALL
108 Java_org_simgrid_msg_Comm_waitCompletion(JNIEnv *env, jobject jcomm, jdouble timeout) {
109 msg_comm_t comm = (msg_comm_t) (long) (*env)->GetLongField(env, jcomm, jcomm_field_Comm_bind);
111 jxbt_throw_native(env,bprintf("comm is null"));
115 MSG_error_t status = MSG_comm_wait(comm,(double)timeout);
116 if (status == MSG_OK) {
117 jcomm_bind_task(env,jcomm);
121 jcomm_throw(env,status);