DIY-pcap 0.2.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +14 -6
 - data/lib/diy/builder.rb +34 -3
 - data/lib/diy/controller.rb +39 -4
 - data/lib/diy/packet.rb +1 -0
 - data/lib/diy/utils.rb +1 -1
 - data/lib/diy/version.rb +1 -1
 - data/simple/diy-strategy-pcap.rb +2 -0
 - data/simple/pcap.rb +2 -0
 - data/spec/utils_spec.rb +1 -1
 - metadata +2 -2
 
    
        data/README.md
    CHANGED
    
    | 
         @@ -9,7 +9,7 @@ DIY-pcap 
     | 
|
| 
       9 
9 
     | 
    
         | 
| 
       10 
10 
     | 
    
         
             
            1. 安装很简单
         
     | 
| 
       11 
11 
     | 
    
         | 
| 
       12 
     | 
    
         
            -
                ```
         
     | 
| 
      
 12 
     | 
    
         
            +
                ```bash
         
     | 
| 
       13 
13 
     | 
    
         
             
                    gem install DIY-pcap
         
     | 
| 
       14 
14 
     | 
    
         
             
                ```
         
     | 
| 
       15 
15 
     | 
    
         | 
| 
         @@ -32,9 +32,9 @@ DIY-pcap 
     | 
|
| 
       32 
32 
     | 
    
         | 
| 
       33 
33 
     | 
    
         
             
            3. 开始发送与接收数据
         
     | 
| 
       34 
34 
     | 
    
         | 
| 
       35 
     | 
    
         
            -
              * 服务端,执行 `rpcap spec.rb`
         
     | 
| 
      
 35 
     | 
    
         
            +
              * 服务端,执行 `rpcap -f spec.rb`
         
     | 
| 
       36 
36 
     | 
    
         | 
| 
       37 
     | 
    
         
            -
              * 本机, 执行 `pcap spec.rb`
         
     | 
| 
      
 37 
     | 
    
         
            +
              * 本机, 执行 `pcap -f spec.rb`
         
     | 
| 
       38 
38 
     | 
    
         | 
| 
       39 
39 
     | 
    
         
             
            ## 使用方法, 回放pcap报文 ( 二 )
         
     | 
| 
       40 
40 
     | 
    
         | 
| 
         @@ -46,10 +46,18 @@ DIY-pcap 
     | 
|
| 
       46 
46 
     | 
    
         
             
                      DIY::Builder.new do
         
     | 
| 
       47 
47 
     | 
    
         
             
                        pcapfile "pcaps/simple.pcap"
         
     | 
| 
       48 
48 
     | 
    
         
             
                        use DIY::SimpleStrategy.new
         
     | 
| 
      
 49 
     | 
    
         
            +
                        client "x.x.x.x" # 配置客户端ip, 缺省端口为7878
         
     | 
| 
      
 50 
     | 
    
         
            +
                        server "x.x.x.x" # 配置服务端ip, 缺省端口为7879, 以上都可以写为 x.x.x.x:x 的形式, 与 rpcap或pcap的 -i 参数对应
         
     | 
| 
       49 
51 
     | 
    
         
             
                      end
         
     | 
| 
       50 
52 
     | 
    
         
             
                ```
         
     | 
| 
       51 
     | 
    
         
            -
            3.  
     | 
| 
      
 53 
     | 
    
         
            +
            3. 使用方法( 准备三台主机或逻辑主机 )
         
     | 
| 
      
 54 
     | 
    
         
            +
             
     | 
| 
      
 55 
     | 
    
         
            +
              * 服务端, 执行 `rpcap` ( 如果启动出错, 请参考 rpcap -h 中参数 -i 与 -n )
         
     | 
| 
      
 56 
     | 
    
         
            +
              
         
     | 
| 
      
 57 
     | 
    
         
            +
              * 客户端, 执行 `pcap` ( 如果启动出错, 请参考 pcap -h 中参数 -i 与 -n )
         
     | 
| 
      
 58 
     | 
    
         
            +
              
         
     | 
| 
      
 59 
     | 
    
         
            +
              * 控制端, 执行 `ruby spec.rb`, OK, 开始交互, 结束后, 会有 cost time 与 fail count 输出.
         
     | 
| 
       52 
60 
     | 
    
         | 
| 
       53 
     | 
    
         
            -
            4. (其他说明) 扩展策略, 自定义日志,  
     | 
| 
      
 61 
     | 
    
         
            +
            4. (其他说明) 扩展策略, 自定义日志, 修改报文内容参见 [Wiki Home](/windy/DIY-pcap/wiki).
         
     | 
