Class: Kanrisuru::Core::IP::Parser::Neighbour

Inherits:
Base
  • Object
show all
Defined in:
lib/kanrisuru/core/ip/parsers/neighbour.rb

Class Method Summary collapse

Methods inherited from Base

ip_address_label_result_json, ip_address_label_result_parse, parse_address_info, parse_alias, parse_ip_maddr_name, parse_ip_row, parse_link, parse_rx, parse_tx, parse_valid

Class Method Details

.ip_neighbour_result_json(rows) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kanrisuru/core/ip/parsers/neighbour.rb', line 23

def ip_neighbour_result_json(rows)
  rows.map do |row|
    neighbour = Kanrisuru::Core::IP::IPNeighbour.new(
      IPAddr.new(row['dst']),
      row['dev'],
      row['lladdr'],
      row['state']
    )

    if row.key?('used') || row.key?('confirmed') ||
       row.key?('refcnt') || row.key?('updated') ||
       row.key?('probes')
      neighbour.stats = Kanrisuru::Core::IP::IPNeighbourStats.new

      neighbour.stats.ref_count = row['refcnt']
      neighbour.stats.used = row['used']
      neighbour.stats.confirmed = row['confirmed']
      neighbour.stats.probes = row['probes']
      neighbour.stats.updated = row['updated']
    end

    neighbour
  end
end

.ip_neighbour_result_parse(lines) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/kanrisuru/core/ip/parsers/neighbour.rb', line 48

def ip_neighbour_result_parse(lines)
  rows = []
  lines.each do |line|
    values = line.split

    neighbour = Kanrisuru::Core::IP::IPNeighbour.new(IPAddr.new(values[0]))
    neighbour.state = [values[values.length - 1]]

    if line.include?('probes') || line.include?('used') || line.include?('ref')
      neighbour.stats = Kanrisuru::Core::IP::IPNeighbourStats.new
    end

    values.each.with_index do |value, index|
      case value
      when 'dev'
        neighbour.device = values[index + 1]
      when 'lladdr'
        neighbour.lladdr = values[index + 1]
      when 'ref'
        neighbour.stats.ref_count = values[index + 1] ? values[index + 1].to_i : nil
      when 'used'
        used, confirmed, updated = values[index + 1].split('/')

        neighbour.stats.used = used ? used.to_i : nil
        neighbour.stats.updated = updated ? updated.to_i : nil
        neighbour.stats.confirmed = confirmed ? confirmed.to_i : nil
      when 'probes'
        neighbour.stats.probes = values[index + 1] ? values[index + 1].to_i : nil
      end
    end

    rows << neighbour
  end

  rows
end

.parse(command, action, version) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kanrisuru/core/ip/parsers/neighbour.rb', line 9

def parse(command, action, version)
  return unless %w[show list].include?(action)

  if version >= Kanrisuru::Core::IP::IPROUTE2_JSON_VERSION
    begin
      ip_neighbour_result_json(command.to_json)
    rescue JSON::ParserError
      ip_neighbour_result_parse(command.to_a)
    end
  else
    ip_neighbour_result_parse(command.to_a)
  end
end