Class RandomSeeder
- java.lang.Object
-
- io.github.pr0methean.betterrandom.util.Looper
-
- io.github.pr0methean.betterrandom.seed.RandomSeeder
-
- All Implemented Interfaces:
Serializable
- Direct Known Subclasses:
LegacyRandomSeeder
public class RandomSeeder extends Looper
Thread that loops overByteArrayReseedableRandom
instances and reseeds them. NoEntropyCountingRandom
will be reseeded when it's already had more input than output.- Author:
- Chris Hennick
- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
RandomSeeder.DefaultThreadFactory
AThreadFactory
that sets the name and priority of the threads it creates.
-
Field Summary
Fields Modifier and Type Field Description protected Set<ByteArrayReseedableRandom>
byteArrayPrngs
HoldsByteArrayReseedableRandom
instances that should be reseeded when their entropy is low, or as often as possible if they don't implementEntropyCountingRandom
.protected static long
DEFAULT_STOP_IF_EMPTY_FOR_NANOS
Default waiting time before an empty instance terminates if still empty.protected static long
FIRST_POLL_INTERVAL
Time in seconds to wait before checking again whether any PRNGs need more entropy, after one iteration when they didn't.protected static long
REPEAT_POLL_INTERVAL
Time in seconds to wait before checking again whether any PRNGs need more entropy, after more than one iteration when they didn't.protected SeedGenerator
seedGenerator
The seed generator this seeder uses.protected long
stopIfEmptyForNanos
Time in nanoseconds after which this thread will terminate if no PRNGs are attached.protected Condition
waitForEntropyDrain
Signaled by an associatedBaseRandom
when it runs out of entropy.protected Condition
waitWhileEmpty
Signaled when a PRNG is added.-
Fields inherited from class io.github.pr0methean.betterrandom.util.Looper
DEFAULT_THREAD_FACTORY, factory, lock, thread, threadLock
-
-
Constructor Summary
Constructors Constructor Description RandomSeeder(SeedGenerator seedGenerator)
Creates an instance using aRandomSeeder.DefaultThreadFactory
.RandomSeeder(SeedGenerator seedGenerator, ThreadFactory threadFactory)
Creates an instance whose thread will terminate if no PRNGs have been associated with it for 5 seconds.RandomSeeder(SeedGenerator seedGenerator, ThreadFactory threadFactory, long stopIfEmptyForNanos)
Creates an instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
add(ByteArrayReseedableRandom... randoms)
AddsByteArrayReseedableRandom
instances.void
add(Collection<? extends ByteArrayReseedableRandom> randoms)
AddsByteArrayReseedableRandom
instances.protected void
clear()
Removes all PRNGs from this seeder.boolean
contains(Object random)
Checks whether the given PRNG is currently registered with this RandomSeeder.boolean
equals(Object o)
SeedGenerator
getSeedGenerator()
Returns theSeedGenerator
this seeder uses.int
hashCode()
protected void
initTransientFields()
Initializes the transient instance fields for this class.boolean
isEmpty()
Returns true if noRandom
instances are registered with this LegacyRandomSeeder.protected boolean
iterate()
The task that will be iterated until it returns false.void
remove(ByteArrayReseedableRandom... randoms)
Removes PRNGs so that they will no longer be reseeded.void
remove(Collection<? extends ByteArrayReseedableRandom> randoms)
Removes PRNGs so that they will no longer be reseeded.protected boolean
reseedByteArrayReseedableRandoms(Iterable<? extends ByteArrayReseedableRandom> randoms)
Reseeds all the PRNGs that need reseeding inrandoms
.protected void
reseedWithLong(Random random)
Generates an 8-byte seed, converts it to a long and callsRandom.setSeed(long)
.void
shutDown()
Shut down this thread even ifRandom
instances are registered with it.protected boolean
stillEmptyAfterWaiting()
WaitsstopIfEmptyForNanos
forwaitWhileEmpty
to be signaledvoid
stopIfEmpty()
Shut down this thread if noRandom
instances are registered with it.protected static void
unregisterWithAll(Set<?> randoms)
Informs the given PRNGs that they no longer have a seed generator.protected void
waitForEntropyDrainOrUpdateFlag(boolean entropyConsumed)
If entropy was consumed this iteration, waits untilwakeUp()
is called or the polling interval expires.void
wakeUp()
Ensures this seeder's thread is started, and signals conditions it may be waiting on.
-
-
-
Field Detail
-
FIRST_POLL_INTERVAL
protected static final long FIRST_POLL_INTERVAL
Time in seconds to wait before checking again whether any PRNGs need more entropy, after one iteration when they didn't.- See Also:
- Constant Field Values
-
REPEAT_POLL_INTERVAL
protected static final long REPEAT_POLL_INTERVAL
Time in seconds to wait before checking again whether any PRNGs need more entropy, after more than one iteration when they didn't.- See Also:
- Constant Field Values
-
DEFAULT_STOP_IF_EMPTY_FOR_NANOS
protected static final long DEFAULT_STOP_IF_EMPTY_FOR_NANOS
Default waiting time before an empty instance terminates if still empty.- See Also:
- Constant Field Values
-
seedGenerator
protected final SeedGenerator seedGenerator
The seed generator this seeder uses.
-
byteArrayPrngs
protected transient Set<ByteArrayReseedableRandom> byteArrayPrngs
HoldsByteArrayReseedableRandom
instances that should be reseeded when their entropy is low, or as often as possible if they don't implementEntropyCountingRandom
.
-
waitWhileEmpty
protected transient Condition waitWhileEmpty
Signaled when a PRNG is added.
-
waitForEntropyDrain
protected transient Condition waitForEntropyDrain
Signaled by an associatedBaseRandom
when it runs out of entropy.
-
stopIfEmptyForNanos
protected final long stopIfEmptyForNanos
Time in nanoseconds after which this thread will terminate if no PRNGs are attached.
-
-
Constructor Detail
-
RandomSeeder
public RandomSeeder(SeedGenerator seedGenerator, ThreadFactory threadFactory)
Creates an instance whose thread will terminate if no PRNGs have been associated with it for 5 seconds.- Parameters:
seedGenerator
- the seed generatorthreadFactory
- theThreadFactory
that will create this seeder's thread
-
RandomSeeder
public RandomSeeder(SeedGenerator seedGenerator)
Creates an instance using aRandomSeeder.DefaultThreadFactory
.- Parameters:
seedGenerator
- the seed generator
-
RandomSeeder
public RandomSeeder(SeedGenerator seedGenerator, ThreadFactory threadFactory, long stopIfEmptyForNanos)
Creates an instance.- Parameters:
seedGenerator
- the seed generatorthreadFactory
- theThreadFactory
that will create this seeder's threadstopIfEmptyForNanos
- time in nanoseconds after which this thread will terminate if no PRNGs are attached
-
-
Method Detail
-
remove
public void remove(ByteArrayReseedableRandom... randoms)
Removes PRNGs so that they will no longer be reseeded.- Parameters:
randoms
- the PRNGs to remove
-
remove
public void remove(Collection<? extends ByteArrayReseedableRandom> randoms)
Removes PRNGs so that they will no longer be reseeded.- Parameters:
randoms
- the PRNGs to remove
-
contains
public boolean contains(Object random)
Checks whether the given PRNG is currently registered with this RandomSeeder.- Parameters:
random
- the PRNG to check the status of- Returns:
- true if registered; false if not
-
add
public void add(ByteArrayReseedableRandom... randoms)
AddsByteArrayReseedableRandom
instances.- Parameters:
randoms
- the PRNGs to start reseeding
-
add
public void add(Collection<? extends ByteArrayReseedableRandom> randoms)
AddsByteArrayReseedableRandom
instances.- Parameters:
randoms
- the PRNGs to start reseeding
-
wakeUp
public void wakeUp()
Ensures this seeder's thread is started, and signals conditions it may be waiting on.
-
initTransientFields
protected void initTransientFields()
Initializes the transient instance fields for this class. Called by constructors and during deserialization.- Overrides:
initTransientFields
in classLooper
-
iterate
protected boolean iterate()
Description copied from class:Looper
The task that will be iterated until it returns false. Cannot be abstract for serialization reasons, but must be overridden in subclasses if they are instantiated without a targetRunnable
.
-
stillEmptyAfterWaiting
protected boolean stillEmptyAfterWaiting() throws InterruptedException
WaitsstopIfEmptyForNanos
forwaitWhileEmpty
to be signaled- Returns:
- true if not signaled; false if signaled
- Throws:
InterruptedException
- if interrupted
-
waitForEntropyDrainOrUpdateFlag
protected void waitForEntropyDrainOrUpdateFlag(boolean entropyConsumed) throws InterruptedException
If entropy was consumed this iteration, waits untilwakeUp()
is called or the polling interval expires. Updates whether the next polling wait will be short or long.- Parameters:
entropyConsumed
- whether entropy was consumed this iteration- Throws:
InterruptedException
- if interrupted
-
reseedByteArrayReseedableRandoms
protected boolean reseedByteArrayReseedableRandoms(Iterable<? extends ByteArrayReseedableRandom> randoms)
Reseeds all the PRNGs that need reseeding inrandoms
.- Parameters:
randoms
- the PRNGs to reseed- Returns:
- true if at least one PRNG was reseeded; false otherwise
-
reseedWithLong
protected void reseedWithLong(Random random)
Generates an 8-byte seed, converts it to a long and callsRandom.setSeed(long)
.- Parameters:
random
- the PRNG to reseed
-
shutDown
public void shutDown()
Shut down this thread even ifRandom
instances are registered with it.
-
clear
protected void clear()
Removes all PRNGs from this seeder.
-
unregisterWithAll
protected static void unregisterWithAll(Set<?> randoms)
Informs the given PRNGs that they no longer have a seed generator. Only affects those that supportBaseRandom.setRandomSeeder(RandomSeeder)
.- Parameters:
randoms
- the PRNGs to unregister with
-
isEmpty
public boolean isEmpty()
Returns true if noRandom
instances are registered with this LegacyRandomSeeder.- Returns:
- true if no
Random
instances are registered with this LegacyRandomSeeder.
-
stopIfEmpty
public void stopIfEmpty()
Shut down this thread if noRandom
instances are registered with it.
-
getSeedGenerator
public SeedGenerator getSeedGenerator()
Returns theSeedGenerator
this seeder uses.- Returns:
- this seeder's
SeedGenerator
-
-