Class: DIY::Controller
- Inherits:
-
Object
- Object
- DIY::Controller
- Defined in:
- lib/diy/controller.rb
Instance Method Summary collapse
- #before_send(&block) ⇒ Object
- #before_send_call(pkts, client_flag) ⇒ Object
- #client_send(client, pkts) ⇒ Object
- #do_trap ⇒ Object
- #error_on_stop ⇒ Object
-
#initialize(client, server, offline, strategy) ⇒ Controller
constructor
A new instance of Controller.
- #offline_result ⇒ Object
- #one_round(client, server, pkts) ⇒ Object
-
#recv_pkt_proc_set(queue) ⇒ Object
设置回调入口, 由 worker 通过DRb 远程调用.
- #run ⇒ Object
- #stats_result(cost_time, fail_count) ⇒ Object
- #stop ⇒ Object
- #timeout(timeout) ⇒ Object
- #wait_recv_ok(pkts) ⇒ Object
Constructor Details
#initialize(client, server, offline, strategy) ⇒ Controller
Returns a new instance of Controller.
6 7 8 9 10 11 12 13 14 15 |
# 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 @error_on_stop = nil @fail_count = 0 end |
Instance Method Details
#before_send(&block) ⇒ Object
152 153 154 |
# File 'lib/diy/controller.rb', line 152 def before_send(&block) @before_send = block end |
#before_send_call(pkts, client_flag) ⇒ Object
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 |
# File 'lib/diy/controller.rb', line 135 def before_send_call(pkts, client_flag) return unless @before_send pkts.delete_if do |pkt| begin ret = @before_send.call( pkt, client_flag ) 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 ! ret end pkts end |
#client_send(client, pkts) ⇒ Object
126 127 128 129 130 131 132 133 |
# File 'lib/diy/controller.rb', line 126 def client_send(client, pkts) begin client.inject(pkts) rescue FFI::PCap::LibError =>e DIY::Logger.info("SendPacketError Catch: " + e ) raise e end end |
#do_trap ⇒ Object
72 73 74 75 76 77 78 |
# File 'lib/diy/controller.rb', line 72 def do_trap Signal.trap("INT") do DIY::Logger.info "bye.." stop exit 0 end end |
#error_on_stop ⇒ Object
160 161 162 |
# File 'lib/diy/controller.rb', line 160 def error_on_stop(*) @error_on_stop = true end |
#offline_result ⇒ Object
169 170 171 |
# File 'lib/diy/controller.rb', line 169 def offline_result sprintf "%4d files, running %8d packets", @offline.files_size, @offline.now_size end |
#one_round(client, server, pkts) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/diy/controller.rb', line 85 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})" # check is client or server at this time # give the flag to **before_send** block client_or_server = client == @client before_send_call(pkts, client_or_server) recv_pkt_proc_set( pkts ) @ready = false server.ready(&@recv_pkt_proc) @ready = true client_send(client, pkts) wait_recv_ok(pkts) server.terminal end |
#recv_pkt_proc_set(queue) ⇒ Object
设置回调入口, 由 worker 通过DRb 远程调用
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/diy/controller.rb', line 107 def recv_pkt_proc_set(queue) @queue_keeper = queue # 不重新赋值, 防止 DRb 回收 @recv_pkt_proc ||= lambda do |recv_pkt| begin next unless @ready # strip this recv pkt unless it's ready to. next if @error_flag # error accur waiting other thread do with it @recv_pkt_keeper = Packet.new(recv_pkt) @strategy.call(@queue_keeper.first, @recv_pkt_keeper, @queue_keeper) 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 end |
#run ⇒ Object
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/diy/controller.rb', line 17 def run do_trap client = @client server = @server @fail_count = 0 start_time = Time.now #clear client.terminal server.terminal loop do begin pkts, where = @offline.nexts case where when :A client, server = @client, @server when :B client, server = @server, @client end one_round( client, server, pkts ) 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 if @error_on_stop and e.kind_of?(HopePacketTimeoutError) client.terminal server.terminal DIY::Logger.info "Error_on_stop flag opened, stopping..." DIY::Logger.info "Dump mac learn table(size is #{@offline.mac_learner.size})... " DIY::Logger.info @offline.mac_learner.dump break end begin @offline.next_pcap client.terminal server.terminal rescue EOFError client.terminal server.terminal break end #~ client,server = @client, @server rescue EOFError client.terminal server.terminal break ensure #~ client, server = server, client 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
164 165 166 167 |
# File 'lib/diy/controller.rb', line 164 def stats_result( cost_time, fail_count ) DIY::Logger.info " Finished in #{cost_time} seconds" DIY::Logger.info " #{offline_result}, #{fail_count} failures" end |
#stop ⇒ Object
80 81 82 83 |
# File 'lib/diy/controller.rb', line 80 def stop @client.terminal @server.terminal end |
#timeout(timeout) ⇒ Object
156 157 158 |
# File 'lib/diy/controller.rb', line 156 def timeout(timeout) @timeout = timeout end |
#wait_recv_ok(pkts) ⇒ Object
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/diy/controller.rb', line 173 def wait_recv_ok(pkts) @timeout ||= 10 tick = 0 until pkts.empty? do now_size = pkts.size raise @error_flag if @error_flag tick += 0.01 sleep 0.01 if pkts.size < now_size # clear tick time if at least one packet has passed the judgement of stratgies. tick = 0 end raise DIY::HopePacketTimeoutError.new("hope packet wait timeout after #{@timeout} seconds") if !pkts.empty? && tick >= @timeout end end |