1 /* 2 * Copyright (c) 1998-2002 The Jgroup Team. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License version 2 as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU Lesser General Public License for more details. 12 * 13 * You should have received a copy of the GNU Lesser General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 16 * 17 */ 18 19 package jgroup.relacs.mss; 20 21 import java.util.Random; 22 23 /** 24 * Class for generating random and timeout values. 25 * 26 * @author Salvatore Cammarata 27 * @author Alberto Montresor 28 * @author Hein Meling 29 * @since Jgroup 1.2 30 */ 31 final class RandomGenerator 32 { 33 34 //////////////////////////////////////////////////////////////////////////////////////////// 35 // Static section 36 //////////////////////////////////////////////////////////////////////////////////////////// 37 38 /** Random generator */ 39 private static final Random random = new Random(); 40 41 42 //////////////////////////////////////////////////////////////////////////////////////////// 43 // Probabilistic Parameters 44 //////////////////////////////////////////////////////////////////////////////////////////// 45 46 // FIXME Change this static fields 47 private static final long RANDOM_SECTOR = 4; 48 49 /* Random part to be used for computing a random value. */ 50 private static long RANDOM_TIMEOUT; 51 52 /* Fixed part to be used for computing a random value. */ 53 private static long FIXED_TIMEOUT; 54 55 /* Last NACK deadline; initialized with the current time. */ 56 private static long lastNackDeadline = System.currentTimeMillis(); 57 58 59 //////////////////////////////////////////////////////////////////////////////////////////// 60 // Static methods 61 //////////////////////////////////////////////////////////////////////////////////////////// 62 63 /** 64 * Generate a positive integer within the integer range. This is 65 * used to generate message identifiers. 66 */ 67 public static int newNonce() 68 { 69 return random.nextInt(Integer.MAX_VALUE); 70 } 71 72 73 /** 74 * Compute a routing timeout value based on a random value and the 75 * values previously set using the <code>setRoutingTimeout</code> 76 * method. 77 */ 78 public static long getRoutingTimeout() { 79 return (long) (random.nextDouble() * RANDOM_TIMEOUT + FIXED_TIMEOUT); 80 } 81 82 83 /** 84 * Compute routing timeout values based on the given value. 85 */ 86 public static void setRoutingTimeout(long rt) 87 { 88 RANDOM_TIMEOUT = rt / RANDOM_SECTOR; 89 FIXED_TIMEOUT = rt - RANDOM_TIMEOUT; 90 } 91 92 93 /** 94 * Compute a random timeout value based on the given value. 95 */ 96 public static long getRandomTimeout(long rt) 97 { 98 long randomTimeout = rt / RANDOM_SECTOR; 99 long fixedTimeout = rt - randomTimeout; 100 101 double deltaRandom = random.nextDouble() * randomTimeout; 102 long deltaNew = (long) (deltaRandom + fixedTimeout); 103 104 long current = System.currentTimeMillis(); 105 if (deltaNew + current < lastNackDeadline) { 106 lastNackDeadline += deltaRandom; 107 deltaNew = lastNackDeadline - current; 108 } else { 109 lastNackDeadline = current + deltaNew; 110 } 111 return deltaNew; 112 } 113 114 } // END RandomGenerator