Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more informative error message
[simgrid.git] / src / bindings / java / jmsg.c
index 41d7f36..ce3444a 100644 (file)
@@ -1,10 +1,10 @@
 /* Java Wrappers to the MSG API.                                            */
 
-/* Copyright (c) 2007-2012. The SimGrid Team.
+/* Copyright (c) 2007-2014. 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. */
+ * under the terms of the license (GNU LGPL) which comes with this package. */
 
 #include <msg/msg.h>
 #include <simgrid/simix.h>
@@ -16,6 +16,8 @@
 
 #include "jmsg_process.h"
 
+#include "jmsg_as.h"
+
 #include "jmsg_host.h"
 #include "jmsg_task.h"
 #include "jxbt_utilities.h"
@@ -31,6 +33,8 @@
 #endif
 /* end of eclipse-mandated pimple */
 
+int JAVA_HOST_LEVEL;
+
 static int create_jprocess(int argc, char *argv[]);
 
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(jmsg);
@@ -62,7 +66,7 @@ void jmsg_throw_status(JNIEnv *env, msg_error_t status) {
         jxbt_throw_host_failure(env,NULL);
     break;
     default:
-        jxbt_throw_native(env,bprintf("communication failed"));
+        jxbt_throw_native(env,xbt_strdup("communication failed"));
   }
 }
 
@@ -77,6 +81,10 @@ Java_org_simgrid_msg_Msg_getClock(JNIEnv * env, jclass cls)
   return (jdouble) MSG_get_clock();
 }
 
+static void __JAVA_host_priv_free(void *host)
+{
+}
+
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
 {
@@ -86,16 +94,17 @@ Java_org_simgrid_msg_Msg_init(JNIEnv * env, jclass cls, jobjectArray jargs)
   jstring jval;
   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")) {
-       XBT_INFO("Using Coroutines. Your simulation is on steroid.");
-       smx_factory_initializer_to_use = SIMIX_ctx_cojava_factory_init;
-  }
-  else {
-       XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
-       smx_factory_initializer_to_use = SIMIX_ctx_java_factory_init;
-  }
+  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;
   jthrowable exc = (*env)->ExceptionOccurred(env);
   if (exc) {
     (*env)->ExceptionClear(env);
@@ -120,10 +129,19 @@ 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);
+
   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)
+    XBT_INFO("Using regular java threads. Coroutines could speed your simulation up.");
+  else
+    xbt_die("Unknown context factory. Please report bug.");
 }
 
 JNIEXPORT void JNICALL
@@ -139,14 +157,13 @@ JNIEXPORT void JNICALL
   rv = MSG_main();
   XBT_DEBUG("Done running MSG_MAIN");
   jxbt_check_res("MSG_main()", rv, MSG_OK,
-                 bprintf
-                 ("unexpected error : MSG_main() failed .. please report this bug "));
+                 xbt_strdup("unexpected error : MSG_main() failed .. please report this bug "));
 
   XBT_INFO("MSG_main finished; Cleaning up the simulation...");
   /* Cleanup java hosts */
   hosts = MSG_hosts_as_dynar();
   for (index = 0; index < xbt_dynar_length(hosts) - 1; index++) {
-    jhost = (jobject) MSG_host_get_data(xbt_dynar_get_as(hosts,index,msg_host_t));
+    jhost = (jobject) xbt_lib_get_level(xbt_dynar_get_as(hosts,index,msg_host_t), JAVA_HOST_LEVEL);
     if (jhost)
       jhost_unref(env, jhost);
 
@@ -166,6 +183,26 @@ Java_org_simgrid_msg_Msg_createEnvironment(JNIEnv * env, jclass cls,
 
   (*env)->ReleaseStringUTFChars(env, jplatformFile, platformFile);
 }
+
+JNIEXPORT jobject JNICALL
+Java_org_simgrid_msg_Msg_environmentGetRoutingRoot(JNIEnv * env, jclass cls)
+{
+  msg_as_t as = MSG_environment_get_routing_root();
+  jobject jas = jas_new_instance(env);
+  if (!jas) {
+    jxbt_throw_jni(env, "java As instantiation failed");
+    return NULL;
+  }
+  jas = jas_ref(env, jas);
+  if (!jas) {
+    jxbt_throw_jni(env, "new global ref allocation failed");
+    return NULL;
+  }
+  jas_bind(jas, as, env);
+
+  return (jobject) jas;
+}
+
 JNIEXPORT void JNICALL
 Java_org_simgrid_msg_Msg_debug(JNIEnv * env, jclass cls, jstring js)
 {
@@ -231,7 +268,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.");
+  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]);