author | Stefan Vogel <sv@exept.de> |
Tue, 14 Mar 2006 22:04:26 +0100 | |
changeset 1613 | feef745939ea |
parent 1612 | 2877abd0a002 |
child 1647 | 124e977b6a8f |
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 |
||
13 |
"{ Package: 'stx:libbasic2' }" |
|
14 |
||
15 |
ByteArray variableByteSubclass:#UUID |
|
16 |
instanceVariableNames:'' |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
17 |
classVariableNames:'CachedMACAddress Lock SequenceNumber LastTime Increment |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
18 |
NameSpaceDNS NameSpaceDN NameSpaceURL NameSpaceOID' |
1188 | 19 |
poolDictionaries:'' |
1304 | 20 |
category:'Net-Communication-Support' |
1188 | 21 |
! |
22 |
||
23 |
!UUID class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
1213 | 37 |
! |
38 |
||
39 |
documentation |
|
40 |
" |
|
1544 | 41 |
128-bit Universal Unique Ids (UUIDs) as defined by OpenGroup/DCE |
42 |
http://www.opengroup.org/onlinepubs/9629399/apdxa.htm. |
|
1213 | 43 |
|
1602 | 44 |
See also RFC4122. |
45 |
||
1213 | 46 |
A UUID is unique in time and space (at least until about Year 3400). |
47 |
||
1602 | 48 |
Several fields if the UUID are interpreted as integers, so host/network byte |
49 |
order is relevant. UUIDs are stored in a ByteArray in network byte order (MSB-first), |
|
50 |
so they may be exported/imported between different machines. |
|
51 |
||
52 |
You can import UUIDs in host byte order using #fromNativeBytes: |
|
53 |
||
1213 | 54 |
[author:] |
55 |
||
56 |
[instance variables:] |
|
57 |
||
58 |
[class variables:] |
|
59 |
||
60 |
[see also:] |
|
61 |
||
62 |
" |
|
63 |
! ! |
|
64 |
||
65 |
!UUID class methodsFor:'initialization'! |
|
66 |
||
67 |
initialize |
|
68 |
"I want to get informed about image restarts" |
|
69 |
||
70 |
Lock isNil ifTrue:[ |
|
71 |
Lock := RecursionLock new name:'UUID'. |
|
72 |
LastTime := 0. |
|
73 |
Increment := 0. |
|
74 |
ObjectMemory addDependent:self. |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
75 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
76 |
NameSpaceDNS := self fromBytes:#[16r6B 16rA7 16rB8 16r10 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
77 |
NameSpaceURL := self fromBytes:#[16r6B 16rA7 16rB8 16r11 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
78 |
NameSpaceOID := self fromBytes:#[16r6B 16rA7 16rB8 16r12 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
79 |
NameSpaceDN := self fromBytes:#[16r6B 16rA7 16rB8 16r14 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]. |
1213 | 80 |
] |
81 |
! |
|
82 |
||
83 |
update:something with:aParameter from:changedObject |
|
84 |
"flush cached MAC address (may have been restarted on another host)" |
|
85 |
||
86 |
(something == #restarted) ifTrue:[ |
|
87 |
CachedMACAddress := nil. |
|
88 |
SequenceNumber := nil. |
|
89 |
] |
|
1188 | 90 |
! ! |
91 |
||
92 |
!UUID class methodsFor:'instance creation'! |
|
93 |
||
1382 | 94 |
fromBytes:aByteArray |
1602 | 95 |
"set uuid from aByteArray. |
96 |
aByteArray must be 16 bytes in network byte order (MSB-first)" |
|
1382 | 97 |
|
1602 | 98 |
|uuid| |
1188 | 99 |
|
100 |
uuid := self new. |
|
101 |
uuid replaceFrom:1 to:16 with:aByteArray. |
|
1602 | 102 |
^ uuid. |
1188 | 103 |
|
1602 | 104 |
" |
105 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
106 |
16r05 16r06 16r07 16r08 |
|
107 |
16r09 16r10 16r11 16r12 |
|
108 |
16r13 16r14 16r15 16r16]. |
|
109 |
" |
|
110 |
! |
|
1188 | 111 |
|
1602 | 112 |
fromBytes:aByteArray msb:msb |
113 |
"Set the UUID from aByteArray. UUIDS are stored internally as MSB-first. |
|
114 |
So, alway set |
|
115 |
msb = true if reading from network or persistent storage" |
|
1188 | 116 |
|
1602 | 117 |
|uuid| |
118 |
||
119 |
uuid := self fromBytes:aByteArray. |
|
120 |
msb ifFalse:[ |
|
121 |
self adjustByteOrder:uuid. |
|
1188 | 122 |
]. |
123 |
||
124 |
^ uuid |
|
125 |
||
126 |
" |
|
127 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
128 |
16r05 16r06 16r07 16r08 |
|
129 |
16r09 16r10 16r11 16r12 |
|
130 |
16r13 16r14 16r15 16r16] msb:false. |
|
131 |
" |
|
132 |
" |
|
133 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
134 |
16r05 16r06 16r07 16r08 |
|
135 |
16r09 16r10 16r11 16r12 |
|
136 |
16r13 16r14 16r15 16r16] msb:true. |
|
137 |
" |
|
138 |
! |
|
139 |
||
1602 | 140 |
fromNativeBytes:aByteArray |
141 |
"convert bytes to uuid. |
|
142 |
aByteArray represents a UUID in host byte order |
|
143 |
- e.g. an UUID fetched from the Windows OS" |
|
144 |
||
145 |
|uuid| |
|
146 |
||
147 |
uuid := self fromBytes:aByteArray. |
|
148 |
self isBigEndian ifFalse:[ |
|
149 |
self adjustByteOrder:uuid. |
|
150 |
]. |
|
151 |
^ uuid. |
|
152 |
||
153 |
" |
|
154 |
UUID fromNativeBytes:#[16r01 16r02 16r03 16r04 |
|
155 |
16r05 16r06 16r07 16r08 |
|
156 |
16r09 16r10 16r11 16r12 |
|
157 |
16r13 16r14 16r15 16r16]. |
|
158 |
||
159 |
" |
|
160 |
! |
|
161 |
||
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
162 |
genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
163 |
"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
|
164 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
165 |
^ self new genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
166 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
167 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
168 |
self genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
169 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
170 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
171 |
|
1213 | 172 |
genUUID |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
173 |
"generate a uuid. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
174 |
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
|
175 |
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
|
176 |
otherwise a random uuid will be generated." |
1188 | 177 |
|
178 |
^ self new genUUID |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
179 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
180 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
181 |
self genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
182 |
" |
1188 | 183 |
! |
184 |
||
185 |
new |
|
186 |
^ super new:16 |
|
187 |
! |
|
188 |
||
189 |
new:size |
|
1320
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
190 |
"allow creating with exact size. We need this for XMLStandardDecoder" |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
191 |
|
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
192 |
size ~~ 16 ifTrue:[ |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
193 |
^ self shouldNotImplement. |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
194 |
]. |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
195 |
^ super new:size. |
1213 | 196 |
! |
197 |
||
198 |
readFrom:aStringOrStream onError:errorBlock |
|
199 |
|d offs s uuid t byte| |
|
200 |
||
1543 | 201 |
Error handle:[:ex | |
202 |
^ errorBlock value. |
|
203 |
] do:[ |
|
204 |
s := aStringOrStream readStream. |
|
205 |
uuid := self new. |
|
1213 | 206 |
|
1543 | 207 |
s skipSeparators. |
208 |
s peek == ${ ifTrue:[s next]. |
|
209 |
s skipSeparators. |
|
1213 | 210 |
|
1543 | 211 |
t := s next:8. |
212 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
1602 | 213 |
uuid unsignedLongAt:1 put:d bigEndian:true. |
214 |
offs := 5. |
|
1213 | 215 |
|
1602 | 216 |
s next. "skip $-" |
1213 | 217 |
|
1601
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
218 |
1 to:2 do:[:i | |
1543 | 219 |
t := s next:4. |
220 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
1602 | 221 |
uuid unsignedShortAt:offs put:d bigEndian:true. |
1543 | 222 |
offs := offs + 2. |
1602 | 223 |
s next. "skip $-" |
1543 | 224 |
]. |
1213 | 225 |
|
1602 | 226 |
1 to:2 do:[:i | |
227 |
t := s next:2. |
|
228 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
229 |
uuid at:offs put:d. |
|
230 |
offs := offs + 1. |
|
231 |
]. |
|
232 |
s next. "skip $-" |
|
1601
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
233 |
|
1543 | 234 |
1 to:6 do:[:i | |
235 |
t := s next:2. |
|
236 |
byte := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
237 |
uuid at:offs put:byte. |
|
238 |
offs := offs + 1. |
|
239 |
]. |
|
1213 | 240 |
]. |
241 |
^ uuid |
|
242 |
||
243 |
" |
|
244 |
UUID readFrom:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' |
|
1601
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
245 |
UUID fromString:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' |
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
246 |
UUID fromString:'00000001-0000-0000-C000-000000000046' |
1213 | 247 |
" |
1188 | 248 |
! ! |
249 |
||
1213 | 250 |
!UUID class methodsFor:'helpers'! |
251 |
||
1602 | 252 |
adjustByteOrder:aByteArray |
253 |
"change the byte order of the uuid" |
|
254 |
||
255 |
|d| |
|
256 |
||
257 |
d := aByteArray unsignedLongAt:1 bigEndian:false. |
|
258 |
aByteArray unsignedLongAt:1 put:d bigEndian:true. |
|
259 |
||
260 |
d := aByteArray unsignedShortAt:1+4 bigEndian:false. |
|
261 |
aByteArray unsignedShortAt:1+4 put:d bigEndian:true. |
|
262 |
||
263 |
d := aByteArray unsignedShortAt:1+4+2 bigEndian:false. |
|
264 |
aByteArray unsignedShortAt:1+4+2 put:d bigEndian:true. |
|
265 |
! |
|
266 |
||
1213 | 267 |
getDtssUtcTime |
268 |
"return the DTSS based time in 100 nsec intervals |
|
1613 | 269 |
DTSS UTC base time is October 15, 1582 (the start of the Gregorian Calendar). |
1213 | 270 |
Unix base time is January 1, 1970. |
1612 | 271 |
The difference between both is: 122192928000000000" |
1213 | 272 |
|
1613 | 273 |
|t| |
274 |
||
275 |
t := Timestamp now. |
|
276 |
^ (t getMilliseconds + (t utcOffset * 1000) + 12219292800000) * 10000. |
|
1213 | 277 |
! |
278 |
||
279 |
getValidMACAddress |
|
280 |
"return the first valid MAC address (i.e. having at least one byte ~~ 0)" |
|
281 |
||
282 |
CachedMACAddress isNil ifTrue:[ |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
283 |
|dictOfIf ipAddr| |
1213 | 284 |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
285 |
CachedMACAddress := false. "cache 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
|
286 |
[ |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
287 |
dictOfIf := OperatingSystem getNetworkMACAddresses. |
1213 | 288 |
|
1577
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
289 |
dictOfIf do:[:macAddress | |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
290 |
(macAddress contains:[:byte | byte ~~ 0]) ifTrue:[ |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
291 |
^ CachedMACAddress := macAddress |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
292 |
]. |
1213 | 293 |
]. |
1577
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
294 |
] on:PrimitiveFailure do:[:ex| "ignore"]. |
1213 | 295 |
]. |
296 |
||
297 |
^ CachedMACAddress |
|
298 |
||
299 |
" |
|
300 |
CachedMACAddress := nil. |
|
301 |
self getValidMACAddress |
|
302 |
" |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
303 |
|
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
304 |
"Modified: / 17-11-2004 / 01:45:53 / cg" |
1213 | 305 |
! ! |
306 |
||
307 |
!UUID methodsFor:'accessing'! |
|
308 |
||
1602 | 309 |
clockSeqHiAndReserved |
310 |
^ self at:9 |
|
311 |
! |
|
1213 | 312 |
|
1602 | 313 |
clockSeqLow |
314 |
^ self at:10 |
|
1213 | 315 |
! |
316 |
||
317 |
node |
|
1602 | 318 |
^ self copyFrom:11 to:16 |
1213 | 319 |
! |
320 |
||
321 |
timeHighAndVersion |
|
1602 | 322 |
^ self unsignedShortAt:7 bigEndian:true |
1213 | 323 |
! |
324 |
||
325 |
timeLow |
|
1602 | 326 |
^ self unsignedLongAt:1 bigEndian:true |
1213 | 327 |
! |
328 |
||
329 |
timeMid |
|
1602 | 330 |
^ self unsignedShortAt:5 bigEndian:true |
1213 | 331 |
! ! |
332 |
||
1188 | 333 |
!UUID methodsFor:'converting'! |
334 |
||
1382 | 335 |
asBytes |
1602 | 336 |
"convert this UUID to a ByteArray in network byte order (MSB-first)" |
1382 | 337 |
|
1602 | 338 |
|bytes| |
1188 | 339 |
|
340 |
bytes := ByteArray new:16. |
|
341 |
bytes replaceFrom:1 to:16 with:self. |
|
1602 | 342 |
^ bytes |
1188 | 343 |
|
1602 | 344 |
" |
345 |
|bytes| |
|
346 |
||
347 |
bytes := #[16r01 16r02 16r03 16r04 |
|
348 |
16r05 16r06 16r07 16r08 |
|
349 |
16r09 16r10 16r11 16r12 |
|
350 |
16r13 16r14 16r15 16r16]. |
|
351 |
(UUID fromBytes:bytes) asBytes ~= bytes ifTrue:[self halt] |
|
352 |
" |
|
353 |
! |
|
1188 | 354 |
|
1602 | 355 |
asBytesMSB:msb |
356 |
"convert this UUID to a ByteArray. |
|
357 |
If msb == false, it is converted into LSB-first byte oredering" |
|
358 |
||
359 |
|bytes| |
|
360 |
||
361 |
bytes := self asBytes. |
|
362 |
||
363 |
msb ifFalse:[ |
|
364 |
self class adjustByteOrder:bytes. |
|
1188 | 365 |
]. |
366 |
^ bytes |
|
1602 | 367 |
! |
368 |
||
369 |
asNativeBytes |
|
370 |
"convert this uuid to a ByteArray in host byte order. |
|
371 |
Use this only to pass the UUID to the OS (Windows)" |
|
372 |
||
373 |
|bytes| |
|
374 |
||
375 |
bytes := self asBytes. |
|
376 |
self class isBigEndian ifFalse:[ |
|
377 |
self class adjustByteOrder:bytes. |
|
378 |
]. |
|
379 |
^ bytes. |
|
1188 | 380 |
! ! |
381 |
||
382 |
!UUID methodsFor:'generating uuids'! |
|
383 |
||
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
384 |
genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
385 |
"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
|
386 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
387 |
self replaceFrom:1 to:16 with:(RandomGenerator new nextBytes:16). |
1612 | 388 |
"multiplex the 4 bit version number (Version 4 -> Random UUID) in high bits of byte 7" |
389 |
self at:7 put:(((self at:7) bitAnd:16r0F) bitOr:16r40). |
|
390 |
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
|
391 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
392 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
393 |
self new genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
394 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
395 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
396 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
397 |
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
|
398 |
Transcript show:el. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
399 |
Transcript show:': '. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
400 |
Transcript showCR:(UUID new genRandomUUID). |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
401 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
402 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
403 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
404 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
405 |
genTimestampUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
406 |
"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
|
407 |
|
1610 | 408 |
|macBytes utcTime seqNr| |
1188 | 409 |
|
1213 | 410 |
macBytes := self class getValidMACAddress. |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
411 |
macBytes size ~~ 6 ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
412 |
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
|
413 |
]. |
1188 | 414 |
|
1612 | 415 |
"use 60 bit counter of 100ns ticks since 00:00:00 15.oct 1582 (sigh)" |
1610 | 416 |
utcTime := self class getDtssUtcTime. |
417 |
||
1213 | 418 |
Lock critical:[ |
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
419 |
SequenceNumber isNil ifTrue:[ |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
420 |
SequenceNumber := RandomGenerator new nextIntegerBetween:0 and:16383. |
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
421 |
]. |
1612 | 422 |
utcTime > (LastTime+Increment) ifTrue:[ |
1213 | 423 |
Increment := 0. |
424 |
] ifFalse:[ |
|
425 |
LastTime = utcTime ifTrue:[ |
|
426 |
"clock didn't advance since last call. Simply add a tick" |
|
427 |
Increment := Increment + 1. |
|
428 |
] ifFalse:[ |
|
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
429 |
"clock went backwards - increment SequenceNumber" |
1213 | 430 |
Increment := 0. |
1214 | 431 |
SequenceNumber := SequenceNumber + 1. |
432 |
SequenceNumber >= 16384 ifTrue:[SequenceNumber := 0]. |
|
1213 | 433 |
]. |
1188 | 434 |
]. |
1214 | 435 |
|
1213 | 436 |
LastTime := utcTime. |
437 |
utcTime := utcTime + Increment. |
|
1610 | 438 |
seqNr := SequenceNumber. |
1188 | 439 |
]. |
440 |
||
1612 | 441 |
|
442 |
"time low: long" |
|
443 |
self unsignedLongAt:1 put:(utcTime bitAnd:16rFFFFFFFF) bigEndian:true. |
|
444 |
||
445 |
"time med: short" |
|
446 |
utcTime := utcTime bitShift:-32. |
|
447 |
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
|
448 |
|
1612 | 449 |
"time high and version: short |
450 |
multiplex the 4 bit version number in highest 4 bits (version 1 -> time based version)" |
|
451 |
self unsignedShortAt:7 put:((utcTime bitShift:-16) bitOr:16r1000) bigEndian:true. |
|
1188 | 452 |
|
1612 | 453 |
"2 sequence bytes + reserved bits (this is not a short!!)" |
454 |
self at:9 put:(((seqNr bitShift:-8) bitAnd:16r3F) bitOr:16r80). |
|
455 |
self at:10 put:(seqNr bitAnd:16rFF). |
|
1213 | 456 |
|
457 |
"48 bits of MAC-Address" |
|
458 |
self replaceFrom:11 to:16 with:macBytes startingAt:1. |
|
1188 | 459 |
|
460 |
" |
|
1612 | 461 |
self new genTimestampUUID |
1213 | 462 |
" |
1188 | 463 |
|
1213 | 464 |
" |
465 |
1 to: 100 do:[ : el | |
|
466 |
Transcript show:el. |
|
467 |
Transcript show:': '. |
|
1612 | 468 |
Transcript showCR:(UUID new genTimestampUUID). |
1213 | 469 |
]. |
470 |
" |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
471 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
472 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
473 |
genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
474 |
"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
|
475 |
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
|
476 |
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
|
477 |
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
|
478 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
479 |
CachedMACAddress isNil ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
480 |
self class getValidMACAddress. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
481 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
482 |
CachedMACAddress == false ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
483 |
"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
|
484 |
^ self genRandomUUID. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
485 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
486 |
^ self genTimestampUUID. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
487 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
488 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
489 |
self genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
490 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
491 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
492 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
493 |
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
|
494 |
Transcript show:el. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
495 |
Transcript show:': '. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
496 |
Transcript showCR:(UUID genUUID). |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
497 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
498 |
" |
1188 | 499 |
! ! |
500 |
||
501 |
!UUID methodsFor:'printing'! |
|
502 |
||
503 |
displayString |
|
504 |
^ self printString |
|
505 |
! |
|
506 |
||
507 |
printOn:aStream |
|
1602 | 508 |
|d tmpStream| |
1188 | 509 |
|
1213 | 510 |
tmpStream := '' writeStream. |
1188 | 511 |
|
1612 | 512 |
1 to:4 do:[:idx| |
513 |
d := self at:idx. |
|
514 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
515 |
]. |
|
1602 | 516 |
tmpStream nextPut:$-. |
1612 | 517 |
5 to:6 do:[:idx| |
518 |
d := self at:idx. |
|
519 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
520 |
]. |
|
1602 | 521 |
tmpStream nextPut:$-. |
1612 | 522 |
7 to:8 do:[:idx| |
523 |
d := self at:idx. |
|
524 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
525 |
]. |
|
1602 | 526 |
tmpStream nextPut:$-. |
1612 | 527 |
9 to:10 do:[:idx| |
528 |
d := self at:idx. |
|
529 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
530 |
]. |
|
1213 | 531 |
tmpStream nextPut:$-. |
1602 | 532 |
11 to:16 do:[:idx| |
533 |
d := self at:idx. |
|
1213 | 534 |
d printOn:tmpStream base:16 size:2 fill:$0. |
1188 | 535 |
]. |
536 |
||
1213 | 537 |
aStream nextPutAll:(tmpStream contents asLowercase). |
1188 | 538 |
! ! |
539 |
||
540 |
!UUID class methodsFor:'documentation'! |
|
541 |
||
542 |
version |
|
1613 | 543 |
^ '$Header: /cvs/stx/stx/libbasic2/UUID.st,v 1.21 2006-03-14 21:04:26 stefan Exp $' |
1188 | 544 |
! ! |
1213 | 545 |
|
546 |
UUID initialize! |