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