Sha256: 74d11888abe7b47dec0dba5ae063bfb7290bd1939d8e0568c7d70811028aa5e3

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# encoding : utf-8
require 'logger'
module DIY
  class StrategyBuilder
    def initialize
      @ins = []
      @logger = DIY::Logger
    end
    attr_reader :queue
    
    def add(strategy)
      @ins << strategy
    end
    alias << add
    
    def logger=(logger)
      @logger = logger
    end
    
    def logger
      @logger
    end
    
    def call(hope_pkt, recv_pkt, queue)
      logger.debug("recv_pkt, I hope: #{ Utils.pp(hope_pkt) rescue nil }...")
      
      return if hope_pkt.nil?
      
      @ins.each do |strategy|
        begin
          ret = strategy.call(hope_pkt.content, recv_pkt.content, queue)
        rescue Exception => e
          #~ logger.error("user strategy exception: #{e.class} -> #{e.message}")
          raise StrategyCallError.new(e)
        else
          if ret == Strategy::OK
            logger.info("pkt same:")
            queue.shift
            return
          elsif ret == Strategy::OK_NO_POP
            logger.info("pkt skip:")
            return
          elsif ret == Strategy::FAIL
            logger.warn("pkt fail:")
            logger.warn("pkt fail: hope_pkt is #{hope_pkt.pretty_print}")
            logger.warn("pkt fail: recv_pkt is #{recv_pkt.pretty_print}")
            e = RuntimeError.new("Strategy FAIL: hope #{hope_pkt.pretty_print} but get #{recv_pkt.pretty_print}")
            e.set_backtrace(caller) # not used
            raise UnExpectPacketError.new(e)
          elsif ret == Strategy::NONE
            #~ logger.debug("pkt jumpped:")
            next
          end # end of if
        end # end of begin
      end # end of each
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
DIY-pcap-0.3.0 lib/diy/strategy_builder.rb