Skip to content

Commit

Permalink
Merge pull request #8 from vitorjoaofg/master
Browse files Browse the repository at this point in the history
Adding support to receive Log events
  • Loading branch information
italorossi authored Dec 29, 2016
2 parents b8c7770 + 18610b8 commit 9352ca8
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
9 changes: 8 additions & 1 deletion greenswitch/esl.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ def handle_event(self, event):
else:
length = int(event.headers['Content-Length'])
data = self._read_socket(self.sock_file, length)
event.parse_data(data)
if event.headers.get('Content-Type') == 'log/data':
event.data = data
else:
event.parse_data(data)
self._esl_event_queue.put(event)

def _safe_exec_handler(self, handler, event):
Expand All @@ -148,6 +151,10 @@ 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 not handlers and event.headers.get('Content-Type') == 'log/data':
handlers = self.event_handlers.get('log')

if not handlers:
continue

Expand Down
5 changes: 5 additions & 0 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ def send_fake_event_plain(self, data):
self.switch_esl.fake_event_plain(data.encode('utf-8'))
gevent.sleep(0.1)

def send_fake_raw_event_plain(self, data):
self.switch_esl.fake_raw_event_plain(data.encode('utf-8'))
gevent.sleep(0.1)


def send_batch_fake_event_plain(self, events):
for event in events:
self.send_fake_event_plain(event)
Expand Down
3 changes: 3 additions & 0 deletions tests/fakeeslserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ def fake_event_plain(self, data):
'Content-Length: %s' % len(data)])
self._client_socket.send(data)

def fake_raw_event_plain(self, data):
self._client_socket.send(data)

def disconnect(self):
self.protocol_send(['Content-Type: text/disconnect-notice',
'Content-Length: 67'])
Expand Down
22 changes: 22 additions & 0 deletions tests/test_lib_esl.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,28 @@ def on_heartbeat(self, event):
self.send_fake_event_plain(event_plain)
self.assertTrue(self.esl.heartbeat)

def test_event_socket_data(self):
"""Should call registered handler for events."""
self.log = False

def on_log(event):
self.log = True
self.esl.register_handle('log', on_log)
event_plain = dedent("""\
Content-Type: log/data
Content-Length: 126
Log-Level: 7
Text-Channel: 3
Log-File: switch_core_state_machine.c
Log-Func: switch_core_session_destroy_state
Log-Line: 710
User-Data: 4c882cc4-cd02-11e6-8b82-395b501876f9
2016-12-28 10:34:08.398763 [DEBUG] switch_core_state_machine.c:710 (sofia/internal/7071@devitor) State DESTROY going to sleep
""")
self.send_fake_raw_event_plain(event_plain)
self.assertTrue(self.log)

def test_event_with_multiline_channel_variables_content(self):
"""Should not break parse from ESL Event when."""
def on_channel_create(self, event):
Expand Down

0 comments on commit 9352ca8

Please sign in to comment.