-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsender.py
237 lines (187 loc) · 5.83 KB
/
sender.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
from socket import socket, AF_INET, SOCK_DGRAM, timeout
from sys import argv
#from common import ip_checksum
import random
import time
import threading
from packet import*
from header import*
from common import*
from controller import*
dest_addr = argv[1]
dest_port = int(argv[2])
dest = (dest_addr, dest_port)
listen_addr = '127.0.0.1'
listen_port = 1501
listen = (listen_addr, listen_port)
filename = argv[3]
MWS = int(argv[4])
MSS = int(argv[5])
timeout = float(argv[6])/(1000*1.0)
pdrop = float(argv[7])
COMPLETE = False
temp = 0
FRTcounter = 0
random.seed(int(argv[8]))
NODA = 0
RSBuffer = {}
class sender(object):
"""docstring for main"""
def __init__(self):
super(sender, self).__init__()
self.ack = 1
self.packetList = []
self.sender_socket = socket(AF_INET,SOCK_DGRAM)
self.sender_socket.bind(listen)
self.logFile = open("Sender_log.txt","w")
self.totalSize = 0
with open(filename) as Source:
self.content = Source.read()
self.totalSize = len(self.content)
Source.close()
self.iniTime = 0
self.controller = controller(self.content,self.sender_socket,dest,self.logFile,MWS,MSS,timeout,pdrop)
self.ISN = random.randint(0,1000)
self.ISNR = 0 #save the ISN for receiver
self.wavehand = False
self.begin()
#begin program
def begin(self):
###### start send function#######
self.iniTime = time.time()
self.controller.setIniTime(self.iniTime)
self.TWHS()
l1 = threading.Thread(target = self.listen, args = (self.sender_socket,dest))
l1.daemon=True
l1.start()
s1 = threading.Thread(target = self.send, args = (self.sender_socket,))
s1.daemon=True
s1.start()
#while True:
o1 = threading.Thread(target = self.observer, args = ())
o1.daemon=True
o1.start()
#three-way hand shake
def TWHS(self):
SYNsegment = packet(self.ISN,0,'',1,0)
self.sender_socket.sendto(SYNsegment.encoded, dest)
self.writeLOG("snd","S",self.ISN,0,0)
SYNsegment.display()
try:
message, address = self.sender_socket.recvfrom(4096)
print "Received " + message
except timeout:
pass
else:
decoded= []
decoded = decode(message)
receive_seq = int(decoded[0])
receive_ack = int(decoded[2])
receive_data = str(decoded[5])
receive_syn = int(decoded[3])
receive_fin = int(decoded[4])
self.ISNR = receive_seq
self.writeLOG("rcv","SA",receive_seq,0,receive_ack)
ACKsegment = packet(receive_ack,receive_seq+1,'',0,0)
self.sender_socket.sendto(ACKsegment.encoded, dest)
self.writeLOG("snd","A",receive_ack,0,receive_seq+1)
ACKsegment.display()
self.controller.ini(receive_ack,self.ISNR)
def listen(self,sender_socket,dest):
global RSBuffer
global NODA
while self.wavehand == False:
try:
#sLock.acquire()
#print "check"
message, address = self.sender_socket.recvfrom(4096)
print "Received " + message
#self.logFile.write("in\n")
except timeout:
pass
else:
decoded= []
decoded = decode(message)
receive_seq = int(decoded[0])
receive_ack = int(decoded[2])
receive_data = str(decoded[5])
receive_syn = int(decoded[3])
receive_fin = int(decoded[4])
#self.logFile.write("in\n")
self.writeLOG("rcv","D",receive_seq,0,receive_ack)
self.FRTdetect(receive_ack)
self.wavehand = self.controller.GOTACK(receive_ack)
#finally:
#sLock.release()
#self.listen(self.sender_socket,dest)
print "complete"
# print self.totalSize + self.ISN + 1
self.FIN()
#ending method for finalise the program including close socket, close log file
def FIN(self):
global NODA
FINsegment = packet(self.totalSize + self.ISN + 1,self.ISNR+1,'',0,1)
self.sender_socket.sendto(FINsegment.encoded, dest)
self.writeLOG("snd","F", self.totalSize + self.ISN + 1 ,0,self.ISNR)
FINsegment.display()
try:
message, address = self.sender_socket.recvfrom(4096)
except timeout:
pass
else:
print "FIN " + message
decoded= []
decoded = decode(message)
receive_seq = int(decoded[0])
receive_ack = int(decoded[2])
receive_data = str(decoded[5])
receive_syn = int(decoded[3])
receive_fin = int(decoded[4])
self.writeLOG("rcv","FA",receive_seq,0,receive_ack)
ACKsegment = packet(receive_ack,receive_seq+1,'',0,0)
self.sender_socket.sendto(ACKsegment.encoded, dest)
self.writeLOG("snd","A",receive_ack,0,receive_seq+1)
ACKsegment.display()
static = self.controller.getStatic()
self.logFile.write("Amount of Data Transferred: " + str(self.totalSize)+"\n")
self.logFile.write("Number of Data Segments Sent: " + str(static[0])+"\n")
self.logFile.write("Number of Packets Dropped: " + str(static[1])+"\n")
self.logFile.write("Number of Retransmitted Segments: " + str(static[2])+"\n")
self.logFile.write("Number of Duplicate Acknowledgements received: " + str(static[3])+"\n")
self.sender_socket.close()
self.logFile.close()
time.sleep(1)
global COMPLETE
COMPLETE = True
self.sender_socket.close()
print "TRANSMISSION COMPLETED!"
def send(self, sender_socket):
self.controller.sending()
#start the observer in contronlled
def observer(self):
self.controller.observer()
#fast retranmission detector
def FRTdetect(self, receive_ack):
global COMPLETE
if COMPLETE == False:
global temp
global FRTcounter
if temp == receive_ack:
FRTcounter += 1
if FRTcounter == 3:
FRTcounter = 0
timer = threading.Thread(self.controller.resend(temp,1))
timer.start()
else:
FRTcounter = 0
temp = receive_ack
#write down log for the packets has been sent or recevied during the
#hand shack and disconnect
def writeLOG(self,state,type,seq,size,ack):
T = time.time()-self.iniTime
roundTime = round(T,3)
self.logFile.write(str(state)+" "+str(roundTime)+" "+str(type)+" "+str(seq)+" "+str(size)+" "+str(ack)+"\n")
if __name__ == "__main__":
sender()
while COMPLETE == False:
pass