Commit 07aa3d1b authored by wx002's avatar wx002

added index within ACK packets

parent 666ecd73
from socket import *
import re
import base64
import linecache
import sys
dest = ('127.0.0.1', 8888)
currentIndex = -1
sock = socket(AF_INET, SOCK_DGRAM)
sock.bind(dest)
......@@ -11,6 +13,16 @@ sock.bind(dest)
fileList = []
lineStr = ''
def PrintException():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
print ('EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj))
def get_decode_list(byteString):
......@@ -26,54 +38,77 @@ def verify_packet_content(stringList):
index 1: content hashed form in base64
index 2: actual data
'''
try:
encoded_str = base64.b64decode(bytes(stringList[1], 'utf-8')).decode('utf-8')
if stringList[1] != None and stringList[2] != None:
encoded_str = base64.b64decode(bytes(stringList[1], 'utf-8')).decode('utf-8')
if encoded_str == stringList[2]:
return True
else:
return False
except Exception:
return False
if encoded_str == stringList[2]:
return True
else:
return False
while True:
data, addr = sock.recvfrom(1048)
currentHash = None
#print("recv data: {}".format(data))
while data != b'####' and data != b'END':
try:
lineStr += data.decode('utf-8')
except Exception:
# bad packet ask for resend
sock.sendto(b'NAK', addr)
sock.sendto(b'NNNN', addr)
# print(get_decode_list(lineStr))
data, addr = sock.recvfrom(1048)
if data == b'####':
#print('Got line end!')
packetList = get_decode_list(lineStr)
#print(packetList)
# check header
if currentHash != packetList[0]:
print('got new packet')
# verify content
# print(verify_packet_content(packetList))
if verify_packet_content(packetList):
sock.sendto(b'ACK', addr)
fileList.append(packetList[2])
# update currentHash
currentHash = packetList[0]
else: # duplicated packet, resend ack
sock.sendto(b'ACK', addr)
try:
index = int(base64.b64decode(packetList[0].encode()))
print('Got packet index {}\t Expecting packet index {}'.format(index, currentIndex+1))
if index == currentIndex + 1:
print('index match, verfying packet!')
# verify content
# print(verify_packet_content(packetList))
if verify_packet_content(packetList):
fileList.append(packetList[2])
# update currentIndex
currentIndex += 1
print("Got packet! Recv Index: {}\tNext index: {}\tCurrentLine: {}".format(index, currentIndex+1,
packetList[2]))
print('sending ACK...')
sock.sendto(b'ACK\t'+ base64.b64encode(str(index).encode()), addr)
# print('recvived 1 line!')
else:
# corrupted content, ask for the same line again
print('packet {} is corrupted, ask for line again!'.format(index))
sock.sendto(b'NNNN', addr)
elif index == currentIndex and len(packetList) == 3: # duplicated packet, resend ack
print('resending ACK for packet index {}, next packet index is {}'.format(index, currentIndex+1))
sock.sendto(b'ACK\t'+base64.b64encode(str(currentIndex).encode()), addr)
continue
else:
print('packet error for index {} and current index {}! Ask for resend!',format(index, currentIndex))
sock.sendto(b'NNNN', addr)
except:
print(index, packetList)
PrintException()
sock.sendto(b'NNNN', addr)
# print(fileList)
lineStr = ''
# data = ''
#print(fileList)
print('recvived 1 line!')
continue
if data == b'END':
print("got all contents!")
sock.sendto(b'END', addr)
sock.close()
# make file
file = open('recvData/recv.txt', 'w+')
......
This source diff could not be displayed because it is too large. You can view the blob instead.
import socket
import base64
import re
import linecache
import sys
def PrintException():
exc_type, exc_obj, tb = sys.exc_info()
f = tb.tb_frame
lineno = tb.tb_lineno
filename = f.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, lineno, f.f_globals)
print ('EXCEPTION IN ({}, LINE {} "{}"): {}'.format(filename, lineno, line.strip(), exc_obj))
def chunk(line, size=1028):
return [line[i:i+size] for i in range(0,len(line), size)]
......@@ -33,56 +44,53 @@ def rdt_sendFile(network, dest, filename, size=65536):
# generate lines from file
file = open(filename)
lines = file.readlines()
for line in lines:
line_count = len(lines)
currentIndex = 0
while currentIndex < line_count:
ack = False
while not ack:
try:
# build hash
hashed_line = build_packet(lines.index(line), line)
print(hashed_line)
hashed_line = build_packet(currentIndex, lines[currentIndex])
#print('index: {}\tLine:{}'.format(currentIndex,lines[currentIndex]))
send_line(network, dest, hashed_line, size)
print('sending packet index {}'.format(currentIndex))
data = network.recv(size)
print(data)
if data == b'ACK':
#handle acks
ackList = re.split('\t', data.decode(), maxsplit=1)
index = int(base64.b64decode(ackList[1].encode()))
print('Got ack for packet {} and currentIndex is {}'.format(index, currentIndex))
if ackList[0] == 'ACK' and index == currentIndex:
ack = True
if data == b'NAK':
currentIndex += 1
print("acked! packet index is now {}".format(currentIndex))
elif data == b'NNNN':
#current_line = build_packet(currentIndex, lines[currentIndex])
print('resending packet {},{}'.format(currentIndex, lines[currentIndex]))
send_line(network, dest, hashed_line, size)
except socket.timeout:
print('timeout! resend line')
send_line(network, dest, hashed_line, size)
network.sendto(b'END', dest)
print('timeout!')
except:
PrintException()
END = False
while not END:
network.sendto(b'END', dest)
data = network.recv(size)
if data == b'END':
END = True
network.close()
if __name__ == '__main__':
ip = '127.0.0.1'
port = 8880
dest = (ip, port)
timeOut = 1
timeOut = 10
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(timeOut)
filename = 'sendData/alice.txt'
rdt_sendFile(sock, dest, filename)
'''
ack = False
testLines = ['\n','\n','test line 1\n','test line 2\n','test line 3\n']
for line in testLines:
ack = False
while not ack:
try:
# build hash
hash_index = hash_string(str(testLines.index(line)))
hash_str = hash_string(line)
hashed_line = hash_index+hash_str + line.encode()
send_line(sock, hashed_line, 1048)
data = sock.recv(1048)
print(data)
if data == b'ACK':
ack = True
except socket.timeout:
print('timeout! resend line')
#send_line(sock, line, 1048)
# end with END packet
sock.sendto(b'END', dest)
'''
......@@ -13,7 +13,9 @@ to connect to the UDP_bbox.
client <---> UDP_bbox <---> server
(c) 2019 Alan Marchiori
Good: --loss_rate 0.01 --ooo_rate 0.01 --dupe_rate 0.01 --ber 1e-8
Medium: --loss_rate 0.02 --ooo_rate 0.02 --dupe_rate 0.02 --ber 1e-6
Terrible: --loss_rate 0.1 --ooo_rate 0.03 --dupe_rate 0.05 --ber 1e-3
"""
import argparse
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment