elastic-mapreduce 0.0.3 → 2011.11.23

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Copyright 2008-2011 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4
+
5
+ require 'commands'
6
+ require 'simple_logger'
7
+ require 'simple_executor'
8
+
9
+ exit_code = 0
10
+ begin
11
+ logger = SimpleLogger.new
12
+ executor = SimpleExecutor.new
13
+ commands = Commands::create_and_execute_commands(
14
+ ARGV, Amazon::Coral::ElasticMapReduceClient, logger, executor
15
+ )
16
+ rescue SystemExit => e
17
+ exit_code = -1
18
+ rescue Exception => e
19
+ STDERR.puts("Error: " + e.message)
20
+ STDERR.puts(e.backtrace.join("\n"))
21
+ exit_code = -1
22
+ end
23
+
24
+ exit(exit_code)
@@ -0,0 +1,8 @@
1
+ #
2
+ # Copyright 2008-2010 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
+
4
+ $LOAD_PATH << File.dirname(__FILE__)
5
+
6
+ require 'tests/commands_test'
7
+
8
+
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2005-2008 Robert Aman
2
+ # UUIDTools, Copyright (c) 2005-2008 Bob Aman
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
5
5
  # a copy of this software and associated documentation files (the
@@ -31,625 +31,644 @@ require 'digest/md5'
31
31
 
32
32
  require 'uuidtools/version'
33
33
 
34
- # Backwards compatibility with old method of versioning.
35
- UUID_TOOLS_VERSION = UUID::UUID_TOOLS_VERSION::STRING
34
+ begin
35
+ require 'securerandom'
36
+ rescue LoadError
37
+ require File.join(File.dirname(__FILE__), 'compat', 'securerandom')
38
+ end
36
39
 
37
- # Because it's impossible to hype a UUID generator on its genuine merits,
38
- # I give you... Really bad ASCII art in the comments:
39
- #
40
- #
41
- # \
42
- # /
43
- # +
44
- # ]
45
- # ]
46
- # |
47
- # /
48
- # Mp___
49
- # `~0NNp,
50
- # __ggM'
51
- # g0M~"`
52
- # ]0M*-
53
- #
54
- # ___
55
- # _g000M00g,
56
- # j0M~ ~M&
57
- # j0M" ~N,
58
- # j0P M&
59
- # jM 1
60
- # j0 ]1
61
- # .0P 0,
62
- # 00' M&
63
- # 0M ]0L
64
- # ]0f ___ M0
65
- # M0NN0M00MMM~"'M 0&
66
- # `~ ~0 ]0,
67
- # ]M ]0&
68
- # M& M0,
69
- # ____gp_ M& M0_
70
- # __p0MPM8MM&_ M/ ^0&_
71
- # gN"` M0N_j0, MM&__
72
- # _gF `~M0P` __ M00g
73
- # g0' gM0&, ~M0&
74
- # _pM` 0, ]M1 "00&
75
- # _00 /g1MMgj01 ]0MI
76
- # _0F t"M,7MMM 00I
77
- # g0' _ N&j& 40'
78
- # g0' _p0Mq_ ' N0QQNM#g,
79
- # 0' _g0000000g__ ~M@MMM000g
80
- # f _jM00@` ~M0000Mgppg, "P00&
81
- # | g000~ `~M000000&_ ~0&
82
- # ]M _M00F "00MM` ~#&
83
- # `0L m000F #E "0f
84
- # 9r j000M` 40, 00
85
- # ]0g_ j00M` ^M0MNggp#gqpg M0&
86
- # ~MPM0f ~M000000000g_ ,_ygg&M00f
87
- # `~~~M00000000000000
88
- # `M0000000000f
89
- # ~@@@MF~`
90
- #
91
- #
92
-
93
- #= uuidtools.rb
94
- #
95
- # UUIDTools was designed to be a simple library for generating any
96
- # of the various types of UUIDs. It conforms to RFC 4122 whenever
97
- # possible.
98
- #
99
- #== Example
100
- # UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com")
101
- # => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
102
- # UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com")
103
- # => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
104
- # UUID.timestamp_create
105
- # => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
106
- # UUID.random_create
107
- # => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>
108
- class UUID
109
- @@mac_address = nil
110
- @@last_timestamp = nil
111
- @@last_node_id = nil
112
- @@last_clock_sequence = nil
113
- @@state_file = nil
114
- @@mutex = Mutex.new
115
-
116
- def initialize(time_low, time_mid, time_hi_and_version,
117
- clock_seq_hi_and_reserved, clock_seq_low, nodes)
118
- unless time_low >= 0 && time_low < 4294967296
119
- raise ArgumentError,
120
- "Expected unsigned 32-bit number for time_low, got #{time_low}."
121
- end
122
- unless time_mid >= 0 && time_mid < 65536
123
- raise ArgumentError,
124
- "Expected unsigned 16-bit number for time_mid, got #{time_mid}."
125
- end
126
- unless time_hi_and_version >= 0 && time_hi_and_version < 65536
127
- raise ArgumentError,
128
- "Expected unsigned 16-bit number for time_hi_and_version, " +
129
- "got #{time_hi_and_version}."
130
- end
131
- unless clock_seq_hi_and_reserved >= 0 && clock_seq_hi_and_reserved < 256
132
- raise ArgumentError,
133
- "Expected unsigned 8-bit number for clock_seq_hi_and_reserved, " +
134
- "got #{clock_seq_hi_and_reserved}."
135
- end
136
- unless clock_seq_low >= 0 && clock_seq_low < 256
137
- raise ArgumentError,
138
- "Expected unsigned 8-bit number for clock_seq_low, " +
139
- "got #{clock_seq_low}."
140
- end
141
- unless nodes.respond_to? :size
142
- raise ArgumentError,
143
- "Expected nodes to respond to :size."
144
- end
145
- unless nodes.size == 6
146
- raise ArgumentError,
147
- "Expected nodes to have size of 6."
148
- end
149
- for node in nodes
150
- unless node >= 0 && node < 256
40
+ module UUIDTools
41
+ #= uuidtools.rb
42
+ #
43
+ # UUIDTools was designed to be a simple library for generating any
44
+ # of the various types of UUIDs. It conforms to RFC 4122 whenever
45
+ # possible.
46
+ #
47
+ #== Example
48
+ # UUID.md5_create(UUID_DNS_NAMESPACE, "www.widgets.com")
49
+ # => #<UUID:0x287576 UUID:3d813cbb-47fb-32ba-91df-831e1593ac29>
50
+ # UUID.sha1_create(UUID_DNS_NAMESPACE, "www.widgets.com")
51
+ # => #<UUID:0x2a0116 UUID:21f7f8de-8051-5b89-8680-0195ef798b6a>
52
+ # UUID.timestamp_create
53
+ # => #<UUID:0x2adfdc UUID:64a5189c-25b3-11da-a97b-00c04fd430c8>
54
+ # UUID.random_create
55
+ # => #<UUID:0x19013a UUID:984265dc-4200-4f02-ae70-fe4f48964159>
56
+ class UUID
57
+ include Comparable
58
+
59
+ @@last_timestamp = nil
60
+ @@last_node_id = nil
61
+ @@last_clock_sequence = nil
62
+ @@state_file = nil
63
+ @@mutex = Mutex.new
64
+
65
+ def initialize(time_low, time_mid, time_hi_and_version,
66
+ clock_seq_hi_and_reserved, clock_seq_low, nodes)
67
+ unless time_low >= 0 && time_low < 4294967296
68
+ raise ArgumentError,
69
+ "Expected unsigned 32-bit number for time_low, got #{time_low}."
70
+ end
71
+ unless time_mid >= 0 && time_mid < 65536
72
+ raise ArgumentError,
73
+ "Expected unsigned 16-bit number for time_mid, got #{time_mid}."
74
+ end
75
+ unless time_hi_and_version >= 0 && time_hi_and_version < 65536
76
+ raise ArgumentError,
77
+ "Expected unsigned 16-bit number for time_hi_and_version, " +
78
+ "got #{time_hi_and_version}."
79
+ end
80
+ unless clock_seq_hi_and_reserved >= 0 && clock_seq_hi_and_reserved < 256
81
+ raise ArgumentError,
82
+ "Expected unsigned 8-bit number for clock_seq_hi_and_reserved, " +
83
+ "got #{clock_seq_hi_and_reserved}."
84
+ end
85
+ unless clock_seq_low >= 0 && clock_seq_low < 256
86
+ raise ArgumentError,
87
+ "Expected unsigned 8-bit number for clock_seq_low, " +
88
+ "got #{clock_seq_low}."
89
+ end
90
+ unless nodes.kind_of?(Enumerable)
91
+ raise TypeError,
92
+ "Expected Enumerable, got #{nodes.class.name}."
93
+ end
94
+ unless nodes.size == 6
151
95
  raise ArgumentError,
