DIY-pcap 0.2.6 → 0.2.7

Sign up to get free protection for your applications and to get access to all the features.
@@ -27,8 +27,8 @@ module DIY
27
27
  pkts = @offline.nexts
28
28
  one_round( client, server, pkts )
29
29
  client, server = server, client
30
- rescue HopePacketTimeoutError
31
- DIY::Logger.warn( "Timeout: Hope packet is #{pkts[0].inspect} ")
30
+ rescue HopePacketTimeoutError, UserError, FFI::PCap::LibError => e
31
+ DIY::Logger.warn( "Timeout: Hope packet is #{pkts[0].inspect} ") if e.kind_of?(HopePacketTimeoutError)
32
32
  @fail_count += 1
33
33
  begin
34
34
  @offline.next_pcap
@@ -50,12 +50,22 @@ module DIY
50
50
  end
51
51
 
52
52
  def one_round( client, server, pkts )
53
+ @error_flag = nil
53
54
  @round_count = 0 unless @round_count
54
55
  @round_count += 1
55
- DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].inspect}:(size= #{pkts.size})"
56
+ DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].inspect}:(queue= #{pkts.size})"
56
57
  server.ready do |recv_pkt|
58
+ next if @error_flag # error accur waiting other thread do with it
57
59
  recv_pkt = Packet.new(recv_pkt)
58
- @strategy.call(pkts.first, recv_pkt, pkts)
60
+ begin
61
+ @strategy.call(pkts.first, recv_pkt, pkts)
62
+ rescue DIY::StrategyCallError =>e
63
+ DIY::Logger.warn("UserError Catch: " + e.inspect)
64
+ e.backtrace.each do |msg|
65
+ DIY::Logger.info(msg)
66
+ end
67
+ @error_flag = e
68
+ end
59
69
  end
60
70
  client_send(client, pkts)
61
71
  wait_recv_ok(pkts)
@@ -63,16 +73,26 @@ module DIY
63
73
  end
64
74
 
65
75
  def client_send(client, pkts)
66
- if ! @before_send
67
- client.inject(pkts)
68
- else
76
+ if @before_send
69
77
  pkts = pkts.collect do |pkt|
70
78
  content = pkt.content
71
- pkt.content = @before_send.call(content)
79
+ begin
80
+ pkt.content = @before_send.call(content)
81
+ rescue Exception => e
82
+ DIY::Logger.warn("UserError Catch: " + error = BeforeSendCallError.new(e) )
83
+ error.backtrace.each do |msg|
84
+ DIY::Logger.info(msg)
85
+ end
86
+ raise error
87
+ end
72
88
  pkt
73
89
  end
74
-
90
+ end
91
+ begin
75
92
  client.inject(pkts)
93
+ rescue FFI::PCap::LibError =>e
94
+ DIY::Logger.warn("SendPacketError Catch: " + e )
95
+ raise e
76
96
  end
77
97
  end
78
98
 
@@ -90,14 +110,19 @@ module DIY
90
110
  end
91
111
 
92
112
  def wait_recv_ok(pkts)
93
- wait_until(@timeout ||= 10) { pkts.empty? }
113
+ wait_until(@timeout ||= 10) do
114
+ if @error_flag
115
+ raise @error_flag
116
+ end
117
+ pkts.empty?
118
+ end
94
119
  end
95
120
 
96
121
  def wait_until( timeout = 10, &block )
97
122
  Timeout.timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout} seconds") ) do
98
123
  loop do
99
124
  break if block.call
100
- sleep 0.01
125
+ sleep 0.1
101
126
  end
102
127
  end
103
128
  end
@@ -12,4 +12,22 @@ module DIY
12
12
  # 不可能出现的报文出现
13
13
  class UnExpectPacketError < Error; end
14
14
 
15
+ class UserError < Error
16
+ def initialize(real_exception)
17
+ puts "new..."
18
+ @real_exception = real_exception
19
+ @name = real_exception.class
20
+ @message = real_exception.message
21
+ set_backtrace( Utils.filter_backtrace(real_exception) )
22
+ end
23
+
24
+ def inspect
25
+ "#<#{self.class.name}: @real_exception=#{@name}, @real_msg=#{@message}>"
26
+ end
27
+ end
28
+
29
+ # 策略执行异常
30
+ class StrategyCallError < UserError; end
31
+ # before_send 异常
32
+ class BeforeSendCallError < UserError; end
15
33
  end
@@ -30,8 +30,8 @@ module DIY
30
30
  begin
31
31
  ret = strategy.call(hope_pkt.content, recv_pkt.content, queue)
32
32
  rescue Exception => e
33
- logger.error("user strategy exception: #{e.class} -> #{e.message}")
34
- raise
33
+ #~ logger.error("user strategy exception: #{e.class} -> #{e.message}")
34
+ raise StrategyCallError.new(e)
35
35
  else
36
36
  if ret == Strategy::OK
37
37
  logger.info("pkt same:")
data/lib/diy/utils.rb CHANGED
@@ -34,6 +34,28 @@ module DIY
34
34
  end
35
35
  end
36
36
  end
