Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Propagate recent modifications of change_host function to java.
[simgrid.git] / src / java / jmsg.c
index 5239f29..681c19e 100644 (file)
@@ -12,8 +12,7 @@
 #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"
@@ -23,6 +22,8 @@
 
 #include "jmsg.h"
 
+#include "msg/mailbox.h"
+
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
 
 static JavaVM * __java_vm = NULL;
@@ -47,7 +48,7 @@ get_current_thread_env(void) {
 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;     
 }
 
 
@@ -61,7 +62,8 @@ Java_simgrid_msg_MsgNative_processCreate(JNIEnv* env, jclass cls, jobject jproce
   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);
@@ -96,6 +98,8 @@ Java_simgrid_msg_MsgNative_processCreate(JNIEnv* env, jclass cls, jobject jproce
   (*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);
@@ -132,6 +136,12 @@ Java_simgrid_msg_MsgNative_processCreate(JNIEnv* env, jclass cls, jobject jproce
     
   /* 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);
+
        
 }
 
@@ -292,14 +302,8 @@ Java_simgrid_msg_MsgNative_processSelfPPID(JNIEnv* env, jclass cls) {
 }
 
 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);
@@ -307,7 +311,7 @@ Java_simgrid_msg_MsgNative_processChangeHost(JNIEnv* env, jclass cls, jobject jp
   }
 
   /* 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"));
 }
 
@@ -733,6 +737,7 @@ Java_simgrid_msg_MsgNative_taskGet(JNIEnv* env, jclass cls,
   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);
@@ -786,6 +791,8 @@ Java_simgrid_msg_MsgNative_hostPut(JNIEnv* env, jclass cls,
     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, 
@@ -831,7 +838,7 @@ Java_simgrid_msg_Msg_init(JNIEnv* env, jclass cls, jobjectArray jargs) {
 
   argc++;
        
-  argv = (char**)calloc(argc,sizeof(char*));
+  argv = xbt_new0(char*, argc);
         
   argv[0] = strdup("java");
        
@@ -975,16 +982,156 @@ Java_simgrid_msg_MsgNative_allHosts(JNIEnv * env, jclass cls_arg) {
 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);
+
+   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);
+   }   
+}
+
+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);
        
-       /* get the C string from the java string*/
-       const char* name = (*env)->GetStringUTFChars(env, jname, 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"));
+}
 
-       rv = xbt_context_select_factory(name);
+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) {
        
-       if(rv)
-               jxbt_throw_native(env, xbt_strdup("xbt_select_context_factory() failed"));       
+  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;
 }