152
- "Expected unsigned 8-bit number for each node, " +
153
- "got #{node}."
96
+ "Expected nodes to have size of 6."
154
97
  end
98
+ for node in nodes
99
+ unless node >= 0 && node < 256
100
+ raise ArgumentError,
101
+ "Expected unsigned 8-bit number for each node, " +
102
+ "got #{node}."
103
+ end
104
+ end
105
+ @time_low = time_low
106
+ @time_mid = time_mid
107
+ @time_hi_and_version = time_hi_and_version
108
+ @clock_seq_hi_and_reserved = clock_seq_hi_and_reserved
109
+ @clock_seq_low = clock_seq_low
110
+ @nodes = nodes
155
111
  end
156
- @time_low = time_low
157
- @time_mid = time_mid
158
- @time_hi_and_version = time_hi_and_version
159
- @clock_seq_hi_and_reserved = clock_seq_hi_and_reserved
160
- @clock_seq_low = clock_seq_low
161
- @nodes = nodes
162
- end
163
-
164
- attr_accessor :time_low
165
- attr_accessor :time_mid
166
- attr_accessor :time_hi_and_version
167
- attr_accessor :clock_seq_hi_and_reserved
168
- attr_accessor :clock_seq_low
169
- attr_accessor :nodes
170
-
171
- # Parses a UUID from a string.
172
- def self.parse(uuid_string)
173
- unless uuid_string.kind_of? String
174
- raise ArgumentError,
175
- "Expected String, got #{uuid_string.class.name} instead."
176
- end
177
- uuid_components = uuid_string.downcase.scan(
178
- Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" +
179
- "([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")).first
180
- raise ArgumentError, "Invalid UUID format." if uuid_components.nil?
181
- time_low = uuid_components[0].to_i(16)
182
- time_mid = uuid_components[1].to_i(16)
183
- time_hi_and_version = uuid_components[2].to_i(16)
184
- clock_seq_hi_and_reserved = uuid_components[3].to_i(16)
185
- clock_seq_low = uuid_components[4].to_i(16)
186
- nodes = []
187
- for i in 0..5
188
- nodes << uuid_components[5][(i * 2)..(i * 2) + 1].to_i(16)
189
- end
190
- return self.new(time_low, time_mid, time_hi_and_version,
191
- clock_seq_hi_and_reserved, clock_seq_low, nodes)
192
- end
193
-
194
- # Parses a UUID from a raw byte string.
195
- def self.parse_raw(raw_string)
196
- unless raw_string.kind_of? String
197
- raise ArgumentError,
198
- "Expected String, got #{raw_string.class.name} instead."
199
- end
200
- integer = self.convert_byte_string_to_int(raw_string)
201
-
202
- time_low = (integer >> 96) & 0xFFFFFFFF
203
- time_mid = (integer >> 80) & 0xFFFF
204
- time_hi_and_version = (integer >> 64) & 0xFFFF
205
- clock_seq_hi_and_reserved = (integer >> 56) & 0xFF
206
- clock_seq_low = (integer >> 48) & 0xFF
207
- nodes = []
208
- for i in 0..5
209
- nodes << ((integer >> (40 - (i * 8))) & 0xFF)
210
- end
211
- return self.new(time_low, time_mid, time_hi_and_version,
212
- clock_seq_hi_and_reserved, clock_seq_low, nodes)
213
- end
214
112
 
215
- # Creates a UUID from a random value.
216
- def self.random_create()
217
- new_uuid = self.parse_raw(self.random_128)
218
- new_uuid.time_hi_and_version &= 0x0FFF
219
- new_uuid.time_hi_and_version |= (4 << 12)
220
- new_uuid.clock_seq_hi_and_reserved &= 0x3F
221
- new_uuid.clock_seq_hi_and_reserved |= 0x80
222
- return new_uuid
223
- end
113
+ attr_accessor :time_low
114
+ attr_accessor :time_mid
115
+ attr_accessor :time_hi_and_version
116
+ attr_accessor :clock_seq_hi_and_reserved
117
+ attr_accessor :clock_seq_low
118
+ attr_accessor :nodes
224
119
 
