Class: DIY::Controller
- Inherits:
-
Object
- Object
- DIY::Controller
- Defined in:
- lib/diy/controller.rb
Instance Method Summary collapse
- #before_send(&block) ⇒ Object
- #client_send(client, pkts) ⇒ Object
-
#initialize(client, server, offline, strategy) ⇒ Controller
constructor
A new instance of Controller.
- #one_round(client, server, pkts) ⇒ Object
- #run ⇒ Object
- #stats_result(cost_time, fail_count) ⇒ Object
- #timeout(timeout) ⇒ Object
- #wait_recv_ok(pkts) ⇒ Object
- #wait_until(timeout = 10, &block) ⇒ Object
Constructor Details
#initialize(client, server, offline, strategy) ⇒ Controller
Returns a new instance of Controller.
6 7 8 9 10 11 12 13 |
# File 'lib/diy/controller.rb', line 6 def initialize( client, server, offline, strategy) @client = client @server = server @offline = offline @strategy = strategy @before_send = nil @timeout = nil end |
Instance Method Details
#before_send(&block) ⇒ Object
103 104 105 |
# File 'lib/diy/controller.rb', line 103 def before_send(&block) @before_send = block end |
#client_send(client, pkts) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/diy/controller.rb', line 79 def client_send(client, pkts) if @before_send pkts = pkts.collect do |pkt| content = pkt.content begin pkt.content = @before_send.call(content) rescue Exception => e DIY::Logger.warn("UserError Catch: " + error = BeforeSendCallError.new(e) ) error.backtrace.each do |msg| DIY::Logger.info(msg) end raise error end pkt end end begin client.inject(pkts) rescue FFI::PCap::LibError =>e DIY::Logger.warn("SendPacketError Catch: " + e ) raise e end end |
#one_round(client, server, pkts) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/diy/controller.rb', line 53 def one_round( client, server, pkts ) @error_flag = nil @round_count = 0 unless @round_count @round_count += 1 DIY::Logger.info "round #{@round_count}: (c:#{client.__drburi} / s:#{server.__drburi}) #{pkts[0].pretty_print}:(queue= #{pkts.size})" if pkts.size >= 10 DIY::Logger.warn "queue size too big: #{pkts.size}, maybe something error" end server.ready do |recv_pkt| next if @error_flag # error accur waiting other thread do with it recv_pkt = Packet.new(recv_pkt) begin @strategy.call(pkts.first, recv_pkt, pkts) rescue DIY::UserError =>e DIY::Logger.warn("UserError Catch: " + e.inspect) e.backtrace.each do |msg| DIY::Logger.info(msg) end @error_flag = e end end client_send(client, pkts) wait_recv_ok(pkts) server.terminal end |
#run ⇒ Object
15 16 17 18 19 20 21 22 23 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 |
# File 'lib/diy/controller.rb', line 15 def run client = @client server = @server @fail_count = 0 start_time = Time.now #clear client.terminal server.terminal loop do begin pkts = @offline.nexts one_round( client, server, pkts ) client, server = server, client rescue HopePacketTimeoutError, UserError, FFI::PCap::LibError => e DIY::Logger.warn( "Timeout: Hope packet is #{pkts[0].pretty_print} ") if e.kind_of?(HopePacketTimeoutError) @fail_count += 1 begin @offline.next_pcap server.terminal rescue EOFError client.terminal server.terminal break end client,server = @client, @server rescue EOFError client.terminal server.terminal break end end DRb.stop_service end_time = Time.now stats_result( end_time - start_time, @fail_count ) end |
#stats_result(cost_time, fail_count) ⇒ Object
111 112 113 114 |
# File 'lib/diy/controller.rb', line 111 def stats_result( cost_time, fail_count ) DIY::Logger.info " ====== Finished in #{cost_time} seconds" DIY::Logger.info " ====== Total fail_count: #{fail_count} failures" end |
#timeout(timeout) ⇒ Object
107 108 109 |
# File 'lib/diy/controller.rb', line 107 def timeout(timeout) @timeout = timeout end |
#wait_recv_ok(pkts) ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/diy/controller.rb', line 116 def wait_recv_ok(pkts) wait_until(@timeout ||= 10) do if @error_flag raise @error_flag end pkts.empty? end end |
#wait_until(timeout = 10, &block) ⇒ Object
125 126 127 128 129 130 131 132 |
# File 'lib/diy/controller.rb', line 125 def wait_until( timeout = 10, &block ) Timeout.timeout(timeout, DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{timeout} seconds") ) do loop do break if block.call sleep 0.1 end end end |