Skip to content

Commit

Permalink
fixes #34 - Properly detect the disconnect event on outboudn session …
Browse files Browse the repository at this point in the history
…and raise OutboundSessionHasGoneAway for pending commands.
  • Loading branch information
italorossi committed Sep 4, 2018
1 parent 339612f commit ebe791f
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion greenswitch/esl.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ def process_events(self):
handlers = self.event_handlers.get(event.headers.get('Event-Subclass'))
else:
handlers = self.event_handlers.get(event.headers.get('Event-Name'))

if event.headers.get('Content-Type') == 'text/disconnect-notice':
handlers = self.event_handlers.get('DISCONNECT')
if not handlers and event.headers.get('Content-Type') == 'log/data':
handlers = self.event_handlers.get('log')

Expand Down Expand Up @@ -262,6 +263,7 @@ def __init__(self, client_address, sock):
self.start_event_handlers()
self.register_handle('*', self.on_event)
self.register_handle('CHANNEL_HANGUP', self.on_hangup)
self.register_handle('DISCONNECT', self.on_disconnect)
self.expected_events = {}
self._outbound_connected = False

Expand All @@ -277,6 +279,18 @@ def call_uuid(self):
def caller_id_number(self):
return self.session_data.get('Caller-Caller-ID-Number')

def on_disconnect(self, event):
if self._lingering:
logging.debug('Socket lingering..')
elif not self.connected:
logging.debug('Socket closed: %s' % event.headers)
logging.debug('Raising OutboundSessionHasGoneAway for all pending'
'results')
for event_name in self.expected_events:
for variable, value, async_result in \
self.expected_events[event_name]:
async_result.set_exception(OutboundSessionHasGoneAway())

def on_hangup(self, event):
logging.info('Caller %s has gone away.' % self.caller_id_number)

Expand Down

0 comments on commit ebe791f

Please sign in to comment.