DIY-pcap 0.3.8 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -94,11 +94,11 @@ module DIY
94
94
  @strategies.each { |builder| @strategy_builder.add(builder) }
95
95
  find_worker_keepers
96
96
  set_filter
97
- controller = Controller.new( @client, @server, @offline, @strategy_builder )
98
- controller.before_send(&@before_send_hook)
99
- controller.timeout(@timeout) if @timeout
100
- controller.error_on_stop if @error_on_stop
101
- controller.run
97
+ @controller = Controller.new( @client, @server, @offline, @strategy_builder )
98
+ @controller.before_send(&@before_send_hook)
99
+ @controller.timeout(@timeout) if @timeout
100
+ @controller.error_on_stop if @error_on_stop
101
+ @controller.run
102
102
  end
103
103
 
104
104
  end
@@ -11,6 +11,7 @@ module DIY
11
11
  @before_send = nil
12
12
  @timeout = nil
13
13
  @error_on_stop = nil
14
+ @fail_count = 0
14
15
  end
15
16
 
16
17
  def run
@@ -86,9 +87,12 @@ module DIY
86
87
  @round_count = 0 unless @round_count
87
88
  @round_count += 1
88
89
  DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].pretty_print}:(queue= #{pkts.size})"
89
- if pkts.size >= 10
90
- DIY::Logger.info "queue size too big: #{pkts.size}, maybe something error"
91
- end
90
+
91
+ # check is client or server at this time
92
+ # give the flag to **before_send** block
93
+ client_or_server = client == @client
94
+
95
+ before_send_call(pkts, client_or_server)
92
96
 
93
97
  recv_pkt_proc_set( pkts )
94
98
  @ready = false
@@ -120,29 +124,32 @@ module DIY
120
124
  end
121
125
 
122
126
  def client_send(client, pkts)
123
- if @before_send
124
- pkts = pkts.collect do |pkt|
125
- content = pkt.content
126
- begin
127
- pkt.content = @before_send.call(content)
128
- rescue Exception => e
129
- DIY::Logger.warn("UserError Catch: " + error = BeforeSendCallError.new(e) )
130
- error.backtrace.each do |msg|
131
- DIY::Logger.info(msg)
132
- end
133
- raise error
134
- end
135
- pkt
136
- end
137
- end
127
+ #~ pkt_contents = pkts.collect { |pkt| pkt.content }
138
128
  begin
139
129
  client.inject(pkts)
140
130
  rescue FFI::PCap::LibError =>e
141
- DIY::Logger.warn("SendPacketError Catch: " + e )
131
+ DIY::Logger.info("SendPacketError Catch: " + e )
142
132
  raise e
143
133
  end
144
134
  end
145
135
 
136
+ def before_send_call(pkts, client_flag)
137
+ return unless @before_send
138
+ pkts.delete_if do |pkt|
139
+ begin
140
+ ret = @before_send.call( pkt, client_flag )
141
+ rescue Exception => e
142
+ DIY::Logger.warn("UserError Catch: " + error = BeforeSendCallError.new(e) )
143
+ error.backtrace.each do |msg|
144
+ DIY::Logger.info(msg)
145
+ end
146
+ raise error
147
+ end
148
+ ! ret
149
+ end
150
+ pkts
151
+ end
152
+
146
153
  def before_send(&block)
147
154
  @before_send = block
148
155
  end
@@ -161,15 +168,17 @@ module DIY
161
168
  end
162
169
 
163
170
  def offline_result
164
- sprintf "%4d files, %8d packets", @offline.files_size, @offline.now_size
171
+ sprintf "%4d files, running %8d packets", @offline.files_size, @offline.now_size
165
172
  end
166
173
 
167
174
  def wait_recv_ok(pkts)
168
- wait_until(@timeout ||= 10) do
169
- if @error_flag
170
- raise @error_flag
175
+ loop do
176
+ now_size = pkts.size
177
+ break if now_size == 0
178
+ wait_until(@timeout ||= 10) do
179
+ raise @error_flag if @error_flag
180
+ pkts.size < now_size
171
181
  end
172
- pkts.empty?
173
182
  end
174
183
  end
175
184
 
@@ -26,7 +26,8 @@ module DIY
26
26
  next unless pkt
27
27
  block.call(this, pkt)
28
28
  end
29
- sleep 0.1
29
+ block.call(nil, nil) # at least every 0.01 min do it.
30
+ sleep 0.01
30
31
  end
31
32
  DIY::Logger.debug "stopped loop recv..."
32
33
  end
@@ -1,5 +1,5 @@
1
1
  module DIY
2
2
  class PCAP
3
- VERSION = "0.3.8"
3
+ VERSION = "0.4.1"
4
4
  end
5
5
  end
@@ -31,9 +31,17 @@ module DIY
31
31
  DIY::Logger.info "start thread recving pkt..."
32
32
  @live.loop do |this, pkt|
33
33
  if ! @start
34
+ DIY::Logger.debug "looprecv stop..." unless @recv_stop_flag
34
35
  @recv_stop_flag = true
35
36
  next
36
37
  end
38
+
39
+ if @start
40
+ DIY::Logger.debug "looprecv start..." if @recv_stop_flag
41
+ @recv_stop_flag = false
42
+ end
43
+
44
+ next unless pkt
37
45
  @queue.push(pkt.body)
38
46
  end
39
47
  DIY::Logger.debug "worker: stopped loop recv"
@@ -45,15 +53,22 @@ module DIY
45
53
  @callback_t = Thread.new do
46
54
  #~ DIY::Logger.info "start thread callbacking pkt..."
47
55
  while @running do
56
+ if ! @start
57
+ DIY::Logger.debug "callback stop..." unless @callback_stop_flag
58
+ @callback_stop_flag = true
59
+ sleep 0.01
60
+ next
61
+ end
62
+
63
+ if @start
64
+ DIY::Logger.debug "callback start..." if @callback_stop_flag
65
+ @callback_stop_flag = false
66
+ end
67
+
48
68
  begin
49
69
  pkt = @queue.pop
50
70
  #~ DIY::Logger.info "callback: #{pkt}"
51
71
 
52
- if ! @start
53
- @callback_stop_flag = true
54
- next
55
- end
56
-
57
72
  if @block and pkt
58
73
  @block.call(pkt)
59
74
  end
@@ -81,7 +96,7 @@ module DIY
81
96
  # 停止收发
82
97
  def terminal
83
98
  DIY::Logger.info("stop recv pkt")
84
- pause
99
+ stopping
85
100
  end
86
101
 
87
102
  # 停止线程
@@ -107,9 +122,11 @@ module DIY
107
122
  pause
108
123
  while ! paused?
109
124
  end
125
+ DIY::Logger.debug "stop success"
110
126
  end
111
127
 
112
128
  def pause
129
+ DIY::Logger.debug "pausing..."
113
130
  @start = false
114
131
  @recv_stop_flag = false
115
132
  @callback_stop_flag = false
@@ -118,7 +135,15 @@ module DIY
118
135
  end
119
136
 
120
137
  def start
138
+ DIY::Logger.debug "starting..."
121
139
  @start = true
140
+ while ! start?
141
+ end
142
+ DIY::Logger.debug "start success"
143
+ end
144
+
145
+ def start?
146
+ @start and ! recv_stop? and ! callback_stop?
122
147
  end
123
148
 
124
149
  def paused?
@@ -41,9 +41,11 @@ describe "Controller" do
41
41
  builder = DIY::Builder.new do
42
42
  pcapfiles "helper/http.pcap"
43
43
  use DIY::SimpleStrategy.new
44
- timeout 10
44
+ timeout 1
45
45
  end
46
+ #~ builder.run
46
47
  lambda { builder.run }.should_not raise_error
48
+ builder.instance_variable_get("@controller").instance_variable_get("@fail_count").should == 0
47
49
  end
48
50
 
49
51
  it "#run none_hope_skip" do
@@ -62,9 +64,10 @@ describe "Controller" do
62
64
  pcapfiles "helper/http.pcap"
63
65
  use hope_skip
64
66
  use DIY::SimpleStrategy.new
65
- timeout 10
67
+ timeout 2
66
68
  end
67
69
  lambda { builder.run }.should_not raise_error
70
+ builder.instance_variable_get("@controller").instance_variable_get("@fail_count").should == 0
68
71
  end
69
72
 
70
73
  it "#run stragety error" do
@@ -79,9 +82,10 @@ describe "Controller" do
79
82
  builder = DIY::Builder.new do
80
83
  pcapfiles "helper/http.pcap"
81
84
  use wrongUserStragety
82
- timeout 10
85
+ timeout 1
83
86
  end
84
87
  lambda { builder.run }.should_not raise_error
88
+ builder.instance_variable_get("@controller").instance_variable_get("@fail_count").should == 1
85
89
  end
86
90
 
87
91
  it "#run before_send error" do
@@ -93,17 +97,21 @@ describe "Controller" do
93
97
  pcapfiles "helper/http.pcap"
94
98
  end
95
99
  lambda { build2.run }.should_not raise_error
100
+ build2.instance_variable_get("@controller").instance_variable_get("@fail_count").should == 1
96
101
  end
97
102
 
98
103
  it "#run big packet " do
99
104
  sleep 1
100
105
  build2 = DIY::Builder.new do
101
- before_send do |pkt|
106
+ before_send do |pkt, flag|
102
107
  new_pkt = "a" * 10000
108
+ pkt.content = new_pkt
109
+ true
103
110
  end
104
111
  pcapfiles "helper/http.pcap"
105
112
  end
106
113
  lambda { build2.run }.should_not raise_error
114
+ build2.instance_variable_get("@controller").instance_variable_get("@fail_count").should == 1
107
115
  end
108
116
 
109
117
  it "#run stragety fail" do
@@ -120,6 +128,7 @@ describe "Controller" do
120
128
  pcapfiles "helper/http.pcap"
121
129
  end
122
130
  lambda { build2.run }.should_not raise_error
131
+ build2.instance_variable_get("@controller").instance_variable_get("@fail_count").should == 1
123
132
  end
124
133
 
125
134
  it "#run with filter" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: DIY-pcap
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 3
9
- - 8
10
- version: 0.3.8
8
+ - 4
9
+ - 1
10
+ version: 0.4.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - yafei Lee
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-10-29 00:00:00 Z
18
+ date: 2012-11-06 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: ffi-pcap