225
- # Creates a UUID from a timestamp.
226
- def self.timestamp_create(timestamp=nil)
227
- # We need a lock here to prevent two threads from ever
228
- # getting the same timestamp.
229
- @@mutex.synchronize do
230
- # Always use GMT to generate UUIDs.
231
- if timestamp.nil?
232
- gmt_timestamp = Time.now.gmtime
233
- else
234
- gmt_timestamp = timestamp.gmtime
235
- end
236
- # Convert to 100 nanosecond blocks
237
- gmt_timestamp_100_nanoseconds = (gmt_timestamp.tv_sec * 10000000) +
238
- (gmt_timestamp.tv_usec * 10) + 0x01B21DD213814000
239
- mac_address = self.get_mac_address
240
- if mac_address == nil || mac_address == ""
241
- raise StandardError,
242
- "MAC address could not be autodetected. " +
243
- "Set the MAC address manually."
120
+ # Parses a UUID from a string.
121
+ def self.parse(uuid_string)
122
+ unless uuid_string.kind_of? String
123
+ raise TypeError,
124
+ "Expected String, got #{uuid_string.class.name} instead."
244
125
  end
245
- nodes = mac_address.split(":").collect do |octet|
246
- octet.to_i(16)
247
- end
248
- node_id = 0
126
+ uuid_components = uuid_string.downcase.scan(
127
+ Regexp.new("^([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-" +
128
+ "([0-9a-f]{2})([0-9a-f]{2})-([0-9a-f]{12})$")).first
129
+ raise ArgumentError, "Invalid UUID format." if uuid_components.nil?
130
+ time_low = uuid_components[0].to_i(16)
131
+ time_mid = uuid_components[1].to_i(16)
132
+ time_hi_and_version = uuid_components[2].to_i(16)
133
+ clock_seq_hi_and_reserved = uuid_components[3].to_i(16)
134
+ clock_seq_low = uuid_components[4].to_i(16)
135
+ nodes = []
249
136
  for i in 0..5
250
- node_id += (nodes[i] << (40 - (i * 8)))
137
+ nodes << uuid_components[5][(i * 2)..(i * 2) + 1].to_i(16)
251
138
  end
252
- clock_sequence = @@last_clock_sequence
253
- if clock_sequence.nil?
254
- clock_sequence = self.convert_byte_string_to_int(self.random_128)
139
+ return self.new(time_low, time_mid, time_hi_and_version,
140
+ clock_seq_hi_and_reserved, clock_seq_low, nodes)
141
+ end
142
+
143
+ # Parses a UUID from a raw byte string.
144
+ def self.parse_raw(raw_string)
145
+ unless raw_string.kind_of? String
146
+ raise TypeError,
147
+ "Expected String, got #{raw_string.class.name} instead."
255
148
  end
256
- if @@last_node_id != nil && @@last_node_id != node_id
257
- # The node id has changed. Change the clock id.
258
- clock_sequence = self.convert_byte_string_to_int(self.random_128)
259
- elsif @@last_timestamp != nil &&
260
- gmt_timestamp_100_nanoseconds <= @@last_timestamp
261
- clock_sequence = clock_sequence + 1
149
+ integer = self.convert_byte_string_to_int(raw_string)
150
+
151
+ time_low = (integer >> 96) & 0xFFFFFFFF
152
+ time_mid = (integer >> 80) & 0xFFFF
153
+ time_hi_and_version = (integer >> 64) & 0xFFFF
154
+ clock_seq_hi_and_reserved = (integer >> 56) & 0xFF
155
+ clock_seq_low = (integer >> 48) & 0xFF
156
+ nodes = []
157
+ for i in 0..5
158
+ nodes << ((integer >> (40 - (i * 8))) & 0xFF)
262
159
  end
263
- @@last_timestamp = gmt_timestamp_100_nanoseconds
264
- @@last_node_id = node_id
265
- @@last_clock_sequence = clock_sequence
266
-
267
- time_low = gmt_timestamp_100_nanoseconds & 0xFFFFFFFF
268
- time_mid = ((gmt_timestamp_100_nanoseconds >> 32) & 0xFFFF)
269
- time_hi_and_version = ((gmt_timestamp_100_nanoseconds >> 48) & 0x0FFF)
270
- time_hi_and_version |= (1 << 12)
271
- clock_seq_low = clock_sequence & 0xFF;
272
- clock_seq_hi_and_reserved = (clock_sequence & 0x3F00) >> 8
273
- clock_seq_hi_and_reserved |= 0x80
274
-
275
160
  return self.new(time_low, time_mid, time_hi_and_version,
276
161
  clock_seq_hi_and_reserved, clock_seq_low, nodes)
277
162
  end
278
- end
279
163
 
280
- # Creates a UUID using the MD5 hash. (Version 3)
281
- def self.md5_create(namespace, name)
282
- return self.create_from_hash(Digest::MD5, namespace, name)
283
- end
284
-
285
- # Creates a UUID using the SHA1 hash. (Version 5)
286
- def self.sha1_create(namespace, name)
287
- return self.create_from_hash(Digest::SHA1, namespace, name)
288
- end
289
-
290
- # This method applies only to version 1 UUIDs.
291
- # Checks if the node ID was generated from a random number
292
- # or from an IEEE 802 address (MAC address).
293
- # Always returns false for UUIDs that aren't version 1.
294
- # This should not be confused with version 4 UUIDs where
295
- # more than just the node id is random.
296
- def random_node_id?
297
- return false if self.version != 1
298
- return ((self.nodes.first & 0x01) == 1)
299
- end
300
-
301
- # Returns true if this UUID is the
302
- # nil UUID (00000000-0000-0000-0000-000000000000).
303
- def nil_uuid?
304
- return false if self.time_low != 0
305
- return false if self.time_mid != 0
306
- return false if self.time_hi_and_version != 0
307
- return false if self.clock_seq_hi_and_reserved != 0
308
- return false if self.clock_seq_low != 0
309
- self.nodes.each do |node|
310
- return false if node != 0
311
- end
312
- return true
313
- end
314
-
315
- # Returns the UUID version type.
316
- # Possible values:
317
- # 1 - Time-based with unique or random host identifier
318
- # 2 - DCE Security version (with POSIX UIDs)
319
- # 3 - Name-based (MD5 hash)
320
- # 4 - Random
321
- # 5 - Name-based (SHA-1 hash)
322
- def version
323
- return (time_hi_and_version >> 12)
324
- end
164
+ # Parses a UUID from an Integer.
165
+ def self.parse_int(uuid_int)
166
+ unless uuid_int.kind_of?(Integer)
167
+ raise ArgumentError,
168
+ "Expected Integer, got #{uuid_int.class.name} instead."
169
+ end
170
+ return self.parse_raw(self.convert_int_to_byte_string(uuid_int, 16))
171
+ end
325
172
 
