-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathentity1.py
More file actions
167 lines (144 loc) · 4.89 KB
/
entity1.py
File metadata and controls
167 lines (144 loc) · 4.89 KB
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
from socket import *
from frame import *
from threading import *
import time
import pickle
import random
import string
from network import transmit
HOST = gethostbyname(gethostname()) # The receiver's hostname or IP address
PORT = 9999 # The port used by the receiver
address=(HOST,PORT)
mySocket=socket(AF_INET,SOCK_DGRAM)
mySocket.bind(address)
mySocket.settimeout(3)
otherSocket=socket(AF_INET, SOCK_DGRAM)
ADDRESS=(HOST,9998) #address where the other entity is located
otherSocket.settimeout(0.01)
t0=time.time()
sentPackets=0
newPackets=0
resentPackets=0
newRecieved=0
totalRecieved=0
bytesRecieved=0
lock=Lock()
#Generate random words of given length
def randomword(length):
letters = string.ascii_lowercase
return ''.join(random.choice(letters) for i in range(length))
#Generate packets of given size
def generatePacket(index, size=0):
length=(size-256)/8
data=randomword(length)
d=dataframe(size,index,data)
return d
#This is our client function
#It generates and sends packet over the network and recieves acknowledgement for the packets sent
def sender():
base=0
window=7 # Window size
sendNext=0 # Packet index to be sent nest
timeout=1 # maximum waiting time before ack is recieved/packet resent
lastackreceived= time.time() # Time when the previous packets ack recieved for the first time
packets=[] # window packets generated stored stored in this
last_ack=-1 # Index of the previous ack recieved
global sentPackets, newPackets, resentPackets
#SENDING PACKETS
while True:
if(sendNext<base+window):
size=int(random.uniform(512,2048))
pkt=generatePacket(sendNext,size)
packets.append(pkt)
sendNext+=1
pickledpkt=pickle.dumps(pkt)
transmit(otherSocket,pickledpkt,ADDRESS)
with lock:
sentPackets+=1
newPackets+=1
#RECEIPT OF AN ACK
try:
pickledack,sss = otherSocket.recvfrom(1024)
ack = pickle.loads(pickledack)
#Check the object being received is of type ackframe
if ack.__class__.__name__=="ackframe":
last_ack=ack.index
# slide window and reset timer
while ack.index>=base and packets:
lastackreceived = time.time()
for i in range(ack.index-base+1):
del packets[0]
base = ack.index + 1
# else:
# print "packet in ack ",ack.index
#TIMEOUT
except:
#Resend the packets that were in the window
if(time.time()-lastackreceived>timeout):
for i in packets:
transmit(otherSocket,pickle.dumps(i),ADDRESS)
with lock:
sentPackets+=1
resentPackets+=1
#This is our server function
#It recieves packet over the network and sends acknowledgement for the packets recieved
def receiver():
global newRecieved, totalRecieved, bytesRecieved
expected_ack_idx=0
last_pkt_received_time = time.time()
start_time = time.time()
while True:
try:
packet_raw,sender_address=mySocket.recvfrom(4096)
packet=pickle.loads(packet_raw)
#Check the object being received is of type dataframe
if packet.__class__.__name__=="dataframe":
with lock:
totalRecieved+=1
bytesRecieved+=packet.length
# Send the next ack if packet recieved in-order
if(packet.index==expected_ack_idx):
with lock:
newRecieved+=1
send_packet=ackframe(256,expected_ack_idx)
expected_ack_idx += 1
transmit(mySocket,pickle.dumps(send_packet),sender_address)
# Send the previous ack
else:
send_packet=ackframe(256,expected_ack_idx-1)
transmit(mySocket,pickle.dumps(send_packet),sender_address)
except:
#Nothing recieved
x=0
def summary():
global sentPackets, newPackets, resentPackets, newRecieved, totalRecieved, t0, bytesRecieved
#Summary of previous 2 seconds
while True:
# print "summary"
if time.time()-t0>2:
with lock:
print ""
print "A brief summary of previous 2 seconds"
print "Total number of packets sent: ", sentPackets
print "Number of new packets generated: ", newPackets
print "Number of packets resent: ", resentPackets
print "Total number of packets recieved: ", totalRecieved
print "Number of packets recieved in order: ", newRecieved
print "Number of bytes recieved: ", bytesRecieved
print "##############################################"
t0=time.time()
sentPackets=0
newPackets=0
resentPackets=0
newRecieved=0
totalRecieved=0
bytesRecieved=0
if __name__=="__main__":
t0=time.time()
ts=Thread(target=sender)
tr=Thread(target=receiver)
su=Thread(target=summary)
# start the 3 threads
ts.start()
tr.start()
su.start()