Class: Adhearsion::PunchblockPlugin::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/adhearsion/punchblock_plugin/initializer.rb

Class Method Summary collapse

Class Method Details

.connectObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 79

def connect
  return unless Process.state_name == :booting
  m = Mutex.new
  blocker = ConditionVariable.new

  Events.punchblock Punchblock::Connection::Connected do
    Adhearsion::Process.booted
    m.synchronize { blocker.broadcast }
  end

  Events.shutdown do
    logger.info "Shutting down while connecting. Breaking the connection block."
    m.synchronize { blocker.broadcast }
  end

  Adhearsion::Process.important_threads << Thread.new do
    catching_standard_errors { connect_to_server }
  end

  # Wait for the connection to establish
  m.synchronize { blocker.wait m }

  throw :boot_aborted if self.attempts >= self.config.reconnect_attempts
end

.connect_to_serverObject



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 104

def connect_to_server
  logger.info "Starting connection to server"
  client.run
rescue Punchblock::DisconnectedError => e
  # We only care about disconnects if the process is up or booting
  return unless [:booting, :running].include? Adhearsion::Process.state_name

  Adhearsion::Process.reset unless Adhearsion::Process.state_name == :booting

  self.attempts += 1

  if self.attempts >= self.config.reconnect_attempts
    logger.fatal "Connection lost. Connection retry attempts exceeded."
    Adhearsion::Process.stop!
    return
  end

  logger.error "Connection lost. Attempting reconnect #{self.attempts} of #{self.config.reconnect_attempts}"
  sleep self.config.reconnect_timer
  retry
rescue Punchblock::ProtocolError => e
  logger.fatal "The connection failed due to a protocol error: #{e.name}."
  raise e
end

.connectionObject



172
173
174
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 172

def connection
  client.connection
end

.dispatch_call_event(event) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 145

def dispatch_call_event(event)
  call = Adhearsion.active_calls[event.target_call_id]
  if call && call.alive?
    call.async.deliver_message event
  else
    logger.warn "Event received for inactive call #{event.target_call_id}: #{event.inspect}"
  end
end

.dispatch_offer(offer) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 129

def dispatch_offer(offer)
  catching_standard_errors do
    call = Call.new(offer)
    Adhearsion.active_calls << call
    case Adhearsion::Process.state_name
    when :booting, :rejecting
      logger.info "Declining call because the process is not yet running."
      call.reject :decline
    when :running, :stopping
      Adhearsion.router.handle call
    else
      call.reject :error
    end
  end
end

.handle_event(event) ⇒ Object



154
155
156
157
158
159
160
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 154

def handle_event(event)
  Events.trigger :punchblock, event
  case event
  when Punchblock::Event::Asterisk::AMI::Event
    Events.trigger :ami, event
  end
end

.initObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 14

def init
  self.config = Adhearsion.config[:punchblock]

  username = self.config.username
  if (self.config.platform || :xmpp) == :xmpp
    username = Blather::JID.new username
    username = Blather::JID.new username.node, username.domain, resource unless username.resource
    username = username.to_s
  end

  connection_options = {
    :username           => username,
    :password           => self.config.password,
    :connection_timeout => self.config.connection_timeout,
    :host               => self.config.host,
    :port               => self.config.port,
    :certs              => self.config.certs_directory,
    :root_domain        => self.config.root_domain
  }

  self.client = Punchblock.client_with_connection self.config.platform, connection_options

  # Tell the Punchblock connection that we are ready to process calls.
  Events.register_callback :after_initialized do
    connection.ready!
  end

  # When quiescence is requested, change our status to "Do Not Disturb"
  # This should prevent the telephony engine from sending us any new calls.
  Events.register_callback :quiesced do
    connection.not_ready! if connection.connected?
  end

  # Make sure we stop everything when we shutdown
  Events.register_callback :shutdown do
    client.stop
  end

  # Handle events from Punchblock via events system
  self.client.register_event_handler do |event|
    handle_event event
  end

  Events.punchblock Punchblock::Connection::Connected do |event|
    logger.info "Connected to Punchblock server"
    self.attempts = 0
  end

  Events.punchblock Punchblock::Event::Offer do |offer|
    dispatch_offer offer
  end

  Events.punchblock proc { |e| e.respond_to?(:source) }, :source do |event|
    event.source.trigger_event_handler event
  end

  Events.punchblock proc { |e| e.respond_to?(:target_call_id) }, :target_call_id do |event|
    dispatch_call_event event
  end
end

.machine_identifierObject



166
167
168
169
170
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 166

def machine_identifier
  Adhearsion::Process.fqdn
rescue SocketError
  Socket.gethostname
end

.resourceObject



162
163
164
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 162

def resource
  [machine_identifier, ::Process.pid].join '-'
end

.runObject



75
76
77
# File 'lib/adhearsion/punchblock_plugin/initializer.rb', line 75

def run
  connect
end