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