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 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
- @before_send_hook = block
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
- #~ controller.before_send(&@before_send_hook)
69
+ controller.before_send(&@before_send_hook)
39
70
  controller.run
40
71
  end
41
72
 
@@ -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}: #{client} #{server} #{pkts[0].inspect}:(size= #{pkts.size})"
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.inject(pkts)
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 = 20, &block )
56
- timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout} senconds") ) do
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
@@ -5,6 +5,7 @@ module DIY
5
5
  @detail_msg = detail_msg
6
6
  end
7
7
  attr_reader :content, :detail_msg
8
+ attr_writer :content
8
9
 
9
10
  def to_s
10
11
  @content
data/lib/diy/utils.rb CHANGED
@@ -5,7 +5,7 @@ module DIY
5
5
  def pp(pkt)
6
6
  pkt = pkt.content if pkt.kind_of?(DIY::Packet)
7
7
  return nil if pkt.nil?
8
- ( pkt[0..10] + "..." ).dump
8
+ ( pkt[0..10] + "..." ).dump + "(#{pkt.size} sizes)"
9
9
  end
10
10
 
11
11
  def src_mac(pkt)
data/lib/diy/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module DIY
2
2
  class PCAP
3
- VERSION = "0.2.0"
3
+ VERSION = "0.2.1"
4
4
  end
5
5
  end
@@ -1,3 +1,5 @@
1
+ require 'rubygems'
2
+ require 'diy-pcap'
1
3
  class NoMacEqualStrategy < DIY::BasicStrategy
2
4
  def call(hope_pkt, recv_pkt, queue)
3
5
  return OK if hope_pkt[12..-1] == recv_pkt[12..-1]
data/simple/pcap.rb CHANGED
@@ -8,6 +8,8 @@ ss = DIY::SimpleStrategy.new
8
8
  a = DIY::Builder.new do
9
9
  use ss
10
10
  pcapfile Dir["pcaps/*.pcap"]
11
+ client "127.0.0.1"
12
+ server "127.0.0.1"
11
13
  end
12
14
 
13
15
  a.run
data/spec/utils_spec.rb CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe DIY::Utils do
4
4
  it "#pp" do
5
- DIY::Utils.pp('a' * 100).should == ('a' * 11 + '...').dump
5
+ DIY::Utils.pp('a' * 100).should == ('a' * 11 + '...').dump + "(100 sizes)"
6
6
  end
7
7
 
8
8
  it "#src_mac" do
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 0
9
- version: 0.2.0
8
+ - 1
9
+ version: 0.2.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - yafei Lee