37
+
38
+ def filter_backtrace(e)
39
+ filter_ary = [ "/lib/diy/controller.rb", "/lib/diy/strategy_builder.rb" ]
40
+ new_bt = []
41
+ e.backtrace.each do |msg|
42
+ if ! Utils.ary_match(filter_ary, msg)
43
+ new_bt << msg
44
+ else
45
+ puts "break"
46
+ break
47
+ end
48
+ end
49
+ new_bt
50
+ end
51
+
52
+ def ary_match(ary, msg)
53
+ ary.each do |e|
54
+ return true if /#{Regexp.escape(e)}/ === msg
55
+ end
56
+ nil
57
+ end
58
+
37
59
  end
38
60
  end
39
61
  end
data/lib/diy/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  module DIY
2
2
  class PCAP
3
- VERSION = "0.2.6"
3
+ VERSION = "0.2.7"
4
4
  end
5
5
  end
@@ -1,37 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe DIY::Builder do
4
-
5
- before(:each) do
6
- @device_name = FFI::PCap.dump_devices[0][0]
7
- DIY::Logger.info( "Initialize Live: #{@device_name}" )
8
- @live = DIY::Live.new(@device_name)#FFI::PCap::Live.new(:dev=>@device_name, :handler => FFI::PCap::Handler, :promisc => true)
9
- @live2 = DIY::Live.new(@device_name)#FFI::PCap::Live.new(:dev=>@device_name, :handler => FFI::PCap::Handler, :promisc => true)
10
-
11
- @curi = "druby://localhost:7878"
12
- @suri = "druby://localhost:7879"
13
-
14
- end
15
-
16
- it "#run" do
17
- # client create
18
- Thread.abort_on_exception = true
19
- client_t = Thread.new do
20
- client = DIY::Worker.new(@live)
21
- DIY::WorkerKeeper.new(client, @curi).run
22
- end
23
-
24
- server_t = Thread.new do
25
- server = DIY::Worker.new(@live2)
26
- DIY::WorkerKeeper.new(server, @suri).run
27
- end
28
-
29
- sleep 1
30
- builder = DIY::Builder.new do
31
- pcapfiles "helper/http.pcap"
32
- use DIY::SimpleStrategy.new
33
- timeout 10
34
- end
35
- builder.run
36
- end
37
- end
@@ -0,0 +1,85 @@
1
+ require 'spec_helper'
2
+ require 'diy/device_finder'
3
+
4
+ def raise_me
5
+ raise "Error on me"
6
+ end
7
+
8
+ describe "Controller" do
9
+
10
+ before(:all) do
11
+ @device_name = DIY::DeviceFinder.smart_select
12
+ @live = DIY::Live.new(@device_name)
13
+ @live2 = DIY::Live.new(@device_name)
14
+
15
+ @curi = "druby://localhost:7878"
16
+ @suri = "druby://localhost:7879"
17
+
18
+ # client create
19
+ Thread.abort_on_exception = true
20
+ @client_t ||= Thread.new do
21
+ client = DIY::Worker.new(@live)
22
+ DIY::WorkerKeeper.new(client, @curi).run
23
+ end
24
+
25
+ @server_t ||= Thread.new do
26
+ server = DIY::Worker.new(@live2)
27
+ DIY::WorkerKeeper.new(server, @suri).run
28
+ end
29
+ end
30
+
31
+ after(:all) do
32
+ @client_t.kill
33
+ @server_t.kill
34
+ end
35
+
36
+ it "#run " do
37
+ sleep 1
38
+ builder = DIY::Builder.new do
39
+ pcapfiles "helper/http.pcap"
40
+ use DIY::SimpleStrategy.new
41
+ timeout 10
42
+ end
43
+ lambda { builder.run }.should_not raise_error
44
+ end
45
+
46
+ it "#run stragety error" do
47
+
48
+
49
+ wrongUserStragety = lambda {
50
+ raise_me
51
+ #~ raise "error one me"
52
+ }
53
+
54
+ sleep 1
55
+ builder = DIY::Builder.new do
56
+ pcapfiles "helper/http.pcap"
57
+ use wrongUserStragety
58
+ timeout 10
59
+ end
60
+ lambda { builder.run }.should_not raise_error
61
+ end
62
+
63
+ it "#run before_send error" do
64
+ sleep 1
65
+ build2 = DIY::Builder.new do
66
+ before_send do
67
+ raise "error on me"
68
+ end
69
+ pcapfiles "helper/http.pcap"
70
+ end
71
+ lambda { build2.run }.should_not raise_error
72
+ end
73
+
74
+ it "#run big packet " do
75
+ sleep 1
76
+ build2 = DIY::Builder.new do
77
+ before_send do |pkt|
78
+ new_pkt = "a" * 10000
79
+ end
80
+ pcapfiles "helper/http.pcap"
81
+ end
82
+ lambda { build2.run }.should_not raise_error
83
+ end
84
+
85
+ end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 2
8
- - 6
9
- version: 0.2.6
8
+ - 7
9
+ version: 0.2.7
10
10
  platform: ruby
11
11
  authors:
12
12
  - yafei Lee
@@ -141,6 +141,7 @@ files:
141
141
  - spec/mu_parser_spec.rb
142
142
  - spec/offline_spec.rb
143
143
  - spec/spec_helper.rb
144
+ - spec/usererror_spec.rb
144
145
  - spec/utils_spec.rb
145
146
  - spec/worker_spec.rb
146
147
  has_rdoc: true