| 
       54 
62 
     | 
    
         | 
| 
       55 
     | 
    
         
            -
            OK,  
     | 
| 
      
 63 
     | 
    
         
            +
            OK, 一切如故.
         
     | 
    
        data/lib/diy/builder.rb
    CHANGED
    
    | 
         @@ -11,8 +11,12 @@ module DIY 
     | 
|
| 
       11 
11 
     | 
    
         
             
                  @strategies.unshift(what)
         
     | 
| 
       12 
12 
     | 
    
         
             
                end
         
     | 
| 
       13 
13 
     | 
    
         | 
| 
       14 
     | 
    
         
            -
                def before_send(&block)
         
     | 
| 
       15 
     | 
    
         
            -
                   
     | 
| 
      
 14 
     | 
    
         
            +
                def before_send(arg= nil, &block)
         
     | 
| 
      
 15 
     | 
    
         
            +
                  if arg
         
     | 
| 
      
 16 
     | 
    
         
            +
                    @before_send_hook = arg
         
     | 
| 
      
 17 
     | 
    
         
            +
                  else
         
     | 
| 
      
 18 
     | 
    
         
            +
                    @before_send_hook = block
         
     | 
| 
      
 19 
     | 
    
         
            +
                  end
         
     | 
| 
       16 
20 
     | 
    
         
             
                end
         
     | 
| 
       17 
21 
     | 
    
         | 
| 
       18 
22 
     | 
    
         
             
                def find_worker_keepers
         
     | 
| 
         @@ -23,6 +27,33 @@ module DIY 
     | 
|
| 
       23 
27 
     | 
    
         
             
                  @server = DRbObject.new_with_uri(@suri)
         
     | 
| 
       24 
28 
     | 
    
         
             
                end
         
     | 
| 
       25 
29 
     | 
    
         | 
| 
      
 30 
     | 
    
         
            +
                def client(ip_or_iport)
         
     | 
| 
      
 31 
     | 
    
         
            +
                  default_port = "7878"
         
     | 
| 
      
 32 
     | 
    
         
            +
                  if ! ip_or_iport.include?(':')
         
     | 
| 
      
 33 
     | 
    
         
            +
                    iport = ip_or_iport + ':' + default_port
         
     | 
| 
      
 34 
     | 
    
         
            +
                  else
         
     | 
| 
      
 35 
     | 
    
         
            +
                    iport = ip_or_iport
         
     | 
| 
      
 36 
     | 
    
         
            +
                  end
         
     | 
| 
      
 37 
     | 
    
         
            +
                  @curi = ip2druby(iport)
         
     | 
| 
      
 38 
     | 
    
         
            +
                end
         
     | 
| 
      
 39 
     | 
    
         
            +
                
         
     | 
| 
      
 40 
     | 
    
         
            +
                def server(ip_or_iport)
         
     | 
| 
      
 41 
     | 
    
         
            +
                  default_port = "7879"
         
     | 
| 
      
 42 
     | 
    
         
            +
                  if ! ip_or_iport.include?(':')
         
     | 
| 
      
 43 
     | 
    
         
            +
                    iport = ip_or_iport + ':' + default_port
         
     | 
| 
      
 44 
     | 
    
         
            +
                  else
         
     | 
| 
      
 45 
     | 
    
         
            +
                    iport = ip_or_iport
         
     | 
| 
      
 46 
     | 
    
         
            +
                  end
         
     | 
| 
      
 47 
     | 
    
         
            +
                  @suri = ip2druby(iport)
         
     | 
| 
      
 48 
     | 
    
         
            +
                end
         
     | 
| 
      
 49 
     | 
    
         
            +
                
         
     | 
| 
      
 50 
     | 
    
         
            +
                def ip2druby(ip)
         
     | 
| 
      
 51 
     | 
    
         
            +
                  if ! ip.include?('://')
         
     | 
| 
      
 52 
     | 
    
         
            +
                    return "druby://" + ip
         
     | 
| 
      
 53 
     | 
    
         
            +
                  end
         
     | 
| 
      
 54 
     | 
    
         
            +
                  return ip
         
     | 
| 
      
 55 
     | 
    
         
            +
                end
         
     | 
| 
      
 56 
     | 
    
         
            +
                
         
     | 
| 
       26 
57 
     | 
    
         
             
                def pcapfile(pcaps)
         
     | 
| 
       27 
58 
     | 
    
         
             
                  DIY::Logger.info( "Initialize Offline: #{pcaps.to_a.join(', ')}" )
         
     | 
| 
       28 
59 
     | 
    
         
             
                  @offline = DIY::Offline.new(pcaps)
         
     | 
