Class: Punchblock::Component::ComponentNode

Inherits:
Punchblock::CommandNode show all
Includes:
HasGuardedHandlers
Defined in:
lib/punchblock/component/component_node.rb

Instance Attribute Summary

Attributes inherited from RayoNode

#client, #connection, #original_component

Instance Method Summary collapse

Methods inherited from Punchblock::CommandNode

#response

Methods inherited from RayoNode

#==, class_from_registration, from_xml, #inherit, #inspect, #rayo_attributes, #rayo_children, register, #source, #to_rayo, #to_xml

Constructor Details

#initialize(*args) ⇒ ComponentNode

Returns a new instance of ComponentNode.



8
9
10
11
12
13
# File 'lib/punchblock/component/component_node.rb', line 8

def initialize(*args)
  super
  @complete_event_resource = FutureResource.new
  @mutex = Mutex.new
  register_internal_handlers
end

Instance Method Details

#add_event(event) ⇒ Object



22
23
24
# File 'lib/punchblock/component/component_node.rb', line 22

def add_event(event)
  trigger_handler :internal, event
end

#complete_event(timeout = nil) ⇒ Object



50
51
52
# File 'lib/punchblock/component/component_node.rb', line 50

def complete_event(timeout = nil)
  @complete_event_resource.resource timeout
end

#complete_event=(other) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/punchblock/component/component_node.rb', line 54

def complete_event=(other)
  @mutex.synchronize do
    return if @complete_event_resource.set_yet?
    client.delete_component_registration self if client
    complete!
    @complete_event_resource.resource = other
  end
rescue StateMachine::InvalidTransition => e
  e.message << " for component #{self}"
  raise e
end

#register_event_handler(*guards, &block) ⇒ Object



30
31
32
# File 'lib/punchblock/component/component_node.rb', line 30

def register_event_handler(*guards, &block)
  register_handler :event, *guards, &block
end

#register_internal_handlersObject



15
16
17
18
19
20
# File 'lib/punchblock/component/component_node.rb', line 15

def register_internal_handlers
  register_handler :internal, Event::Complete do |event|
    self.complete_event = event
    throw :pass
  end
end

#response=(other) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/punchblock/component/component_node.rb', line 39

def response=(other)
  @mutex.synchronize do
    if other.is_a?(Ref)
      @component_id = other.component_id
      @source_uri = other.uri.to_s
      client.register_component self if client
    end
    super
  end
end

#stop!(options = {}) ⇒ Object

Sends a Rayo stop message for the current component

Raises:



78
79
80
81
# File 'lib/punchblock/component/component_node.rb', line 78

def stop!(options = {})
  raise InvalidActionError, "Cannot stop a #{self.class.name.split("::").last} that is #{state}" unless executing?
  stop_action.tap { |action| write_action action }
end

#stop_actionStop

Create a Rayo stop message

Returns:

  • (Stop)

    a Rayo stop message



71
72
73
# File 'lib/punchblock/component/component_node.rb', line 71

def stop_action
  Stop.new :component_id => component_id, :target_call_id => target_call_id
end

#trigger_event_handler(event) ⇒ Object



26
27
28
# File 'lib/punchblock/component/component_node.rb', line 26

def trigger_event_handler(event)
  trigger_handler :event, event
end

#write_action(action) ⇒ Object



34
35
36
37
# File 'lib/punchblock/component/component_node.rb', line 34

def write_action(action)
  client.execute_command action, :target_call_id => target_call_id, :component_id => component_id
  action
end