/*
 * Copyright (c) 2014, Friedrich-Alexander University Erlangen-Nuremberg.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the university nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 */

package org.contikios.cooja;

import java.util.Random;

/**
 * This ensures that the functions of the random number generator are
 * only called by the the thread initializing a simulation or the
 * simulation thread itself.
 * Rationale: By allowing another thread to use the random number
 * generator concurrency is introduced, thus it can not be guaranteed
 * that simulations are reproducible.
 *
 */
public class SafeRandom extends Random {
  
  Simulation sim = null;
  Thread initThread = null;
  
  private void assertSimThread() {
    // It we are in the simulation thread everything is fine (the default)
    // sim can be null, because setSeed is called by the super-constructor.
    if(sim != null && !sim.isSimulationThread()) {
      // The thread initializing the simulation might differ from the simulation thread.
      // If they are the same that is ok, too.
      if(initThread == null) initThread = Thread.currentThread();
      if(Thread.currentThread() == initThread ) return;
      throw new RuntimeException("A random-function was not called from the simulation thread. This can break things!");
    }
  }
  
  public SafeRandom(Simulation sim) {
    // assertSimThread is called by the super-constructor.
    super();
    this.sim = sim;
  }
  
  public SafeRandom(Simulation sim, long seed) {
    // assertSimThread is called by the super-constructor.
    super(seed);
    this.sim = sim;
  }
  
  synchronized public void setSeed(long seed) {
    assertSimThread();
    super.setSeed(seed);
  }
  
  /*
   * This function is called by all functions returning random numbers
   * @see java.util.Random#next(int)
   */
  protected int next(int bits) {
    assertSimThread();
    return super.next(bits);
  }
  
}

@@ -2098,16 +2098,46 @@
 = null;%0A
+  Boolean simStarted = false;%0A
   %0A  pri
@@ -2170,83 +2170,8 @@
 ) %7B%0A
-    // It we are in the simulation thread everything is fine (the default)%0A
     
@@ -2257,308 +2257,629 @@
 sim 
-!
+=
 = null
- && !sim.isSimulationThread()) %7B%0A      // The thread initializing the simulation might differ from the simulation thread.%0A      // If they are the same that is ok, too.%0A      if(initThread == null) initThread = Thread.currentThread();%0A      if(Thread.currentThread() == initThread ) return;%0A  
+) return;%0A    %0A    // If we are in the simulation thread, everything is fine (the default)%0A    if(sim.isSimulationThread()) %7B%0A      simStarted = true;%0A      return;%0A    %7D%0A    %0A    // Simulation has not started yet. Allow one initialisation thread.%0A    if(!simStarted) %7B%0A      if(initThread == null) initThread = Thread.currentThread();%0A      if(Thread.currentThread() == initThread ) return;%0A    %7D%0A    %0A    //This is done via the GUI - reproducibility is lost anyway!%0A    if(javax.swing.SwingUtilities.isEventDispatchThread()) return;%0A    %0A    // Some other threads seems to access the PNRG. This must not happen.%0A
     
@@ -2997,17 +2997,16 @@
 %22);%0A    
-%7D
 %0A  %7D%0A  %0A

Fix SafeRandom logic