| 
         @@ -35,7 +66,7 @@ module DIY 
     | 
|
| 
       35 
66 
     | 
    
         
             
                  @strategies.each { |builder| @strategy_builder.add(builder) }
         
     | 
| 
       36 
67 
     | 
    
         
             
                  find_worker_keepers
         
     | 
| 
       37 
68 
     | 
    
         
             
                  controller = Controller.new( @client, @server, @offline, @strategy_builder )
         
     | 
| 
       38 
     | 
    
         
            -
                   
     | 
| 
      
 69 
     | 
    
         
            +
                  controller.before_send(&@before_send_hook)
         
     | 
| 
       39 
70 
     | 
    
         
             
                  controller.run
         
     | 
| 
       40 
71 
     | 
    
         
             
                end
         
     | 
| 
       41 
72 
     | 
    
         | 
    
        data/lib/diy/controller.rb
    CHANGED
    
    | 
         @@ -8,12 +8,15 @@ module DIY 
     | 
|
| 
       8 
8 
     | 
    
         
             
                  @server = server
         
     | 
| 
       9 
9 
     | 
    
         
             
                  @offline = offline
         
     | 
| 
       10 
10 
     | 
    
         
             
                  @strategy = strategy
         
     | 
| 
      
 11 
     | 
    
         
            +
                  @before_send = nil
         
     | 
| 
       11 
12 
     | 
    
         
             
                end
         
     | 
| 
       12 
13 
     | 
    
         | 
| 
       13 
14 
     | 
    
         
             
                def run
         
     | 
| 
       14 
15 
     | 
    
         
             
                  client = @client
         
     | 
| 
       15 
16 
     | 
    
         
             
                  server = @server
         
     | 
| 
       16 
17 
     | 
    
         | 
| 
      
 18 
     | 
    
         
            +
                  @fail_count = 0
         
     | 
| 
      
 19 
     | 
    
         
            +
                  start_time = Time.now
         
     | 
| 
       17 
20 
     | 
    
         
             
                  #clear
         
     | 
| 
       18 
21 
     | 
    
         
             
                  client.terminal
         
     | 
| 
       19 
22 
     | 
    
         
             
                  server.terminal
         
     | 
| 
         @@ -25,7 +28,14 @@ module DIY 
     | 
|
| 
       25 
28 
     | 
    
         
             
                    client, server = server, client
         
     | 
| 
       26 
29 
     | 
    
         
             
                    rescue HopePacketTimeoutError
         
     | 
| 
       27 
30 
     | 
    
         
             
                      DIY::Logger.warn( "Timeout: Hope packet is #{pkts[0].inspect} ")
         
     | 
| 
      
 31 
     | 
    
         
            +
                      @fail_count += 1
         
     | 
| 
      
 32 
     | 
    
         
            +
                      begin
         
     | 
| 
       28 
33 
     | 
    
         
             
                      @offline.next_pcap
         
     | 
| 
      
 34 
     | 
    
         
            +
                      rescue EOFError
         
     | 
| 
      
 35 
     | 
    
         
            +
                        client.terminal
         
     | 
| 
      
 36 
     | 
    
         
            +
                        server.terminal
         
     | 
| 
      
 37 
     | 
    
         
            +
                        break
         
     | 
| 
      
 38 
     | 
    
         
            +
                      end
         
     | 
| 
       29 
39 
     | 
    
         
             
                      client,server = @client, @server
         
     | 
| 
       30 
40 
     | 
    
         
             
                    rescue EOFError
         
     | 
| 
       31 
41 
     | 
    
         
             
                      client.terminal
         
     | 
| 
         @@ -33,27 +43,52 @@ module DIY 
     | 
|
| 
       33 
43 
     | 
    
         
             
                      break
         
     | 
| 
       34 
44 
     | 
    
         
             
                    end
         
     | 
| 
       35 
45 
     | 
    
         
             
                  end
         
     | 
| 
      
 46 
     | 
    
         
            +
                  end_time = Time.now
         
     | 
| 
      
 47 
     | 
    
         
            +
                  stats_result( end_time - start_time, @fail_count )
         
     | 
| 
       36 
48 
     | 
    
         
             
                end
         
     | 
| 
       37 
49 
     | 
    
         | 
| 
       38 
50 
     | 
    
         
             
                def one_round( client, server, pkts )
         
     | 
| 
       39 
51 
     | 
    
         
             
                  @round_count = 0 unless @round_count
         
     | 
| 
       40 
52 
     | 
    
         
             
                  @round_count += 1
         
     | 
