diff --git a/README.md b/README.md index 6f6aa9d..369db89 100644 --- a/README.md +++ b/README.md @@ -63,16 +63,23 @@ t = Thread(target=client.listen) t.start() ``` -The client supports setting a custom generator that produces sequence numbers for the PDU packages. Per default a simple in memory generator is used which in conclusion is reset on (re)instantiation of the client, e.g. by an application restart. If you want to keep the sequence number to be persisted across restarts you can implement your own storage backed generator. +A simple in memory generator that produces sequence numbers for the PDU packages is used, which is reset on (re)instantiation of the client, e.g. by an application restart. It starts by default with sequence number 0x00000001 but you may set the starting sequence yourself by declaring the generator beforehand and passing it to the Client: +```python +import smpplib.client + +generator = smpplib.client.SimpleSequenceGenerator(start_sequence=1234) +client = smpplib.client.Client('example.com', SOMEPORTNUMBER, sequence_generator=generator) +... +``` -Example: +The client supports setting a custom generator, so if you need more/different features associated with it, you can implement your own: ```python import smpplib.client import mymodule -generator = mymodule.PersistentSequenceGenerator() +generator = mymodule.MyAwesomeSequenceGenerator() client = smpplib.client.Client('example.com', SOMEPORTNUMBER, sequence_generator=generator) ... ``` diff --git a/smpplib/client.py b/smpplib/client.py index 876f029..8780cb4 100644 --- a/smpplib/client.py +++ b/smpplib/client.py @@ -38,8 +38,19 @@ class SimpleSequenceGenerator(object): MIN_SEQUENCE = 0x00000001 MAX_SEQUENCE = 0x7FFFFFFF - def __init__(self): - self._sequence = self.MIN_SEQUENCE + def __init__(self, start_sequence=None): + if start_sequence is not None: + if not isinstance(start_sequence, int): + raise TypeError('start_sequence must be an int, {} provided'.format( + type(start_sequence) + )) + if start_sequence < self.MIN_SEQUENCE or start_sequence > self.MAX_SEQUENCE: + raise ValueError('start_sequence must be between {} and {}'.format( + self.MIN_SEQUENCE, self.MAX_SEQUENCE + )) + self._sequence = start_sequence + else: + self._sequence = self.MIN_SEQUENCE @property def sequence(self):