#include "msg/msg.h"
#include "msg/private.h"
#include "simix/private.h"
-#include "xbt/xbt_jcontext.h"
-#include "xbt/xbt_context_factory.h"
+#include "xbt/xbt_context_java.h"
#include "jmsg_process.h"
#include "jmsg_host.h"
#include "jmsg_task.h"
#include "jmsg_channel.h"
+#include "jmsg_application_handler.h"
#include "jxbt_utilities.h"
+
#include "jmsg.h"
+#include "msg/mailbox.h"
+
+#include "surf/surfxml_parse.h"
+
+
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
static JavaVM * __java_vm = NULL;
static jobject
native_to_java_process(m_process_t process)
{
- return ((xbt_jcontext_t)(process->simdata->s_process->simdata->context))->jprocess;
+ return ((xbt_ctx_java_t)(process->simdata->s_process->simdata->context))->jprocess;
}
jstring jname; /* the name of the java process instance */
const char* name; /* the C name of the process */
m_process_t process; /* the native process to create */
-
+ char alias[MAX_ALIAS_NAME + 1] = {0};
+ msg_mailbox_t mailbox;
DEBUG4("Java_simgrid_msg_MsgNative_processCreate(env=%p,cls=%p,jproc=%p,jhost=%p)",
env,cls,jprocess_arg,jhost);
(*env)->ReleaseStringUTFChars(env, jname, name);
process->simdata->m_host = jhost_get_native(env,jhost);
+
+
if( ! (process->simdata->m_host) ) { /* not binded */
free(process->simdata);
free(process->data);
SIMIX_jprocess_create(process->name,
process->simdata->m_host->simdata->smx_host,
/*data*/ (void*)process,
- jprocess,env,
+ jprocess,
+ env,
&process->simdata->s_process);
/* add the process to the list of the processes of the simulation */
xbt_fifo_unshift(msg_global->process_list, process);
+
+ sprintf(alias,"%s:%s",(process->simdata->m_host->simdata->smx_host)->name,process->name);
+
+ mailbox = MSG_mailbox_new(alias);
+ MSG_mailbox_set_hostname(mailbox, process->simdata->m_host->simdata->smx_host->name);
+
}
}
JNIEXPORT void JNICALL
-Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv* env, jclass cls, jobject jprocess, jobject jhost){
+Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv* env, jclass cls, jobject jhost){
m_host_t host = jhost_get_native(env,jhost);
- m_process_t process = jprocess_to_native_process(jprocess,env);
-
- if(!process) {
- jxbt_throw_notbound(env,"process",jprocess);
- return;
- }
if(!host) {
jxbt_throw_notbound(env,"host",jhost);
}
/* try to change the host of the process */
- if(MSG_OK != MSG_process_change_host(process,host))
+ if(MSG_OK != MSG_process_change_host(host))
jxbt_throw_native(env, xbt_strdup("MSG_process_change_host() failed"));
}
return (jobject)task->data;
}
+
JNIEXPORT jboolean JNICALL
Java_simgrid_msg_MsgNative_taskProbe(JNIEnv* env, jclass cls, jint chan_id) {
return (jboolean)MSG_task_Iprobe(chan_id);
jxbt_throw_native(env, xbt_strdup("MSG_task_put_with_timeout() failed"));
}
+
+
JNIEXPORT void JNICALL
Java_simgrid_msg_MsgNative_hostPutBounded(JNIEnv* env, jclass cls,
jobject jhost, jint chan_id, jobject jtask,
argc++;
- argv = (char**)calloc(argc,sizeof(char*));
+ argv = xbt_new0(char*, argc);
argv[0] = strdup("java");
JNIEXPORT void JNICALL
Java_simgrid_msg_MsgNative_selectContextFactory(JNIEnv * env, jclass class,jstring jname)
{
- int rv;
+ char *errmsg = NULL;
+ xbt_ex_t e;
- /* get the C string from the java string*/
- const char* name = (*env)->GetStringUTFChars(env, jname, 0);
+ /* get the C string from the java string*/
+ const char* name = (*env)->GetStringUTFChars(env, jname, 0);
+
+ TRY {
+ xbt_context_select_factory(name);
+ } CATCH(e) {
+ errmsg = xbt_strdup(e.msg);
+ xbt_ex_free(e);
+ }
+
+ (*env)->ReleaseStringUTFChars(env, jname, name);
+
+ if(errmsg) {
+ char *thrown = bprintf("xbt_select_context_factory() failed: %s",errmsg);
+ free(errmsg);
+ jxbt_throw_native(env, thrown);
+ }
+}
- rv = xbt_context_select_factory(name);
+JNIEXPORT void JNICALL
+Java_simgrid_msg_MsgNative_taskSend(JNIEnv* env, jclass cls,
+ jstring jalias, jobject jtask,
+ jdouble jtimeout) {
+
+ MSG_error_t rv;
+ const char* alias = (*env)->GetStringUTFChars(env, jalias, 0);
+
+ m_task_t task = jtask_to_native_task(jtask,env);
+
+
+ if(!task){
+ (*env)->ReleaseStringUTFChars(env, jalias, alias);
+ jxbt_throw_notbound(env,"task",jtask);
+ return;
+ }
+
+ rv = MSG_task_send_with_timeout(task,alias,(double)jtimeout);
+
+ (*env)->ReleaseStringUTFChars(env, jalias, alias);
+
+ if(MSG_OK != rv)
+ jxbt_throw_native(env, xbt_strdup("MSG_task_send_with_timeout() failed"));
+
+}
+
+JNIEXPORT void JNICALL
+Java_simgrid_msg_MsgNative_taskSendBounded(JNIEnv* env, jclass cls,
+ jstring jalias, jobject jtask,
+ jdouble jmaxRate) {
+ m_task_t task = jtask_to_native_task(jtask,env);
+ MSG_error_t rv;
+ const char* alias;
+
+ if(!task){
+ jxbt_throw_notbound(env,"task",jtask);
+ return;
+ }
+
+ alias = (*env)->GetStringUTFChars(env, jalias, 0);
+
+ rv = MSG_task_send_bounded(task,alias,(double)jmaxRate);
+
+ (*env)->ReleaseStringUTFChars(env, jalias, alias);
+
+ if(MSG_OK != rv)
+ jxbt_throw_native(env, xbt_strdup("MSG_task_send_bounded() failed"));
+}
+
+JNIEXPORT jobject JNICALL
+Java_simgrid_msg_MsgNative_taskReceive(JNIEnv* env, jclass cls,
+ jstring jalias, jdouble jtimeout, jobject jhost) {
+ MSG_error_t rv;
+ m_task_t task = NULL;
+ m_host_t host = NULL;
+ const char* alias;
+
+ if (jhost) {
+ host = jhost_get_native(env,jhost);
- (*env)->ReleaseStringUTFChars(env, jname, name);
+ if(!host){
+ jxbt_throw_notbound(env,"host",jhost);
+ return NULL;
+ }
+ }
+
+ alias = (*env)->GetStringUTFChars(env, jalias, 0);
+
+ rv = MSG_task_receive_ext(&task,alias,(double)jtimeout,host);
+
+ (*env)->ReleaseStringUTFChars(env, jalias, alias);
+
+ if (MSG_OK != rv)
+ {
+ jxbt_throw_native(env, xbt_strdup("MSG_task_receive_ext() failed"));
+ return NULL;
+ }
+
+ return (jobject)task->data;
+}
+
+JNIEXPORT jboolean JNICALL
+Java_simgrid_msg_MsgNative_taskListen(JNIEnv* env, jclass cls, jstring jalias) {
+
+ const char* alias;
+ int rv;
+
+ alias = (*env)->GetStringUTFChars(env, jalias, 0);
+
+ rv = MSG_task_listen(alias);
+
+ (*env)->ReleaseStringUTFChars(env, jalias, alias);
+
+ return (jboolean)rv;
+}
+
+JNIEXPORT jint JNICALL
+Java_simgrid_msg_MsgNative_taskListenFromHost(JNIEnv* env, jclass cls, jstring jalias, jobject jhost) {
+
+ int rv;
+ const char* alias;
+
+ m_host_t host = jhost_get_native(env,jhost);
+
+ if(!host){
+ jxbt_throw_notbound(env,"host",jhost);
+ return -1;
+ }
+
+ alias = (*env)->GetStringUTFChars(env, jalias, 0);
+
+ rv = MSG_task_listen_from_host(alias,host);
+
+ (*env)->ReleaseStringUTFChars(env, jalias, alias);
+
+ return (jint)rv;
+}
+
+JNIEXPORT jint JNICALL
+Java_simgrid_msg_MsgNative_taskListenFrom(JNIEnv* env, jclass cls, jstring jalias) {
+
+ int rv;
+ const char* alias = (*env)->GetStringUTFChars(env, jalias, 0);
+
+ rv = MSG_task_listen_from(alias);
+
+ (*env)->ReleaseStringUTFChars(env, jalias, alias);
+
+ return (jint)rv;
+}
+
+JNIEXPORT void JNICALL
+Java_simgrid_msg_Msg_deployApplication(JNIEnv* env, jclass cls,jstring jdeploymentFile) {
+
+ const char* deploymentFile = (*env)->GetStringUTFChars(env, jdeploymentFile, 0);
+
+ surf_parse_reset_parser();
+
+ surfxml_add_callback(STag_surfxml_process_cb_list, japplication_handler_on_begin_process);
+
+ surfxml_add_callback(ETag_surfxml_argument_cb_list, japplication_handler_on_process_arg);
+
+ surfxml_add_callback(STag_surfxml_prop_cb_list, japplication_handler_on_property);
+
+ surfxml_add_callback(ETag_surfxml_process_cb_list, japplication_handler_on_end_process);
+
+ surf_parse_open(deploymentFile);
+
+ japplication_handler_on_start_document();
+
+ if(surf_parse())
+ jxbt_throw_native(env, xbt_strdup("surf_parse() failed"));
+
+ surf_parse_close();
+
+ japplication_handler_on_end_document();
- if(rv)
- jxbt_throw_native(env, xbt_strdup("xbt_select_context_factory() failed"));
+ (*env)->ReleaseStringUTFChars(env, jdeploymentFile, deploymentFile);
}