src/examples/match_compare/match-compare.rb in trema-0.4.6 vs src/examples/match_compare/match-compare.rb in trema-0.4.7

- old
+ new

@@ -23,70 +23,70 @@ @rules = [] add_rules end - def packet_in datapath_id, message - match = ExactMatch.from( message ) - action, log = lookup_rules( datapath_id, match ) - info "action=#{ action }, datapath_id=#{ datapath_id.to_hex }, message={#{ match.to_s }}" if log + def packet_in(datapath_id, message) + match = ExactMatch.from(message) + action, log = lookup_rules(datapath_id, match) + info "action=#{ action }, datapath_id=#{ datapath_id.to_hex }, message={#{ match }}" if log if action == :allow - actions = ActionOutput.new( OFPP_FLOOD ) - send_flow_mod_add( datapath_id, :match => match, :idle_timeout => 60, :actions => actions ) - send_packet_out( datapath_id, :packet_in => message, :actions => actions ) + actions = ActionOutput.new(OFPP_FLOOD) + send_flow_mod_add(datapath_id, :match => match, :idle_timeout => 60, :actions => actions) + send_packet_out(datapath_id, :packet_in => message, :actions => actions) else - send_flow_mod_add( datapath_id, :match => match, :idle_timeout => 60 ) + send_flow_mod_add(datapath_id, :match => match, :idle_timeout => 60) end end private def add_rules dl_type_arp = 0x0806 dl_type_ipv4 = 0x0800 - network = "192.168.0.0/16" + network = '192.168.0.0/16' allow :dl_type => dl_type_arp allow :dl_type => dl_type_ipv4, :nw_src => network, :log => true allow :dl_type => dl_type_ipv4, :nw_dst => network, :log => true block :log => true end - def allow hash = {} + def allow(hash = {}) add_rule :allow, hash end - def block hash = {} + def block(hash = {}) add_rule :block, hash end - def add_rule action, hash - datapath_id = hash.key?( :datapath_id ) && hash.delete( :datapath_id ) || nil - log = hash.key?( :log ) && hash.delete( :log ) || false - rule = Struct.new( :action, :datapath_id, :match, :log ) - @rules << rule.new( action, datapath_id, Match.new( hash ), log ) + def add_rule(action, hash) + datapath_id = hash.key?(:datapath_id) && hash.delete(:datapath_id) || nil + log = hash.key?(:log) && hash.delete(:log) || false + rule = Struct.new(:action, :datapath_id, :match, :log) + @rules << rule.new(action, datapath_id, Match.new(hash), log) end - def lookup_rules datapath_id, match + def lookup_rules(datapath_id, match) action = :block # default action log = false @rules.each do | each | - if !each.datapath_id.nil? && datapath_id != each.datapath_id + if each.datapath_id && datapath_id != each.datapath_id next end - if each.match.compare( match ) + if each.match.compare(match) action = each.action log = each.log break end end - return action, log + [action, log] end end ### Local variables: