Skip to content

Commit f85f2b8

Browse files
author
Konstantin Podshumok
authored
Merge pull request #31 from pespin/pespin/poll
client: Add read_once method
2 parents db70fd2 + d27f070 commit f85f2b8

1 file changed

Lines changed: 51 additions & 38 deletions

File tree

smpplib/client.py

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"""SMPP client module"""
2424

2525
import socket
26+
import select
2627
import struct
2728
import binascii
2829
import logging
@@ -270,47 +271,59 @@ def message_sent_handler(pdu, **kwargs):
270271
May be overridden"""
271272
logger.warning('Message sent handler (Override me)')
272273

274+
275+
def read_once(self, ignore_error_codes=None):
276+
"""Read a PDU and act"""
277+
try:
278+
try:
279+
p = self.read_pdu()
280+
except socket.timeout:
281+
logger.debug('Socket timeout, listening again')
282+
p = smpp.make_pdu('enquire_link', client=self)
283+
self.send_pdu(p)
284+
return
285+
286+
if p.is_error():
287+
raise exceptions.PDUError(
288+
'({}) {}: {}'.format(p.status, p.command,
289+
consts.DESCRIPTIONS.get(p.status, 'Unknown status')), int(p.status))
290+
291+
if p.command == 'unbind': # unbind_res
292+
logger.info('Unbind command received')
293+
return
294+
elif p.command == 'submit_sm_resp':
295+
self.message_sent_handler(pdu=p)
296+
elif p.command == 'deliver_sm':
297+
self._message_received(p)
298+
elif p.command == 'enquire_link':
299+
self._enquire_link_received()
300+
elif p.command == 'enquire_link_resp':
301+
pass
302+
elif p.command == 'alert_notification':
303+
self._alert_notification(p)
304+
else:
305+
logger.warning('Unhandled SMPP command "%s"', p.command)
306+
except exceptions.PDUError as e:
307+
if ignore_error_codes \
308+
and len(e.args) > 1 \
309+
and e.args[1] in ignore_error_codes:
310+
logging.warning('(%d) %s. Ignored.' %
311+
(e.args[1], e.args[0]))
312+
else:
313+
raise
314+
315+
def poll(self, ignore_error_codes=None):
316+
'''Act on available PDUs and return'''
317+
while True:
318+
readable, writable, exceptional = select.select([self._socket], [], [], 0)
319+
if not readable:
320+
break
321+
self.read_once(ignore_error_codes)
322+
273323
def listen(self, ignore_error_codes=None):
274324
"""Listen for PDUs and act"""
275-
276325
while True:
277-
try:
278-
try:
279-
p = self.read_pdu()
280-
except socket.timeout:
281-
logger.debug('Socket timeout, listening again')
282-
p = smpp.make_pdu('enquire_link', client=self)
283-
self.send_pdu(p)
284-
continue
285-
286-
if p.is_error():
287-
raise exceptions.PDUError(
288-
'({}) {}: {}'.format(p.status, p.command,
289-
consts.DESCRIPTIONS.get(p.status, 'Unknown status')), int(p.status))
290-
291-
if p.command == 'unbind': # unbind_res
292-
logger.info('Unbind command received')
293-
break
294-
elif p.command == 'submit_sm_resp':
295-
self.message_sent_handler(pdu=p)
296-
elif p.command == 'deliver_sm':
297-
self._message_received(p)
298-
elif p.command == 'enquire_link':
299-
self._enquire_link_received()
300-
elif p.command == 'enquire_link_resp':
301-
pass
302-
elif p.command == 'alert_notification':
303-
self._alert_notification(p)
304-
else:
305-
logger.warning('Unhandled SMPP command "%s"', p.command)
306-
except exceptions.PDUError, e:
307-
if ignore_error_codes \
308-
and len(e.args) > 1 \
309-
and e.args[1] in ignore_error_codes:
310-
logging.warning('(%d) %s. Ignored.' %
311-
(e.args[1], e.args[0]))
312-
else:
313-
raise
326+
self.read_once(ignore_error_codes)
314327

315328
def send_message(self, **kwargs):
316329
"""Send message

0 commit comments

Comments
 (0)