Skip to content

Commit

Permalink
don't grow sequence when it is not required
Browse files Browse the repository at this point in the history
+ pep8 a little
  • Loading branch information
Konstantin Podshumok committed May 6, 2013
1 parent 724c38b commit a4c2679
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 273 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
extra['use_2to3'] = True

setup(name="python-smpplib",
version='1.0.0',
version='1.0.1',
url='https://github.com/podshumok/python-smpplib',
description='SMPP library for python',
packages=find_packages(),
Expand Down
71 changes: 14 additions & 57 deletions smpplib/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import logging

from . import smpp
from . import pdu
from . import command
from . import exceptions
from . import consts

Expand All @@ -47,7 +45,6 @@ class Client(object):
_socket = None
sequence = 0


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

Expand All @@ -57,8 +54,8 @@ def __init__(self, host, port, timeout=5):
self._socket.settimeout(timeout)
self.receiver_mode = False


def __del__(self):
"""Disconnect when client object is destroyed"""
if self._socket is not None:
try:
self.unbind()
Expand All @@ -69,7 +66,6 @@ def __del__(self):
logger.warning('%s. Ignored', e)
self.disconnect()


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

Expand All @@ -83,7 +79,6 @@ def connect(self):
except socket.error:
raise exceptions.ConnectionError("Connection refused")


def disconnect(self):
"""Disconnect from the SMSC"""
logger.info('Disconnecting...')
Expand All @@ -92,7 +87,6 @@ def disconnect(self):
self.state = consts.SMPP_CLIENT_STATE_CLOSED
self._socket = None


def _bind(self, command_name, **kwargs):
"""Send bind_transmitter command to the SMSC"""

Expand All @@ -103,54 +97,46 @@ def _bind(self, command_name, **kwargs):
#smppinst = smpp.get_instance()
p = smpp.make_pdu(command_name, client=self, **kwargs)

res = self.send_pdu(p)
self.send_pdu(p)
return self.read_pdu()


def bind_transmitter(self, **kwargs):
"""Bind as a transmitter"""

return self._bind('bind_transmitter', **kwargs)


def bind_receiver(self, **kwargs):
"""Bind as a receiver"""

return self._bind('bind_receiver', **kwargs)


def bind_transceiver(self, **kwargs):
"""Bind as a transmitter and receiver at once"""

return self._bind('bind_transceiver', **kwargs)


def unbind(self):
"""Unbind from the SMSC"""

p = smpp.make_pdu('unbind', client=self)

res = self.send_pdu(p)
self.send_pdu(p)
return self.read_pdu()


def send_pdu(self, p):
"""Send PDU to the SMSC"""

if not self.state in consts.COMMAND_STATES[p.command]:
raise exceptions.PDUError("Command %s failed: %s" \
% (p.command, consts.DESCRIPTIONS[consts.SMPP_ESME_RINVBNDSTS]))
raise exceptions.PDUError("Command %s failed: %s" %
(p.command, consts.DESCRIPTIONS[consts.SMPP_ESME_RINVBNDSTS]))

logger.debug('Sending %s PDU', p.command)

generated = p.generate()

logger.debug('>>%s (%d bytes)', binascii.b2a_hex(generated), len(generated))
res = self._socket.send(generated)
logger.debug('>>%s (%d bytes)', binascii.b2a_hex(generated),
len(generated))
self._socket.send(generated)

return True


def read_pdu(self):
"""Read PDU from the SMSC"""

Expand Down Expand Up @@ -183,12 +169,10 @@ def read_pdu(self):

return p


def accept(self, obj):
"""Accept an object"""
raise NotImplementedError('not implemented')


def _message_received(self, p):
"""Handler for received message event"""
self.message_received_handler(pdu=p)
Expand All @@ -198,6 +182,7 @@ def _message_received(self, p):
self.send_pdu(dsmr)

def _enquire_link_received(self):
"""Response to enquire_link"""
ler = smpp.make_pdu('enquire_link_resp', client=self)
#, message_id=args['pdu'].sm_default_msg_id)
self.send_pdu(ler)
Expand All @@ -219,7 +204,8 @@ def message_received_handler(pdu, **kwargs):

@staticmethod
def message_sent_handler(pdu, **kwargs):
"""Called when SMPP server accept message (SUBMIT_SM_RESP). May be overridden"""
"""Called when SMPP server accept message (SUBMIT_SM_RESP).
May be overridden"""
logger.warning('Message sent handler (Override me)')

def listen(self, ignore_error_codes=None):
Expand All @@ -235,7 +221,7 @@ def listen(self, ignore_error_codes=None):
self.send_pdu(p)
continue

if p.command == 'unbind': #unbind_res
if p.command == 'unbind': # unbind_res
logger.info('Unbind command received')
break
elif p.command == 'submit_sm_resp':
Expand All @@ -252,11 +238,11 @@ def listen(self, ignore_error_codes=None):
if ignore_error_codes \
and len(e.args) > 1 \
and e.args[1] in ignore_error_codes:
logging.warning('(%d) %s. Ignored.' % (e.args[1], e.args[0]))
logging.warning('(%d) %s. Ignored.' %
(e.args[1], e.args[0]))
else:
raise


def send_message(self, **kwargs):
"""Send message
Expand All @@ -271,32 +257,3 @@ def send_message(self, **kwargs):
ssm = smpp.make_pdu('submit_sm', client=self, **kwargs)
self.send_pdu(ssm)
return ssm

#
# Main block for testing
#
if __name__ == '__main__':

import sys
sys.path.insert(0, '..')
import smpplib

def recv_handler(**kwargs):
p = kwargs['pdu']
msg = p.short_message
logger.info('Message received: %s', msg)
logger.info('Source address: %s', p.source_addr)
logger.info('Destination address: %s', p.destination_addr)

client = smpplib.client.Client('localhost', 11111)
client.connect()

client.set_message_received_handler(recv_handler)

try:
client.bind_transceiver(system_id='omni', password='omni', system_type='www')
client.listen()
finally:
client.unbind()
client.disconnect()

Loading

0 comments on commit a4c2679

Please sign in to comment.