Class: DIY::MacLearner

Inherits:
Object
  • Object
show all
Defined in:
lib/diy/mac_learner.rb

Instance Method Summary collapse

Constructor Details

#initialize(default_host = :A) ⇒ MacLearner

Returns a new instance of MacLearner.



3
4
5
6
# File 'lib/diy/mac_learner.rb', line 3

def initialize(default_host = :A)
  @default_host = default_host
  @table = {}
end

Instance Method Details

#_learn(mac, where) ⇒ Object



15
16
17
# File 'lib/diy/mac_learner.rb', line 15

def _learn(mac, where)
  @table[mac] = where
end

#learn(packet, where) ⇒ Object



9
10
11
12
13
# File 'lib/diy/mac_learner.rb', line 9

def learn(packet, where)
  raise "Only receive :A or B for where argument" unless where == :A or where == :B
  #~ @table.delete( src(packet) )
  _learn( src(packet), where)
end

#other(where) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/diy/mac_learner.rb', line 33

def other(where)
  if where == :A
    return :B
  elsif where == :B
    return :A
  else
    raise "Argument error"
  end
end

#tellme(packet) ⇒ Object

报告包所在的端口 A or B 如果包不在学习表内, 返回缺省端口(默认为A)



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/diy/mac_learner.rb', line 21

def tellme(packet)
  src_p = src(packet)
  if @table.has_key? src_p
    where =  @table[src_p]
  else
    where = @default_host
    _learn( src(packet), where )
  end
  _learn( dst(packet), other(where) )
  where
end