author | Claus Gittinger <cg@exept.de> |
Thu, 06 Sep 2007 17:31:09 +0200 | |
changeset 1894 | 8bf137acc445 |
parent 1647 | 124e977b6a8f |
child 1898 | 8041791c02c3 |
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 |
|
1647 | 269 |
DTSS UTC base time is October 15, 1582 (the start of the Gregorian Calendar)." |
270 |
||
271 |
"Unix base time is January 1, 1970. |
|
272 |
The difference between both is: 122192928000000000" |
|
1213 | 273 |
|
1647 | 274 |
" |
275 |
(Timestamp epoch asDate subtractDate:(Date day:15 month:10 year:1582)) |
|
276 |
* (24 * 60 * 60) |
|
277 |
" |
|
1613 | 278 |
|
1647 | 279 |
^ (Timestamp now getMilliseconds + 12219292800000) * 10000. |
1213 | 280 |
! |
281 |
||
282 |
getValidMACAddress |
|
283 |
"return the first valid MAC address (i.e. having at least one byte ~~ 0)" |
|
284 |
||
285 |
CachedMACAddress isNil ifTrue:[ |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
286 |
|dictOfIf ipAddr| |
1213 | 287 |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
288 |
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
|
289 |
[ |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
290 |
dictOfIf := OperatingSystem getNetworkMACAddresses. |
1213 | 291 |
|
1577
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
292 |
dictOfIf do:[:macAddress | |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
293 |
(macAddress contains:[:byte | byte ~~ 0]) ifTrue:[ |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
294 |
^ CachedMACAddress := macAddress |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
295 |
]. |
1213 | 296 |
]. |
1577
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
297 |
] on:PrimitiveFailure do:[:ex| "ignore"]. |
1213 | 298 |
]. |
299 |
||
300 |
^ CachedMACAddress |
|
301 |
||
302 |
" |
|
303 |
CachedMACAddress := nil. |
|
304 |
self getValidMACAddress |
|
305 |
" |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
306 |
|
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
307 |
"Modified: / 17-11-2004 / 01:45:53 / cg" |
1213 | 308 |
! ! |
309 |
||
310 |
!UUID methodsFor:'accessing'! |
|
311 |
||
1602 | 312 |
clockSeqHiAndReserved |
313 |
^ self at:9 |
|
314 |
! |
|
1213 | 315 |
|
1602 | 316 |
clockSeqLow |
317 |
^ self at:10 |
|
1213 | 318 |
! |
319 |
||
320 |
node |
|
1602 | 321 |
^ self copyFrom:11 to:16 |
1213 | 322 |
! |
323 |
||
324 |
timeHighAndVersion |
|
1602 | 325 |
^ self unsignedShortAt:7 bigEndian:true |
1213 | 326 |
! |
327 |
||
328 |
timeLow |
|
1602 | 329 |
^ self unsignedLongAt:1 bigEndian:true |
1213 | 330 |
! |
331 |
||
332 |
timeMid |
|
1602 | 333 |
^ self unsignedShortAt:5 bigEndian:true |
1213 | 334 |
! ! |
335 |
||
1188 | 336 |
!UUID methodsFor:'converting'! |
337 |
||
1382 | 338 |
asBytes |
1602 | 339 |
"convert this UUID to a ByteArray in network byte order (MSB-first)" |
1382 | 340 |
|
1602 | 341 |
|bytes| |
1188 | 342 |
|
343 |
bytes := ByteArray new:16. |
|
344 |
bytes replaceFrom:1 to:16 with:self. |
|
1602 | 345 |
^ bytes |
1188 | 346 |
|
1602 | 347 |
" |
348 |
|bytes| |
|
349 |
||
350 |
bytes := #[16r01 16r02 16r03 16r04 |
|
351 |
16r05 16r06 16r07 16r08 |
|
352 |
16r09 16r10 16r11 16r12 |
|
353 |
16r13 16r14 16r15 16r16]. |
|
354 |
(UUID fromBytes:bytes) asBytes ~= bytes ifTrue:[self halt] |
|
355 |
" |
|
356 |
! |
|
1188 | 357 |
|
1602 | 358 |
asBytesMSB:msb |
359 |
"convert this UUID to a ByteArray. |
|
360 |
If msb == false, it is converted into LSB-first byte oredering" |
|
361 |
||
362 |
|bytes| |
|
363 |
||
364 |
bytes := self asBytes. |
|
365 |
||
366 |
msb ifFalse:[ |
|
367 |
self class adjustByteOrder:bytes. |
|
1188 | 368 |
]. |
369 |
^ bytes |
|
1602 | 370 |
! |
371 |
||
372 |
asNativeBytes |
|
373 |
"convert this uuid to a ByteArray in host byte order. |
|
374 |
Use this only to pass the UUID to the OS (Windows)" |
|
375 |
||
376 |
|bytes| |
|
377 |
||
378 |
bytes := self asBytes. |
|
379 |
self class isBigEndian ifFalse:[ |
|
380 |
self class adjustByteOrder:bytes. |
|
381 |
]. |
|
382 |
^ bytes. |
|
1188 | 383 |
! ! |
384 |
||
385 |
!UUID methodsFor:'generating uuids'! |
|
386 |
||
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
387 |
genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
388 |
"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
|
389 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
390 |
self replaceFrom:1 to:16 with:(RandomGenerator new nextBytes:16). |
1612 | 391 |
"multiplex the 4 bit version number (Version 4 -> Random UUID) in high bits of byte 7" |
392 |
self at:7 put:(((self at:7) bitAnd:16r0F) bitOr:16r40). |
|
393 |
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
|
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 |
self new genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
397 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
398 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
399 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
400 |
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
|
401 |
Transcript show:el. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
402 |
Transcript show:': '. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
403 |
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
|
404 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
405 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
406 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
407 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
408 |
genTimestampUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
409 |
"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
|
410 |
|
1610 | 411 |
|macBytes utcTime seqNr| |
1188 | 412 |
|
1213 | 413 |
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
|
414 |
macBytes size ~~ 6 ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
415 |
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
|
416 |
]. |
1188 | 417 |
|
1612 | 418 |
"use 60 bit counter of 100ns ticks since 00:00:00 15.oct 1582 (sigh)" |
1610 | 419 |
utcTime := self class getDtssUtcTime. |
420 |
||
1213 | 421 |
Lock critical:[ |
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
422 |
SequenceNumber isNil ifTrue:[ |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
423 |
SequenceNumber := RandomGenerator new nextIntegerBetween:0 and:16383. |
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
424 |
]. |
1612 | 425 |
utcTime > (LastTime+Increment) ifTrue:[ |
1213 | 426 |
Increment := 0. |
427 |
] ifFalse:[ |
|
428 |
LastTime = utcTime ifTrue:[ |
|
429 |
"clock didn't advance since last call. Simply add a tick" |
|
430 |
Increment := Increment + 1. |
|
431 |
] ifFalse:[ |
|
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
432 |
"clock went backwards - increment SequenceNumber" |
1213 | 433 |
Increment := 0. |
1214 | 434 |
SequenceNumber := SequenceNumber + 1. |
435 |
SequenceNumber >= 16384 ifTrue:[SequenceNumber := 0]. |
|
1213 | 436 |
]. |
1188 | 437 |
]. |
1214 | 438 |
|
1213 | 439 |
LastTime := utcTime. |
440 |
utcTime := utcTime + Increment. |
|
1610 | 441 |
seqNr := SequenceNumber. |
1188 | 442 |
]. |
443 |
||
1612 | 444 |
|
445 |
"time low: long" |
|
446 |
self unsignedLongAt:1 put:(utcTime bitAnd:16rFFFFFFFF) bigEndian:true. |
|
447 |
||
448 |
"time med: short" |
|
449 |
utcTime := utcTime bitShift:-32. |
|
450 |
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
|
451 |
|
1612 | 452 |
"time high and version: short |
453 |
multiplex the 4 bit version number in highest 4 bits (version 1 -> time based version)" |
|
454 |
self unsignedShortAt:7 put:((utcTime bitShift:-16) bitOr:16r1000) bigEndian:true. |
|
1188 | 455 |
|
1612 | 456 |
"2 sequence bytes + reserved bits (this is not a short!!)" |
457 |
self at:9 put:(((seqNr bitShift:-8) bitAnd:16r3F) bitOr:16r80). |
|
458 |
self at:10 put:(seqNr bitAnd:16rFF). |
|
1213 | 459 |
|
460 |
"48 bits of MAC-Address" |
|
461 |
self replaceFrom:11 to:16 with:macBytes startingAt:1. |
|
1188 | 462 |
|
463 |
" |
|
1612 | 464 |
self new genTimestampUUID |
1213 | 465 |
" |
1188 | 466 |
|
1213 | 467 |
" |
468 |
1 to: 100 do:[ : el | |
|
469 |
Transcript show:el. |
|
470 |
Transcript show:': '. |
|
1612 | 471 |
Transcript showCR:(UUID new genTimestampUUID). |
1213 | 472 |
]. |
473 |
" |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
474 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
475 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
476 |
genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
477 |
"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
|
478 |
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
|
479 |
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
|
480 |
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
|
481 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
482 |
CachedMACAddress isNil ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
483 |
self class getValidMACAddress. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
484 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
485 |
CachedMACAddress == false ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
486 |
"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
|
487 |
^ self genRandomUUID. |
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 genTimestampUUID. |
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 |
self genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
493 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
494 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
495 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
496 |
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
|
497 |
Transcript show:el. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
498 |
Transcript show:': '. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
499 |
Transcript showCR:(UUID genUUID). |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
500 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
501 |
" |
1188 | 502 |
! ! |
503 |
||
504 |
!UUID methodsFor:'printing'! |
|
505 |
||
506 |
displayString |
|
507 |
^ self printString |
|
508 |
! |
|
509 |
||
510 |
printOn:aStream |
|
1602 | 511 |
|d tmpStream| |
1188 | 512 |
|
1213 | 513 |
tmpStream := '' writeStream. |
1188 | 514 |
|
1612 | 515 |
1 to:4 do:[:idx| |
516 |
d := self at:idx. |
|
517 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
518 |
]. |
|
1602 | 519 |
tmpStream nextPut:$-. |
1612 | 520 |
5 to:6 do:[:idx| |
521 |
d := self at:idx. |
|
522 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
523 |
]. |
|
1602 | 524 |
tmpStream nextPut:$-. |
1612 | 525 |
7 to:8 do:[:idx| |
526 |
d := self at:idx. |
|
527 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
528 |
]. |
|
1602 | 529 |
tmpStream nextPut:$-. |
1612 | 530 |
9 to:10 do:[:idx| |
531 |
d := self at:idx. |
|
532 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
533 |
]. |
|
1213 | 534 |
tmpStream nextPut:$-. |
1602 | 535 |
11 to:16 do:[:idx| |
536 |
d := self at:idx. |
|
1213 | 537 |
d printOn:tmpStream base:16 size:2 fill:$0. |
1188 | 538 |
]. |
539 |
||
1213 | 540 |
aStream nextPutAll:(tmpStream contents asLowercase). |
1188 | 541 |
! ! |
542 |
||
543 |
!UUID class methodsFor:'documentation'! |
|
544 |
||
545 |
version |
|
1647 | 546 |
^ '$Header: /cvs/stx/stx/libbasic2/UUID.st,v 1.22 2006-07-17 09:50:33 stefan Exp $' |
1188 | 547 |
! ! |
1213 | 548 |
|
549 |
UUID initialize! |