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:''
|
|
17 |
classVariableNames:'NextSeqNr'
|
|
18 |
poolDictionaries:''
|
|
19 |
category:'System-Communication-Support'
|
|
20 |
!
|
|
21 |
|
|
22 |
AbsoluteTime variableByteSubclass:#UTCTime
|
|
23 |
instanceVariableNames:''
|
|
24 |
classVariableNames:''
|
|
25 |
poolDictionaries:''
|
|
26 |
privateIn:UUID
|
|
27 |
!
|
|
28 |
|
|
29 |
Object variableByteSubclass:#UUIDRandom
|
|
30 |
instanceVariableNames:''
|
|
31 |
classVariableNames:'rand_m rand_ia rand_ib rand_irand'
|
|
32 |
poolDictionaries:''
|
|
33 |
privateIn:UUID
|
|
34 |
!
|
|
35 |
|
|
36 |
!UUID class methodsFor:'documentation'!
|
|
37 |
|
|
38 |
copyright
|
|
39 |
"
|
|
40 |
COPYRIGHT (c) 2002 by eXept Software AG
|
|
41 |
All Rights Reserved
|
|
42 |
|
|
43 |
This software is furnished under a license and may be used
|
|
44 |
only in accordance with the terms of that license and with the
|
|
45 |
inclusion of the above copyright notice. This software may not
|
|
46 |
be provided or otherwise made available to, or used by, any
|
|
47 |
other person. No title to or ownership of the software is
|
|
48 |
hereby transferred.
|
|
49 |
"
|
|
50 |
! !
|
|
51 |
|
|
52 |
!UUID class methodsFor:'instance creation'!
|
|
53 |
|
|
54 |
fromBytes:aByteArray msb:msb
|
|
55 |
|uuid d1 d2 d3|
|
|
56 |
|
|
57 |
uuid := self new.
|
|
58 |
uuid replaceFrom:1 to:16 with:aByteArray.
|
|
59 |
|
|
60 |
msb ifTrue:[
|
|
61 |
d1 := aByteArray unsignedLongAt:1 bigEndian:msb.
|
|
62 |
uuid unsignedLongAt:1 put:d1 bigEndian:false.
|
|
63 |
|
|
64 |
d2 := aByteArray unsignedShortAt:1+4 bigEndian:msb.
|
|
65 |
uuid unsignedShortAt:1+4 put:d2 bigEndian:false.
|
|
66 |
|
|
67 |
d3 := aByteArray unsignedShortAt:1+4+2 bigEndian:msb.
|
|
68 |
uuid unsignedShortAt:1+4+2 put:d3 bigEndian:false.
|
|
69 |
].
|
|
70 |
|
|
71 |
^ uuid
|
|
72 |
|
|
73 |
"
|
|
74 |
UUID fromBytes:#[16r01 16r02 16r03 16r04
|
|
75 |
16r05 16r06 16r07 16r08
|
|
76 |
16r09 16r10 16r11 16r12
|
|
77 |
16r13 16r14 16r15 16r16] msb:false.
|
|
78 |
"
|
|
79 |
"
|
|
80 |
UUID fromBytes:#[16r01 16r02 16r03 16r04
|
|
81 |
16r05 16r06 16r07 16r08
|
|
82 |
16r09 16r10 16r11 16r12
|
|
83 |
16r13 16r14 16r15 16r16] msb:true.
|
|
84 |
"
|
|
85 |
!
|
|
86 |
|
|
87 |
fromString:aString
|
|
88 |
|d offs s uuid t byte|
|
|
89 |
|
|
90 |
s := aString readStream.
|
|
91 |
uuid := self new.
|
|
92 |
offs := 1.
|
|
93 |
|
|
94 |
s skipSeparators.
|
|
95 |
s peek == ${ ifTrue:[s next].
|
|
96 |
s skipSeparators.
|
|
97 |
|
|
98 |
t := s next:8.
|
|
99 |
d := Integer readFrom:t radix:16.
|
|
100 |
uuid unsignedLongAt:1 put:d bigEndian:false.
|
|
101 |
offs := offs + 4.
|
|
102 |
|
|
103 |
s next.
|
|
104 |
|
|
105 |
1 to:2 do:[:i |
|
|
106 |
t := s next:4.
|
|
107 |
d := Integer readFrom:t radix:16.
|
|
108 |
uuid unsignedShortAt:offs put:d bigEndian:false.
|
|
109 |
offs := offs + 2.
|
|
110 |
s next.
|
|
111 |
].
|
|
112 |
|
|
113 |
t := s next:4.
|
|
114 |
d := Integer readFrom:t radix:16.
|
|
115 |
uuid unsignedShortAt:offs put:d bigEndian:true.
|
|
116 |
offs := offs + 2.
|
|
117 |
s next.
|
|
118 |
|
|
119 |
1 to:6 do:[:i |
|
|
120 |
t := s next:2.
|
|
121 |
byte := Integer readFrom:t radix:16.
|
|
122 |
uuid at:offs put:byte.
|
|
123 |
offs := offs + 1.
|
|
124 |
].
|
|
125 |
^ uuid
|
|
126 |
|
|
127 |
"
|
|
128 |
UUID fromString:'5ab2e9b4-3d48-11d2-9ea4-80c5140aaa77'
|
|
129 |
"
|
|
130 |
!
|
|
131 |
|
|
132 |
genUUID
|
|
133 |
^ self new genUUID
|
|
134 |
!
|
|
135 |
|
|
136 |
new
|
|
137 |
^ super new:16
|
|
138 |
!
|
|
139 |
|
|
140 |
new:size
|
|
141 |
self shouldNotImplement
|
|
142 |
! !
|
|
143 |
|
|
144 |
!UUID class methodsFor:'defaults'!
|
|
145 |
|
|
146 |
uuidVersion
|
|
147 |
|
|
148 |
^ 1
|
|
149 |
! !
|
|
150 |
|
|
151 |
!UUID methodsFor:'converting'!
|
|
152 |
|
|
153 |
asBytesMSB:msb
|
|
154 |
|bytes d1 d2 d3|
|
|
155 |
|
|
156 |
bytes := ByteArray new:16.
|
|
157 |
bytes replaceFrom:1 to:16 with:self.
|
|
158 |
|
|
159 |
msb ifTrue:[
|
|
160 |
d1 := self unsignedLongAt:1 bigEndian:false.
|
|
161 |
d2 := self unsignedShortAt:1+4 bigEndian:false.
|
|
162 |
d3 := self unsignedShortAt:1+4+2 bigEndian:false.
|
|
163 |
|
|
164 |
bytes unsignedLongAt:1 put:d1 bigEndian:true.
|
|
165 |
bytes unsignedShortAt:1+4 put:d2 bigEndian:true.
|
|
166 |
bytes unsignedShortAt:1+4+2 put:d3 bigEndian:true.
|
|
167 |
].
|
|
168 |
^ bytes
|
|
169 |
! !
|
|
170 |
|
|
171 |
!UUID methodsFor:'generating uuids'!
|
|
172 |
|
|
173 |
genUUID
|
|
174 |
|macBytes utcTime length byte uuidClockSeq|
|
|
175 |
|
|
176 |
NextSeqNr isNil ifTrue:[
|
|
177 |
NextSeqNr := 1. Socket new
|
|
178 |
].
|
|
179 |
macBytes := self getValidMACAdress.
|
|
180 |
macBytes isNil ifTrue:[ ^ nil].
|
|
181 |
|
|
182 |
self replaceFrom:16-6+1 to:16 with:macBytes startingAt:1.
|
|
183 |
|
|
184 |
"/ use 60 bit 100ns ticks since 00:00:00 15.oct 1582 (sigh)
|
|
185 |
utcTime := UTCTime now.
|
|
186 |
length := utcTime digitLength.
|
|
187 |
"/ time_low bytes 1-4
|
|
188 |
1 to: 4 do:[ : el |
|
|
189 |
byte := utcTime digitAt:(length - el + 1).
|
|
190 |
self at:el + 1 put:byte.
|
|
191 |
].
|
|
192 |
"/ time_mid bytes 5-6
|
|
193 |
5 to: 6 do:[ : el |
|
|
194 |
byte := utcTime digitAt:(length - el + 1).
|
|
195 |
self at:el put:byte.
|
|
196 |
].
|
|
197 |
"/ time_high bytes 6-7 multiplexed with version
|
|
198 |
7 to: 8 do:[ : el |
|
|
199 |
byte := utcTime digitAt:(length - el + 1).
|
|
200 |
(el == 6) ifTrue:[
|
|
201 |
byte := byte bitOr:(self class uuidVersion bitShift:4).
|
|
202 |
].
|
|
203 |
self at:el put:byte.
|
|
204 |
].
|
|
205 |
"/ clock_seq bytes 8-9
|
|
206 |
uuidClockSeq := UUIDRandom getUuidRandom.
|
|
207 |
9 to: 10 do:[ : el |
|
|
208 |
byte := uuidClockSeq digitAt:2 - el + 9.
|
|
209 |
self at:el put:byte.
|
|
210 |
].
|
|
211 |
|
|
212 |
"
|
|
213 |
self genUUID.
|
|
214 |
"
|
|
215 |
"
|
|
216 |
|
|
217 |
1 to: 100 do:[ : el |
|
|
218 |
Transcript show:el.
|
|
219 |
Transcript show:': '.
|
|
220 |
Transcript showCR:(UUID genUUID).
|
|
221 |
].
|
|
222 |
|
|
223 |
"
|
|
224 |
!
|
|
225 |
|
|
226 |
getValidMACAdress
|
|
227 |
|
|
228 |
| macBytes dictOfIf |
|
|
229 |
|
|
230 |
dictOfIf := OperatingSystem getNetworkMACAdresses.
|
|
231 |
|
|
232 |
dictOfIf keysAndValuesDo:[ : key : el |
|
|
233 |
((el value select:([:e | e ~= 0] )) notEmpty) ifTrue:[
|
|
234 |
macBytes := el value.
|
|
235 |
^ macBytes
|
|
236 |
].
|
|
237 |
].
|
|
238 |
^ nil
|
|
239 |
! !
|
|
240 |
|
|
241 |
!UUID methodsFor:'printing'!
|
|
242 |
|
|
243 |
displayString
|
|
244 |
^ self printString
|
|
245 |
!
|
|
246 |
|
|
247 |
printOn:aStream
|
|
248 |
|offs d byte|
|
|
249 |
|
|
250 |
offs := 1.
|
|
251 |
|
|
252 |
d := self unsignedLongAt:1 bigEndian:false.
|
|
253 |
offs := offs + 4.
|
|
254 |
aStream nextPutAll:((d printStringRadix:16) asLowercase leftPaddedTo:8 with:$0).
|
|
255 |
|
|
256 |
aStream nextPut:$-.
|
|
257 |
|
|
258 |
2 timesRepeat:[
|
|
259 |
d := self unsignedShortAt:offs bigEndian:false.
|
|
260 |
offs := offs + 2.
|
|
261 |
aStream nextPutAll:((d printStringRadix:16) asLowercase leftPaddedTo:4 with:$0).
|
|
262 |
aStream nextPut:$-.
|
|
263 |
].
|
|
264 |
|
|
265 |
d := self unsignedShortAt:offs bigEndian:true.
|
|
266 |
offs := offs + 2.
|
|
267 |
aStream nextPutAll:((d printStringRadix:16) asLowercase leftPaddedTo:4 with:$0).
|
|
268 |
aStream nextPut:$-.
|
|
269 |
|
|
270 |
6 timesRepeat:[
|
|
271 |
byte := self at:offs.
|
|
272 |
offs := offs + 1.
|
|
273 |
aStream nextPutAll:((byte printStringRadix:16) asLowercase leftPaddedTo:2 with:$0).
|
|
274 |
].
|
|
275 |
! !
|
|
276 |
|
|
277 |
!UUID::UTCTime class methodsFor:'constants'!
|
|
278 |
|
|
279 |
baseTimeDiff
|
|
280 |
|
|
281 |
"
|
|
282 |
return the constant designation difference in Unix and DTSS base times in 100 nsec intervals
|
|
283 |
DTSS UTC base time is October 15, 1582.
|
|
284 |
Unix base time is January 1, 1970.
|
|
285 |
"
|
|
286 |
^ 16r01B21DD213814000
|
|
287 |
! !
|
|
288 |
|
|
289 |
!UUID::UTCTime class methodsFor:'instance creation'!
|
|
290 |
|
|
291 |
now
|
|
292 |
|
|
293 |
| utcTime |
|
|
294 |
|
|
295 |
utcTime := super now.
|
|
296 |
^ utcTime getMilliseconds * 10000 + self baseTimeDiff.
|
|
297 |
! !
|
|
298 |
|
|
299 |
!UUID::UUIDRandom class methodsFor:'defaults'!
|
|
300 |
|
|
301 |
rand_ia_init
|
|
302 |
|
|
303 |
^ 11113
|
|
304 |
!
|
|
305 |
|
|
306 |
rand_ib_init
|
|
307 |
|
|
308 |
^ 104322
|
|
309 |
!
|
|
310 |
|
|
311 |
rand_irand_init
|
|
312 |
|
|
313 |
^ 4181
|
|
314 |
!
|
|
315 |
|
|
316 |
rand_m_init
|
|
317 |
|
|
318 |
^ 971
|
|
319 |
! !
|
|
320 |
|
|
321 |
!UUID::UUIDRandom class methodsFor:'instance creation'!
|
|
322 |
|
|
323 |
getUuidRandom
|
|
324 |
|
|
325 |
| utcTime length seed retValue|
|
|
326 |
|
|
327 |
rand_m isNil ifTrue:[
|
|
328 |
rand_m := self rand_m_init.
|
|
329 |
].
|
|
330 |
rand_ia isNil ifTrue:[
|
|
331 |
rand_ia := self rand_ia_init.
|
|
332 |
].
|
|
333 |
rand_ib isNil ifTrue:[
|
|
334 |
rand_ib := self rand_ib_init.
|
|
335 |
].
|
|
336 |
rand_irand isNil ifTrue:[
|
|
337 |
rand_irand := self rand_irand_init.
|
|
338 |
utcTime := UUID::UTCTime now.
|
|
339 |
length := utcTime digitLength.
|
|
340 |
seed := 0.
|
|
341 |
1 to: length do:[ : el |
|
|
342 |
seed := seed bitXor:(utcTime digitAt:el).
|
|
343 |
].
|
|
344 |
rand_irand := rand_irand + seed + OperatingSystem getProcessId.
|
|
345 |
].
|
|
346 |
rand_m := rand_m + 7.
|
|
347 |
rand_ia := rand_ia + 1907.
|
|
348 |
rand_ib := rand_ib + 73939.
|
|
349 |
|
|
350 |
(rand_m >= 9973) ifTrue:[ rand_m := rand_m - 9871 ].
|
|
351 |
(rand_ia >= 99991) ifTrue:[ rand_ia := rand_ia - 89989 ].
|
|
352 |
(rand_ib >= 224729) ifTrue:[ rand_ib := rand_ib - 96233 ].
|
|
353 |
|
|
354 |
rand_irand := (rand_irand * rand_m) + rand_ia + rand_ib.
|
|
355 |
retValue := ((rand_irand bitShift:-16) bitXor:(rand_irand bitAnd:16r3fff)).
|
|
356 |
^ retValue
|
|
357 |
|
|
358 |
|
|
359 |
"/ self getUuidRandom
|
|
360 |
! !
|
|
361 |
|
|
362 |
!UUID class methodsFor:'documentation'!
|
|
363 |
|
|
364 |
version
|
|
365 |
^ '$Header: /cvs/stx/stx/libbasic2/UUID.st,v 1.1 2003-04-28 12:09:08 cg Exp $'
|
|
366 |
! !
|