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) ⇒ Controller

Public: Initialize the Controller, setting up the listening Socket and thread to manage the mailbox.

parent - Pid of the parent Controller, if applicable.



14
15
16
17
18
19
20
21
22
23
# File 'lib/shatter/controller.rb', line 14

def initialize(parent)
  @parent  = parent
  @socket  = listen
  @mailbox = Queue.new
  @known   = Shatter::Pidlist.new
  @chunks  = {}

  pass(@parent, :system, [:childpid, pid]) if @parent
  Thread.new { mailbox_loop }
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.



45
46
47
48
49
50
51
# File 'lib/shatter/controller.rb', line 45

def pid
  unless @pid
    _, port, _, ip = @socket.addr.map(&:freeze)
    @pid           = Shatter::Pid.new($$, ip, port, '')
  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.



30
31
32
33
34
35
36
37
38
39
# File 'lib/shatter/controller.rb', line 30

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