Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[java] Fix RngStream.setPackageSeed
authorGabriel Corona <gabriel.corona@loria.fr>
Mon, 18 Jul 2016 10:23:22 +0000 (12:23 +0200)
committerGabriel Corona <gabriel.corona@loria.fr>
Mon, 18 Jul 2016 10:23:36 +0000 (12:23 +0200)
* it is static so the first argument is not an instance;

* we need to convert jint to unsigned long to make the Java binding
  talk to the C binding correctly.

examples/java/app/bittorrent/Main.java
src/bindings/java/jmsg_rngstream.cpp

index 8c7fb2c..b918505 100644 (file)
@@ -8,6 +8,7 @@ package app.bittorrent;
 
 import org.simgrid.msg.Msg;
 import org.simgrid.msg.MsgException;
 
 import org.simgrid.msg.Msg;
 import org.simgrid.msg.MsgException;
+import org.simgrid.msg.RngStream;
 
 class Main{
   private Main() {
 
 class Main{
   private Main() {
@@ -15,6 +16,9 @@ class Main{
   }
 
   public static void main(String[] args) throws MsgException {
   }
 
   public static void main(String[] args) throws MsgException {
+    int[] seed = { 12345, 12345, 12345, 12345, 12345, 12345 };
+    RngStream.setPackageSeed(seed);
+
     Msg.init(args);
     if(args.length < 2) {
       Msg.info("Usage   : Bittorrent platform_file deployment_file");
     Msg.init(args);
     if(args.length < 2) {
       Msg.info("Usage   : Bittorrent platform_file deployment_file");
index 11db6a5..2dda4ca 100644 (file)
@@ -6,6 +6,8 @@
 /* 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. */
 
 /* 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 <xbt/sysdep.h>
+
 #include "jmsg_rngstream.h"
 #include "jxbt_utilities.h"
 
 #include "jmsg_rngstream.h"
 #include "jxbt_utilities.h"
 
@@ -43,16 +45,22 @@ JNIEXPORT void JNICALL Java_org_simgrid_msg_RngStream_nativeFinalize(JNIEnv *env
 
 JNIEXPORT jboolean JNICALL
 Java_org_simgrid_msg_RngStream_setPackageSeed(JNIEnv *env, jobject jrngstream, jintArray jseed) {
 
 JNIEXPORT jboolean JNICALL
 Java_org_simgrid_msg_RngStream_setPackageSeed(JNIEnv *env, jobject jrngstream, jintArray jseed) {
-  jint buffer[6];
 
 
-  env->GetIntArrayRegion(jseed, 0, 6, buffer);
-
-  RngStream rngstream = jrngstream_to_native(env, jrngstream);
-  if (!rngstream)
+  if (jseed == nullptr) {
+    jxbt_throw_null(env, xbt_strdup("seed argument is null"));
     return JNI_FALSE;
     return JNI_FALSE;
+  }
+
+  jint buffer[6];
+  env->GetIntArrayRegion(jseed, 0, 6, buffer);
 
 
-  int result = RngStream_SetPackageSeed((unsigned long*)buffer);
+  // The C API expects unsigned long which are wider than int on LP64.
+  // We need to convert:
+  unsigned long seed[6];
+  for (int i = 0; i != 6; ++i)
+    seed[i] = buffer[i];
 
 
+  int result = RngStream_SetPackageSeed(seed);
   return result == -1 ? JNI_FALSE : JNI_TRUE;
 }
 
   return result == -1 ? JNI_FALSE : JNI_TRUE;
 }