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 overByteArrayReseedableRandominstances and reseeds them. NoEntropyCountingRandomwill 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 classRandomSeeder.DefaultThreadFactoryAThreadFactorythat sets the name and priority of the threads it creates.
-
Field Summary
Fields Modifier and Type Field Description protected Set<ByteArrayReseedableRandom>byteArrayPrngsHoldsByteArrayReseedableRandominstances that should be reseeded when their entropy is low, or as often as possible if they don't implementEntropyCountingRandom.protected static longDEFAULT_STOP_IF_EMPTY_FOR_NANOSDefault waiting time before an empty instance terminates if still empty.protected static longFIRST_POLL_INTERVALTime in seconds to wait before checking again whether any PRNGs need more entropy, after one iteration when they didn't.protected static longREPEAT_POLL_INTERVALTime in seconds to wait before checking again whether any PRNGs need more entropy, after more than one iteration when they didn't.protected SeedGeneratorseedGeneratorThe seed generator this seeder uses.protected longstopIfEmptyForNanosTime in nanoseconds after which this thread will terminate if no PRNGs are attached.protected ConditionwaitForEntropyDrainSignaled by an associatedBaseRandomwhen it runs out of entropy.protected ConditionwaitWhileEmptySignaled 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 voidadd(ByteArrayReseedableRandom... randoms)AddsByteArrayReseedableRandominstances.voidadd(Collection<? extends ByteArrayReseedableRandom> randoms)AddsByteArrayReseedableRandominstances.protected voidclear()Removes all PRNGs from this seeder.booleancontains(Object random)Checks whether the given PRNG is currently registered with this RandomSeeder.booleanequals(Object o)SeedGeneratorgetSeedGenerator()Returns theSeedGeneratorthis seeder uses.inthashCode()protected voidinitTransientFields()Initializes the transient instance fields for this class.booleanisEmpty()Returns true if noRandominstances are registered with this LegacyRandomSeeder.protected booleaniterate()The task that will be iterated until it returns false.voidremove(ByteArrayReseedableRandom... randoms)Removes PRNGs so that they will no longer be reseeded.voidremove(Collection<? extends ByteArrayReseedableRandom> randoms)Removes PRNGs so that they will no longer be reseeded.protected booleanreseedByteArrayReseedableRandoms(Iterable<? extends ByteArrayReseedableRandom> randoms)Reseeds all the PRNGs that need reseeding inrandoms.protected voidreseedWithLong(Random random)Generates an 8-byte seed, converts it to a long and callsRandom.setSeed(long).voidshutDown()Shut down this thread even ifRandominstances are registered with it.protected booleanstillEmptyAfterWaiting()WaitsstopIfEmptyForNanosforwaitWhileEmptyto be signaledvoidstopIfEmpty()Shut down this thread if noRandominstances are registered with it.protected static voidunregisterWithAll(Set<?> randoms)Informs the given PRNGs that they no longer have a seed generator.protected voidwaitForEntropyDrainOrUpdateFlag(boolean entropyConsumed)If entropy was consumed this iteration, waits untilwakeUp()is called or the polling interval expires.voidwakeUp()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
HoldsByteArrayReseedableRandominstances 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 associatedBaseRandomwhen 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- theThreadFactorythat 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- theThreadFactorythat 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)
AddsByteArrayReseedableRandominstances.- Parameters:
randoms- the PRNGs to start reseeding
-
add
public void add(Collection<? extends ByteArrayReseedableRandom> randoms)
AddsByteArrayReseedableRandominstances.- 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:
initTransientFieldsin classLooper
-
iterate
protected boolean iterate()
Description copied from class:LooperThe 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 InterruptedExceptionWaitsstopIfEmptyForNanosforwaitWhileEmptyto be signaled- Returns:
- true if not signaled; false if signaled
- Throws:
InterruptedException- if interrupted
-
waitForEntropyDrainOrUpdateFlag
protected void waitForEntropyDrainOrUpdateFlag(boolean entropyConsumed) throws InterruptedExceptionIf 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 ifRandominstances 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 noRandominstances are registered with this LegacyRandomSeeder.- Returns:
- true if no
Randominstances are registered with this LegacyRandomSeeder.
-
stopIfEmpty
public void stopIfEmpty()
Shut down this thread if noRandominstances are registered with it.
-
getSeedGenerator
public SeedGenerator getSeedGenerator()
Returns theSeedGeneratorthis seeder uses.- Returns:
- this seeder's
SeedGenerator
-
-