Class: OsVm::MacAddressGenerator

Inherits:
Object
  • Object
show all
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

Constructor Details

#initializeMacAddressGenerator

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_macString

Returns:

  • (String)


56
57
58
# File 'lib/osvm/mac_address_generator.rb', line 56

def next_mac
  instance.next_mac
end

.register_mac(mac) ⇒ String

Parameters:

  • mac (String)

Returns:

  • (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_macString

Generate and register a unique MAC address

Returns:

  • (String)


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

Parameters:

  • mac (String)

Returns:

  • (String)


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