Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[simix] Kill cojava
[simgrid.git] / src / bindings / java / jmsg.c
index bfc4115..d588664 100644 (file)
@@ -1,24 +1,24 @@
 /* Java Wrappers to the MSG API.                                            */
 
-/* Copyright (c) 2007-2014. The SimGrid Team.
+/* Copyright (c) 2007-2015. The SimGrid Team.
  * All rights reserved.                                                     */
 
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include <msg/msg.h>
+#include <simgrid/msg.h>
 #include <simgrid/simix.h>
 #include <surf/surfxml_parse.h>
 #include <locale.h>
 
 #include "smx_context_java.h"
-#include "smx_context_cojava.h"
 
 #include "jmsg_process.h"
 
 #include "jmsg_as.h"
 
 #include "jmsg_host.h"
+#include "jmsg_storage.h"
 #include "jmsg_task.h"
 #include "jxbt_utilities.h"
 
@@ -34,6 +34,7 @@
 /* end of eclipse-mandated pimple */
 
 int JAVA_HOST_LEVEL;
+int JAVA_STORAGE_LEVEL;
 
 static int create_jprocess(int argc, char *argv[]);
 
@@ -65,8 +66,11 @@ void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
     case MSG_HOST_FAILURE:
         jxbt_throw_host_failure(env,NULL);
     break;
+    case MSG_TASK_CANCELED:
+        jxbt_throw_task_cancelled(env,NULL);
+    break;
     default:
-        jxbt_throw_native(env,xbt_strdup("communication failed"));
+        jxbt_throw_native(env,xbt_strdup("undefined message failed (please see jmsg_throw_status function in jmsg.c)"));
   }
 }
 
@@ -85,6 +89,10 @@ static void __JAVA_host_priv_free(void *host)
 {
 }
 
+static void __JAVA_storage_priv_free(void *storage)
+{
+}
+
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
 {
@@ -95,16 +103,11 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
   const char *tmp;
 
   XBT_LOG_CONNECT(jmsg);
-#ifdef HAVE_TRACING
   XBT_LOG_CONNECT(jtrace);
-#endif
 
   (*env)->GetJavaVM(env, &__java_vm);
 
-  if ((*env)->FindClass(env, "java/dyn/Coroutine"))
-    smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
-  else
-    smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
+  smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
   jthrowable exc = (*env)->ExceptionOccurred(env);
   if (exc) {
     (*env)->ExceptionClear(env);
@@ -117,12 +120,12 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
 
   argc++;
   argv = xbt_new(char *, argc + 1);
-  argv[0] = strdup("java");
+  argv[0] = xbt_strdup("java");
 
   for (index = 0; index < argc - 1; index++) {
     jval = (jstring) (*env)->GetObjectArrayElement(env, jargs, index);
     tmp = (*env)->GetStringUTFChars(env, jval, 0);
-    argv[index + 1] = strdup(tmp);
+    argv[index + 1] = xbt_strdup(tmp);
     (*env)->ReleaseStringUTFChars(env, jval, tmp);
   }
   argv[argc] = NULL;
@@ -130,15 +133,14 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
   MSG_init(&argc, argv);
 
   JAVA_HOST_LEVEL = xbt_lib_add_level(host_lib, (void_f_pvoid_t) __JAVA_host_priv_free);
+  JAVA_STORAGE_LEVEL = xbt_lib_add_level(storage_lib, (void_f_pvoid_t) __JAVA_storage_priv_free);
 
   for (index = 0; index < argc; index++)
     free(argv[index]);
 
   free(argv);
 
-  if (smx_factory_initializer_to_use == SIMIX_ctx_cojava_factory_init)
-    XBT_INFO("Using Coroutines. Your simulation is on steroid.");
-  else if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
+  if (smx_factory_initializer_to_use == SIMIX_ctx_java_factory_init)
     XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
   else
     xbt_die("Unknown context factory. Please report bug.");
@@ -149,8 +151,9 @@ JNIEXPORT void JNICALL
 {
   msg_error_t rv;
   int index;
-  xbt_dynar_t hosts;
-  jobject jhost;
+  xbt_dynar_t hosts, storages;
+  jobject jhost, jstorage;
+
 
   /* Run everything */
   XBT_DEBUG("Ready to run MSG_MAIN");
@@ -169,6 +172,18 @@ JNIEXPORT void JNICALL
 
   }
   xbt_dynar_free(&hosts);
+
+  /* Cleanup java storages */
+  storages = MSG_storages_as_dynar();
+  if(!xbt_dynar_is_empty(storages)){
+    for (index = 0; index < xbt_dynar_length(storages) - 1; index++) {
+      jstorage = (jobject) xbt_lib_get_level(xbt_dynar_get_as(storages,index,msg_storage_t), JAVA_STORAGE_LEVEL);
+      if (jstorage)
+        jstorage_unref(env, jstorage);
+    }
+  }
+  xbt_dynar_free(&storages);
+
 }
 
 JNIEXPORT void JNICALL
@@ -268,7 +283,7 @@ static int create_jprocess(int argc, char *argv[]) {
   jclass class_Process = (*env)->FindClass(env, argv[0]);
   xbt_str_subst(argv[0],'/','.',0);
   //Retrieve the methodID for the constructor
-  xbt_assert((class_Process != NULL), "Class not found (%s).", argv[0]);
+  xbt_assert((class_Process != NULL), "Class not found (%s). The deployment file must use the fully qualified class name, including the package. The case is important.", argv[0]);
   jmethodID constructor_Process = (*env)->GetMethodID(env, class_Process, "<init>", "(Lorg/simgrid/msg/Host;Ljava/lang/String;[Ljava/lang/String;)V");
   xbt_assert((constructor_Process != NULL), "Constructor not found for class %s. Is there a (Host, String ,String[]) constructor in your class ?", argv[0]);
 
@@ -299,4 +314,3 @@ static int create_jprocess(int argc, char *argv[]) {
 
   return 0;
 }
-