author | Stefan Vogel <sv@exept.de> |
Fri, 24 Apr 2015 14:18:00 +0200 | |
changeset 3542 | b00b42c1378d |
parent 3122 | 9d0a142eee9f |
child 3549 | ae696bf10bf5 |
permissions | -rw-r--r-- |
3542 | 1 |
"{ Encoding: utf8 }" |
2 |
||
1188 | 3 |
" |
4 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
5 |
All Rights Reserved |
|
6 |
||
7 |
This software is furnished under a license and may be used |
|
8 |
only in accordance with the terms of that license and with the |
|
9 |
inclusion of the above copyright notice. This software may not |
|
10 |
be provided or otherwise made available to, or used by, any |
|
11 |
other person. No title to or ownership of the software is |
|
12 |
hereby transferred. |
|
13 |
" |
|
14 |
"{ Package: 'stx:libbasic2' }" |
|
15 |
||
3542 | 16 |
"{ NameSpace: Smalltalk }" |
17 |
||
1188 | 18 |
ByteArray variableByteSubclass:#UUID |
19 |
instanceVariableNames:'' |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
20 |
classVariableNames:'CachedMACAddress Lock SequenceNumber LastTime Increment |
2977 | 21 |
NameSpaceToUuidBytes' |
1188 | 22 |
poolDictionaries:'' |
1304 | 23 |
category:'Net-Communication-Support' |
1188 | 24 |
! |
25 |
||
26 |
!UUID class methodsFor:'documentation'! |
|
27 |
||
28 |
copyright |
|
29 |
" |
|
30 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
31 |
All Rights Reserved |
|
32 |
||
33 |
This software is furnished under a license and may be used |
|
34 |
only in accordance with the terms of that license and with the |
|
35 |
inclusion of the above copyright notice. This software may not |
|
36 |
be provided or otherwise made available to, or used by, any |
|
37 |
other person. No title to or ownership of the software is |
|
38 |
hereby transferred. |
|
39 |
" |
|
1213 | 40 |
! |
41 |
||
42 |
documentation |
|
43 |
" |
|
1544 | 44 |
128-bit Universal Unique Ids (UUIDs) as defined by OpenGroup/DCE |
45 |
http://www.opengroup.org/onlinepubs/9629399/apdxa.htm. |
|
1213 | 46 |
|
1602 | 47 |
See also RFC4122. |
48 |
||
1213 | 49 |
A UUID is unique in time and space (at least until about Year 3400). |
50 |
||
1602 | 51 |
Several fields if the UUID are interpreted as integers, so host/network byte |
52 |
order is relevant. UUIDs are stored in a ByteArray in network byte order (MSB-first), |
|
53 |
so they may be exported/imported between different machines. |
|
54 |
||
55 |
You can import UUIDs in host byte order using #fromNativeBytes: |
|
56 |
||
1213 | 57 |
[author:] |
58 |
||
59 |
[instance variables:] |
|
60 |
||
61 |
[class variables:] |
|
62 |
||
63 |
[see also:] |
|
64 |
||
65 |
" |
|
66 |
! ! |
|
67 |
||
68 |
!UUID class methodsFor:'initialization'! |
|
69 |
||
70 |
initialize |
|
71 |
"I want to get informed about image restarts" |
|
72 |
||
73 |
Lock isNil ifTrue:[ |
|
2977 | 74 |
Lock := RecursionLock new name:#UUID. |
1213 | 75 |
LastTime := 0. |
76 |
Increment := 0. |
|
77 |
ObjectMemory addDependent:self. |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
78 |
|
2977 | 79 |
NameSpaceToUuidBytes := Dictionary withKeysAndValues:#( |
80 |
DNS #[16r6B 16rA7 16rB8 16r10 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8] |
|
81 |
URL #[16r6B 16rA7 16rB8 16r11 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8] |
|
82 |
"ASN.1 OID in DER or as Text" |
|
83 |
OID #[16r6B 16rA7 16rB8 16r12 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8] |
|
84 |
"X.500 DN as DER or as Text" |
|
85 |
X500 #[16r6B 16rA7 16rB8 16r14 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8] |
|
86 |
). |
|
1213 | 87 |
] |
88 |
! |
|
89 |
||
90 |
update:something with:aParameter from:changedObject |
|
91 |
"flush cached MAC address (may have been restarted on another host)" |
|
92 |
||
93 |
(something == #restarted) ifTrue:[ |
|
94 |
CachedMACAddress := nil. |
|
95 |
SequenceNumber := nil. |
|
96 |
] |
|
1188 | 97 |
! ! |
98 |
||
99 |
!UUID class methodsFor:'instance creation'! |
|
100 |
||
1923
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
101 |
basicNew |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
102 |
^ super basicNew:16 |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
103 |
! |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
104 |
|
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
105 |
basicNew:size |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
106 |
"allow creating with exact size. We need this for XMLStandardDecoder" |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
107 |
|
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
108 |
size ~~ 16 ifTrue:[ |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
109 |
^ self shouldNotImplement. |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
110 |
]. |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
111 |
^ super basicNew:size. |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
112 |
! |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
113 |
|
2017 | 114 |
decodeFromLiteralArray:anArray |
2018 | 115 |
anArray size == 2 ifTrue:[ |
116 |
"/ UUID 'uuid-string' |
|
117 |
^ self readFrom:(anArray at:2). |
|
118 |
]. |
|
119 |
"/ UUID bytes... |
|
120 |
^ super decodeFromLiteralArray:anArray "/ self readFrom:(anArray at:2) |
|
2017 | 121 |
|
122 |
" |
|
2018 | 123 |
(UUID decodeFromLiteralArray:#(UUID '5b023ce0-41f1-11dd-b99f-001558137da0')) |
124 |
literalArrayEncoding |
|
2017 | 125 |
" |
126 |
! |
|
127 |
||
1382 | 128 |
fromBytes:aByteArray |
1602 | 129 |
"set uuid from aByteArray. |
130 |
aByteArray must be 16 bytes in network byte order (MSB-first)" |
|
1382 | 131 |
|
1602 | 132 |
|uuid| |
1188 | 133 |
|
1923
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
134 |
uuid := super basicNew:16. |
1188 | 135 |
uuid replaceFrom:1 to:16 with:aByteArray. |
1602 | 136 |
^ uuid. |
1188 | 137 |
|
1602 | 138 |
" |
139 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
140 |
16r05 16r06 16r07 16r08 |
|
141 |
16r09 16r10 16r11 16r12 |
|
142 |
16r13 16r14 16r15 16r16]. |
|
143 |
" |
|
144 |
! |
|
1188 | 145 |
|
2950 | 146 |
fromBytes:aByteArray MSB:msb |
1602 | 147 |
"Set the UUID from aByteArray. UUIDS are stored internally as MSB-first. |
148 |
So, alway set |
|
149 |
msb = true if reading from network or persistent storage" |
|
1188 | 150 |
|
1602 | 151 |
|uuid| |
152 |
||
153 |
uuid := self fromBytes:aByteArray. |
|
154 |
msb ifFalse:[ |
|
155 |
self adjustByteOrder:uuid. |
|
1188 | 156 |
]. |
157 |
||
158 |
^ uuid |
|
159 |
||
160 |
" |
|
161 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
162 |
16r05 16r06 16r07 16r08 |
|
163 |
16r09 16r10 16r11 16r12 |
|
164 |
16r13 16r14 16r15 16r16] msb:false. |
|
165 |
" |
|
166 |
" |
|
167 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
168 |
16r05 16r06 16r07 16r08 |
|
169 |
16r09 16r10 16r11 16r12 |
|
170 |
16r13 16r14 16r15 16r16] msb:true. |
|
171 |
" |
|
172 |
! |
|
173 |
||
2950 | 174 |
fromBytes:aByteArray msb:msb |
2978 | 175 |
<resource: #obsolete> |
2950 | 176 |
"Set the UUID from aByteArray. UUIDS are stored internally as MSB-first. |
177 |
So, alway set |
|
178 |
msb = true if reading from network or persistent storage" |
|
179 |
||
180 |
self obsoleteMethodWarning:'use fromBytes:MSB:'. |
|
181 |
^ self fromBytes:aByteArray MSB:msb |
|
182 |
||
183 |
" |
|
184 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
185 |
16r05 16r06 16r07 16r08 |
|
186 |
16r09 16r10 16r11 16r12 |
|
187 |
16r13 16r14 16r15 16r16] msb:false. |
|
188 |
" |
|
189 |
" |
|
190 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
191 |
16r05 16r06 16r07 16r08 |
|
192 |
16r09 16r10 16r11 16r12 |
|
193 |
16r13 16r14 16r15 16r16] msb:true. |
|
194 |
" |
|
195 |
! |
|
196 |
||
1602 | 197 |
fromNativeBytes:aByteArray |
198 |
"convert bytes to uuid. |
|
199 |
aByteArray represents a UUID in host byte order |
|
200 |
- e.g. an UUID fetched from the Windows OS" |
|
201 |
||
202 |
|uuid| |
|
203 |
||
204 |
uuid := self fromBytes:aByteArray. |
|
3542 | 205 |
IsBigEndian ifFalse:[ |
1602 | 206 |
self adjustByteOrder:uuid. |
207 |
]. |
|
208 |
^ uuid. |
|
209 |
||
210 |
" |
|
211 |
UUID fromNativeBytes:#[16r01 16r02 16r03 16r04 |
|
212 |
16r05 16r06 16r07 16r08 |
|
213 |
16r09 16r10 16r11 16r12 |
|
214 |
16r13 16r14 16r15 16r16]. |
|
215 |
||
216 |
" |
|
217 |
! |
|
218 |
||
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
219 |
genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
220 |
"generate a new random UUID" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
221 |
|
1923
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
222 |
^ (super basicNew:16) genRandomUUID |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
223 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
224 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
225 |
self genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
226 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
227 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
228 |
|
2431 | 229 |
genTimestampUUID |
230 |
"generate a new timestamp UUID" |
|
231 |
||
232 |
^ (super basicNew:16) genTimestampUUID |
|
233 |
||
234 |
" |
|
235 |
self genTimestampUUID |
|
236 |
" |
|
237 |
! |
|
238 |
||
1213 | 239 |
genUUID |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
240 |
"generate a uuid. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
241 |
If a physical mac address can be retrieved from the OS, |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
242 |
a mac-address/timestamp uuid is generated, |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
243 |
otherwise a random uuid will be generated." |
1188 | 244 |
|
1923
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
245 |
^ (super basicNew:16) genUUID |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
246 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
247 |
" |
1923
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
248 |
self genUUID |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
249 |
" |
1188 | 250 |
! |
251 |
||
2977 | 252 |
genUUID:nameStringOrBytes inNameSpace:namespaceString |
253 |
"generate a namespace UUID (Version 5, hashed by SHA-1). |
|
254 |
See RFC4122." |
|
255 |
||
256 |
^ (super basicNew:16) genUUID:nameStringOrBytes inNameSpace:namespaceString |
|
257 |
||
258 |
" |
|
259 |
self genUUID:'www.example.org' inNameSpace:'DNS'. |
|
260 |
self genUUID:'http://www.exept.de' inNameSpace:'URL'. |
|
261 |
self genUUID:'1.2.3.4.5' inNameSpace:'OID'. |
|
262 |
self genUUID:(OSI::DERCoder encode:(OSI::ASN1_OID newID:#(1 2 3 4 5))) |
|
263 |
inNameSpace:'OID'. |
|
264 |
self genUUID:'c=de, o=eXept Software AG, cn=Development' |
|
265 |
inNameSpace:'X500'. |
|
266 |
self genUUID:(OSI::DERCoder encode:(OSI::DistinguishedName fromString:'c=de, o=eXept Software AG, cn=Development') asAsn1Value) |
|
267 |
inNameSpace:'X500'. |
|
268 |
" |
|
269 |
! |
|
270 |
||
1188 | 271 |
new |
1923
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
272 |
^ (super basicNew:16) genUUID |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
273 |
|
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
274 |
" |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
275 |
self new |
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
276 |
" |
1188 | 277 |
! |
278 |
||
279 |
new:size |
|
1320
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
280 |
"allow creating with exact size. We need this for XMLStandardDecoder" |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
281 |
|
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
282 |
size ~~ 16 ifTrue:[ |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
283 |
^ self shouldNotImplement. |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
284 |
]. |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
285 |
^ super new:size. |
1213 | 286 |
! |
287 |
||
288 |
readFrom:aStringOrStream onError:errorBlock |
|
2954 | 289 |
|d offs s uuid t byte hasBrackets| |
1213 | 290 |
|
1543 | 291 |
Error handle:[:ex | |
292 |
^ errorBlock value. |
|
293 |
] do:[ |
|
294 |
s := aStringOrStream readStream. |
|
1923
6380ea5d7385
#new is now the same as genUUID (squeak compatibility)
Claus Gittinger <cg@exept.de>
parents:
1898
diff
changeset
|
295 |
uuid := super basicNew:16. |
2954 | 296 |
hasBrackets := false. |
1213 | 297 |
|
1543 | 298 |
s skipSeparators. |
2954 | 299 |
s peek == ${ ifTrue:[ |
300 |
s next; skipSeparators. |
|
301 |
hasBrackets := true. |
|
302 |
]. |
|
1213 | 303 |
|
1543 | 304 |
t := s next:8. |
305 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
1602 | 306 |
uuid unsignedLongAt:1 put:d bigEndian:true. |
307 |
offs := 5. |
|
1213 | 308 |
|
1602 | 309 |
s next. "skip $-" |
1213 | 310 |
|
1601
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
311 |
1 to:2 do:[:i | |
1543 | 312 |
t := s next:4. |
313 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
1602 | 314 |
uuid unsignedShortAt:offs put:d bigEndian:true. |
1543 | 315 |
offs := offs + 2. |
1602 | 316 |
s next. "skip $-" |
1543 | 317 |
]. |
1213 | 318 |
|
1602 | 319 |
1 to:2 do:[:i | |
320 |
t := s next:2. |
|
321 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
322 |
uuid at:offs put:d. |
|
323 |
offs := offs + 1. |
|
324 |
]. |
|
325 |
s next. "skip $-" |
|
1601
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
326 |
|
1543 | 327 |
1 to:6 do:[:i | |
328 |
t := s next:2. |
|
329 |
byte := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
330 |
uuid at:offs put:byte. |
|
331 |
offs := offs + 1. |
|
332 |
]. |
|
2954 | 333 |
hasBrackets ifTrue:[ |
334 |
s peek == $} |
|
335 |
ifTrue:[s next] |
|
336 |
ifFalse:errorBlock |
|
337 |
]. |
|
1213 | 338 |
]. |
339 |
^ uuid |
|
340 |
||
341 |
" |
|
342 |
UUID readFrom:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' |
|
2954 | 343 |
UUID readFrom:'{5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77}' |
344 |
UUID readFrom:'{5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' |
|
345 |
UUID fromString:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77 bllll' |
|
1898 | 346 |
UUID fromString:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' |
1601
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
347 |
UUID fromString:'00000001-0000-0000-C000-000000000046' |
1898 | 348 |
UUID fromString:'00000001-0000-0000-C000-000000004600' |
349 |
UUID fromString:'00000001-0000-0000-C000-000000460000' |
|
350 |
UUID fromString:'00000001-0000-0000-C000-000046000000' |
|
351 |
UUID fromString:'00000001-0000-0000-C000-004600000000' |
|
352 |
UUID fromString:'00000001-0000-0000-C000-460000000000' |
|
353 |
UUID fromString:'00000001-0000-0000-C046-000000000000' |
|
354 |
UUID fromString:'00000001-0000-0046-C000-000000000000' |
|
355 |
UUID fromString:'00000001-0000-4600-C000-000000000000' |
|
356 |
UUID fromString:'00000001-0046-0000-C000-000000000000' |
|
357 |
UUID fromString:'00000001-4600-0000-C000-000000000000' |
|
358 |
UUID fromString:'00000100-4600-0000-C000-000000000000' |
|
359 |
UUID fromString:'00010000-4600-0000-C000-000000000000' |
|
360 |
UUID fromString:'10000000-4600-0000-C000-000000000000' |
|
1213 | 361 |
" |
1898 | 362 |
|
363 |
"Modified: / 10-10-2007 / 23:03:47 / cg" |
|
1188 | 364 |
! ! |
365 |
||
1213 | 366 |
!UUID class methodsFor:'helpers'! |
367 |
||
1602 | 368 |
adjustByteOrder:aByteArray |
369 |
"change the byte order of the uuid" |
|
370 |
||
371 |
|d| |
|
372 |
||
373 |
d := aByteArray unsignedLongAt:1 bigEndian:false. |
|
374 |
aByteArray unsignedLongAt:1 put:d bigEndian:true. |
|
375 |
||
376 |
d := aByteArray unsignedShortAt:1+4 bigEndian:false. |
|
377 |
aByteArray unsignedShortAt:1+4 put:d bigEndian:true. |
|
378 |
||
379 |
d := aByteArray unsignedShortAt:1+4+2 bigEndian:false. |
|
380 |
aByteArray unsignedShortAt:1+4+2 put:d bigEndian:true. |
|
381 |
! |
|
382 |
||
1213 | 383 |
getDtssUtcTime |
384 |
"return the DTSS based time in 100 nsec intervals |
|
1647 | 385 |
DTSS UTC base time is October 15, 1582 (the start of the Gregorian Calendar)." |
386 |
||
387 |
"Unix base time is January 1, 1970. |
|
388 |
The difference between both is: 122192928000000000" |
|
1213 | 389 |
|
1647 | 390 |
" |
391 |
(Timestamp epoch asDate subtractDate:(Date day:15 month:10 year:1582)) |
|
392 |
* (24 * 60 * 60) |
|
393 |
" |
|
1613 | 394 |
|
1647 | 395 |
^ (Timestamp now getMilliseconds + 12219292800000) * 10000. |
1213 | 396 |
! |
397 |
||
398 |
getValidMACAddress |
|
2978 | 399 |
"return the first valid MAC address (i.e. having at least 6 bytes with the first six bytes ~~ 0)" |
1213 | 400 |
|
401 |
CachedMACAddress isNil ifTrue:[ |
|
2978 | 402 |
CachedMACAddress := false. "negative caching: remember the fact, that there is no MAC address" |
1577
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
403 |
[ |
2977 | 404 |
CachedMACAddress := OperatingSystem getNetworkMACAddresses |
2978 | 405 |
detect:[:macAddress | macAddress size >= 6 |
406 |
and:[(macAddress startsWith:#[0 0 0 0 0 0]) not]] |
|
2977 | 407 |
ifNone:false. |
1577
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
408 |
] on:PrimitiveFailure do:[:ex| "ignore"]. |
1213 | 409 |
]. |
410 |
||
411 |
^ CachedMACAddress |
|
412 |
||
413 |
" |
|
414 |
CachedMACAddress := nil. |
|
415 |
self getValidMACAddress |
|
416 |
" |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
417 |
|
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
418 |
"Modified: / 17-11-2004 / 01:45:53 / cg" |
1213 | 419 |
! ! |
420 |
||
2954 | 421 |
|
1213 | 422 |
!UUID methodsFor:'accessing'! |
423 |
||
1602 | 424 |
clockSeqHiAndReserved |
425 |
^ self at:9 |
|
426 |
! |
|
1213 | 427 |
|
1602 | 428 |
clockSeqLow |
429 |
^ self at:10 |
|
1213 | 430 |
! |
431 |
||
432 |
node |
|
2738 | 433 |
"answer the node (ethernet) address of this uuid" |
434 |
||
3122 | 435 |
self isTimestampUUID ifFalse:[ |
436 |
ConversionError raiseWith:self errorString:' - trying to get node address from random UUID'. |
|
437 |
]. |
|
2738 | 438 |
^ (ByteArray new:6) replaceFrom:1 to:6 with:self startingAt:11. |
439 |
||
440 |
" |
|
441 |
(self allInstances |
|
442 |
select:[:e| e isTimestampUUID] |
|
443 |
thenCollect:[:e| e node]) asBag |
|
444 |
" |
|
1213 | 445 |
! |
446 |
||
447 |
timeHighAndVersion |
|
1602 | 448 |
^ self unsignedShortAt:7 bigEndian:true |
1213 | 449 |
! |
450 |
||
451 |
timeLow |
|
1602 | 452 |
^ self unsignedLongAt:1 bigEndian:true |
1213 | 453 |
! |
454 |
||
455 |
timeMid |
|
1602 | 456 |
^ self unsignedShortAt:5 bigEndian:true |
2738 | 457 |
! |
458 |
||
459 |
timestamp |
|
460 |
"Get the UTC timestamp, when the UUID has been created. |
|
461 |
This is only valid for timestampUUIDs" |
|
462 |
||
463 |
|low med high dtssUtcTime| |
|
464 |
||
3121 | 465 |
self isTimestampUUID ifFalse:[ |
466 |
ConversionError raiseWith:self errorString:' - trying to get timestamp from random UUID'. |
|
467 |
]. |
|
468 |
||
2738 | 469 |
low := self unsignedLongAt:1 bigEndian:true. |
470 |
med := self unsignedShortAt:5 bigEndian:true. |
|
471 |
high := (self unsignedShortAt:7 bigEndian:true) bitAnd:16rFFF. |
|
472 |
||
473 |
dtssUtcTime := low + ((med + (high bitShift:16)) bitShift:32). |
|
474 |
||
2739 | 475 |
^ Timestamp utcMillisecondsSince1970:(dtssUtcTime//10000) - 12219292800000. |
2738 | 476 |
|
477 |
" |
|
478 |
self genTimestampUUID timestamp |
|
3121 | 479 |
self genRandomUUID timestamp |
2738 | 480 |
|
481 |
(self allInstances asSet asArray select:[:e| e isTimestampUUID] |
|
482 |
thenCollect:[:e| e timestamp]) sort |
|
483 |
" |
|
484 |
! |
|
485 |
||
486 |
version |
|
487 |
"the version number of the uuid" |
|
488 |
||
489 |
^ ((self at:7) bitAnd:16rF0) bitShift:-4. |
|
490 |
||
491 |
" |
|
492 |
self genTimestampUUID version |
|
493 |
self genRandomUUID version |
|
494 |
" |
|
1213 | 495 |
! ! |
496 |
||
1188 | 497 |
!UUID methodsFor:'converting'! |
498 |
||
1382 | 499 |
asBytes |
1602 | 500 |
"convert this UUID to a ByteArray in network byte order (MSB-first)" |
1382 | 501 |
|
1602 | 502 |
|bytes| |
1188 | 503 |
|
504 |
bytes := ByteArray new:16. |
|
505 |
bytes replaceFrom:1 to:16 with:self. |
|
1602 | 506 |
^ bytes |
1188 | 507 |
|
1602 | 508 |
" |
509 |
|bytes| |
|
510 |
||
511 |
bytes := #[16r01 16r02 16r03 16r04 |
|
512 |
16r05 16r06 16r07 16r08 |
|
513 |
16r09 16r10 16r11 16r12 |
|
514 |
16r13 16r14 16r15 16r16]. |
|
515 |
(UUID fromBytes:bytes) asBytes ~= bytes ifTrue:[self halt] |
|
516 |
" |
|
517 |
! |
|
1188 | 518 |
|
1602 | 519 |
asBytesMSB:msb |
520 |
"convert this UUID to a ByteArray. |
|
1898 | 521 |
If msb == false, it is converted into LSB-first byte ordering" |
1602 | 522 |
|
523 |
|bytes| |
|
524 |
||
525 |
bytes := self asBytes. |
|
526 |
||
527 |
msb ifFalse:[ |
|
528 |
self class adjustByteOrder:bytes. |
|
1188 | 529 |
]. |
530 |
^ bytes |
|
1898 | 531 |
|
532 |
"Modified: / 10-10-2007 / 22:51:09 / cg" |
|
1602 | 533 |
! |
534 |
||
535 |
asNativeBytes |
|
536 |
"convert this uuid to a ByteArray in host byte order. |
|
537 |
Use this only to pass the UUID to the OS (Windows)" |
|
538 |
||
539 |
|bytes| |
|
540 |
||
541 |
bytes := self asBytes. |
|
3542 | 542 |
IsBigEndian ifFalse:[ |
1602 | 543 |
self class adjustByteOrder:bytes. |
544 |
]. |
|
545 |
^ bytes. |
|
2021 | 546 |
! |
547 |
||
2170 | 548 |
asUUID |
549 |
^ self |
|
550 |
! |
|
551 |
||
2021 | 552 |
literalArrayEncoding |
553 |
^ Array |
|
554 |
with:(self class name) |
|
555 |
with:(self printString). |
|
1188 | 556 |
! ! |
557 |
||
2132 | 558 |
!UUID methodsFor:'copying'! |
559 |
||
560 |
deepCopy |
|
2738 | 561 |
"I am never changed, after I have been created. |
562 |
So there is no need to make a copy" |
|
563 |
||
2132 | 564 |
^ self |
565 |
! |
|
566 |
||
567 |
deepCopyUsing:aDictionary postCopySelector:postCopySelector |
|
2738 | 568 |
"I am never changed, after I have been created. |
569 |
So there is no need to make a copy" |
|
570 |
||
2132 | 571 |
^ self |
572 |
! |
|
573 |
||
574 |
shallowCopy |
|
2738 | 575 |
"I am never changed, after I have been created. |
576 |
So there is no need to make a copy" |
|
577 |
||
2132 | 578 |
^ self |
579 |
! |
|
580 |
||
581 |
simpleDeepCopy |
|
2738 | 582 |
"I am never changed, after I have been created. |
583 |
So there is no need to make a copy" |
|
584 |
||
2132 | 585 |
^ self |
586 |
! ! |
|
587 |
||
1188 | 588 |
!UUID methodsFor:'generating uuids'! |
589 |
||
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
590 |
genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
591 |
"answer a randomly generated uuid as defined in RFC4122 section 4.4" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
592 |
|
2738 | 593 |
(self at:7) ~~ 0 ifTrue:[ |
594 |
"once created, an UUID is immutable" |
|
595 |
self noModificationError. |
|
596 |
]. |
|
597 |
||
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
598 |
self replaceFrom:1 to:16 with:(RandomGenerator new nextBytes:16). |
1612 | 599 |
"multiplex the 4 bit version number (Version 4 -> Random UUID) in high bits of byte 7" |
600 |
self at:7 put:(((self at:7) bitAnd:16r0F) bitOr:16r40). |
|
601 |
self at:9 put:(((self at:9) bitAnd:16r3F) bitOr:16r80). |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
602 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
603 |
" |
2978 | 604 |
self genRandomUUID |
2738 | 605 |
self genRandomUUID genRandomUUID |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
606 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
607 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
608 |
" |
2431 | 609 |
|uuids sample| |
610 |
sample := 100000. |
|
611 |
uuids := Set new:sample. |
|
612 |
sample timesRepeat:[ |
|
613 |
uuids add:UUID genRandomUUID. |
|
614 |
]. |
|
2433
b225b9e4cde8
comment/format in: #genRandomUUID
Stefan Vogel <sv@exept.de>
parents:
2431
diff
changeset
|
615 |
self assert:(uuids size = sample). |
b225b9e4cde8
comment/format in: #genRandomUUID
Stefan Vogel <sv@exept.de>
parents:
2431
diff
changeset
|
616 |
uuids |
2431 | 617 |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
618 |
1 to: 100 do:[ : el | |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
619 |
Transcript show:el. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
620 |
Transcript show:': '. |
2431 | 621 |
Transcript showCR:(UUID genRandomUUID). |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
622 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
623 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
624 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
625 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
626 |
genTimestampUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
627 |
"generate a timestamp (and mac-address) based uuid" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
628 |
|
1610 | 629 |
|macBytes utcTime seqNr| |
1188 | 630 |
|
2701 | 631 |
(macBytes := CachedMACAddress) isNil ifTrue:[ |
632 |
macBytes := self class getValidMACAddress. |
|
633 |
]. |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
634 |
macBytes size ~~ 6 ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
635 |
self error:'no mac address - cannot generate UUID'. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
636 |
]. |
1188 | 637 |
|
2738 | 638 |
(self at:7) ~~ 0 ifTrue:[ |
639 |
"once created, an UUID is immutable" |
|
640 |
self noModificationError. |
|
641 |
]. |
|
642 |
||
1612 | 643 |
"use 60 bit counter of 100ns ticks since 00:00:00 15.oct 1582 (sigh)" |
1610 | 644 |
utcTime := self class getDtssUtcTime. |
645 |
||
1213 | 646 |
Lock critical:[ |
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
647 |
SequenceNumber isNil ifTrue:[ |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
648 |
SequenceNumber := RandomGenerator new nextIntegerBetween:0 and:16383. |
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
649 |
]. |
1612 | 650 |
utcTime > (LastTime+Increment) ifTrue:[ |
1213 | 651 |
Increment := 0. |
652 |
] ifFalse:[ |
|
653 |
LastTime = utcTime ifTrue:[ |
|
654 |
"clock didn't advance since last call. Simply add a tick" |
|
655 |
Increment := Increment + 1. |
|
656 |
] ifFalse:[ |
|
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
657 |
"clock went backwards - increment SequenceNumber" |
1213 | 658 |
Increment := 0. |
1214 | 659 |
SequenceNumber := SequenceNumber + 1. |
660 |
SequenceNumber >= 16384 ifTrue:[SequenceNumber := 0]. |
|
1213 | 661 |
]. |
1188 | 662 |
]. |
1214 | 663 |
|
1213 | 664 |
LastTime := utcTime. |
665 |
utcTime := utcTime + Increment. |
|
1610 | 666 |
seqNr := SequenceNumber. |
1188 | 667 |
]. |
668 |
||
1612 | 669 |
|
670 |
"time low: long" |
|
671 |
self unsignedLongAt:1 put:(utcTime bitAnd:16rFFFFFFFF) bigEndian:true. |
|
672 |
||
673 |
"time med: short" |
|
674 |
utcTime := utcTime bitShift:-32. |
|
675 |
self unsignedShortAt:5 put:(utcTime bitAnd:16rFFFF) bigEndian:true. |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
676 |
|
1612 | 677 |
"time high and version: short |
678 |
multiplex the 4 bit version number in highest 4 bits (version 1 -> time based version)" |
|
679 |
self unsignedShortAt:7 put:((utcTime bitShift:-16) bitOr:16r1000) bigEndian:true. |
|
1188 | 680 |
|
1612 | 681 |
"2 sequence bytes + reserved bits (this is not a short!!)" |
682 |
self at:9 put:(((seqNr bitShift:-8) bitAnd:16r3F) bitOr:16r80). |
|
683 |
self at:10 put:(seqNr bitAnd:16rFF). |
|
1213 | 684 |
|
685 |
"48 bits of MAC-Address" |
|
686 |
self replaceFrom:11 to:16 with:macBytes startingAt:1. |
|
1188 | 687 |
|
688 |
" |
|
2977 | 689 |
self genTimestampUUID |
2738 | 690 |
self genTimestampUUID genTimestampUUID |
1213 | 691 |
" |
1188 | 692 |
|
1213 | 693 |
" |
694 |
1 to: 100 do:[ : el | |
|
695 |
Transcript show:el. |
|
696 |
Transcript show:': '. |
|
1612 | 697 |
Transcript showCR:(UUID new genTimestampUUID). |
1213 | 698 |
]. |
699 |
" |
|
2701 | 700 |
|
701 |
"Modified: / 26-12-2011 / 13:41:23 / cg" |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
702 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
703 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
704 |
genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
705 |
"generate a uuid - the existing uuid bytes are overwritten. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
706 |
If a physical mac address can be retrieved from the OS, |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
707 |
a mac-address/timestamp uuid is generated, |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
708 |
otherwise a random uuid will be generated." |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
709 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
710 |
CachedMACAddress isNil ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
711 |
self class getValidMACAddress. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
712 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
713 |
CachedMACAddress == false ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
714 |
"no mac address - generate random UUID" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
715 |
^ self genRandomUUID. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
716 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
717 |
^ self genTimestampUUID. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
718 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
719 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
720 |
self genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
721 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
722 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
723 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
724 |
1 to: 100 do:[ : el | |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
725 |
Transcript show:el. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
726 |
Transcript show:': '. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
727 |
Transcript showCR:(UUID genUUID). |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
728 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
729 |
" |
2977 | 730 |
! |
731 |
||
732 |
genUUID:nameStringOrBytes inNameSpace:namespaceString |
|
733 |
"generate a namespace UUID (Version 5, hashed by SHA-1). |
|
734 |
See RFC4122." |
|
735 |
||
736 |
|sha1| |
|
737 |
||
738 |
(self at:7) ~~ 0 ifTrue:[ |
|
739 |
"once created, an UUID is immutable" |
|
740 |
self noModificationError. |
|
741 |
]. |
|
742 |
||
743 |
sha1 := SHA1Stream new. |
|
744 |
sha1 |
|
745 |
nextPutAll:(NameSpaceToUuidBytes at:namespaceString); |
|
746 |
nextPutAll:nameStringOrBytes. |
|
747 |
||
748 |
self replaceFrom:1 to:16 with:(sha1 hashValue). |
|
749 |
"multiplex the 4 bit version number (Version 5 -> SHA1 Namspace UUID) in high bits of byte 7" |
|
750 |
self at:7 put:(((self at:7) bitAnd:16r0F) bitOr:16r50). |
|
751 |
self at:9 put:(((self at:9) bitAnd:16r3F) bitOr:16r80). |
|
752 |
||
753 |
" |
|
754 |
self genUUID:'www.example.org' inNameSpace:'DNS'. |
|
755 |
self genUUID:'http://www.exept.de' inNameSpace:'URL'. |
|
756 |
self genUUID:'1.2.3.4.5' inNameSpace:'OID'. |
|
757 |
self genUUID:(OSI::DERCoder encode:(OSI::ASN1_OID newID:#(1 2 3 4 5))) |
|
758 |
inNameSpace:'OID'. |
|
759 |
self genUUID:'c=de, o=eXept Software AG, cn=Development' |
|
760 |
inNameSpace:'X500'. |
|
761 |
self genUUID:(OSI::DERCoder encode:(OSI::DistinguishedName fromString:'c=de, o=eXept Software AG, cn=Development') asAsn1Value) |
|
762 |
inNameSpace:'X500'. |
|
763 |
" |
|
1188 | 764 |
! ! |
765 |
||
2426 | 766 |
!UUID methodsFor:'hashing'! |
767 |
||
768 |
hash |
|
769 |
"Generate a 30 bit hash value. |
|
770 |
For Timestamp-UUIDs: |
|
771 |
Bytes 1,2,3,4 are the least significant bits of the timestamp, |
|
2428 | 772 |
Bytes 13,14,15,16 are the least significant bytes of the mac address - |
2426 | 773 |
but considering these bytes does not generate a better hash to |
774 |
justify the additional computations. |
|
775 |
||
776 |
For random UUIDs, every byte is random anyway." |
|
777 |
||
2428 | 778 |
^ (self computeXorHashFrom:1 to:4) |
779 |
"/ bitXor:(self computeXorHashFrom:13 to:16) |
|
2426 | 780 |
|
781 |
" |
|
782 |
|allHashes| |
|
783 |
allHashes := UUID allInstances collect:[:each| each hash]. |
|
784 |
(allHashes asSet size / allHashes size) asFloat |
|
785 |
" |
|
786 |
! ! |
|
787 |
||
3121 | 788 |
|
1188 | 789 |
!UUID methodsFor:'printing'! |
790 |
||
2495
721d3888cbab
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2433
diff
changeset
|
791 |
displayOn:aGCOrStream |
721d3888cbab
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2433
diff
changeset
|
792 |
"/ what a kludge - Dolphin and Squeak mean: printOn: a stream; |
721d3888cbab
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2433
diff
changeset
|
793 |
"/ ST/X (and some old ST80's) mean: draw-yourself on a GC. |
2977 | 794 |
|
795 |
aGCOrStream isStream ifTrue:[ |
|
2495
721d3888cbab
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2433
diff
changeset
|
796 |
self printOn:aGCOrStream. |
721d3888cbab
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2433
diff
changeset
|
797 |
^ self. |
721d3888cbab
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2433
diff
changeset
|
798 |
]. |
721d3888cbab
Implement display with #displayOn: instead of #displayString
Stefan Vogel <sv@exept.de>
parents:
2433
diff
changeset
|
799 |
|
2977 | 800 |
^ super displayOn:aGCOrStream. |
801 |
||
802 |
" |
|
803 |
self genUUID displayOn:Transcript |
|
804 |
" |
|
1188 | 805 |
! |
806 |
||
807 |
printOn:aStream |
|
1602 | 808 |
|d tmpStream| |
1188 | 809 |
|
1213 | 810 |
tmpStream := '' writeStream. |
1188 | 811 |
|
1612 | 812 |
1 to:4 do:[:idx| |
813 |
d := self at:idx. |
|
814 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
815 |
]. |
|
1602 | 816 |
tmpStream nextPut:$-. |
1612 | 817 |
5 to:6 do:[:idx| |
818 |
d := self at:idx. |
|
819 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
820 |
]. |
|
1602 | 821 |
tmpStream nextPut:$-. |
1612 | 822 |
7 to:8 do:[:idx| |
823 |
d := self at:idx. |
|
824 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
825 |
]. |
|
1602 | 826 |
tmpStream nextPut:$-. |
1612 | 827 |
9 to:10 do:[:idx| |
828 |
d := self at:idx. |
|
829 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
830 |
]. |
|
1213 | 831 |
tmpStream nextPut:$-. |
1602 | 832 |
11 to:16 do:[:idx| |
833 |
d := self at:idx. |
|
1213 | 834 |
d printOn:tmpStream base:16 size:2 fill:$0. |
1188 | 835 |
]. |
836 |
||
1213 | 837 |
aStream nextPutAll:(tmpStream contents asLowercase). |
1898 | 838 |
|
2741 | 839 |
" |
840 |
UUID genUUID printString |
|
841 |
" |
|
842 |
||
843 |
"/ d := self unsignedLongAt:1 bigEndian:true. |
|
844 |
"/ d printOn:tmpStream base:16 size:8 fill:$0. |
|
845 |
"/ tmpStream nextPut:$-. |
|
846 |
"/ |
|
847 |
"/ d := self unsignedShortAt:1+4 bigEndian:true. |
|
848 |
"/ d printOn:tmpStream base:16 size:4 fill:$0. |
|
849 |
"/ tmpStream nextPut:$-. |
|
850 |
"/ |
|
851 |
"/ d := self unsignedShortAt:1+4+2 bigEndian:true. |
|
852 |
"/ d printOn:tmpStream base:16 size:4 fill:$0. |
|
853 |
"/ tmpStream nextPut:$-. |
|
854 |
"/ |
|
855 |
"/ d := self at:1+4+2+2. |
|
856 |
"/ d printOn:tmpStream base:16 size:2 fill:$0. |
|
857 |
"/ d := self at:1+4+2+2+1. |
|
858 |
"/ d printOn:tmpStream base:16 size:2 fill:$0. |
|
859 |
"/ tmpStream nextPut:$-. |
|
860 |
"/ |
|
861 |
"/ 11 to:16 do:[:idx| |
|
862 |
"/ d := self at:idx. |
|
863 |
"/ d printOn:tmpStream base:16 size:2 fill:$0. |
|
864 |
"/ ]. |
|
865 |
||
866 |
||
1898 | 867 |
"Modified: / 10-10-2007 / 23:19:03 / cg" |
2069 | 868 |
! |
869 |
||
870 |
storeOn:aStream |
|
871 |
aStream |
|
2741 | 872 |
nextPut:$(; |
2069 | 873 |
nextPutAll:self class name; |
2741 | 874 |
nextPutAll:' fromString:'''. |
875 |
self printOn:aStream. |
|
876 |
aStream nextPutAll:''')'. |
|
2069 | 877 |
|
878 |
" |
|
879 |
Object readFrom:(UUID genUUID storeString) |
|
880 |
" |
|
1188 | 881 |
! ! |
882 |
||
2755 | 883 |
!UUID methodsFor:'testing'! |
2738 | 884 |
|
3122 | 885 |
isRandomUUID |
886 |
^ self version == 4 |
|
887 |
||
888 |
" |
|
889 |
self genRandomUUID isRandomUUID |
|
890 |
" |
|
891 |
! |
|
892 |
||
2738 | 893 |
isTimestampUUID |
894 |
^ self version == 1 |
|
3122 | 895 |
|
896 |
" |
|
897 |
self genTimestampUUID isTimestampUUID |
|
898 |
" |
|
2755 | 899 |
! |
900 |
||
901 |
isUUID |
|
902 |
^ true |
|
2738 | 903 |
! ! |
904 |
||
1188 | 905 |
!UUID class methodsFor:'documentation'! |
906 |
||
907 |
version |
|
3542 | 908 |
^ '$Header: /cvs/stx/stx/libbasic2/UUID.st,v 1.48 2015-04-24 12:18:00 stefan Exp $' |
2426 | 909 |
! |
910 |
||
911 |
version_CVS |
|
3542 | 912 |
^ '$Header: /cvs/stx/stx/libbasic2/UUID.st,v 1.48 2015-04-24 12:18:00 stefan Exp $' |
1188 | 913 |
! ! |
1213 | 914 |
|
2950 | 915 |
|
1213 | 916 |
UUID initialize! |