Commit 730c5010 authored by Gunnar Noordbruis's avatar Gunnar Noordbruis

All done, works great.

parent ab7433ad
......@@ -19,26 +19,20 @@ delay = args['timeout']
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
num_packets = math.ceil(os.stat(filename).st_size/1000)
randsyn = 0
#randint(0, 0xFFFF)
randsyn = randint(0, 0xFFFF)
print("Randsyn ",randsyn)
stream_id = randint(0, 0xffffffff)
print(randsyn)
stream_id = randint(0, 0xffffffff)
seq = Seq(stream_id, window_size=window, timeout=delay, dest_ip=destination_ip, dest_port=destination_port, socket=sock, randsyn=randsyn)
reader = Read(filename, window, num_packets)
open_handshake(sock, randsyn, seq, delay)
#seq.reset_num()
seq.create_packets(reader)
print(num_packets)
while not seq.finished(num_packets):
seq.check_packets()
seq.size()
#seq.print_packets()
#in the handle add if for syn ack such that we keep receiving the syn ack we send back the ack.
#such that the server finally finishes the opening handshake
handle(sock, seq, delay, randsyn)
seq.create_packets(reader)
print("C: Started closing handshake")
......
......@@ -33,8 +33,7 @@ OGsyn = dec[1]
stream_id = dec[0]
print("OGsyn ",OGsyn)
randsyn = 0
#randint(0,0xFFFF)
randsyn = randint(0,0xFFFF)
seq = Seq(stream_id, window_size=window, timeout=delay, dest_ip=source_ip, dest_port=source_port, socket=sock, randsyn=randsyn)
opening_handshake(sock, seq, data, delay)
......@@ -52,22 +51,18 @@ while loop:
dec = decode_packet(data)
if dec:
(stream_id, syn_number, ack_number, syn, ack, fin, window_size, data_length, content) = dec
#if(number%1000==0):
# print("S recieved: "+ str(number) + " "+ str(syn_number), syn, ack, fin)
print("received syn "+ str(syn_number) + " ack " + str(ack_number), syn, ack, fin, content[:20])
if fin:
#print("Closing shake started")
print("Closing shake started")
closing_handshake(seq, sock, delay, syn_number)
order.write_all()
order.close()
loop = False
elif not (ack or fin or syn):
order.add_packet(stream_id,syn_number-OGsyn-2,content, data)
#order.save_to_file(syn_number,content)
seq.send_unsafe(syn_number, 0, 1, 0, 0, b'')
#print("S ack sent for: ", syn_number)
else:
old+=1
#print("S again acked: ", syn_number)
seq.send_unsafe(int.from_bytes(data[4:6], 'little'), 0, 1, 0, 0, b'')
sock.close()
print("Recieved ", number, " packets and old: ", old)
......
......@@ -4,12 +4,8 @@ from struct import *
from btcp import *
from .packetHandler import receive
def open_handshake(sock, randsyn, seq, timeout):
#stream_id, syn_number, ack_number=0, SYN=0, ACK=0, FIN=0, window_size=100, data_length=0, content=b''
# ack_number, syn, ack, fin, window_size, data_length, content
seq.send_safe(0, 1, 0, 0, 9, b'THISISSYN')
#print(int(round(time.time() * 1000)))
data, (source_ip, source_port) = receive(sock, timeout)
while True:
......@@ -17,14 +13,11 @@ def open_handshake(sock, randsyn, seq, timeout):
(stream_id, syn_number, ack_number, syn, ack, fin, window_size, data_length, content) = decode_packet(data)
if (syn and ack and ack_number==randsyn+1):
seq.delete_packet(randsyn)
#print(int(round(time.time() * 1000)))
seq.send_unsafe(syn_number, 0, 1, 0, 9, b'THISISACK')
return True
else:
seq.check_packets()
data, (source_ip, source_port) = receive(sock, timeout)
data, (source_ip, source_port) = receive(sock, timeout)
def opening_handshake(sock, seq, data, timeout):
dec = decode_packet(data)
......@@ -34,9 +27,6 @@ def opening_handshake(sock, seq, data, timeout):
print("syn_ack sent")
syn = seq.send_safe(a_syn_number+1, 1, 1, 0, 12, b'THISISSYNACK')
seq.adjust_window(a_window_size)
#syn_ack = encode_packet(a_syn_number, a_syn_number+1, 1, 1, 0, a_window_size, a_data_length, a_content)
#seq.send_packet(a_syn_number,syn_ack)
while True:
data, (source_ip, source_port) = receive(sock, timeout)
......@@ -48,18 +38,15 @@ def opening_handshake(sock, seq, data, timeout):
else:
seq.check_packets()
def close_handshake(seq, sock, timeout):
print("C closing handshake")
syn = seq.send_safe(0, 0, 0, 1, 9, b'THISISFIN')
print("1 Syn of the FIN starting close ", syn)
fin_f = None
ack_f = None
ack = 0
while not fin_f and not ack_f and not ack==syn+1:
while not fin_f and not ack_f and not ack==syn-1:
data, addr = receive(sock, timeout)
while not data:
print("Close handshake error")
......@@ -67,32 +54,21 @@ def close_handshake(seq, sock, timeout):
data, addr = receive(sock, timeout)
(stream_id, syn_number, ack_number, syn, ack, fin, window_size, data_length, content) = decode_packet(data)
print("2b syn of FIN ACK ", syn_number, "ack num ", ack_number, "fin ack ", fin,ack)
if(fin and ack):
syn = seq.send_unsafe(syn_number+1, 0, 1, 0, 13, b'HELLOMYFRIEND')
print("4 syn of LAST ACK ", syn," ACK ", syn_number+1, ack)
syn = seq.send_unsafe(syn_number-1, 0, 1, 0, 13, b'HELLOMYFRIEND')
return True
def closing_handshake(seq, sock, timeout, syn):
print("S closing handshake")
syn_number = seq.send_safe(syn+1, 0, 1, 1, 12, b'THISISFINACK')
print("2a Syn of the FIN starting close ", syn_number, "ACK num ",syn+1)
syn_number = seq.send_safe(syn-1, 0, 1, 1, 12, b'THISISFINACK')
for i in range(100):
data, (source_ip, source_port) = receive(sock, timeout)
while not data:
print("Server close error")
seq.check_packets()
data, (source_ip, source_port) = receive(sock, timeout)
print(data)
(_, c_syn_number, ack_number, syn_f, ack_f, fin_f, _, _, _) = decode_packet(data)
if (ack_f and not (syn_f and fin_f)) and ack_number==syn_number+1:
print("Close completed")
return True;
elif(c_syn_number==syn and (fin_f and not (syn_f and ack_f))):
syn_number = seq.send_safe(syn+1, 0, 1, 1, 12, b'THISISFINACK')
data, (source_ip, source_port) = receive(sock, timeout)
if data:
(_, c_syn_number, ack_number, syn_f, ack_f, fin_f, _, _, _) = decode_packet(data)
if (ack_f and not (syn_f and fin_f)) and ack_number==syn_number-1:
print("Close completed")
return True;
elif(c_syn_number==syn and (fin_f and not (syn_f and ack_f))):
syn_number = seq.send_safe(syn-1, 0, 1, 1, 12, b'THISISFINACK')
print("CLOSE NOT GOOD")
......@@ -14,10 +14,10 @@ def handle(sock, seq, timeout, randsyn):
data, (source_ip, source_port) = receive(sock,timeout/10)
if not data==None:
(stream_id, syn_number, ack_number, syn, ack, fin, window_size, data_length, content) = decode_packet(data)
#print("received syn"+ str(syn_number) + " ack" + str(ack_number) + " " + str(syn) + " " + str(ack))
print("received syn "+ str(syn_number) + " ack " + str(ack_number), syn, ack, fin, content[:20])
if fin:
closing_handshake(seq, sock, source_ip, source_port)
elif (syn and ack):
seq.resend_unsafe(,syn_number, 0, 1, 0, 3, b'ack')
seq.resend_unsafe(randsyn+1,syn_number, 0, 1, 0, 3, b'ack')
elif ack:
seq.delete_packet(ack_number)
......@@ -30,7 +30,6 @@ class Seq:
Checks if all packets have been acked
"""
def finished(self, total_packets):
#print("acked:" + str(self.acked))
return self.acked==total_packets
"""
......@@ -87,10 +86,8 @@ class Seq:
self.packets.append((None,self.packet_num+self.randsyn,packet))
self.packet_num+=1
else:
#print("breaked in create_packets")
self.EOF = True
break
#print("number of packets created:" +str(len(self.packets)-a))
"""
Resents timed out packets and sents not yet sent packets
......@@ -139,7 +136,6 @@ class Seq:
packet = encode_packet(self.stream_id, syn_number, ack_number, syn, ack, fin, self.window_own, data_length, content)
self.socket.sendto(packet, (self.ip, self.port))
self.add_packetWT(int(round(time.time() * 1000))+self.outtime, syn_number, packet)
print("Packet_num up send uncoded", self.packet_num, self.packet_num+1)
self.packet_num+=1
return syn_number
......@@ -152,7 +148,6 @@ class Seq:
syn_number = self.randsyn+self.packet_num
packet = encode_packet(self.stream_id, syn_number, ack_number, syn, ack, fin, self.window_own, data_length, content)
self.socket.sendto(packet, (self.ip, self.port))
print("Packet_num up send_ack", self.packet_num, self.packet_num+1)
self.packet_num+=1
return syn_number
......
Randsyn 0
0
Packet_num up send uncoded 0 1
Packet_num up send_ack 1 2
66327
C: Started closing handshake
C closing handshake
Packet_num up send uncoded 793 794
1 Syn of the FIN starting close 793
2b syn of FIN ACK 8902 ack num 794 fin ack 1 1
Packet_num up send_ack 794 795
4 syn of LAST ACK 794 ACK 8903 1
Randsyn 0
0
Packet_num up send uncoded 0 1
Packet_num up send_ack 1 2
66327
C: Started closing handshake
C closing handshake
Packet_num up send uncoded 793 794
1 Syn of the FIN starting close 793
2b syn of FIN ACK 7425 ack num 794 fin ack 1 1
Packet_num up send_ack 794 795
4 syn of LAST ACK 794 ACK 7426 1
Randsyn 0
0
Packet_num up send uncoded 0 1
Packet_num up send_ack 1 2
66327
C: Started closing handshake
C closing handshake
Packet_num up send uncoded 793 794
1 Syn of the FIN starting close 793
2b syn of FIN ACK 1421 ack num 794 fin ack 1 1
Packet_num up send_ack 794 795
4 syn of LAST ACK 794 ACK 1422 1
Randsyn 0
0
Packet_num up send uncoded 0 1
Packet_num up send_ack 1 2
66327
C: Started closing handshake
C closing handshake
Packet_num up send uncoded 793 794
1 Syn of the FIN starting close 793
2b syn of FIN ACK 792 ack num 794 fin ack 1 1
Packet_num up send_ack 794 795
4 syn of LAST ACK 794 ACK 793 1
Randsyn 0
0
Packet_num up send uncoded 0 1
Packet_num up send_ack 1 2
66327
C: Started closing handshake
C closing handshake
Packet_num up send uncoded 793 794
1 Syn of the FIN starting close 793
2b syn of FIN ACK 1947 ack num 794 fin ack 1 1
Packet_num up send_ack 794 795
4 syn of LAST ACK 794 ACK 1948 1
Randsyn 0
0
Packet_num up send uncoded 0 1
Packet_num up send_ack 1 2
66327
C: Started closing handshake
C closing handshake
Packet_num up send uncoded 793 794
1 Syn of the FIN starting close 793
2b syn of FIN ACK 792 ack num 794 fin ack 1 1
Packet_num up send_ack 794 795
4 syn of LAST ACK 794 ACK 793 1
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -49,15 +49,15 @@ def run_command(command,cwd=None, shell=True):
print("2. problem running command : \n ", str(command), " ", process.returncode)
def doTheRest(number):
input = "test/llorem.txt"
run_command("python3 bTCP_client.py -i "+input + " -w "+str(winsize) + " -t " + str(timeout) + " > clientout/"+str(number))
input = "test/s.txt"
run_command("python3 bTCP_client.py -i "+input + " -w "+str(winsize) + " -t " + str(timeout) + " > clientout/"+number)
t.join()
time.sleep(2)
if not filecmp.cmp("testout/out"+number,input):
print("Files are not the same")
raise Exception("Files are not the same")
raise Exception(input+" is not the same as testout/"+number)
else:
print("Files are the same")
print("Files are the same",input,"== testout/"+number)
class TestbTCPFramework(unittest.TestCase):
......@@ -66,9 +66,7 @@ class TestbTCPFramework(unittest.TestCase):
def setUp(self):
"""Prepare for testing"""
# default netem rule (does nothing)
print("setup start")
run_command(netem_add)
print("setup end")
# launch localhost server
global t
global number
......@@ -153,17 +151,17 @@ class TestbTCPFramework(unittest.TestCase):
print("\n\nReordering network:",number)
doTheRest(str(number))
#DONE WORKS
#def test_delayed_network(self):
# """reliability over network with delay relative to the timeout value"""
# # setup environment
# run_command(netem_change.format("delay "+str(timeout)+"ms 20ms"))
# # launch localhost client connecting to server
# # client sends content to server
# # server receives content from client
# # content received by server matches the content sent by client
# global number
# print("\n\nTimeout network:",number)
# doTheRest(str(number))
def test_delayed_network(self):
"""reliability over network with delay relative to the timeout value"""
# setup environment
run_command(netem_change.format("delay "+str(timeout)+"ms 20ms"))
# launch localhost client connecting to server
# client sends content to server
# server receives content from client
# content received by server matches the content sent by client
global number
print("\n\nTimeout network:",number)
doTheRest(str(number))
#Done works
def test_allbad_network(self):
"""reliability over network with all of the above problems"""
......@@ -185,6 +183,10 @@ class TestbTCPFramework(unittest.TestCase):
if __name__ == "__main__":
# Parse command line arguments
print("#"*83)
print("Keep in mind that a timeout test will take a long time when using a large testcase.")
print("Next to that runtime is slower due to print statements to output files.")
print("#"*83)
import argparse
parser = argparse.ArgumentParser(description="bTCP tests")
parser.add_argument("-w", "--window", help="Define bTCP window size used", type=int, default=100)
......
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This source diff could not be displayed because it is too large. You can view the blob instead.
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