Class: OsVm::MacAddressGenerator
- Inherits:
-
Object
- Object
- OsVm::MacAddressGenerator
- Includes:
- Singleton
- Defined in:
- lib/osvm/mac_address_generator.rb
Constant Summary collapse
- PREFIX =
'52:54:00'.freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ MacAddressGenerator
constructor
A new instance of MacAddressGenerator.
-
#next_mac ⇒ String
Generate and register a unique MAC address.
-
#register_mac(mac) ⇒ String
Register an externally provided MAC address.
Constructor Details
#initialize ⇒ MacAddressGenerator
Returns a new instance of MacAddressGenerator.
10 11 12 13 |
# File 'lib/osvm/mac_address_generator.rb', line 10 def initialize @registry = Set.new @mutex = Mutex.new end |
Class Method Details
.next_mac ⇒ String
56 57 58 |
# File 'lib/osvm/mac_address_generator.rb', line 56 def next_mac instance.next_mac end |
.register_mac(mac) ⇒ String
62 63 64 |
# File 'lib/osvm/mac_address_generator.rb', line 62 def register_mac(mac) instance.register_mac(mac) end |
Instance Method Details
#next_mac ⇒ String
Generate and register a unique MAC address
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/osvm/mac_address_generator.rb', line 17 def next_mac synchronize do loop do mac = random_mac next if @registry.include?(mac) @registry << mac return mac end end end |
#register_mac(mac) ⇒ String
Register an externally provided MAC address
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/osvm/mac_address_generator.rb', line 32 def register_mac(mac) synchronize do if @registry.include?(mac) raise ArgumentError, "duplicate MAC address #{mac.inspect}" end @registry << mac mac end end |