Class: Shatter::Controller

Inherits:
Object
  • Object
show all
Defined in:
lib/shatter/controller.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#parentObject (readonly)

Returns the value of attribute parent


8
9
10
# File 'lib/shatter/controller.rb', line 8

def parent
  @parent
end

Instance Method Details

#pidObject

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