Class: Hive::PortAllocator
- Inherits:
-
Object
- Object
- Hive::PortAllocator
- Defined in:
- lib/hive/port_allocator.rb
Defined Under Namespace
Classes: NoPortsAvailable
Instance Method Summary collapse
-
#allocate_port ⇒ Object
Allocate a single port in the range.
-
#allocate_port_range(n) ⇒ Object
Create a new Hive::PortAllocator instance with a number of ports from the range.
-
#initialize(config) ⇒ PortAllocator
constructor
Create a port allocator.
-
#ports ⇒ Object
Full list of all ports, either free or allocated.
-
#release_all_ports ⇒ Object
Release all ports.
-
#release_port(p) ⇒ Object
Relase a single port in the range.
-
#release_port_range(range) ⇒ Object
Release ports that were previously allocated to another Hive::PortAllocator.
Constructor Details
#initialize(config) ⇒ PortAllocator
Create a port allocator
For ports in the range 4000-5000
Hive::PortAllocator.new(minimum: 4000, maximum: 5000)
For ports 6000, 6050 and 7433
Hive::PortAllocator.new(ports: [6000, 6050, 7433])
14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/hive/port_allocator.rb', line 14 def initialize(config) @allocated_ports = [] if config.has_key?(:minimum) and config.has_key?(:maximum) and config[:minimum] > 0 and config[:minimum] <= config[:maximum] @free_ports = Array(config[:minimum]..config[:maximum]) elsif config.has_key?(:ports) and config[:ports].is_a? Array config[:ports].each do |p| raise ArgumentError if ! p.is_a? Integer or p <= 0 end @free_ports = config[:ports] else raise ArgumentError end end |
Instance Method Details
#allocate_port ⇒ Object
Allocate a single port in the range
29 30 31 32 33 34 35 36 |
# File 'lib/hive/port_allocator.rb', line 29 def allocate_port if p = @free_ports.pop @allocated_ports << p p else raise NoPortsAvailable end end |
#allocate_port_range(n) ⇒ Object
Create a new Hive::PortAllocator instance with a number of ports from the range
45 46 47 48 49 50 51 52 53 54 |
# File 'lib/hive/port_allocator.rb', line 45 def allocate_port_range(n) if n <= @free_ports.length ps = @free_ports.take(n) @free_ports = @free_ports.drop(n) @allocated_ports.concat(ps) PortAllocator.new(ports: ps) else raise NoPortsAvailable end end |
#ports ⇒ Object
Full list of all ports, either free or allocated
75 76 77 |
# File 'lib/hive/port_allocator.rb', line 75 def ports [@free_ports, @allocated_ports].flatten end |
#release_all_ports ⇒ Object
Release all ports
69 70 71 72 |
# File 'lib/hive/port_allocator.rb', line 69 def release_all_ports @free_ports.concat(@allocated_ports) @allocated_ports = [] end |
#release_port(p) ⇒ Object
Relase a single port in the range
39 40 41 |
# File 'lib/hive/port_allocator.rb', line 39 def release_port(p) @free_ports << p if @allocated_ports.delete(p) end |
#release_port_range(range) ⇒ Object
Release ports that were previously allocated to another Hive::PortAllocator
Note, this will fail silently if ‘range’ contains ports that are not allocated in the current instance
61 62 63 64 65 66 |
# File 'lib/hive/port_allocator.rb', line 61 def release_port_range(range) if range.ports - @allocated_ports == [] @free_ports.concat(range.ports) @allocated_ports = @allocated_ports - range.ports end end |