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
过滤器
83 84 85 |
# File 'lib/diy/worker.rb', line 83 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
87 88 89 |
# File 'lib/diy/worker.rb', line 87 def inspect "<Worker: #{@live.net}>" end |
#loop_callback ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/diy/worker.rb', line 40 def loop_callback @running = true @callback_t = Thread.new do #~ DIY::Logger.info "start thread callbacking pkt..." while @running do begin pkt = @queue.pop #~ DIY::Logger.info "callback: #{pkt}" @block.call(pkt) if @block rescue DRb::DRbConnError DIY::Logger.info "closed connection by controller" @queue.clear end end DIY::Logger.debug "stopped loop callback" end end |
#loop_recv ⇒ Object
29 30 31 32 33 34 35 36 37 38 |
# 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| next unless @start @queue.push(pkt.body) end DIY::Logger.debug "worker: stopped loop recv" end end |
#ready(&block) ⇒ Object
收包
59 60 61 62 63 64 |
# File 'lib/diy/worker.rb', line 59 def ready(&block) DIY::Logger.info("start recv pkt") @block = block @queue.clear @start = true end |
#stop ⇒ Object
停止线程
74 75 76 77 78 79 80 |
# File 'lib/diy/worker.rb', line 74 def stop @running = false @queue.push nil @live.break Utils.wait_until { @recv_t && ! @recv_t.alive? } Utils.wait_until { @callback_t && ! @callback_t.alive? } end |