| 
       41 
     | 
    
         
            -
                  DIY::Logger.info "round #{@round_count}:  
     | 
| 
      
 53 
     | 
    
         
            +
                  DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].inspect}:(size= #{pkts.size})"
         
     | 
| 
       42 
54 
     | 
    
         
             
                  server.ready do |recv_pkt|
         
     | 
| 
       43 
55 
     | 
    
         
             
                    recv_pkt = Packet.new(recv_pkt)
         
     | 
| 
       44 
56 
     | 
    
         
             
                    @strategy.call(pkts.first, recv_pkt, pkts)
         
     | 
| 
       45 
57 
     | 
    
         
             
                  end
         
     | 
| 
       46 
     | 
    
         
            -
                  client 
     | 
| 
      
 58 
     | 
    
         
            +
                  client_send(client, pkts)
         
     | 
| 
       47 
59 
     | 
    
         
             
                  wait_recv_ok(pkts)
         
     | 
| 
       48 
60 
     | 
    
         
             
                  server.terminal
         
     | 
| 
       49 
61 
     | 
    
         
             
                end
         
     | 
| 
       50 
62 
     | 
    
         | 
| 
      
 63 
     | 
    
         
            +
                def client_send(client, pkts)
         
     | 
| 
      
 64 
     | 
    
         
            +
                  if ! @before_send
         
     | 
| 
      
 65 
     | 
    
         
            +
                    client.inject(pkts)
         
     | 
| 
      
 66 
     | 
    
         
            +
                  else
         
     | 
| 
      
 67 
     | 
    
         
            +
                    pkts = pkts.collect do |pkt| 
         
     | 
| 
      
 68 
     | 
    
         
            +
                      content = pkt.content
         
     | 
| 
      
 69 
     | 
    
         
            +
                      pkt.content = @before_send.call(content)
         
     | 
| 
      
 70 
     | 
    
         
            +
                      pkt
         
     | 
| 
      
 71 
     | 
    
         
            +
                    end
         
     | 
| 
      
 72 
     | 
    
         
            +
                    
         
     | 
| 
      
 73 
     | 
    
         
            +
                    client.inject(pkts)
         
     | 
| 
      
 74 
     | 
    
         
            +
                  end
         
     | 
| 
      
 75 
     | 
    
         
            +
                end
         
     | 
| 
      
 76 
     | 
    
         
            +
                
         
     | 
| 
      
 77 
     | 
    
         
            +
                def before_send(&block)
         
     | 
| 
      
 78 
     | 
    
         
            +
                  @before_send = block
         
     | 
| 
      
 79 
     | 
    
         
            +
                end
         
     | 
| 
      
 80 
     | 
    
         
            +
                
         
     | 
| 
      
 81 
     | 
    
         
            +
                def stats_result( cost_time, fail_count )
         
     | 
| 
      
 82 
     | 
    
         
            +
                  DIY::Logger.info " ====== Finished in #{cost_time} seconds"
         
     | 
| 
      
 83 
     | 
    
         
            +
                  DIY::Logger.info " ====== Total fail_count: #{fail_count} failures"
         
     | 
| 
      
 84 
     | 
    
         
            +
                end
         
     | 
| 
      
 85 
     | 
    
         
            +
                
         
     | 
| 
       51 
86 
     | 
    
         
             
                def wait_recv_ok(pkts)
         
     | 
| 
       52 
87 
     | 
    
         
             
                  wait_until { pkts.empty? }
         
     | 
| 
       53 
88 
     | 
    
         
             
                end
         
     | 
| 
       54 
89 
     | 
    
         | 
| 
       55 
     | 
    
         
            -
                def wait_until( timeout =  
     | 
| 
       56 
     | 
    
         
            -
                  timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout}  
     | 
| 
      
 90 
     | 
    
         
            +
                def wait_until( timeout = 10, &block )
         
     | 
| 
      
 91 
     | 
    
         
            +
                  timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout} seconds") ) do
         
     | 
| 
       57 
92 
     | 
    
         
             
                    loop do
         
     | 
| 
       58 
93 
     | 
    
         
             
                      break if block.call
         
     | 
| 
       59 
94 
     | 
    
         
             
                      sleep 0.01
         
     | 
    
        data/lib/diy/packet.rb
    CHANGED
    
    
    
        data/lib/diy/utils.rb
    CHANGED
    
    
    
        data/lib/diy/version.rb
    CHANGED
    
    
    
        data/simple/diy-strategy-pcap.rb
    CHANGED
    
    
    
        data/simple/pcap.rb
    CHANGED
    
    
    
        data/spec/utils_spec.rb
    CHANGED