fixed GH #2217: UUIDGenerator should allow random seed

This commit is contained in:
Günter Obiltschnig
2020-01-21 21:30:48 +01:00
parent 71585b7daa
commit 2327592bab
2 changed files with 31 additions and 8 deletions

View File

@@ -37,7 +37,7 @@ class Foundation_API UUIDGenerator
/// as specified in Appendix A of the DCE 1.1 Remote Procedure /// as specified in Appendix A of the DCE 1.1 Remote Procedure
/// Call Specification (http://www.opengroup.org/onlinepubs/9629399/), /// Call Specification (http://www.opengroup.org/onlinepubs/9629399/),
/// RFC 2518 (WebDAV), section 6.4.1 and the UUIDs and GUIDs internet /// RFC 2518 (WebDAV), section 6.4.1 and the UUIDs and GUIDs internet
/// draft by Leach/Salz from February, 1998 /// draft by Leach/Salz from February, 1998
/// (http://ftp.ics.uci.edu/pub/ietf/webdav/uuid-guid/draft-leach-uuids-guids-01.txt) /// (http://ftp.ics.uci.edu/pub/ietf/webdav/uuid-guid/draft-leach-uuids-guids-01.txt)
{ {
public: public:
@@ -53,7 +53,7 @@ public:
/// ///
/// Throws a SystemException if no MAC address can be /// Throws a SystemException if no MAC address can be
/// obtained. /// obtained.
UUID createFromName(const UUID& nsid, const std::string& name); UUID createFromName(const UUID& nsid, const std::string& name);
/// Creates a name-based UUID. /// Creates a name-based UUID.
@@ -66,21 +66,28 @@ public:
UUID createFromName(const UUID& nsid, const std::string& name, DigestEngine& de, UUID::Version version); UUID createFromName(const UUID& nsid, const std::string& name, DigestEngine& de, UUID::Version version);
/// Creates a name-based UUID, using the given digest engine and version. /// Creates a name-based UUID, using the given digest engine and version.
UUID createRandom(); UUID createRandom();
/// Creates a random UUID. /// Creates a random UUID.
UUID createOne(); UUID createOne();
/// Tries to create and return a time-based UUID (see create()), and, /// Tries to create and return a time-based UUID (see create()), and,
/// if that does not work due to the unavailability of a MAC address, /// if that does not work due to the unavailability of a MAC address,
/// creates and returns a random UUID (see createRandom()). /// creates and returns a random UUID (see createRandom()).
/// ///
/// The UUID::version() method can be used to determine the actual kind of /// The UUID::version() method can be used to determine the actual kind of
/// the UUID generated. /// the UUID generated.
void seed(UInt32 n);
/// Seeds the internal pseudo random generator for time-based UUIDs with the given seed.
void seed();
/// Seeds the internal pseudo random generator used for time-based UUIDs
/// with a random seed obtained from a RandomInputStream.
static UUIDGenerator& defaultGenerator(); static UUIDGenerator& defaultGenerator();
/// Returns a reference to the default UUIDGenerator. /// Returns a reference to the default UUIDGenerator.
protected: protected:
Timestamp::UtcTimeVal timeStamp(); Timestamp::UtcTimeVal timeStamp();
void getNode(); void getNode();
@@ -92,7 +99,7 @@ private:
int _ticks; int _ticks;
Environment::NodeId _node; Environment::NodeId _node;
bool _haveNode; bool _haveNode;
UUIDGenerator(const UUIDGenerator&); UUIDGenerator(const UUIDGenerator&);
UUIDGenerator& operator = (const UUIDGenerator&); UUIDGenerator& operator = (const UUIDGenerator&);
}; };

View File

@@ -90,7 +90,7 @@ UUID UUIDGenerator::createFromName(const UUID& nsid, const std::string& name, Di
return UUID(buffer, version); return UUID(buffer, version);
} }
UUID UUIDGenerator::createRandom() UUID UUIDGenerator::createRandom()
{ {
char buffer[16]; char buffer[16];
@@ -136,6 +136,22 @@ UUID UUIDGenerator::createOne()
} }
void UUIDGenerator::seed()
{
Poco::FastMutex::ScopedLock lock(_mutex);
_random.seed();
}
void UUIDGenerator::seed(UInt32 n)
{
Poco::FastMutex::ScopedLock lock(_mutex);
_random.seed(n);
}
namespace namespace
{ {
static SingletonHolder<UUIDGenerator> sh; static SingletonHolder<UUIDGenerator> sh;