Class: DIY::StrategyBuilder

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStrategyBuilder

Returns a new instance of StrategyBuilder.



5
6
7
8
# File 'lib/diy/strategy_builder.rb', line 5

def initialize
  @ins = []
  @logger = DIY::Logger
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



9
10
11
# File 'lib/diy/strategy_builder.rb', line 9

def queue
  @queue
end

Instance Method Details

#add(strategy) ⇒ Object Also known as: <<



11
12
13
# File 'lib/diy/strategy_builder.rb', line 11

def add(strategy)
  @ins << strategy
end

#call(hope_pkt, recv_pkt, queue) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/diy/strategy_builder.rb', line 24

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

#loggerObject



20
21
22
# File 'lib/diy/strategy_builder.rb', line 20

def logger
  @logger
end

#logger=(logger) ⇒ Object



16
17
18
# File 'lib/diy/strategy_builder.rb', line 16

def logger=(logger)
  @logger = logger
end