1 //
2 // UUIDGenerator.h
3 //
4 // Library: Foundation
5 // Package: UUID
6 // Module:  UUID
7 //
8 // Definition of the UUIDGenerator class.
9 //
10 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
11 // and Contributors.
12 //
13 // SPDX-License-Identifier:	BSL-1.0
14 //
15 
16 
17 #ifndef Foundation_UUIDGenerator_INCLUDED
18 #define Foundation_UUIDGenerator_INCLUDED
19 
20 
21 #include "Poco/Foundation.h"
22 #include "Poco/UUID.h"
23 #include "Poco/Mutex.h"
24 #include "Poco/Random.h"
25 #include "Poco/Timestamp.h"
26 #include "Poco/Environment.h"
27 
28 
29 namespace Poco {
30 
31 
32 class DigestEngine;
33 
34 
35 class Foundation_API UUIDGenerator
36 	/// This class implements a generator for Universal Unique Identifiers,
37 	/// as specified in Appendix A of the DCE 1.1 Remote Procedure
38 	/// Call Specification (http://www.opengroup.org/onlinepubs/9629399/),
39 	/// RFC 2518 (WebDAV), section 6.4.1 and the UUIDs and GUIDs internet
40 	/// draft by Leach/Salz from February, 1998
41 	/// (http://ftp.ics.uci.edu/pub/ietf/webdav/uuid-guid/draft-leach-uuids-guids-01.txt)
42 {
43 public:
44 	UUIDGenerator();
45 		/// Creates the UUIDGenerator.
46 
47 	~UUIDGenerator();
48 		/// Destroys the UUIDGenerator.
49 
50 	UUID create();
51 		/// Creates a new time-based UUID, using the MAC address of
52 		/// one of the system's ethernet adapters.
53 		///
54 		/// Throws a SystemException if no MAC address can be
55 		/// obtained.
56 
57 	UUID createFromName(const UUID& nsid, const std::string& name);
58 		/// Creates a name-based UUID.
59 
60 	UUID createFromName(const UUID& nsid, const std::string& name, DigestEngine& de);
61 		/// Creates a name-based UUID, using the given digest engine.
62 		///
63 		/// Note: in order to create a standard-compliant UUID, the given DigestEngine
64 		/// must be either an instance of MD5Engine or SHA1Engine. The version field of
65 		/// the UUID will be set accordingly.
66 
67 	UUID createFromName(const UUID& nsid, const std::string& name, DigestEngine& de, UUID::Version version);
68 		/// Creates a name-based UUID, using the given digest engine and version.
69 
70 	UUID createRandom();
71 		/// Creates a random UUID.
72 
73 	UUID createOne();
74 		/// Tries to create and return a time-based UUID (see create()), and,
75 		/// if that does not work due to the unavailability of a MAC address,
76 		/// creates and returns a random UUID (see createRandom()).
77 		///
78 		/// The UUID::version() method can be used to determine the actual kind of
79 		/// the UUID generated.
80 
81 	void seed(UInt32 n);
82 		/// Seeds the internal pseudo random generator for time-based UUIDs with the given seed.
83 
84 	void seed();
85 		/// Seeds the internal pseudo random generator used for time-based UUIDs
86 		/// with a random seed obtained from a RandomInputStream.
87 
88 	static UUIDGenerator& defaultGenerator();
89 		/// Returns a reference to the default UUIDGenerator.
90 
91 protected:
92 	Timestamp::UtcTimeVal timeStamp();
93 	void getNode();
94 
95 private:
96 	FastMutex           _mutex;
97 	Random              _random;
98 	Timestamp           _lastTime;
99 	int                 _ticks;
100 	Environment::NodeId _node;
101 	bool                _haveNode;
102 
103 	UUIDGenerator(const UUIDGenerator&);
104 	UUIDGenerator& operator = (const UUIDGenerator&);
105 };
106 
107 
108 } // namespace Poco
109 
110 
111 #endif // Foundation_UUIDGenerator_INCLUDED
112