Skip to content

Commit

Permalink
small fixes after connecting to gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
smn committed Jan 18, 2014
1 parent b70e35a commit e572e22
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
9 changes: 3 additions & 6 deletions txssmi/builder.py
Expand Up @@ -42,16 +42,13 @@ def __init__(self, **kwargs):
def __iter__(self):
parts = [SSMI_HEADER, self.command_id]
parts.extend([self.values.get(fn) for fn in self.fields])
return iter(map(unicode, parts))

def __unicode__(self):
return u','.join(self)
return iter(parts)

def __str__(self):
return unicode(self).encode('utf-8')
return ','.join(self)

def __eq__(self, other):
return unicode(other) == unicode(self)
return str(other) == str(self)

def __getattr__(self, attr):
if attr in self.fields:
Expand Down
4 changes: 2 additions & 2 deletions txssmi/commands.py
Expand Up @@ -5,9 +5,9 @@


Login = SSMIRequest.create('LOGIN')
SendSMS = SSMIRequest.create('SEND_SMS', {'validity': 0})
SendSMS = SSMIRequest.create('SEND_SMS', {'validity': '0'})
SendBinarySMS = SSMIRequest.create('SEND_BINARY_SMS', {
'validity': 0,
'validity': '0',
'coding': CODING_7BIT,
'pid': PROTOCOL_STANDARD,
})
Expand Down
22 changes: 14 additions & 8 deletions txssmi/protocol.py
@@ -1,4 +1,5 @@
# -*- test-case-name: txssmi.tests.test_protocol -*-
# -*- coding: utf-8 -*-

import random
from collections import defaultdict
Expand All @@ -25,7 +26,7 @@

class SSMIProtocol(LineReceiver):

delimeter = '\r'
delimiter = b'\r'
noisy = False
clock = reactor

Expand All @@ -36,17 +37,22 @@ def __init__(self):
self.imsi_lookup_reply_map = defaultdict(lambda: DeferredQueue())
self.link_check = LoopingCall(self.send_link_request)
self.link_check.clock = self.clock
self.imsi_lookups = {}

def connectionMade(self):
log.msg('Connection made.')

def connectionLost(self, reason):
log.msg('Connection lost: %s' % (reason,))

def emit(self, prefix, msg):
if self.noisy:
log.msg('%s %s' % (prefix, msg))
log.msg('%s %r' % (prefix, msg))

def lineReceived(self, line):
command = SSMIResponse.parse(line)
self.emit('<<', str(command))
self.emit('<<', command)
handler = getattr(self, 'handle_%s' % (command.command_name,))
return maybeDeferred(handler, command)
maybeDeferred(handler, command)

def send_command(self, command):
self.emit('>>', str(command))
Expand Down Expand Up @@ -80,13 +86,13 @@ def cb(cmd):
d.addCallback(cb)
return d

def send_message(self, msisdn, message, validity=0):
def send_message(self, msisdn, message, validity='0'):
d = self.send_command(
SendSMS(msisdn=msisdn, message=message, validity=validity))
d.addCallback(lambda cmd: self.wait_for_reply(cmd.msisdn))
return d

def send_binary_message(self, msisdn, hex_message, validity=0,
def send_binary_message(self, msisdn, hex_message, validity='0',
protocol_id=PROTOCOL_STANDARD,
coding=CODING_7BIT):
d = self.send_command(
Expand Down Expand Up @@ -128,7 +134,7 @@ def send_mms_message(self, msisdn, subject, name, content):

def imsi_lookup(self, msisdn, sequence=None, imsi=None):
if sequence is None:
sequence = unicode(random.randint(0, 1000))
sequence = str(random.randint(0, 1000))
d = self.send_command(IMSILookup(sequence=sequence, msisdn=msisdn,
imsi=imsi))
deferred = Deferred()
Expand Down
4 changes: 2 additions & 2 deletions txssmi/tests/test_protocol.py
Expand Up @@ -64,13 +64,13 @@ def test_authenticate(self):
d = self.protocol.authenticate('username', 'password')
[cmd] = yield self.receive(1)
self.assertEqual(cmd.command_name, 'LOGIN')
yield self.send(Ack(ack_type=1))
yield self.send(Ack(ack_type='1'))
self.assertTrue((yield d))
self.assertTrue(self.protocol.authenticated)

@inlineCallbacks
def test_send_message(self):
self.protocol.send_message('2700000000', 'hi there!', validity=2)
self.protocol.send_message('2700000000', 'hi there!', validity='2')
[cmd] = yield self.receive(1)
self.assertEqual(cmd.command_name, 'SEND_SMS')
self.assertEqual(cmd.msisdn, '2700000000')
Expand Down

0 comments on commit e572e22

Please sign in to comment.