326
- # Returns the UUID variant.
327
- # Possible values:
328
- # 0b000 - Reserved, NCS backward compatibility.
329
- # 0b100 - The variant specified in this document.
330
- # 0b110 - Reserved, Microsoft Corporation backward compatibility.
331
- # 0b111 - Reserved for future definition.
332
- def variant
333
- variant_raw = (clock_seq_hi_and_reserved >> 5)
334
- result = nil
335
- if (variant_raw >> 2) == 0
336
- result = 0x000
337
- elsif (variant_raw >> 1) == 2
338
- result = 0x100
339
- else
340
- result = variant_raw
341
- end
342
- return (result >> 6)
343
- end
344
-
345
- # Returns true if this UUID is valid.
346
- def valid?
347
- if [0b000, 0b100, 0b110, 0b111].include?(self.variant) &&
348
- (1..5).include?(self.version)
173
+ # Parse a UUID from a hexdigest String.
174
+ def self.parse_hexdigest(uuid_hexdigest)
175
+ unless uuid_hexdigest.kind_of?(String)
176
+ raise ArgumentError,
177
+ "Expected String, got #{uuid_hexdigest.class.name} instead."
178
+ end
179
+ return self.parse_int(uuid_hexdigest.to_i(16))
180
+ end
181
+
182
+ # Creates a UUID from a random value.
183
+ def self.random_create()
184
+ new_uuid = self.parse_raw(SecureRandom.random_bytes(16))
185
+ new_uuid.time_hi_and_version &= 0x0FFF
186
+ new_uuid.time_hi_and_version |= (4 << 12)
187
+ new_uuid.clock_seq_hi_and_reserved &= 0x3F
188
+ new_uuid.clock_seq_hi_and_reserved |= 0x80
189
+ return new_uuid
190
+ end
191
+
192
+ # Creates a UUID from a timestamp.
193
+ def self.timestamp_create(timestamp=nil)
194
+ # We need a lock here to prevent two threads from ever
195
+ # getting the same timestamp.
196
+ @@mutex.synchronize do
197
+ # Always use GMT to generate UUIDs.
198
+ if timestamp.nil?
199
+ gmt_timestamp = Time.now.gmtime
200
+ else
201
+ gmt_timestamp = timestamp.gmtime
202
+ end
203
+ # Convert to 100 nanosecond blocks
204
+ gmt_timestamp_100_nanoseconds = (gmt_timestamp.tv_sec * 10000000) +
205
+ (gmt_timestamp.tv_usec * 10) + 0x01B21DD213814000
206
+ mac_address = self.mac_address
207
+ node_id = 0
208
+ if mac_address != nil
209
+ nodes = mac_address.split(":").collect do |octet|
210
+ octet.to_i(16)
211
+ end
212
+ else
213
+ nodes = SecureRandom.random_bytes(6).unpack("C*")
214
+ nodes[0] |= 0b00000001
215
+ end
216
+ for i in 0..5
217
+ node_id += (nodes[i] << (40 - (i * 8)))
218
+ end
219
+ clock_sequence = @@last_clock_sequence
220
+ if clock_sequence.nil?
221
+ clock_sequence = self.convert_byte_string_to_int(
222
+ SecureRandom.random_bytes(16)
223
+ )
224
+ end
225
+ if @@last_node_id != nil && @@last_node_id != node_id
226
+ # The node id has changed. Change the clock id.
227
+ clock_sequence = self.convert_byte_string_to_int(
228
+ SecureRandom.random_bytes(16)
229
+ )
230
+ elsif @@last_timestamp != nil &&
231
+ gmt_timestamp_100_nanoseconds <= @@last_timestamp
232
+ clock_sequence = clock_sequence + 1
233
+ end
234
+ @@last_timestamp = gmt_timestamp_100_nanoseconds
235
+ @@last_node_id = node_id
236
+ @@last_clock_sequence = clock_sequence
237
+
238
+ time_low = gmt_timestamp_100_nanoseconds & 0xFFFFFFFF
239
+ time_mid = ((gmt_timestamp_100_nanoseconds >> 32) & 0xFFFF)
240
+ time_hi_and_version = ((gmt_timestamp_100_nanoseconds >> 48) & 0x0FFF)
241
+ time_hi_and_version |= (1 << 12)
242
+ clock_seq_low = clock_sequence & 0xFF;
243
+ clock_seq_hi_and_reserved = (clock_sequence & 0x3F00) >> 8
244
+ clock_seq_hi_and_reserved |= 0x80
245
+
246
+ return self.new(time_low, time_mid, time_hi_and_version,
247
+ clock_seq_hi_and_reserved, clock_seq_low, nodes)
248
+ end
249
+ end
250
+
251
+ # Creates a UUID using the MD5 hash. (Version 3)
252
+ def self.md5_create(namespace, name)
253
+ return self.create_from_hash(Digest::MD5, namespace, name)
254
+ end
255
+
256
+ # Creates a UUID using the SHA1 hash. (Version 5)
257
+ def self.sha1_create(namespace, name)
258
+ return self.create_from_hash(Digest::SHA1, namespace, name)
259
+ end
260
+
261
+ # This method applies only to version 1 UUIDs.
262
+ # Checks if the node ID was generated from a random number
263
+ # or from an IEEE 802 address (MAC address).
264
+ # Always returns false for UUIDs that aren't version 1.
265
+ # This should not be confused with version 4 UUIDs where
266
+ # more than just the node id is random.
267
+ def random_node_id?
268
+ return false if self.version != 1
269
+ return ((self.nodes.first & 0x01) == 1)
270
+ end
271
+
272
+ # Returns true if this UUID is the
273
+ # nil UUID (00000000-0000-0000-0000-000000000000).
274
+ def nil_uuid?
275
+ return false if self.time_low != 0
276
+ return false if self.time_mid != 0
277
+ return false if self.time_hi_and_version != 0
278
+ return false if self.clock_seq_hi_and_reserved != 0
279
+ return false if self.clock_seq_low != 0
280
+ self.nodes.each do |node|
281
+ return false if node != 0
282
+ end
349
283
  return true
350
- else
351
- return false
352
284
  end
