Class: AlWorker::BroadcastMessage
- Inherits:
-
Object
- Object
- AlWorker::BroadcastMessage
- Defined in:
- lib/al_worker_message.rb
Overview
Broadcast message
(note) スレッド間メッセージングシステム。1:nメッセージを実現する。(通常の1:1メッセージであれば、Ruby標準のQueueで十分。)
Instance Attribute Summary collapse
-
#threads ⇒ Hash <Thread,Queue>
readonly
スレッドとメッセージキュー.
Instance Method Summary collapse
-
#attach ⇒ Object
ブロードキャストメッセージ受信予約.
-
#detach ⇒ Object
ブロードキャストメッセージ受信予約解除.
-
#empty? ⇒ Boolean
メッセージがあるか問い合わせ.
-
#initialize ⇒ BroadcastMessage
constructor
constructor.
-
#receive ⇒ Object
メッセージ受信.
-
#send(msg) ⇒ Object
メッセージ送信.
Constructor Details
#initialize ⇒ BroadcastMessage
constructor
33 34 35 |
# File 'lib/al_worker_message.rb', line 33 def initialize() @threads = {} end |
Instance Attribute Details
#threads ⇒ Hash <Thread,Queue> (readonly)
Returns スレッドとメッセージキュー.
27 28 29 |
# File 'lib/al_worker_message.rb', line 27 def threads @threads end |
Instance Method Details
#attach ⇒ Object
ブロードキャストメッセージ受信予約
41 42 43 44 |
# File 'lib/al_worker_message.rb', line 41 def attach() AlWorker.log("Attach #{Thread.current.object_id}", :debug, "BCM" ) @threads[ Thread.current.object_id ] = [ Thread.current, Queue.new() ] end |
#detach ⇒ Object
ブロードキャストメッセージ受信予約解除
50 51 52 53 |
# File 'lib/al_worker_message.rb', line 50 def detach() AlWorker.log("Detach #{Thread.current.object_id}", :debug, "BCM" ) @threads.delete( Thread.current.object_id ) end |
#empty? ⇒ Boolean
メッセージがあるか問い合わせ
92 93 94 95 96 |
# File 'lib/al_worker_message.rb', line 92 def empty?() @threads[ Thread.current.object_id ][1].empty?() rescue NoMethodError raise "Maybe used empty?() without attach()." end |
#receive ⇒ Object
Note:
メッセージ受信
メッセージがなければ、送られるまで停止する。
80 81 82 83 84 |
# File 'lib/al_worker_message.rb', line 80 def receive() @threads[ Thread.current.object_id ][1].pop() rescue NoMethodError raise "Maybe used receive() without attach()." end |