-
-
Notifications
You must be signed in to change notification settings - Fork 143
Implement Outstanding Operations management #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
796f5cf
b92fc5c
8b8e5d4
97f2d7f
2704e93
070c3bc
b1207ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,7 @@ | |
| import socket | ||
| import struct | ||
|
|
||
| from time import sleep | ||
| from smpplib import consts, exceptions, smpp | ||
|
|
||
|
|
||
|
|
@@ -61,12 +62,14 @@ class Client(object): | |
| vendor = None | ||
| _socket = None | ||
| sequence_generator = None | ||
|
|
||
| def __init__(self, host, port, timeout=5, sequence_generator=None, logger_name=None): | ||
| def __init__(self, host, port, timeout=5, sequence_generator=None, max_outstanding_operations=None, logger_name=None): | ||
| self.host = host | ||
| self.port = int(port) | ||
| self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | ||
| self.timeout = timeout | ||
| self.outstanding_operations = set() | ||
| self.max_outstanding_operations = max_outstanding_operations | ||
| self.logger = logging.getLogger(logger_name or 'smpp.Client.{}'.format(id(self))) | ||
| if sequence_generator is None: | ||
| sequence_generator = SimpleSequenceGenerator() | ||
|
|
@@ -167,18 +170,41 @@ def unbind(self): | |
| except socket.timeout: | ||
| raise exceptions.ConnectionError() | ||
|
|
||
| def send_pdu(self, p): | ||
| def manage_outstanding_operations(self, sending, pdu): | ||
| if not self.max_outstanding_operations: | ||
| return | ||
|
|
||
| if pdu.is_response(): | ||
| operation = '{}{}'.format('S' if sending else 'C', pdu.sequence) | ||
| try: | ||
| self.outstanding_operations.remove(operation) | ||
| self.logger.debug('Unregistering outstanding operation {}'.format(operation)) | ||
| except KeyError: | ||
| self.logger.warning('Failed to unregistered outstanding operation {}'.format(operation)) | ||
| else: | ||
| operation = '{}{}'.format('C' if sending else 'S', pdu.sequence) | ||
| if pdu.sequence in self.outstanding_operations: | ||
| self.logger.warning('Outstanding operation {} already registered'.format(operation)) | ||
| else: | ||
| self.outstanding_operations.add(operation) | ||
| self.logger.debug('Registering outstanding operation {}'.format(operation)) | ||
|
|
||
| def send_pdu(self, pdu): | ||
| """Send PDU to the SMSC""" | ||
|
|
||
| if self.state not in consts.COMMAND_STATES[p.command]: | ||
| if self.state not in consts.COMMAND_STATES[pdu.command]: | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for renaming that :) |
||
| raise exceptions.PDUError("Command %s failed: %s" % ( | ||
| p.command, | ||
| pdu.command, | ||
| consts.DESCRIPTIONS[consts.SMPP_ESME_RINVBNDSTS], | ||
| )) | ||
|
|
||
| self.logger.debug('Sending %s PDU', p.command) | ||
| if self.max_outstanding_operations and pdu.is_request(): | ||
| while len(self.outstanding_operations) >= self.max_outstanding_operations: | ||
| sleep(.1) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd probably use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never used events before, will take a look a that, thank you. |
||
|
|
||
| self.logger.debug('Sending %s PDU', pdu.command) | ||
|
|
||
| generated = p.generate() | ||
| generated = pdu.generate() | ||
|
|
||
| self.logger.debug('>>%s (%d bytes)', binascii.b2a_hex(generated), len(generated)) | ||
|
|
||
|
|
@@ -194,6 +220,8 @@ def send_pdu(self, p): | |
| raise exceptions.ConnectionError() | ||
| sent += sent_last | ||
|
|
||
| self.manage_outstanding_operations(sending=True, pdu=pdu) | ||
|
|
||
| return True | ||
|
|
||
| def read_pdu(self): | ||
|
|
@@ -227,6 +255,8 @@ def read_pdu(self): | |
|
|
||
| self.logger.debug('Read %s PDU', pdu.command) | ||
|
|
||
| self.manage_outstanding_operations(sending=False, pdu=pdu) | ||
|
|
||
| if pdu.is_error(): | ||
| return pdu | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated line which is used in both branches. Could you move it right above the
if?