353
- end
354
-
355
- # Returns the IEEE 802 address used to generate this UUID or
356
- # nil if a MAC address was not used.
357
- def mac_address
358
- return nil if self.version != 1
359
- return nil if self.random_node_id?
360
- return (self.nodes.collect do |node|
361
- sprintf("%2.2x", node)
362
- end).join(":")
363
- end
364
-
365
- # Returns the timestamp used to generate this UUID
366
- def timestamp
367
- return nil if self.version != 1
368
- gmt_timestamp_100_nanoseconds = 0
369
- gmt_timestamp_100_nanoseconds +=
370
- ((self.time_hi_and_version & 0x0FFF) << 48)
371
- gmt_timestamp_100_nanoseconds += (self.time_mid << 32)
372
- gmt_timestamp_100_nanoseconds += self.time_low
373
- return Time.at(
374
- (gmt_timestamp_100_nanoseconds - 0x01B21DD213814000) / 10000000.0)
375
- end
376
-
377
- # Compares two UUIDs lexically
378
- def <=>(other_uuid)
379
- check = self.time_low <=> other_uuid.time_low
380
- return check if check != 0
381
- check = self.time_mid <=> other_uuid.time_mid
382
- return check if check != 0
383
- check = self.time_hi_and_version <=> other_uuid.time_hi_and_version
384
- return check if check != 0
385
- check = self.clock_seq_hi_and_reserved <=>
386
- other_uuid.clock_seq_hi_and_reserved
387
- return check if check != 0
388
- check = self.clock_seq_low <=> other_uuid.clock_seq_low
389
- return check if check != 0
390
- for i in 0..5
391
- if (self.nodes[i] < other_uuid.nodes[i])
392
- return -1
285
+
286
+ # Returns the UUID version type.
287
+ # Possible values:
288
+ # 1 - Time-based with unique or random host identifier
289
+ # 2 - DCE Security version (with POSIX UIDs)
290
+ # 3 - Name-based (MD5 hash)
291
+ # 4 - Random
292
+ # 5 - Name-based (SHA-1 hash)
293
+ def version
294
+ return (time_hi_and_version >> 12)
295
+ end
296
+
297
+ # Returns the UUID variant.
298
+ # Possible values:
299
+ # 0b000 - Reserved, NCS backward compatibility.
300
+ # 0b100 - The variant specified in this document.
301
+ # 0b110 - Reserved, Microsoft Corporation backward compatibility.
302
+ # 0b111 - Reserved for future definition.
303
+ def variant
304
+ variant_raw = (clock_seq_hi_and_reserved >> 5)
305
+ result = nil
306
+ if (variant_raw >> 2) == 0
307
+ result = 0x000
308
+ elsif (variant_raw >> 1) == 2
309
+ result = 0x100
310
+ else
311
+ result = variant_raw
393
312
  end
394
- if (self.nodes[i] > other_uuid.nodes[i])
395
- return 1
313
+ return (result >> 6)
314
+ end
315
+
316
+ # Returns true if this UUID is valid.
317
+ def valid?
318
+ if [0b000, 0b100, 0b110, 0b111].include?(self.variant) &&
319
+ (1..5).include?(self.version)
320
+ return true
321
+ else
322
+ return false
396
323
  end
397
324
  end
398
- return 0
399
- end
400
-
401
- # Returns a representation of the object's state
402
- def inspect
403
- return "#<UUID:0x#{self.object_id.to_s(16)} UUID:#{self.to_s}>"
404
- end
405
-
406
- # Returns the hex digest of the UUID object.
407
- def hexdigest
408
- return self.to_i.to_s(16)
409
- end
410
-
411
- # Returns the raw bytes that represent this UUID.
412
- def raw
413
- return self.class.convert_int_to_byte_string(self.to_i, 16)
414
- end
415
-
416
- # Returns a string representation for this UUID.
417
- def to_s
418
- result = sprintf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", @time_low, @time_mid,
419
- @time_hi_and_version, @clock_seq_hi_and_reserved, @clock_seq_low);
420
- for i in 0..5
421
- result << sprintf("%2.2x", @nodes[i])
422
- end
423
- return result.downcase
424
- end
425
-
426
- # Returns an integer representation for this UUID.
427
- def to_i
428
- bytes = (time_low << 96) + (time_mid << 80) +
429
- (time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) +
430
- (clock_seq_low << 48)
431
- for i in 0..5
432
- bytes += (nodes[i] << (40 - (i * 8)))
433
- end
434
- return bytes
435
- end
436
325
 
437
- # Returns a URI string for this UUID.
438
- def to_uri
439
- return "urn:uuid:#{self.to_s}"
440
- end
326
+ # Returns the IEEE 802 address used to generate this UUID or
327
+ # nil if a MAC address was not used.
328
+ def mac_address
329
+ return nil if self.version != 1
330
+ return nil if self.random_node_id?
331
+ return (self.nodes.collect do |node|
332
+ sprintf("%2.2x", node)
333
+ end).join(":")
334
+ end
441
335
 
442
- # Returns an integer hash value.
443
- def hash
444
- return self.to_i
445
- end
336
+ # Returns the timestamp used to generate this UUID
337
+ def timestamp
338
+ return nil if self.version != 1
339
+ gmt_timestamp_100_nanoseconds = 0
340
+ gmt_timestamp_100_nanoseconds +=
341
+ ((self.time_hi_and_version & 0x0FFF) << 48)
342
+ gmt_timestamp_100_nanoseconds += (self.time_mid << 32)
343
+ gmt_timestamp_100_nanoseconds += self.time_low
344
+ return Time.at(
345
+ (gmt_timestamp_100_nanoseconds - 0x01B21DD213814000) / 10000000.0)
346
+ end
446
347
 
