From 64d8be3badd63b57778ee6ed430a98668512466d Mon Sep 17 00:00:00 2001 From: Jonathan Rouzaud-Cornabas Date: Tue, 19 Mar 2013 18:33:28 +0100 Subject: [PATCH] Add receive bounded to the java binding --- src/bindings/java/jmsg_task.c | 82 +++++++++++++++++++++ src/bindings/java/org/simgrid/msg/Task.java | 49 ++++++++++++ 2 files changed, 131 insertions(+) diff --git a/src/bindings/java/jmsg_task.c b/src/bindings/java/jmsg_task.c index ddc174085f..3ad81676b5 100644 --- a/src/bindings/java/jmsg_task.c +++ b/src/bindings/java/jmsg_task.c @@ -491,6 +491,88 @@ Java_org_simgrid_msg_Task_irecv(JNIEnv * env, jclass cls, jstring jmailbox) { return jcomm; } + +JNIEXPORT jobject JNICALL +Java_org_simgrid_msg_Task_receiveBounded(JNIEnv * env, jclass cls, + jstring jalias, jdouble jtimeout, + jobject jhost, jdouble rate) +{ + msg_error_t rv; + msg_task_t *task = xbt_new(msg_task_t,1); + *task = NULL; + + msg_host_t host = NULL; + jobject jtask_global, jtask_local; + const char *alias; + + if (jhost) { + host = jhost_get_native(env, jhost); + + if (!host) { + jxbt_throw_notbound(env, "host", jhost); + return NULL; + } + } + + alias = (*env)->GetStringUTFChars(env, jalias, 0); + rv = MSG_task_receive_ext_bounded(task, alias, (double) jtimeout, host, (double) rate); + if ((*env)->ExceptionOccurred(env)) + return NULL; + if (rv != MSG_OK) { + jmsg_throw_status(env,rv); + return NULL; + } + jtask_global = MSG_task_get_data(*task); + + /* Convert the global ref into a local ref so that the JVM can free the stuff */ + jtask_local = (*env)->NewLocalRef(env, jtask_global); + (*env)->DeleteGlobalRef(env, jtask_global); + MSG_task_set_data(*task, NULL); + + (*env)->ReleaseStringUTFChars(env, jalias, alias); + + xbt_free(task); + + return (jobject) jtask_local; +} + + +JNIEXPORT jobject JNICALL +Java_org_simgrid_msg_Task_irecvBounded(JNIEnv * env, jclass cls, + jstring jmailbox, jdouble rate) { + msg_comm_t comm; + const char *mailbox; + jclass comm_class; + //pointer to store the task object pointer. + msg_task_t *task = xbt_new(msg_task_t,1); + *task = NULL; + /* There should be a cache here */ + comm_class = (*env)->FindClass(env, "org/simgrid/msg/Comm"); + + if (!comm_class) { + jxbt_throw_native(env,bprintf("fieldID or methodID or class not found.")); + return NULL; + } + + jobject jcomm = (*env)->NewObject(env, comm_class, jtask_method_Comm_constructor); + if (!jcomm) { + jxbt_throw_native(env,bprintf("Can't create a Comm object.")); + return NULL; + } + + mailbox = (*env)->GetStringUTFChars(env, jmailbox, 0); + + comm = MSG_task_irecv_bounded(task,mailbox, (double) rate); + + (*env)->SetLongField(env, jcomm, jtask_field_Comm_bind, (jlong) (long)(comm)); + (*env)->SetLongField(env, jcomm, jtask_field_Comm_taskBind, (jlong) (long)(task)); + (*env)->SetBooleanField(env, jcomm, jtask_field_Comm_receiving, JNI_TRUE); + + (*env)->ReleaseStringUTFChars(env, jmailbox, mailbox); + + return jcomm; +} + JNIEXPORT jobject JNICALL Java_org_simgrid_msg_Task_isend(JNIEnv *env, jobject jtask, jstring jmailbox) { jclass comm_class; diff --git a/src/bindings/java/org/simgrid/msg/Task.java b/src/bindings/java/org/simgrid/msg/Task.java index 1b2e0cdac1..8900da32e4 100644 --- a/src/bindings/java/org/simgrid/msg/Task.java +++ b/src/bindings/java/org/simgrid/msg/Task.java @@ -308,6 +308,55 @@ public class Task { * @param host */ public native static Task receive(String mailbox, double timeout, Host host) throws TransferFailureException, HostFailureException, TimeoutException; + + /** + * Starts listening for receiving a task from an asynchronous communication with a capped rate + * @param mailbox + */ + public static native Comm irecvBounded(String mailbox, double rate); + /** + * Retrieves next task from the mailbox identified by the specified name with a capped rate + * + * @param mailbox + */ + + public static Task receiveBounded(String mailbox, double rate) throws TransferFailureException, HostFailureException, TimeoutException { + return receiveBounded(mailbox, -1.0, null, rate); + } + + /** + * Retrieves next task on the mailbox identified by the specified name (wait at most \a timeout seconds) with a capped rate + * + * @param mailbox + * @param timeout + */ + public static Task receiveBounded(String mailbox, double timeout, double rate) throws TransferFailureException, HostFailureException, TimeoutException { + return receiveBounded(mailbox, timeout, null, rate); + } + + /** + * Retrieves next task sent by a given host on the mailbox identified by the specified alias with a capped rate + * + * @param mailbox + * @param host + */ + + public static Task receiveBounded(String mailbox, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException { + return receiveBounded(mailbox, -1.0, host, rate); + } + + /** + * Retrieves next task sent by a given host on the mailbox identified by the specified alias (wait at most \a timeout seconds) + * with a capped rate + * + * @param mailbox + * @param timeout + * @param host + */ + public native static Task receiveBounded(String mailbox, double timeout, Host host, double rate) throws TransferFailureException, HostFailureException, TimeoutException; + + + /** * Tests whether there is a pending communication on the mailbox identified by the specified alias, and who sent it */ -- 2.20.1