Class: OsCtl::Image::IpAllocator
- Inherits:
-
Object
- Object
- OsCtl::Image::IpAllocator
- Defined in:
- lib/osctl/image/ip_allocator.rb
Overview
Allocate unique IP addresses from network
Instance Method Summary collapse
- #get ⇒ IPAddress::IPv4
-
#initialize(addr) ⇒ IpAllocator
constructor
A new instance of IpAllocator.
- #put(ip) ⇒ Object
Constructor Details
#initialize(addr) ⇒ IpAllocator
Returns a new instance of IpAllocator.
7 8 9 10 11 |
# File 'lib/osctl/image/ip_allocator.rb', line 7 def initialize(addr) @available = IPAddress.parse(addr).hosts @taken = {} @mutex = Mutex.new end |
Instance Method Details
#get ⇒ IPAddress::IPv4
14 15 16 17 18 19 20 |
# File 'lib/osctl/image/ip_allocator.rb', line 14 def get @mutex.synchronize do ip = @available.shift @taken[ip.u32] = ip ip end end |
#put(ip) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/osctl/image/ip_allocator.rb', line 23 def put(ip) @mutex.synchronize do unless @taken.has_key?(ip.u32) raise ArgumentError, "#{ip} was not allocated" end @taken.delete(ip.u32) @available << ip nil end end |