|
23 | 23 | """SMPP client module""" |
24 | 24 |
|
25 | 25 | import socket |
| 26 | +import select |
26 | 27 | import struct |
27 | 28 | import binascii |
28 | 29 | import logging |
@@ -270,47 +271,59 @@ def message_sent_handler(pdu, **kwargs): |
270 | 271 | May be overridden""" |
271 | 272 | logger.warning('Message sent handler (Override me)') |
272 | 273 |
|
| 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 | + |
273 | 323 | def listen(self, ignore_error_codes=None): |
274 | 324 | """Listen for PDUs and act""" |
275 | | - |
276 | 325 | 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) |
314 | 327 |
|
315 | 328 | def send_message(self, **kwargs): |
316 | 329 | """Send message |
|
0 commit comments