Commit 7add831c authored by wx002's avatar wx002

proj rdt working

parent dbe05f21
......@@ -2,7 +2,7 @@ from socket import *
import re
import base64
dest = ('127.0.0.1', 5000)
dest = ('127.0.0.1', 8888)
sock = socket(AF_INET, SOCK_DGRAM)
......@@ -10,16 +10,20 @@ sock.bind(dest)
fileList = []
lineStr = ''
currentHeader = None
def get_decode_list(byteString):
string = byteString.decode('utf-8')
stringList = re.split('\t', string, maxsplit=2)
# print("byteString: {}\tType: {}".format(byteString, type(byteString)))
# string = byteString.decode('utf-8')
stringList = re.split('\t', byteString, maxsplit=2)
return stringList
def get_line_index(stringList):
return stringList[0]
def verify_hash(stringList):
hash_str = stringList[1].encode()
recv_str = stringList[2]
......@@ -27,21 +31,60 @@ def verify_hash(stringList):
return True
else:
return False
def verify_packet_content(stringList):
'''
index 0: header index
index 1: content hashed form in base64
index 2: actual data
'''
try:
encoded_str = base64.b64decode(bytes(stringList[1], 'utf-8')).decode('utf-8')
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'####':
while data != b'####' and data != b'END':
lineStr += data.decode('utf-8')
print(get_decode_list(lineStr))
# print(get_decode_list(lineStr))
data, addr = sock.recvfrom(1048)
if data == b'####':
sock.sendto(b'ACK', addr)
fileList.append(lineStr)
lineStr = ''
data = ''
if data == b'####':
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)
print(fileList)
lineStr = ''
# data = ''
#print(fileList)
print('recvived 1 line!')
continue
if data == b'END':
print("got all contents!")
sock.close()
# make file
file = open('recv.txt', 'w+')
file.writelines(fileList)
exit(1)
This source diff could not be displayed because it is too large. You can view the blob instead.
import socket
import base64
ip = '127.0.0.1'
port = 5000
dest = (ip,port)
timeOut = 1
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.settimeout(timeOut)
def load_data(fileName):
fileList = []
with open(fileName) as f:
line = f.readline()
while line:
fileList.append(line)
line = f.readline()
return fileList
def chunk(line, size=1028):
return [line[i:i+size] for i in range(0,len(line), size)]
def hash_string(string):
str_hash = base64.b64encode(string.encode()) + b'\t'
return str_hash
def send_line(socket, line_bytes, size):
if len(line) < size:
#send the line directly if is less than the max size
socket.sendto(line_bytes, dest)
def send_line(network, dest, line_bytes, size):
if len(line_bytes) < size:
# send the line directly if is less than the max size
network.sendto(line_bytes, dest)
else:
lineChunks = chunk(line_bytes, size)
for c in lineChunks:
socket.sendto(c, dest)
network.sendto(c, dest)
network.sendto(b'####', dest)
def build_packet(index, line):
hash_index = hash_string(str(index))
hash_str = hash_string(line)
hashed_line = hash_index + hash_str + line.encode()
return hashed_line
def rdt_sendFile(network, dest, filename, size=65536):
# generate lines from file
file = open(filename)
lines = file.readlines()
for line in lines:
ack = False
while not ack:
try:
# build hash
hashed_line = build_packet(lines.index(line), line)
print(hashed_line)
send_line(network, dest, hashed_line, size)
data = network.recv(size)
print(data)
if data == b'ACK':
ack = True
except socket.timeout:
print('timeout! resend line')
send_line(network, dest, hashed_line, size)
network.sendto(b'END', dest)
socket.sendto(b'####', dest)
if __name__ == '__main__':
ip = '127.0.0.1'
port = 8888
dest = (ip, port)
timeOut = 1
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:
......@@ -55,10 +76,11 @@ if __name__ == '__main__':
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)
'''
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -55,14 +55,14 @@ def parse_args():
default = '50',
help='Bandwidth in K BYTES per second [50].')
parser.add_argument('--addr',
default = '0.0.0.0',
default = '127.0.0.1',
help='Local addres to listen on [0.0.0.0].')
parser.add_argument('--port',
help='Local port to listen on [8880].',
default=8880,
type=int)
parser.add_argument('--remote_addr',
default = '0.0.0.0',
default = '127.0.0.1',
help='Remote addres to send recieved packets to [localhost].')
parser.add_argument('--remote_port',
help='Remote port to send packets to [8888].',
......
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