author | Claus Gittinger <cg@exept.de> |
Fri, 18 Mar 2005 17:57:23 +0100 | |
changeset 1543 | 929986f0910a |
parent 1491 | 512b21b45a9e |
child 1544 | 6c339f276b91 |
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:'' |
|
1213 | 17 |
classVariableNames:'CachedMACAddress Lock SequenceNumber LastTime Increment' |
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 |
" |
|
40 |
128-bit Universal Unique Ids (UUIDs) as define by OpenGroup/DCE |
|
41 |
http://www.opengroup.org/onlinepubs/9629399/apdxa.htm. |
|
42 |
||
43 |
A UUID is unique in time and space (at least until about Year 3400). |
|
44 |
||
45 |
[author:] |
|
46 |
||
47 |
[instance variables:] |
|
48 |
||
49 |
[class variables:] |
|
50 |
||
51 |
[see also:] |
|
52 |
||
53 |
" |
|
54 |
! ! |
|
55 |
||
56 |
!UUID class methodsFor:'initialization'! |
|
57 |
||
58 |
initialize |
|
59 |
"I want to get informed about image restarts" |
|
60 |
||
61 |
Lock isNil ifTrue:[ |
|
62 |
Lock := RecursionLock new name:'UUID'. |
|
63 |
LastTime := 0. |
|
64 |
Increment := 0. |
|
65 |
ObjectMemory addDependent:self. |
|
66 |
] |
|
67 |
! |
|
68 |
||
69 |
update:something with:aParameter from:changedObject |
|
70 |
"flush cached MAC address (may have been restarted on another host)" |
|
71 |
||
72 |
(something == #restarted) ifTrue:[ |
|
73 |
CachedMACAddress := nil. |
|
74 |
SequenceNumber := nil. |
|
75 |
] |
|
1188 | 76 |
! ! |
77 |
||
78 |
!UUID class methodsFor:'instance creation'! |
|
79 |
||
1382 | 80 |
fromBytes:aByteArray |
81 |
^ self fromBytes:aByteArray msb:(UninterpretedBytes isBigEndian) |
|
82 |
! |
|
83 |
||
1188 | 84 |
fromBytes:aByteArray msb:msb |
85 |
|uuid d1 d2 d3| |
|
86 |
||
87 |
uuid := self new. |
|
88 |
uuid replaceFrom:1 to:16 with:aByteArray. |
|
89 |
||
90 |
msb ifTrue:[ |
|
91 |
d1 := aByteArray unsignedLongAt:1 bigEndian:msb. |
|
92 |
uuid unsignedLongAt:1 put:d1 bigEndian:false. |
|
93 |
||
94 |
d2 := aByteArray unsignedShortAt:1+4 bigEndian:msb. |
|
95 |
uuid unsignedShortAt:1+4 put:d2 bigEndian:false. |
|
96 |
||
97 |
d3 := aByteArray unsignedShortAt:1+4+2 bigEndian:msb. |
|
98 |
uuid unsignedShortAt:1+4+2 put:d3 bigEndian:false. |
|
99 |
]. |
|
100 |
||
101 |
^ uuid |
|
102 |
||
103 |
" |
|
104 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
105 |
16r05 16r06 16r07 16r08 |
|
106 |
16r09 16r10 16r11 16r12 |
|
107 |
16r13 16r14 16r15 16r16] msb:false. |
|
108 |
" |
|
109 |
" |
|
110 |
UUID fromBytes:#[16r01 16r02 16r03 16r04 |
|
111 |
16r05 16r06 16r07 16r08 |
|
112 |
16r09 16r10 16r11 16r12 |
|
113 |
16r13 16r14 16r15 16r16] msb:true. |
|
114 |
" |
|
115 |
! |
|
116 |
||
1213 | 117 |
genUUID |
118 |
"generate a new UUID" |
|
1188 | 119 |
|
120 |
^ self new genUUID |
|
1447 | 121 |
" |
122 |
self genUUID |
|
123 |
" |
|
1188 | 124 |
! |
125 |
||
126 |
new |
|
127 |
^ super new:16 |
|
128 |
! |
|
129 |
||
130 |
new:size |
|
1320
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
131 |
"allow creating with exact size. We need this for XMLStandardDecoder" |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
132 |
|
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
133 |
size ~~ 16 ifTrue:[ |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
134 |
^ self shouldNotImplement. |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
135 |
]. |
fc8265f89040
XMLStandardDecoder compatibility
Stefan Vogel <sv@exept.de>
parents:
1304
diff
changeset
|
136 |
^ super new:size. |
1213 | 137 |
! |
138 |
||
139 |
readFrom:aStringOrStream onError:errorBlock |
|
140 |
|d offs s uuid t byte| |
|
141 |
||
1543 | 142 |
Error handle:[:ex | |
143 |
^ errorBlock value. |
|
144 |
] do:[ |
|
145 |
s := aStringOrStream readStream. |
|
146 |
uuid := self new. |
|
147 |
offs := 1. |
|
1213 | 148 |
|
1543 | 149 |
s skipSeparators. |
150 |
s peek == ${ ifTrue:[s next]. |
|
151 |
s skipSeparators. |
|
1213 | 152 |
|
1543 | 153 |
t := s next:8. |
154 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
155 |
uuid unsignedLongAt:1 put:d bigEndian:false. |
|
156 |
offs := offs + 4. |
|
1213 | 157 |
|
1543 | 158 |
s next. |
1213 | 159 |
|
1543 | 160 |
1 to:3 do:[:i | |
161 |
t := s next:4. |
|
162 |
d := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
163 |
uuid unsignedShortAt:offs put:d bigEndian:false. |
|
164 |
offs := offs + 2. |
|
165 |
s next. |
|
166 |
]. |
|
1213 | 167 |
|
1543 | 168 |
1 to:6 do:[:i | |
169 |
t := s next:2. |
|
170 |
byte := Integer readFrom:t radix:16 onError:[^ errorBlock value]. |
|
171 |
uuid at:offs put:byte. |
|
172 |
offs := offs + 1. |
|
173 |
]. |
|
1213 | 174 |
]. |
175 |
^ uuid |
|
176 |
||
177 |
" |
|
178 |
UUID readFrom:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' |
|
179 |
UUID fromString:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77' |
|
180 |
" |
|
1188 | 181 |
! ! |
182 |
||
183 |
!UUID class methodsFor:'defaults'! |
|
184 |
||
185 |
uuidVersion |
|
186 |
||
187 |
^ 1 |
|
188 |
! ! |
|
189 |
||
1213 | 190 |
!UUID class methodsFor:'helpers'! |
191 |
||
192 |
getDtssUtcTime |
|
193 |
"return the DTSS based time in 100 nsec intervals |
|
194 |
DTSS UTC base time is October 15, 1582. |
|
195 |
Unix base time is January 1, 1970. |
|
196 |
The difference between both is: 16r01B21DD213814000" |
|
197 |
||
1433
47729d4283ef
Use Timestamp/#asTimestamp instead of AbsoluteTime/#asAbsoluteTime
Stefan Vogel <sv@exept.de>
parents:
1382
diff
changeset
|
198 |
^ Timestamp now getMilliseconds * 10000 + 16r01B21DD213814000. |
1213 | 199 |
! |
200 |
||
201 |
getValidMACAddress |
|
202 |
"return the first valid MAC address (i.e. having at least one byte ~~ 0)" |
|
203 |
||
204 |
CachedMACAddress isNil ifTrue:[ |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
205 |
|dictOfIf ipAddr| |
1213 | 206 |
|
207 |
dictOfIf := OperatingSystem getNetworkMACAddresses. |
|
208 |
||
209 |
dictOfIf do:[:macAddress | |
|
210 |
(macAddress contains:[:byte | byte ~~ 0]) ifTrue:[ |
|
211 |
^ CachedMACAddress := macAddress |
|
212 |
]. |
|
213 |
]. |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
214 |
'UUID [warning]: no mac address - using ipAddress' errorPrintCR. |
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
215 |
ipAddr := Socket ipAddressOfHost:(OperatingSystem getHostName). |
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
216 |
^ ipAddr , #[16r55 16r55]. |
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
217 |
"/ ^ #[16r55 16r55 16r55 16r55 16r55 16r55]. |
1213 | 218 |
]. |
219 |
||
220 |
^ CachedMACAddress |
|
221 |
||
222 |
" |
|
223 |
CachedMACAddress := nil. |
|
224 |
self getValidMACAddress |
|
225 |
" |
|
1491
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
226 |
|
512b21b45a9e
noMacAddress fallBack (NT4.0)
Claus Gittinger <cg@exept.de>
parents:
1467
diff
changeset
|
227 |
"Modified: / 17-11-2004 / 01:45:53 / cg" |
1213 | 228 |
! ! |
229 |
||
230 |
!UUID methodsFor:'accessing'! |
|
231 |
||
232 |
clockSeqAndReserved |
|
233 |
||
234 |
^ self unsignedShortAt:9 bigEndian:false |
|
235 |
! |
|
236 |
||
237 |
node |
|
238 |
||
239 |
^ self copyFrom:10 to:16 |
|
240 |
! |
|
241 |
||
242 |
timeHighAndVersion |
|
243 |
||
244 |
^ self unsignedShortAt:7 bigEndian:false |
|
245 |
! |
|
246 |
||
247 |
timeLow |
|
248 |
||
249 |
^ self unsignedLongAt:1 bigEndian:false |
|
250 |
! |
|
251 |
||
252 |
timeMid |
|
253 |
||
254 |
^ self unsignedShortAt:5 bigEndian:false |
|
255 |
! ! |
|
256 |
||
1188 | 257 |
!UUID methodsFor:'converting'! |
258 |
||
1382 | 259 |
asBytes |
260 |
^ self asBytesMSB:(UninterpretedBytes isBigEndian) |
|
261 |
! |
|
262 |
||
1188 | 263 |
asBytesMSB:msb |
264 |
|bytes d1 d2 d3| |
|
265 |
||
266 |
bytes := ByteArray new:16. |
|
267 |
bytes replaceFrom:1 to:16 with:self. |
|
268 |
||
269 |
msb ifTrue:[ |
|
270 |
d1 := self unsignedLongAt:1 bigEndian:false. |
|
271 |
d2 := self unsignedShortAt:1+4 bigEndian:false. |
|
272 |
d3 := self unsignedShortAt:1+4+2 bigEndian:false. |
|
273 |
||
274 |
bytes unsignedLongAt:1 put:d1 bigEndian:true. |
|
275 |
bytes unsignedShortAt:1+4 put:d2 bigEndian:true. |
|
276 |
bytes unsignedShortAt:1+4+2 put:d3 bigEndian:true. |
|
277 |
]. |
|
278 |
^ bytes |
|
279 |
! ! |
|
280 |
||
281 |
!UUID methodsFor:'generating uuids'! |
|
282 |
||
283 |
genUUID |
|
1213 | 284 |
|macBytes utcTime| |
1188 | 285 |
|
1213 | 286 |
macBytes := self class getValidMACAddress. |
1188 | 287 |
|
1213 | 288 |
"use 60 bit counter of 100ns ticks since 00:00:00 15.oct 1582 (sigh)" |
289 |
Lock critical:[ |
|
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
290 |
SequenceNumber isNil ifTrue:[ |
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
291 |
SequenceNumber := Random nextIntegerBetween:0 and:16383. |
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
292 |
]. |
1213 | 293 |
utcTime := self class getDtssUtcTime. |
294 |
LastTime < utcTime ifTrue:[ |
|
295 |
Increment := 0. |
|
296 |
] ifFalse:[ |
|
297 |
LastTime = utcTime ifTrue:[ |
|
298 |
"clock didn't advance since last call. Simply add a tick" |
|
299 |
Increment := Increment + 1. |
|
300 |
] ifFalse:[ |
|
1467
a394ade68816
Protect against a nil SequenceNumber
Stefan Vogel <sv@exept.de>
parents:
1447
diff
changeset
|
301 |
"clock went backwards - increment SequenceNumber" |
1213 | 302 |
Increment := 0. |
1214 | 303 |
SequenceNumber := SequenceNumber + 1. |
304 |
SequenceNumber >= 16384 ifTrue:[SequenceNumber := 0]. |
|
1213 | 305 |
]. |
1188 | 306 |
]. |
1214 | 307 |
|
1213 | 308 |
LastTime := utcTime. |
309 |
utcTime := utcTime + Increment. |
|
1188 | 310 |
]. |
311 |
||
1213 | 312 |
"first 60 bits of timestamp" |
313 |
self replaceFrom:1 to:8 with:utcTime digitBytes. |
|
314 |
"multiplex the 4 bit version number in high bits of byte 8" |
|
315 |
self at:8 put:((self at:8) bitOr:(self class uuidVersion bitShift:4)). |
|
1188 | 316 |
|
1213 | 317 |
"2 sequence bytes + reserved bits" |
318 |
self at:9 put:(SequenceNumber digitAt:1). |
|
319 |
self at:10 put:((SequenceNumber digitAt:2) bitOr:2r10000000). |
|
320 |
||
321 |
"48 bits of MAC-Address" |
|
322 |
self replaceFrom:11 to:16 with:macBytes startingAt:1. |
|
1188 | 323 |
|
324 |
" |
|
1213 | 325 |
self genUUID |
326 |
" |
|
1188 | 327 |
|
1213 | 328 |
" |
329 |
1 to: 100 do:[ : el | |
|
330 |
Transcript show:el. |
|
331 |
Transcript show:': '. |
|
332 |
Transcript showCR:(UUID genUUID). |
|
333 |
]. |
|
334 |
" |
|
1188 | 335 |
! ! |
336 |
||
337 |
!UUID methodsFor:'printing'! |
|
338 |
||
339 |
displayString |
|
340 |
^ self printString |
|
341 |
! |
|
342 |
||
343 |
printOn:aStream |
|
1213 | 344 |
|offs d tmpStream| |
1188 | 345 |
|
1213 | 346 |
tmpStream := '' writeStream. |
1188 | 347 |
|
348 |
d := self unsignedLongAt:1 bigEndian:false. |
|
1213 | 349 |
d printOn:tmpStream base:16 size:8 fill:$0. |
1188 | 350 |
|
1213 | 351 |
tmpStream nextPut:$-. |
1188 | 352 |
|
1213 | 353 |
offs := 5. |
354 |
3 timesRepeat:[ |
|
1188 | 355 |
d := self unsignedShortAt:offs bigEndian:false. |
356 |
offs := offs + 2. |
|
1213 | 357 |
d printOn:tmpStream base:16 size:4 fill:$0. |
358 |
tmpStream nextPut:$-. |
|
1188 | 359 |
]. |
360 |
||
361 |
6 timesRepeat:[ |
|
1213 | 362 |
d := self at:offs. |
1188 | 363 |
offs := offs + 1. |
1213 | 364 |
d printOn:tmpStream base:16 size:2 fill:$0. |
1188 | 365 |
]. |
366 |
||
1213 | 367 |
aStream nextPutAll:(tmpStream contents asLowercase). |
1188 | 368 |
! ! |
369 |
||
370 |
!UUID class methodsFor:'documentation'! |
|
371 |
||
372 |
version |
|
1543 | 373 |
^ '$Header: /cvs/stx/stx/libbasic2/UUID.st,v 1.12 2005-03-18 16:57:23 cg Exp $' |
1188 | 374 |
! ! |
1213 | 375 |
|
376 |
UUID initialize! |