447
- # Returns true if this UUID is exactly equal to the other UUID.
448
- def eql?(other)
449
- return (self <=> other) == 0
450
- end
451
-
452
- def self.create_from_hash(hash_class, namespace, name) #:nodoc:
453
- if hash_class == Digest::MD5
454
- version = 3
455
- elsif hash_class == Digest::SHA1
456
- version = 5
457
- else
458
- raise ArgumentError,
459
- "Expected Digest::SHA1 or Digest::MD5, got #{hash_class.name}."
460
- end
461
- hash = hash_class.new
462
- hash.update(namespace.raw)
463
- hash.update(name)
464
- hash_string = hash.to_s[0..31]
465
- new_uuid = self.parse("#{hash_string[0..7]}-#{hash_string[8..11]}-" +
466
- "#{hash_string[12..15]}-#{hash_string[16..19]}-#{hash_string[20..31]}")
467
-
468
- new_uuid.time_hi_and_version &= 0x0FFF
469
- new_uuid.time_hi_and_version |= (version << 12)
470
- new_uuid.clock_seq_hi_and_reserved &= 0x3F
471
- new_uuid.clock_seq_hi_and_reserved |= 0x80
472
- return new_uuid
473
- end
348
+ # Compares two UUIDs lexically
349
+ def <=>(other_uuid)
350
+ check = self.time_low <=> other_uuid.time_low
351
+ return check if check != 0
352
+ check = self.time_mid <=> other_uuid.time_mid
353
+ return check if check != 0
354
+ check = self.time_hi_and_version <=> other_uuid.time_hi_and_version
355
+ return check if check != 0
356
+ check = self.clock_seq_hi_and_reserved <=>
357
+ other_uuid.clock_seq_hi_and_reserved
358
+ return check if check != 0
359
+ check = self.clock_seq_low <=> other_uuid.clock_seq_low
360
+ return check if check != 0
361
+ for i in 0..5
362
+ if (self.nodes[i] < other_uuid.nodes[i])
363
+ return -1
364
+ end
365
+ if (self.nodes[i] > other_uuid.nodes[i])
366
+ return 1
367
+ end
368
+ end
369
+ return 0
370
+ end
371
+
372
+ # Returns a representation of the object's state
373
+ def inspect
374
+ return "#<UUID:0x#{self.object_id.to_s(16)} UUID:#{self.to_s}>"
375
+ end
376
+
377
+ # Returns the hex digest of the UUID object.
378
+ def hexdigest
379
+ return @hexdigest unless @hexdigest.nil?
380
+ if self.frozen?
381
+ return generate_hexdigest
382
+ else
383
+ return (@hexdigest = generate_hexdigest)
384
+ end
385
+ end
386
+
387
+ # Returns the raw bytes that represent this UUID.
388
+ def raw
389
+ return @raw unless @raw.nil?
390
+ if self.frozen?
391
+ return generate_raw
392
+ else
393
+ return (@raw = generate_raw)
394
+ end
395
+ end
396
+
397
+ # Returns a string representation for this UUID.
398
+ def to_s
399
+ return @string unless @string.nil?
400
+ if self.frozen?
401
+ return generate_s
402
+ else
403
+ return (@string = generate_s)
404
+ end
405
+ end
406
+ alias_method :to_str, :to_s
407
+
408
+ # Returns an integer representation for this UUID.
409
+ def to_i
410
+ return @integer unless @integer.nil?
411
+ if self.frozen?
412
+ return generate_i
413
+ else
414
+ return (@integer = generate_i)
415
+ end
416
+ end
474
417
 
475
- # Returns the MAC address of the current computer's network card.
476
- # Returns nil if a MAC address could not be found.
477
- def self.mac_address #:nodoc:
478
- if @@mac_address.nil?
479
- require 'rbconfig'
480
- os_platform = Config::CONFIG['target_os']
481
- if os_platform =~ /win/ && !(os_platform =~ /darwin/)
482
- script_in_path = true
418
+ # Returns a URI string for this UUID.
419
+ def to_uri
420
+ return "urn:uuid:#{self.to_s}"
421
+ end
422
+
423
+ # Returns an integer hash value.
424
+ def hash
425
+ return @hash unless @hash.nil?
426
+ if self.frozen?
427
+ return generate_hash
483
428
  else
484
- script_in_path = !(`which ifconfig`.strip =~ /no .+ in/)
429
+ return (@hash = generate_hash)
485
430
  end
