-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStopNWaitClient.py
158 lines (108 loc) · 4.6 KB
/
StopNWaitClient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# Author: Alden Sahi
# Client for Stop and Wait Protocol
import socket
import time
import select
import random
import copy
from packet import Packet
SERVER_ADDRESS_PORT = ("127.0.0.1", 20001)
BUFFER_SIZE = 4096
TIMEOUT_DURATION = 1
################### METHODS #######################################
def createBitError(packet: Packet) -> Packet:
# Params: Packet
# Output: New Packet with same hash value but different data
# This achieves theory because when packet gets to the server
# the server will recompute hash of data sent vs the one couple with data
# these two hashes will be different because we altered orginal data that was used
tempPacket = copy.deepcopy(packet)
#print(f'Original Data {packet.data} \n')
if random.random() < 0.002:
#print("Inducing Bit Error",file='SNW_C.txt')
splitData = tempPacket.data.split('|')
#print(f'Data After split {splitData} \n')
# Extract the data portion (before the "|")
bNum = bin(int(splitData[0]))
#removes binary prefix '0b'
bNum = bNum[2:]
#make bin str mutable
bList = list(bNum)
#generates random index within indicies
randIndex = random.randint(0,len(bList)-1)
# flips bit at random index
bList[randIndex] = '1' if bList[randIndex] == '0' else '0'
# Convert the binary list back to a string of bits
bitErrorData = str(int(''.join(bList), 2))
#print(f'Data Trying to Inject: {bitErrorData} \n')
#print(f'List Before to Inject: {splitData} \n')
#inject bit error data into packet
splitData[0] = bitErrorData
#print(f'List After to Inject: {splitData} \n')
#seal it back up
tempPacket.data = '|'.join(splitData)
return tempPacket
# Return
tempPacket.data = packet.data
return tempPacket
####################################################################
# Global Variables
count = 1
cachedPacket: Packet
nakFlag = 0
# client socket setup on (family)IPV4 and (type)UDP
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# starts runtime
start_time = time.time()
# cant use for-loop because we need a way to resend packets multiple times
# counts number of packets
with open('SNW_C.txt', 'w') as file:
while count<=1000:
# 25ms wait on client side to simulate 50ms RTT
time.sleep(0.025)
# CACHING LOGIC
if nakFlag:
print("Sending Cached Packet!", file=file)
# Bit Error
packetToSend = createBitError(cachedPacket)
# Sends encoded packet data
UDPClientSocket.sendto(packetToSend.data.encode(), SERVER_ADDRESS_PORT)
print("Packet Number: ", packetToSend.packet_num, file=file)
print("Sending Packet Data: ", packetToSend.data, file=file)
else:
# Creates Packet
cachedPacket = Packet(packet_num=count)
# Bit Error
packetToSend = createBitError(cachedPacket)
# Sends encoded packet data
UDPClientSocket.sendto(packetToSend.data.encode(), SERVER_ADDRESS_PORT)
print("Packet Number: ", packetToSend.packet_num, file=file)
print("Sending Packet Data: ", packetToSend.data, file=file)
########################## RECIEVING RESPONSE ####################################
while True:
# Waits for ACK, NAK or TIMEOUT
check = select.select([UDPClientSocket], [], [], TIMEOUT_DURATION)
# If response occurs
if check[0]:
# Extract Response
resp, _ = UDPClientSocket.recvfrom(BUFFER_SIZE)
# Determines if Packet was sent correctly
if resp.decode() == "ACK":
# Received ACK, proceed to the next packet
print(f'Packet Number {count} was ACK\n', file=file)
nakFlag = 0
count += 1
break
elif resp.decode() == "NAK":
print(f'Packet Number {count} was NAK\n', file=file)
nakFlag = 1
# Received NAK, retransmit the same packet
break
else:
# Packet Loss occured, retransmit the same packet (TODO: Save Old Packet!
print("TimeOut: Resending....",file=file)
break
# Runtime Results
end_time = time.time()
elapsed_time = end_time - start_time
print("Elapsed time:", elapsed_time, "seconds",file=file)