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