Class: OsCtld::Routing::Table::Version

Inherits:
Object
  • Object
show all
Includes:
Lockable
Defined in:
lib/osctld/routing/table.rb

Overview

Routing table for specific IP version

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#exclusively, included, #inclusively, #init_lock, #lock, #unlock

Constructor Details

#initialize(addrs: nil) ⇒ Version

Returns a new instance of Version



15
16
17
18
# File 'lib/osctld/routing/table.rb', line 15

def initialize(addrs: nil)
  @routes = addrs || []
  init_lock
end

Instance Attribute Details

#routesObject (readonly, protected)

Returns the value of attribute routes



96
97
98
# File 'lib/osctld/routing/table.rb', line 96

def routes
  @routes
end

Class Method Details

.load(cfg) ⇒ Object

Parameters:

  • cfg (Array<String>)


11
12
13
# File 'lib/osctld/routing/table.rb', line 11

def self.load(cfg)
  new(addrs: cfg.map { |route| Routing::Route.load(route) })
end

Instance Method Details

#<<(addr) ⇒ Object

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)


21
22
23
24
# File 'lib/osctld/routing/table.rb', line 21

def <<(addr)
  add(addr)
  self
end

#add(addr, via: nil) ⇒ Routing::Route

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)
  • via (IPAddress::IPv4, IPAddress::IPv6)

Returns:



29
30
31
32
33
34
35
# File 'lib/osctld/routing/table.rb', line 29

def add(addr, via: nil)
  exclusively do
    r = Routing::Route.new(addr, via: via)
    routes << r
    r
  end
end

#any?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/osctld/routing/table.rb', line 71

def any?
  !empty?
end

#clearObject



75
76
77
# File 'lib/osctld/routing/table.rb', line 75

def clear
  exclusively { routes.clear }
end

#contains?(addr) ⇒ Boolean

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)

Returns:

  • (Boolean)


61
62
63
64
65
# File 'lib/osctld/routing/table.rb', line 61

def contains?(addr)
  exclusively do
    routes.detect { |r| r.addr == addr } ? true : false
  end
end

#dumpArray<String>

Returns:

  • (Array<String>)


91
92
93
# File 'lib/osctld/routing/table.rb', line 91

def dump
  exclusively { routes.map(&:dump) }
end

#empty?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/osctld/routing/table.rb', line 67

def empty?
  exclusively { routes.empty? }
end

#exportArray<String>

Returns:

  • (Array<String>)


86
87
88
# File 'lib/osctld/routing/table.rb', line 86

def export
  exclusively { routes.map(&:export) }
end

#getArray

Get all routed addresses

Returns:

  • (Array)


81
82
83
# File 'lib/osctld/routing/table.rb', line 81

def get
  exclusively { routes.clone }
end

#remove(addr) ⇒ Routing::Route?

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)

Returns:



39
40
41
42
43
44
45
# File 'lib/osctld/routing/table.rb', line 39

def remove(addr)
  exclusively do
    r = routes.detect { |r| r.addr == addr }
    routes.delete(r) if r
    r
  end
end

#remove_if(&block) ⇒ Object



47
48
49
# File 'lib/osctld/routing/table.rb', line 47

def remove_if(&block)
  exclusively { routes.delete_if(&block) }
end

#route?(addr) ⇒ Boolean

Parameters:

  • addr (IPAddress::IPv4, IPAddress::IPv6)

Returns:

  • (Boolean)


52
53
54
55
56
57
58
# File 'lib/osctld/routing/table.rb', line 52

def route?(addr)
  ret = exclusively do
    routes.detect { |r| r.route?(addr) }
  end

  ret ? true : false
end