Skip to content

Commit

Permalink
Merge pull request python-smpplib#6 from jakob-o/feature/sequence_str…
Browse files Browse the repository at this point in the history
…ategy

Feature/sequence strategy
  • Loading branch information
Konstantin Podshumok committed Jul 29, 2014
2 parents 9a079a4 + 096f2cb commit 7929867
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ python-libsmpp

SMPP library for Python. Forked from [google code](https://code.google.com/p/smpplib/).


Example:
```python
import logging
Expand Down Expand Up @@ -53,5 +52,15 @@ 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.

Example:
```python
import smpplib.client

import mymodule

generator = mymodule.PersistentSequenceGenerator()
client = smpplib.client.Client('example.com', SOMEPORTNUMBER, sequence_generator=generator)
...
```
32 changes: 30 additions & 2 deletions smpplib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,24 @@

logger = logging.getLogger('smpplib.client')

class SimpleSequenceGenerator(object):

MIN_SEQUENCE = 0x00000001
MAX_SEQUENCE = 0x7FFFFFFF

def __init__(self):
self._sequence = self.MIN_SEQUENCE

@property
def sequence(self):
return self._sequence

def next_sequence(self):
if self._sequence == self.MAX_SEQUENCE:
self._sequence = self.MIN_SEQUENCE
else:
self._sequence += 1
return self._sequence

class Client(object):
"""SMPP client class"""
Expand All @@ -43,16 +61,19 @@ class Client(object):
port = None
vendor = None
_socket = None
sequence = 1
sequence_generator = None

def __init__(self, host, port, timeout=5):
def __init__(self, host, port, timeout=5, sequence_generator=None):
"""Initialize"""

self.host = host
self.port = int(port)
self._socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._socket.settimeout(timeout)
self.receiver_mode = False
if sequence_generator is None:
sequence_generator = SimpleSequenceGenerator()
self.sequence_generator = sequence_generator

def __del__(self):
"""Disconnect when client object is destroyed"""
Expand All @@ -66,6 +87,13 @@ def __del__(self):
logger.warning('%s. Ignored', e)
self.disconnect()

@property
def sequence(self):
return self.sequence_generator.sequence

def next_sequence(self):
return self.sequence_generator.next_sequence()

def connect(self):
"""Connect to SMSC"""

Expand Down
3 changes: 1 addition & 2 deletions smpplib/pdu.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,7 @@ def _set_sequence(self, sequence):

def _next_seq(self):
"""Return next sequence number"""
self._client.sequence += 1
return self._client.sequence
return self._client.next_sequence()

def is_vendor(self):
"""Return True if this is a vendor PDU, False otherwise"""
Expand Down

0 comments on commit 7929867

Please sign in to comment.