Skip to content

Commit cb14741

Browse files
committed
1 parent cd04425 commit cb14741

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

asterisk/ami/client.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,16 @@ class AMIClient(object):
5252
asterisk_start_regex = re.compile('^Asterisk *Call *Manager/(?P<version>([0-9]+\.)*[0-9]+)', re.IGNORECASE)
5353
asterisk_line_regex = re.compile(b'\r\n', re.IGNORECASE | re.MULTILINE)
5454
asterisk_pack_regex = re.compile(b'\r\n\r\n', re.IGNORECASE | re.MULTILINE)
55+
# EventList start pattern
56+
asterisk_start_list_regex = re.compile(
57+
b'^Response: Success\r\nActionID: \d+\r\nEventList: start',
58+
re.IGNORECASE | re.MULTILINE
59+
)
60+
# EventList end pattern
61+
asterisk_end_list_regex = re.compile(
62+
b'EventList: Complete\r\n.+\r\n.+\r\n\r\n',
63+
re.IGNORECASE | re.MULTILINE
64+
)
5565

5666
def __init__(self, address='127.0.0.1', port=5038,
5767
encoding='utf-8', encoding_errors='replace',
@@ -160,9 +170,20 @@ def _next_pack(self):
160170
yield self._decode_pack(pack)
161171
break
162172
while not self.finished.is_set():
163-
while self.asterisk_pack_regex.search(data):
173+
is_list = self.asterisk_start_list_regex.match(data)
174+
list_ends = self.asterisk_end_list_regex.search(data)
175+
# If data has complete EventList even, get and yield it
176+
if is_list and list_ends:
177+
ending = list_ends.group()
178+
(pack, data) = self.asterisk_end_list_regex.split(data, 1)
179+
yield self._decode_pack(pack + ending)
180+
# If data has Event divider CLRFCLRF and it is not EventList
181+
# (has to starts with EventList start pattern
182+
# get anf yield it
183+
elif not is_list and self.asterisk_pack_regex.search(data):
164184
(pack, data) = self.asterisk_pack_regex.split(data, 1)
165185
yield self._decode_pack(pack)
186+
# Read the data
166187
recv = self._socket.recv(self._buffer_size)
167188
if recv == b'':
168189
self.finished.set()

0 commit comments

Comments
 (0)