486
- if os_platform =~ /solaris/
487
- begin
488
- ifconfig_output =
489
- (script_in_path ? `ifconfig -a` : `/sbin/ifconfig -a`)
490
- ip_addresses = ifconfig_output.scan(
491
- /inet\s?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
492
- ip = ip_addresses.find {|addr| addr[0] != '127.0.0.1'}[0]
493
- @@mac_address = `/usr/sbin/arp #{ip}`.split(' ')[3]
494
- rescue Exception
431
+ end
432
+
433
+ #These methods generate the appropriate representations the above methods cache
434
+ protected
435
+
436
+ # Generates the hex digest of the UUID object.
437
+ def generate_hexdigest
438
+ return self.to_i.to_s(16).rjust(32, "0")
439
+ end
440
+
441
+ # Generates an integer hash value.
442
+ def generate_hash
443
+ return self.to_i % 0x3fffffff
444
+ end
445
+
446
+ # Generates an integer representation for this UUID.
447
+ def generate_i
448
+ return (begin
449
+ bytes = (time_low << 96) + (time_mid << 80) +
450
+ (time_hi_and_version << 64) + (clock_seq_hi_and_reserved << 56) +
451
+ (clock_seq_low << 48)
452
+ for i in 0..5
453
+ bytes += (nodes[i] << (40 - (i * 8)))
495
454
  end
496
- if @@mac_address == "" || @@mac_address == nil
497
- begin
498
- ifconfig_output =
499
- (script_in_path ?
500
- `ifconfig -a` : `/sbin/ifconfig -a`).split(' ')
501
- index = ifconfig_output.index("inet") + 1
502
- ip = ifconfig_output[index]
503
- @@mac_address = `arp #{ip}`.split(' ')[3]
504
- rescue Exception
505
- end
455
+ bytes
456
+ end)
457
+ end
458
+
459
+ # Generates a string representation for this UUID.
460
+ def generate_s
461
+ result = sprintf("%8.8x-%4.4x-%4.4x-%2.2x%2.2x-", @time_low, @time_mid,
462
+ @time_hi_and_version, @clock_seq_hi_and_reserved, @clock_seq_low);
463
+ for i in 0..5
464
+ result << sprintf("%2.2x", @nodes[i])
465
+ end
466
+ return result.downcase
467
+ end
468
+
469
+ # Generates the raw bytes that represent this UUID.
470
+ def generate_raw
471
+ return self.class.convert_int_to_byte_string(self.to_i, 16)
472
+ end
473
+
474
+ public
475
+
476
+ # Returns true if this UUID is exactly equal to the other UUID.
477
+ def eql?(other)
478
+ return self == other
479
+ end
480
+
481
+ # Returns the MAC address of the current computer's network card.
482
+ # Returns nil if a MAC address could not be found.
483
+ def self.mac_address #:nodoc:
484
+ if !defined?(@@mac_address)
485
+ require 'rbconfig'
486
+ os_platform = Config::CONFIG['target_os']
487
+ os_class = nil
488
+ if (os_platform =~ /win/i && !(os_platform =~ /darwin/i)) ||
489
+ os_platform =~ /w32/i
490
+ os_class = :windows
491
+ elsif os_platform =~ /solaris/i
492
+ os_class = :solaris
493
+ elsif os_platform =~ /netbsd/i
494
+ os_class = :netbsd
495
+ elsif os_platform =~ /openbsd/i
496
+ os_class = :openbsd
506
497
  end
507
- elsif os_platform =~ /win/ && !(os_platform =~ /darwin/)
508
- begin
509
- ifconfig_output = `ipconfig /all`
510
- mac_addresses = ifconfig_output.scan(
511
- Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join("-")})"))
512
- if mac_addresses.size > 0
513
- @@mac_address = mac_addresses.first.first.downcase.gsub(/-/, ":")
514
- end
515
- rescue
498
+ mac_regexps = [
499
+ Regexp.new("address:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
500
+ Regexp.new("addr:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
501
+ Regexp.new("ether:? (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
502
+ Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"),
503
+ Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join("-")})")
504
+ ]
505
+ parse_mac = lambda do |output|
506
+ (mac_regexps.map do |regexp|
507
+ result = output[regexp, 1]
508
+ result.downcase.gsub(/-/, ":") if result != nil
509
+ end).compact.first
516
510
  end
517
- else
518
- begin
519
- mac_addresses = []
520
- if os_platform =~ /netbsd/
521
- ifconfig_output =
522
- (script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
523
- mac_addresses = ifconfig_output.scan(
524
- Regexp.new("address\: (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
525
- elsif os_platform =~ /openbsd/
526
- ifconfig_output = `/sbin/ifconfig -a 2>&1`
527
- ifconfig_output =
528
- (script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
529
- mac_addresses = ifconfig_output.scan(
530
- Regexp.new("addr (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
531
- elsif File.exists?('/sbin/ifconfig')
532
- ifconfig_output =
533
- (script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`)
534
- mac_addresses = ifconfig_output.scan(
535
- Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
536
- if mac_addresses.size == 0
537
- ifconfig_output =
538
- (script_in_path ?
539
- `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
540
- mac_addresses = ifconfig_output.scan(
541
- Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
542
- end
543
- if mac_addresses.size == 0
544
- ifconfig_output =
545
- (script_in_path ?
546
- `ifconfig | grep HWaddr | cut -c39- 2>&1` :
547
- `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`)
548
- mac_addresses = ifconfig_output.scan(
549
- Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
550
- end
551
- else
511
+ if os_class == :windows
512
+ script_in_path = true
513
+ else
514
+ script_in_path = Kernel.system("which ifconfig 2>&1 > /dev/null")
515
+ end
516
+ if os_class == :solaris
517
+ begin
552
518
  ifconfig_output =
553
- (script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`)
554
- mac_addresses = ifconfig_output.scan(
555
- Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
556
- if mac_addresses.size == 0
557
- ifconfig_output =
558
- (script_in_path ?
559
- `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`)
560
- mac_addresses = ifconfig_output.scan(
561
- Regexp.new("ether (#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
562
- end
563
- if mac_addresses.size == 0
519
+ (script_in_path ? `ifconfig -a` : `/sbin/ifconfig -a`)
520
+ ip_addresses = ifconfig_output.scan(
521
+ /inet\s?(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/)
522
+ ip = ip_addresses.find {|addr| addr[0] != '127.0.0.1'}[0]
523
+ @@mac_address = `/usr/sbin/arp #{ip}`.split(' ')[3]
524
+ rescue Exception
525
+ end
526
+ if @@mac_address == "" || @@mac_address == nil
527
+ begin
564
528
  ifconfig_output =
565
529
  (script_in_path ?
566
- `ifconfig | grep HWaddr | cut -c39- 2>&1` :
567
- `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`)
568
- mac_addresses = ifconfig_output.scan(
569
- Regexp.new("(#{(["[0-9a-fA-F]{2}"] * 6).join(":")})"))
530
+ `ifconfig -a` : `/sbin/ifconfig -a`).split(' ')
531
+ index = ifconfig_output.index("inet") + 1
532
+ ip = ifconfig_output[index]
533
+ @@mac_address = `arp #{ip}`.split(' ')[3]
534
+ rescue Exception
570
535
  end
571
536
  end
572
- if mac_addresses.size > 0
573
- @@mac_address = mac_addresses.first.first
537
+ elsif os_class == :windows
538
+ begin
539
+ @@mac_address = parse_mac.call(`ipconfig /all`)
540
+ rescue
574
541
  end
575
- rescue
576
- end
577
- end
578
- if @@mac_address != nil
579
- if @@mac_address.respond_to?(:to_str)
580
- @@mac_address = @@mac_address.to_str
581
542
  else
582
- @@mac_address = @@mac_address.to_s
543
+ begin
544
+ if os_class == :netbsd
545
+ @@mac_address = parse_mac.call(
546
+ script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
547
+ )
548
+ elsif os_class == :openbsd
549
+ @@mac_address = parse_mac.call(
550
+ script_in_path ? `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
551
+ )
552
+ elsif File.exists?('/sbin/ifconfig')
553
+ @@mac_address = parse_mac.call(
554
+ script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`
555
+ )
556
+ if @@mac_address == nil
557
+ @@mac_address = parse_mac.call(
558
+ script_in_path ?
559
+ `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
560
+ )
561
+ end
562
+ if @@mac_address == nil
563
+ @@mac_address = parse_mac.call(
564
+ script_in_path ?
565
+ `ifconfig | grep HWaddr | cut -c39- 2>&1` :
566
+ `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`
567
+ )
568
+ end
569
+ else
570
+ @@mac_address = parse_mac.call(
571
+ script_in_path ? `ifconfig 2>&1` : `/sbin/ifconfig 2>&1`
572
+ )
573
+ if @@mac_address == nil
574
+ @@mac_address = parse_mac.call(
575
+ script_in_path ?
576
+ `ifconfig -a 2>&1` : `/sbin/ifconfig -a 2>&1`
577
+ )
578
+ end
579
+ if @@mac_address == nil
580
+ @@mac_address = parse_mac.call(
581
+ script_in_path ?
582
+ `ifconfig | grep HWaddr | cut -c39- 2>&1` :
583
+ `/sbin/ifconfig | grep HWaddr | cut -c39- 2>&1`
584
+ )
585
+ end
586
+ end
587
+ rescue
588
+ end
589
+ end
590
+ if @@mac_address != nil
591
+ if @@mac_address.respond_to?(:to_str)
592
+ @@mac_address = @@mac_address.to_str
593
+ else
594
+ @@mac_address = @@mac_address.to_s
595
+ end
596
+ @@mac_address.downcase!
597
+ @@mac_address.strip!
583
598
  end
584
- @@mac_address.downcase!
585
- @@mac_address.strip!
586
- end
587
599
 
588
- # Verify that the MAC address is in the right format.
589
- # Nil it out if it isn't.
590
- unless @@mac_address.respond_to?(:scan) &&
591
- @@mac_address.scan(/#{(["[0-9a-f]{2}"] * 6).join(":")}/)
592
- @@mac_address = nil
593
- end
594
- end
595
- return @@mac_address
596
- end
597
- class <<self
598
- alias_method :get_mac_address, :mac_address
599
- end
600
-
601
- # Allows users to set the MAC address manually in cases where the MAC
602
- # address cannot be obtained programatically.
603
- def self.mac_address=(new_mac_address)
604
- @@mac_address = new_mac_address
605
- end
606
-
607
- # 128 bits of unpredictable data.
608
- def self.random_128 #:nodoc:
609
- if !defined?(@random_device) || @random_device == nil
610
- begin
611
- @random_device = nil
612
- if File.exists? "/dev/urandom"
613
- @random_device = File.open "/dev/urandom", "r"
614
- elsif File.exists? "/dev/random"
615
- @random_device = File.open "/dev/random", "r"
600
+ # Verify that the MAC address is in the right format.
601
+ # Nil it out if it isn't.
602
+ unless @@mac_address.respond_to?(:scan) &&
603
+ @@mac_address.scan(/#{(["[0-9a-f]{2}"] * 6).join(":")}/)
604
+ @@mac_address = nil
616
605
  end
617
- rescue Exception
618
606
  end
607
+ return @@mac_address
619
608
  end
620
- begin
621
- return @random_device.read(16) if @random_device != nil
622
- rescue Exception
623
- end
624
- return (1..8).to_a.map { rand(0x10000) }.pack("n8")
625
- end
626
-
627
- def self.convert_int_to_byte_string(integer, size) #:nodoc:
628
- byte_string = ""
629
- if byte_string.respond_to?(:force_encoding)
630
- byte_string.force_encoding(Encoding::ASCII_8BIT)
609
+
610
+ # Allows users to set the MAC address manually in cases where the MAC
611
+ # address cannot be obtained programatically.
612
+ def self.mac_address=(new_mac_address)
613
+ @@mac_address = new_mac_address
631
614
  end
632
- for i in 0..(size - 1)
633
- byte_string << ((integer >> (((size - 1) - i) * 8)) & 0xFF)
615
+
616
+ # The following methods are not part of the public API,
617
+ # and generally should not be called directly.
618
+
619
+ # Creates a new UUID from a SHA1 or MD5 hash
620
+ def self.create_from_hash(hash_class, namespace, name) #:nodoc:
621
+ if hash_class == Digest::MD5
622
+ version = 3
623
+ elsif hash_class == Digest::SHA1
624
+ version = 5
625
+ else
626
+ raise ArgumentError,
627
+ "Expected Digest::SHA1 or Digest::MD5, got #{hash_class.name}."
628
+ end
629
+ hash = hash_class.new
630
+ hash.update(namespace.raw)
631
+ hash.update(name)
632
+ hash_string = hash.to_s[0..31]
633
+ new_uuid = self.parse("#{hash_string[0..7]}-#{hash_string[8..11]}-" +
634
+ "#{hash_string[12..15]}-#{hash_string[16..19]}-#{hash_string[20..31]}")
635
+
636
+ new_uuid.time_hi_and_version &= 0x0FFF
637
+ new_uuid.time_hi_and_version |= (version << 12)
638
+ new_uuid.clock_seq_hi_and_reserved &= 0x3F
639
+ new_uuid.clock_seq_hi_and_reserved |= 0x80
640
+ return new_uuid
634
641
  end
635
- return byte_string
636
- end
637
642
 
638
- def self.convert_byte_string_to_int(byte_string) #:nodoc:
639
- if byte_string.respond_to?(:force_encoding)
640
- byte_string.force_encoding(Encoding::ASCII_8BIT)
643
+ def self.convert_int_to_byte_string(integer, size) #:nodoc:
644
+ byte_string = ""
645
+ if byte_string.respond_to?(:force_encoding)
646
+ byte_string.force_encoding(Encoding::ASCII_8BIT)
647
+ end
648
+ for i in 0..(size - 1)
649
+ byte_string << ((integer >> (((size - 1) - i) * 8)) & 0xFF)
650
+ end
651
+ return byte_string
641
652
  end
642
- integer = 0
643
- size = byte_string.size
644
- for i in 0..(size - 1)
645
- ordinal = (byte_string[i].respond_to?(:ord) ? byte_string[i].ord : byte_string[i])
646
- integer += (ordinal << (((size - 1) - i) * 8))
653
+
654
+ def self.convert_byte_string_to_int(byte_string) #:nodoc:
655
+ if byte_string.respond_to?(:force_encoding)
656
+ byte_string.force_encoding(Encoding::ASCII_8BIT)
657
+ end
658
+ integer = 0
659
+ size = byte_string.size
660
+ for i in 0..(size - 1)
661
+ ordinal = (byte_string[i].respond_to?(:ord) ?
662
+ byte_string[i].ord : byte_string[i])
663
+ integer += (ordinal << (((size - 1) - i) * 8))
664
+ end
665
+ return integer
647
666
  end
648
- return integer
649
667
  end
668
+
669
+ UUID_DNS_NAMESPACE = UUID.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
670
+ UUID_URL_NAMESPACE = UUID.parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
671
+ UUID_OID_NAMESPACE = UUID.parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
672
+ UUID_X500_NAMESPACE = UUID.parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")
650
673
  end
651
674
 
652
- UUID_DNS_NAMESPACE = UUID.parse("6ba7b810-9dad-11d1-80b4-00c04fd430c8")
653
- UUID_URL_NAMESPACE = UUID.parse("6ba7b811-9dad-11d1-80b4-00c04fd430c8")
654
- UUID_OID_NAMESPACE = UUID.parse("6ba7b812-9dad-11d1-80b4-00c04fd430c8")
655
- UUID_X500_NAMESPACE = UUID.parse("6ba7b814-9dad-11d1-80b4-00c04fd430c8")