Skip to content

Commit

Permalink
Fixing bug with keep alive packets
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay committed Jan 25, 2016
1 parent 47d9488 commit 3a58709
Showing 1 changed file with 27 additions and 8 deletions.
35 changes: 27 additions & 8 deletions dwgc.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DWGD:
conn = None
ping_timer = 45.0
check_timer = 5.0
ping_count = 0

def __init__(self, conn):
self.conn = conn
Expand Down Expand Up @@ -73,7 +74,9 @@ def parseType(self, htype, data):
"""
sdata = {'type': 0,
'body': ''}
if htype == 7: # Status message
if htype == 0: # recieved keep alive
self.ping_count = 0
elif htype == 7: # Status message
sdata['type'] = 8
sdata['body'] = pack('!?', False)
elif htype == 5: # Receive message
Expand Down Expand Up @@ -118,11 +121,19 @@ def pingDWG(self):
"""
Ping DWG
"""
sdata = {'type': 0,
'body': ''}
self.sendDWG(self.create_header(), sdata)
self.ping_t = Timer(self.ping_timer, self.pingDWG)
self.ping_t.start()
if self.ping_count > 2:
if not self.ping_t is None:
self.ping_t.cancel()
if not self.check_t is None:
self.check_t.cancel()
self.conn.close()
else:
sdata = {'type': 0,
'body': ''}
self.ping_count += self.ping_count
self.sendDWG(self.create_header(), sdata)
self.ping_t = Timer(self.ping_timer, self.pingDWG)
self.ping_t.start()

def sendDWG(self, header, sdata):
"""
Expand Down Expand Up @@ -152,7 +163,11 @@ def saveSMS(self, body):
Saving SMS to file
"""
try:
sms = codecs.open('%s%s.%s' % (dwgconfig.income_path, body['number'], int(time())), 'w', 'utf-8')
sms_partfilename = '%s%s.%s' % (dwgconfig.income_path, body['number'], int(time()))
sms_filename = '%s.%s' % (sms_partfilename, randint(1, 999999))
while os.path.isfile(sms_filename):
sms_filename = '%s.%s' % (sms_partfilename, randint(1, 999999))
sms = codecs.open(sms_filename, 'w', 'utf-8')
sms.write('Number: %s\n' % body['number'])
sms.write('Port: %s\n' % body['port'])
sms.write('Time: %s\n' % body['timestamp'])
Expand All @@ -173,7 +188,11 @@ def saveUSSD(self, body):
Saving USSD to file
"""
try:
ussd = codecs.open('%s%s.%s' % (dwgconfig.ussd_income_path, body['port'], int(time())), 'w', 'utf-8')
ussd_partfilename = '%s%s.%s' % (dwgconfig.ussd_income_path, body['port'], int(time()))
ussd_filename = '%s.%s' % (ussd_partfilename, randint(1, 999999))
while os.path.isfile(ussd_filename):
ussd_filename = '%s.%s' % (ussd_partfilename, randint(1, 999999))
ussd = codecs.open(ussd_filename, 'w', 'utf-8')
ussd.write('Port: %s\n' % body['port'])
ussd.write('Time: %s\n' % strftime('%d%m%Y%H%M%S'))
ussd.write('Status: %s\n' % body['status'])
Expand Down

0 comments on commit 3a58709

Please sign in to comment.