-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.py
More file actions
212 lines (173 loc) · 5.95 KB
/
node.py
File metadata and controls
212 lines (173 loc) · 5.95 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
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
import sys
import socket
import select
import time
import random
from threading import Timer
# Settable parameters
BANDWIDTH = 100000 # 1000 = 1KB, in turn, 10000 = 10KB (B/SEC)
MTU = 1000 # Maximum Transmit Unit for this medium (B)
PDELAY = 0.1 # Propagation delay (s)
RECV_BUFFER = MTU # Receive buffer size
MEDIUM_STATUS = 'I'
NUM_OF_COLLISION = 0
trans_data = 'DATA'
DURING_TRANSMIT = False
START = None
END = 10
TIMEOUT = False
def node():
global MEDIUM_STATUS
global NUM_OF_COLLISION
global DURING_TRANSMIT
global trans_data
global START
global END
global TIMEOUT
t = None
handling_collision = False
before_collision = 0
tout = False
s = connect_to_medium() # Connection
START = time.time()
# recv_packet is the function that receiving packets. Transmission and receiving is separated.
t = Timer(0,recv_packet,[s])
t.start()
# Util before timeout or even if timeout, finishing handling collision.
while time.time()-START < END:
# Random packet generator. It use gauss distribution. Mean is set by 50, and Standard
randnum = generate_random()
if randnum > 54:
# Before real transmit packet routine, save how many occur sequential collision before
before_collision = NUM_OF_COLLISION
# Decide this routine is for handling collsion or new transmit packet.
if NUM_OF_COLLISION == 0 and handling_collision == False:
trans_data = 'DATA' # Data will be stored in packet
while MEDIUM_STATUS == 'B':
#if time.time()-START > END:
# tout=True
# break
continue
else: # Collision handling
# Set the backoff time slot by picking random number
if NUM_OF_COLLISION < 6:
slot = random.randint( 0, 2**(NUM_OF_COLLISION+2)-1 )
else:
slot = random.randint( 0, 2**8-1 )
while slot != 0 and tout == False:
# Before sleep a slot time, if medium is BUSY, then wait until become IDLE
while MEDIUM_STATUS == 'B':
#if time.time()-START > END:
# tout=True
# break
continue
# Sleep a slot time, 1/100 of packet transmission time
time.sleep(MTU/(BANDWIDTH*100))
slot -= 1
if tout == True:
break
# Checking Begin Transmit
DURING_TRANSMIT = True
transmit(s,trans_data) # Transmit a data packet
# Waiting until process receive information that medium is idle. It is required for avoiding self-collision.
while DURING_TRANSMIT == True and time.time()-START < END:
continue
# Checking occur collision or not during this transmit.
# If the transmission occur collision, set handling_collision true.
# If not, just finish the transmit, if handling_collision is true, then reset the collision status.
if NUM_OF_COLLISION!=0 and before_collision != NUM_OF_COLLISION:
handling_collision = True
else:
if handling_collision == True:
NUM_OF_COLLISION = 0
handling_collision = False
else:
time.sleep(0.0001)
# After while routine, set TIMEOUT True and wait until receiving thread is canceled, and then system exit.
TIMEOUT=True
t.cancel()
t.join()
s.close()
sys.exit()
# This function receives packets and divides whether a packet is data packet or signal of medium status.
def recv_packet(s):
global MEDIUM_STATUS
global RECV_BUFFER
global NUM_OF_COLLISION
global trans_data
global DURING_TRANSMIT
global START
global END
global TIMEOUT
# loop until timeout.
while not TIMEOUT:
socket_list = [s]
# Get the list sockets which are readable
ready_to_read, ready_to_write, in_error = select.select(socket_list, [], [], 0)
for sock in ready_to_read:
# Incoming data packet from medium
packet = sock.recv(RECV_BUFFER) # Recive a packet
data = extract_data(packet) # Extract data in a packet
if not data:
print('\nDisconnected')
sys.exit()
else:
# Sensing the medium status
if data[0:6] == '\t:!?:\t':
#print 'sign ' + data[6]
# Sensing Medium Collision
if data[6]=='C':
NUM_OF_COLLISION += 1
# Sensing Medium become BUSY or IDLE
else:
MEDIUM_STATUS = data[6]
if MEDIUM_STATUS == 'I' and DURING_TRANSMIT==True:
DURING_TRANSMIT = False
# Receive a packet
else:
print("\nReceive a packet : %s" % data)
sys.exit()
# Connect a node to medium ----- recommand not to modify
def connect_to_medium():
host = '127.0.0.1' # Local host address
port = 9009 # Medium port number
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
try:
s.connect((host, port))
except:
print('Unable to connect')
sys.exit()
print('Connected. You can start sending packets')
return s
# Make and transmit a data packet
def transmit (s, trans_data):
global PDELAY
packet = trans_data
if len(packet) > MTU:
print('Cannot transmit a packet -----> packet size exceeds MTU')
else:
packet = packet + '0'*(MTU-(len(trans_data)))
s.send(packet)
time.sleep(PDELAY)
print('Transmit a packet')
# Extract data
def extract_data(packet):
i=0
for c in packet:
if c == '0':
break
else:
i=i+1
continue
data = packet[0:i]
return data
# Generate random number with random.gauss() function. random.gauss() pick a number according to gaussian distribution.
# The first option is mean, and second option is standard deviation.
def generate_random():
num = random.gauss(50, 1)
while num <0 or num >100:
num = random.gauss(50, 1)
return num
if __name__ == "__main__":
sys.exit(node())