Class: DIY::Worker
Instance Method Summary collapse
-
#filter(reg) ⇒ Object
过滤器.
-
#initialize(live) ⇒ Worker
constructor
A new instance of Worker.
-
#inject(pkts) ⇒ Object
发包.
- #inspect ⇒ Object
- #loop_callback ⇒ Object
- #loop_recv ⇒ Object
-
#ready(&block) ⇒ Object
收包.
-
#stop ⇒ Object
停止线程.
-
#terminal ⇒ Object
停止收发.
Constructor Details
#initialize(live) ⇒ Worker
Returns a new instance of Worker.
11 12 13 14 15 16 17 18 19 |
# File 'lib/diy/worker.rb', line 11 def initialize(live) @live = live @recv_t = nil @start = false @queue = Queue.new @running = false loop_recv loop_callback end |
Instance Method Details
#filter(reg) ⇒ Object
过滤器
112 113 114 |
# File 'lib/diy/worker.rb', line 112 def filter(reg) @live.set_filter(reg) end |
#inject(pkts) ⇒ Object
发包
22 23 24 25 26 27 |
# File 'lib/diy/worker.rb', line 22 def inject(pkts) pkts.each do |pkt| DIY::Logger.info "send pkt: #{pkt.pretty_print}" @live.send_packet(pkt.content) end end |
#inspect ⇒ Object
116 117 118 |
# File 'lib/diy/worker.rb', line 116 def inspect "<Worker: #{@live.net}>" end |
#loop_callback ⇒ Object
51 52 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 78 79 80 81 82 83 84 85 86 |
# File 'lib/diy/worker.rb', line 51 def loop_callback @running = true @callback_t = Thread.new do #~ DIY::Logger.info "start thread callbacking pkt..." while @running do if ! @start DIY::Logger.debug "callback stop..." unless @callback_stop_flag @callback_stop_flag = true sleep 0.01 next end if @start DIY::Logger.debug "callback start..." if @callback_stop_flag @callback_stop_flag = false end begin pkt = @queue.pop #~ DIY::Logger.info "callback: #{pkt}" if @block and pkt @block.call(pkt) end rescue DRb::DRbConnError DIY::Logger.info "closed connection by controller" @start = false @queue.clear rescue RangeError=>e DIY::Utils.print_backtrace(e) raise e end end DIY::Logger.debug "stopped loop callback" end end |
#loop_recv ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/diy/worker.rb', line 29 def loop_recv @recv_t = Thread.new do DIY::Logger.info "start thread recving pkt..." @live.loop do |this, pkt| if ! @start DIY::Logger.debug "looprecv stop..." unless @recv_stop_flag @recv_stop_flag = true next end if @start DIY::Logger.debug "looprecv start..." if @recv_stop_flag @recv_stop_flag = false end next unless pkt @queue.push(pkt.body) end DIY::Logger.debug "worker: stopped loop recv" end end |
#ready(&block) ⇒ Object
收包
89 90 91 92 93 94 |
# File 'lib/diy/worker.rb', line 89 def ready(&block) stopping DIY::Logger.info("start recv pkt") @block = block start end |
#stop ⇒ Object
停止线程
103 104 105 106 107 108 109 |
# File 'lib/diy/worker.rb', line 103 def stop @running = false pause @live.break Utils.wait_until { @recv_t && ! @recv_t.alive? } Utils.wait_until { @callback_t && ! @callback_t.alive? } end |