Class: Shatter::Controller
- Inherits:
-
Object
- Object
- Shatter::Controller
- Defined in:
- lib/shatter/controller.rb
Instance Attribute Summary collapse
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
Instance Method Summary collapse
-
#initialize(parent, key) ⇒ Controller
constructor
Public: Initialize the Controller, setting up the listening Socket and thread to manage the mailbox.
-
#pid ⇒ Object
Public: Return the Pid representing the Controller, initializing the struct if necessary.
-
#receive(&block) ⇒ Object
Public: Pass items in the current mailbox to a given block, removing them permanently from the mailbox unless the item doesn’t match any pattern in the block.
Constructor Details
#initialize(parent, key) ⇒ Controller
Public: Initialize the Controller, setting up the listening Socket and thread to manage the mailbox.
parent - Pid of the parent Controller, if applicable. key - String containing the shared secret for an RbNaCl SecretBox.
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/shatter/controller.rb', line 15 def initialize(parent, key) @parent = parent @box = RbNaCl::SecretBox.new(key) @socket = listen @mailbox = Queue.new @known = Shatter::Pidlist.new @chunks = {} pass(@parent, :system, [:childpid, pid]) if @parent Thread.new { mailbox_loop(key) } end |
Instance Attribute Details
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
8 9 10 |
# File 'lib/shatter/controller.rb', line 8 def parent @parent end |
Instance Method Details
#pid ⇒ Object
Public: Return the Pid representing the Controller, initializing the struct if necessary.
Returns a Pid.
47 48 49 50 51 52 53 |
# File 'lib/shatter/controller.rb', line 47 def pid unless @pid _, port, _, ip = @socket.addr.map(&:freeze) @pid = Shatter::Pid.new($$, ip, port, @box, '') end @pid end |
#receive(&block) ⇒ Object
Public: Pass items in the current mailbox to a given block, removing them permanently from the mailbox unless the item doesn’t match any pattern in the block.
Returns nothing.
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/shatter/controller.rb', line 32 def receive(&block) newbox = Queue.new item = @mailbox.pop instance_exec { block.(item) } rescue NoMatch newbox << item retry ensure restore_mailbox(@mailbox, newbox, nil) end |