author | Stefan Vogel <sv@exept.de> |
Wed, 21 Nov 2007 18:24:07 +0100 | |
changeset 1917 | 61c602336f3d |
parent 1898 | 8041791c02c3 |
child 1923 | 6380ea5d7385 |
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 |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
17 |
NameSpaceDNS NameSpaceDN NameSpaceURL NameSpaceOID' |
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:[ |
|
70 |
Lock := RecursionLock new name:'UUID'. |
|
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 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
75 |
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
|
76 |
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
|
77 |
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
|
78 |
NameSpaceDN := self fromBytes:#[16r6B 16rA7 16rB8 16r14 16r9D 16rAD 16r11 16rD1 16r80 16rB4 16r0 16rC0 16r4F 16rD4 16r30 16rC8]. |
1213 | 79 |
] |
80 |
! |
|
81 |
||
82 |
update:something with:aParameter from:changedObject |
|
83 |
"flush cached MAC address (may have been restarted on another host)" |
|
84 |
||
85 |
(something == #restarted) ifTrue:[ |
|
86 |
CachedMACAddress := nil. |
|
87 |
SequenceNumber := nil. |
|
88 |
] |
|
1188 | 89 |
! ! |
90 |
||
91 |
!UUID class methodsFor:'instance creation'! |
|
92 |
||
1382 | 93 |
fromBytes:aByteArray |
1602 | 94 |
"set uuid from aByteArray. |
95 |
aByteArray must be 16 bytes in network byte order (MSB-first)" |
|
1382 | 96 |
|
1602 | 97 |
|uuid| |
1188 | 98 |
|
99 |
uuid := self new. |
|
100 |
uuid replaceFrom:1 to:16 with:aByteArray. |
|
1602 | 101 |
^ uuid. |
1188 | 102 |
|
1602 | 103 |
" |
104 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
105 |
16r05 16r06 16r07 16r08 |
|
106 |
16r09 16r10 16r11 16r12 |
|
107 |
16r13 16r14 16r15 16r16]. |
|
108 |
" |
|
109 |
! |
|
1188 | 110 |
|
1602 | 111 |
fromBytes:aByteArray msb:msb |
112 |
"Set the UUID from aByteArray. UUIDS are stored internally as MSB-first. |
|
113 |
So, alway set |
|
114 |
msb = true if reading from network or persistent storage" |
|
1188 | 115 |
|
1602 | 116 |
|uuid| |
117 |
||
118 |
uuid := self fromBytes:aByteArray. |
|
119 |
msb ifFalse:[ |
|
120 |
self adjustByteOrder:uuid. |
|
1188 | 121 |
]. |
122 |
||
123 |
^ uuid |
|
124 |
||
125 |
" |
|
126 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
127 |
16r05 16r06 16r07 16r08 |
|
128 |
16r09 16r10 16r11 16r12 |
|
129 |
16r13 16r14 16r15 16r16] msb:false. |
|
130 |
" |
|
131 |
" |
|
132 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
133 |
16r05 16r06 16r07 16r08 |
|
134 |
16r09 16r10 16r11 16r12 |
|
135 |
16r13 16r14 16r15 16r16] msb:true. |
|
136 |
" |
|
137 |
! |
|
138 |
||
1602 | 139 |
fromNativeBytes:aByteArray |
140 |
"convert bytes to uuid. |
|
141 |
aByteArray represents a UUID in host byte order |
|
142 |
- e.g. an UUID fetched from the Windows OS" |
|
143 |
||
144 |
|uuid| |
|
145 |
||
146 |
uuid := self fromBytes:aByteArray. |
|
147 |
self isBigEndian ifFalse:[ |
|
148 |
self adjustByteOrder:uuid. |
|
149 |
]. |
|
150 |
^ uuid. |
|
151 |
||
152 |
" |
|
153 |
UUID fromNativeBytes:#[16r01 16r02 16r03 16r04 |
|
154 |
16r05 16r06 16r07 16r08 |
|
155 |
16r09 16r10 16r11 16r12 |
|
156 |
16r13 16r14 16r15 16r16]. |
|
157 |
||
158 |
" |
|
159 |
! |
|
160 |
||
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
161 |
genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
162 |
"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
|
163 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
164 |
^ self new genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
165 |
|
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 |
self genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
168 |
" |
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 |
|
1213 | 171 |
genUUID |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
172 |
"generate a uuid. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
173 |
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
|
174 |
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
|
175 |
otherwise a random uuid will be generated." |
1188 | 176 |
|
177 |
^ self new genUUID |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
178 |
|
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 |
self genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
181 |
" |
1188 | 182 |
! |
183 |
||
184 |
new |
|
185 |
^ super new:16 |
|
186 |
! |
|
187 |
||
188 |
new:size |
|
1320
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
189 |
"allow creating with exact size. We need this for XMLStandardDecoder" |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
190 |
|
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
191 |
size ~~ 16 ifTrue:[ |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
192 |
^ self shouldNotImplement. |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
193 |
]. |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
194 |
^ super new:size. |
1213 | 195 |
! |
196 |
||
197 |
readFrom:aStringOrStream onError:errorBlock |
|
198 |
|d offs s uuid t byte| |
|
199 |
||
1543 | 200 |
Error handle:[:ex | |
201 |
^ errorBlock value. |
|
202 |
] do:[ |
|
203 |
s := aStringOrStream readStream. |
|
204 |
uuid := self new. |
|
1213 | 205 |
|
1543 | 206 |
s skipSeparators. |
207 |
s peek == ${ ifTrue:[s next]. |
|
208 |
s skipSeparators. |
|
1213 | 209 |
|
1543 | 210 |
t := s next:8. |
211 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
1602 | 212 |
uuid unsignedLongAt:1 put:d bigEndian:true. |
213 |
offs := 5. |
|
1213 | 214 |
|
1602 | 215 |
s next. "skip $-" |
1213 | 216 |
|
1601
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
217 |
1 to:2 do:[:i | |
1543 | 218 |
t := s next:4. |
219 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
1602 | 220 |
uuid unsignedShortAt:offs put:d bigEndian:true. |
1543 | 221 |
offs := offs + 2. |
1602 | 222 |
s next. "skip $-" |
1543 | 223 |
]. |
1213 | 224 |
|
1602 | 225 |
1 to:2 do:[:i | |
226 |
t := s next:2. |
|
227 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
228 |
uuid at:offs put:d. |
|
229 |
offs := offs + 1. |
|
230 |
]. |
|
231 |
s next. "skip $-" |
|
1601
b0dfbe332caf
WARNING: byte order of internal byte-representation changed;
Claus Gittinger <cg@exept.de>
parents:
1577
diff
changeset
|
232 |
|
1543 | 233 |
1 to:6 do:[:i | |
234 |
t := s next:2. |
|
235 |
byte := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
236 |
uuid at:offs put:byte. |
|
237 |
offs := offs + 1. |
|
238 |
]. |
|
1213 | 239 |
]. |
240 |
^ uuid |
|
241 |
||
242 |
" |
|
243 |
UUID readFrom:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' |
|
1898 | 244 |
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
|
245 |
UUID fromString:'00000001-0000-0000-C000-000000000046' |
1898 | 246 |
UUID fromString:'00000001-0000-0000-C000-000000004600' |
247 |
UUID fromString:'00000001-0000-0000-C000-000000460000' |
|
248 |
UUID fromString:'00000001-0000-0000-C000-000046000000' |
|
249 |
UUID fromString:'00000001-0000-0000-C000-004600000000' |
|
250 |
UUID fromString:'00000001-0000-0000-C000-460000000000' |
|
251 |
UUID fromString:'00000001-0000-0000-C046-000000000000' |
|
252 |
UUID fromString:'00000001-0000-0046-C000-000000000000' |
|
253 |
UUID fromString:'00000001-0000-4600-C000-000000000000' |
|
254 |
UUID fromString:'00000001-0046-0000-C000-000000000000' |
|
255 |
UUID fromString:'00000001-4600-0000-C000-000000000000' |
|
256 |
UUID fromString:'00000100-4600-0000-C000-000000000000' |
|
257 |
UUID fromString:'00010000-4600-0000-C000-000000000000' |
|
258 |
UUID fromString:'10000000-4600-0000-C000-000000000000' |
|
1213 | 259 |
" |
1898 | 260 |
|
261 |
"Modified: / 10-10-2007 / 23:03:47 / cg" |
|
1188 | 262 |
! ! |
263 |
||
1213 | 264 |
!UUID class methodsFor:'helpers'! |
265 |
||
1602 | 266 |
adjustByteOrder:aByteArray |
267 |
"change the byte order of the uuid" |
|
268 |
||
269 |
|d| |
|
270 |
||
271 |
d := aByteArray unsignedLongAt:1 bigEndian:false. |
|
272 |
aByteArray unsignedLongAt:1 put:d bigEndian:true. |
|
273 |
||
274 |
d := aByteArray unsignedShortAt:1+4 bigEndian:false. |
|
275 |
aByteArray unsignedShortAt:1+4 put:d bigEndian:true. |
|
276 |
||
277 |
d := aByteArray unsignedShortAt:1+4+2 bigEndian:false. |
|
278 |
aByteArray unsignedShortAt:1+4+2 put:d bigEndian:true. |
|
279 |
! |
|
280 |
||
1213 | 281 |
getDtssUtcTime |
282 |
"return the DTSS based time in 100 nsec intervals |
|
1647 | 283 |
DTSS UTC base time is October 15, 1582 (the start of the Gregorian Calendar)." |
284 |
||
285 |
"Unix base time is January 1, 1970. |
|
286 |
The difference between both is: 122192928000000000" |
|
1213 | 287 |
|
1647 | 288 |
" |
289 |
(Timestamp epoch asDate subtractDate:(Date day:15 month:10 year:1582)) |
|
290 |
* (24 * 60 * 60) |
|
291 |
" |
|
1613 | 292 |
|
1647 | 293 |
^ (Timestamp now getMilliseconds + 12219292800000) * 10000. |
1213 | 294 |
! |
295 |
||
296 |
getValidMACAddress |
|
297 |
"return the first valid MAC address (i.e. having at least one byte ~~ 0)" |
|
298 |
||
299 |
CachedMACAddress isNil ifTrue:[ |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
300 |
|dictOfIf ipAddr| |
1213 | 301 |
|
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
302 |
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
|
303 |
[ |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
304 |
dictOfIf := OperatingSystem getNetworkMACAddresses. |
1213 | 305 |
|
1577
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
306 |
dictOfIf do:[:macAddress | |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
307 |
(macAddress contains:[:byte | byte ~~ 0]) ifTrue:[ |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
308 |
^ CachedMACAddress := macAddress |
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
309 |
]. |
1213 | 310 |
]. |
1577
c0db2891f228
Ignore primitive failures when doing #getNetworkMACAddresses
Stefan Vogel <sv@exept.de>
parents:
1544
diff
changeset
|
311 |
] on:PrimitiveFailure do:[:ex| "ignore"]. |
1213 | 312 |
]. |
313 |
||
314 |
^ CachedMACAddress |
|
315 |
||
316 |
" |
|
317 |
CachedMACAddress := nil. |
|
318 |
self getValidMACAddress |
|
319 |
" |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
320 |
|
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
321 |
"Modified: / 17-11-2004 / 01:45:53 / cg" |
1213 | 322 |
! ! |
323 |
||
324 |
!UUID methodsFor:'accessing'! |
|
325 |
||
1602 | 326 |
clockSeqHiAndReserved |
327 |
^ self at:9 |
|
328 |
! |
|
1213 | 329 |
|
1602 | 330 |
clockSeqLow |
331 |
^ self at:10 |
|
1213 | 332 |
! |
333 |
||
334 |
node |
|
1602 | 335 |
^ self copyFrom:11 to:16 |
1213 | 336 |
! |
337 |
||
338 |
timeHighAndVersion |
|
1602 | 339 |
^ self unsignedShortAt:7 bigEndian:true |
1213 | 340 |
! |
341 |
||
342 |
timeLow |
|
1602 | 343 |
^ self unsignedLongAt:1 bigEndian:true |
1213 | 344 |
! |
345 |
||
346 |
timeMid |
|
1602 | 347 |
^ self unsignedShortAt:5 bigEndian:true |
1213 | 348 |
! ! |
349 |
||
1188 | 350 |
!UUID methodsFor:'converting'! |
351 |
||
1382 | 352 |
asBytes |
1602 | 353 |
"convert this UUID to a ByteArray in network byte order (MSB-first)" |
1382 | 354 |
|
1602 | 355 |
|bytes| |
1188 | 356 |
|
357 |
bytes := ByteArray new:16. |
|
358 |
bytes replaceFrom:1 to:16 with:self. |
|
1602 | 359 |
^ bytes |
1188 | 360 |
|
1602 | 361 |
" |
362 |
|bytes| |
|
363 |
||
364 |
bytes := #[16r01 16r02 16r03 16r04 |
|
365 |
16r05 16r06 16r07 16r08 |
|
366 |
16r09 16r10 16r11 16r12 |
|
367 |
16r13 16r14 16r15 16r16]. |
|
368 |
(UUID fromBytes:bytes) asBytes ~= bytes ifTrue:[self halt] |
|
369 |
" |
|
370 |
! |
|
1188 | 371 |
|
1602 | 372 |
asBytesMSB:msb |
373 |
"convert this UUID to a ByteArray. |
|
1898 | 374 |
If msb == false, it is converted into LSB-first byte ordering" |
1602 | 375 |
|
376 |
|bytes| |
|
377 |
||
378 |
bytes := self asBytes. |
|
379 |
||
380 |
msb ifFalse:[ |
|
381 |
self class adjustByteOrder:bytes. |
|
1188 | 382 |
]. |
383 |
^ bytes |
|
1898 | 384 |
|
385 |
"Modified: / 10-10-2007 / 22:51:09 / cg" |
|
1602 | 386 |
! |
387 |
||
388 |
asNativeBytes |
|
389 |
"convert this uuid to a ByteArray in host byte order. |
|
390 |
Use this only to pass the UUID to the OS (Windows)" |
|
391 |
||
392 |
|bytes| |
|
393 |
||
394 |
bytes := self asBytes. |
|
395 |
self class isBigEndian ifFalse:[ |
|
396 |
self class adjustByteOrder:bytes. |
|
397 |
]. |
|
398 |
^ bytes. |
|
1188 | 399 |
! ! |
400 |
||
401 |
!UUID methodsFor:'generating uuids'! |
|
402 |
||
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
403 |
genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
404 |
"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
|
405 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
406 |
self replaceFrom:1 to:16 with:(RandomGenerator new nextBytes:16). |
1612 | 407 |
"multiplex the 4 bit version number (Version 4 -> Random UUID) in high bits of byte 7" |
408 |
self at:7 put:(((self at:7) bitAnd:16r0F) bitOr:16r40). |
|
409 |
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
|
410 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
411 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
412 |
self new genRandomUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
413 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
414 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
415 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
416 |
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
|
417 |
Transcript show:el. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
418 |
Transcript show:': '. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
419 |
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
|
420 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
421 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
422 |
! |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
423 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
424 |
genTimestampUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
425 |
"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
|
426 |
|
1610 | 427 |
|macBytes utcTime seqNr| |
1188 | 428 |
|
1213 | 429 |
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
|
430 |
macBytes size ~~ 6 ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
431 |
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
|
432 |
]. |
1188 | 433 |
|
1612 | 434 |
"use 60 bit counter of 100ns ticks since 00:00:00 15.oct 1582 (sigh)" |
1610 | 435 |
utcTime := self class getDtssUtcTime. |
436 |
||
1213 | 437 |
Lock critical:[ |
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
438 |
SequenceNumber isNil ifTrue:[ |
1608
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
439 |
SequenceNumber := RandomGenerator new nextIntegerBetween:0 and:16383. |
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
440 |
]. |
1612 | 441 |
utcTime > (LastTime+Increment) ifTrue:[ |
1213 | 442 |
Increment := 0. |
443 |
] ifFalse:[ |
|
444 |
LastTime = utcTime ifTrue:[ |
|
445 |
"clock didn't advance since last call. Simply add a tick" |
|
446 |
Increment := Increment + 1. |
|
447 |
] ifFalse:[ |
|
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
448 |
"clock went backwards - increment SequenceNumber" |
1213 | 449 |
Increment := 0. |
1214 | 450 |
SequenceNumber := SequenceNumber + 1. |
451 |
SequenceNumber >= 16384 ifTrue:[SequenceNumber := 0]. |
|
1213 | 452 |
]. |
1188 | 453 |
]. |
1214 | 454 |
|
1213 | 455 |
LastTime := utcTime. |
456 |
utcTime := utcTime + Increment. |
|
1610 | 457 |
seqNr := SequenceNumber. |
1188 | 458 |
]. |
459 |
||
1612 | 460 |
|
461 |
"time low: long" |
|
462 |
self unsignedLongAt:1 put:(utcTime bitAnd:16rFFFFFFFF) bigEndian:true. |
|
463 |
||
464 |
"time med: short" |
|
465 |
utcTime := utcTime bitShift:-32. |
|
466 |
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
|
467 |
|
1612 | 468 |
"time high and version: short |
469 |
multiplex the 4 bit version number in highest 4 bits (version 1 -> time based version)" |
|
470 |
self unsignedShortAt:7 put:((utcTime bitShift:-16) bitOr:16r1000) bigEndian:true. |
|
1188 | 471 |
|
1612 | 472 |
"2 sequence bytes + reserved bits (this is not a short!!)" |
473 |
self at:9 put:(((seqNr bitShift:-8) bitAnd:16r3F) bitOr:16r80). |
|
474 |
self at:10 put:(seqNr bitAnd:16rFF). |
|
1213 | 475 |
|
476 |
"48 bits of MAC-Address" |
|
477 |
self replaceFrom:11 to:16 with:macBytes startingAt:1. |
|
1188 | 478 |
|
479 |
" |
|
1612 | 480 |
self new genTimestampUUID |
1213 | 481 |
" |
1188 | 482 |
|
1213 | 483 |
" |
484 |
1 to: 100 do:[ : el | |
|
485 |
Transcript show:el. |
|
486 |
Transcript show:': '. |
|
1612 | 487 |
Transcript showCR:(UUID new genTimestampUUID). |
1213 | 488 |
]. |
489 |
" |
|
1608
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 |
genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
493 |
"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
|
494 |
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
|
495 |
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
|
496 |
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
|
497 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
498 |
CachedMACAddress isNil ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
499 |
self class getValidMACAddress. |
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 |
CachedMACAddress == false ifTrue:[ |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
502 |
"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
|
503 |
^ self genRandomUUID. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
504 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
505 |
^ self genTimestampUUID. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
506 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
507 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
508 |
self genUUID |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
509 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
510 |
|
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
511 |
" |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
512 |
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
|
513 |
Transcript show:el. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
514 |
Transcript show:': '. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
515 |
Transcript showCR:(UUID genUUID). |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
516 |
]. |
e1cf5d2b6f2f
Add random UUID in case there is no mac address
Stefan Vogel <sv@exept.de>
parents:
1602
diff
changeset
|
517 |
" |
1188 | 518 |
! ! |
519 |
||
520 |
!UUID methodsFor:'printing'! |
|
521 |
||
522 |
displayString |
|
523 |
^ self printString |
|
524 |
! |
|
525 |
||
526 |
printOn:aStream |
|
1602 | 527 |
|d tmpStream| |
1188 | 528 |
|
1213 | 529 |
tmpStream := '' writeStream. |
1188 | 530 |
|
1898 | 531 |
"/ d := self unsignedLongAt:1 bigEndian:true. |
532 |
"/ d printOn:tmpStream base:16 size:8 fill:$0. |
|
533 |
"/ tmpStream nextPut:$-. |
|
534 |
"/ |
|
535 |
"/ d := self unsignedShortAt:1+4 bigEndian:true. |
|
536 |
"/ d printOn:tmpStream base:16 size:4 fill:$0. |
|
537 |
"/ tmpStream nextPut:$-. |
|
538 |
"/ |
|
539 |
"/ d := self unsignedShortAt:1+4+2 bigEndian:true. |
|
540 |
"/ d printOn:tmpStream base:16 size:4 fill:$0. |
|
541 |
"/ tmpStream nextPut:$-. |
|
542 |
"/ |
|
543 |
"/ d := self at:1+4+2+2. |
|
544 |
"/ d printOn:tmpStream base:16 size:2 fill:$0. |
|
545 |
"/ d := self at:1+4+2+2+1. |
|
546 |
"/ d printOn:tmpStream base:16 size:2 fill:$0. |
|
547 |
"/ tmpStream nextPut:$-. |
|
548 |
"/ |
|
549 |
"/ 11 to:16 do:[:idx| |
|
550 |
"/ d := self at:idx. |
|
551 |
"/ d printOn:tmpStream base:16 size:2 fill:$0. |
|
552 |
"/ ]. |
|
553 |
||
1612 | 554 |
1 to:4 do:[:idx| |
555 |
d := self at:idx. |
|
556 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
557 |
]. |
|
1602 | 558 |
tmpStream nextPut:$-. |
1612 | 559 |
5 to:6 do:[:idx| |
560 |
d := self at:idx. |
|
561 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
562 |
]. |
|
1602 | 563 |
tmpStream nextPut:$-. |
1612 | 564 |
7 to:8 do:[:idx| |
565 |
d := self at:idx. |
|
566 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
567 |
]. |
|
1602 | 568 |
tmpStream nextPut:$-. |
1612 | 569 |
9 to:10 do:[:idx| |
570 |
d := self at:idx. |
|
571 |
d printOn:tmpStream base:16 size:2 fill:$0. |
|
572 |
]. |
|
1213 | 573 |
tmpStream nextPut:$-. |
1602 | 574 |
11 to:16 do:[:idx| |
575 |
d := self at:idx. |
|
1213 | 576 |
d printOn:tmpStream base:16 size:2 fill:$0. |
1188 | 577 |
]. |
578 |
||
1213 | 579 |
aStream nextPutAll:(tmpStream contents asLowercase). |
1898 | 580 |
|
581 |
"Modified: / 10-10-2007 / 23:19:03 / cg" |
|
1188 | 582 |
! ! |
583 |
||
584 |
!UUID class methodsFor:'documentation'! |
|
585 |
||
586 |
version |
|
1898 | 587 |
^ '$Header: /cvs/stx/stx/libbasic2/UUID.st,v 1.23 2007-10-10 21:17:50 cg Exp $' |
1188 | 588 |
! ! |
1213 | 589 |
|
590 |
UUID initialize! |