-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.py
79 lines (65 loc) · 2.73 KB
/
link.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
'''
Created on Oct 12, 2016
@author: mwitt_000
'''
import Queue
import threading
## An abstraction of a link between router interfaces
class Link:
## creates a link between two objects by looking up and linking node interfaces.
# @param from_node: node from which data will be transfered
# @param from_intf_num: number of the interface on that node
# @param to_node: node to which data will be transfered
# @param to_intf_num: number of the interface on that node
# @param mtu: link maximum transmission unit
def __init__(self, from_node, from_intf_num, to_node, to_intf_num, mtu):
self.from_node = from_node
self.from_intf_num = from_intf_num
self.to_node = to_node
self.to_intf_num = to_intf_num
self.in_intf = from_node.out_intf_L[from_intf_num]
self.out_intf = to_node.in_intf_L[to_intf_num]
#configure the linking interface MTUs
self.in_intf.mtu = mtu
self.out_intf.mtu = mtu
## called when printing the object
def __str__(self):
return 'Link %s-%d to %s-%d' % (self.from_node, self.from_intf_num, self.to_node, self.to_intf_num)
##transmit a packet from the 'from' to the 'to' interface
def tx_pkt(self):
pkt_S = self.in_intf.get()
if pkt_S is None:
return #return if no packet to transfer
if len(pkt_S) > self.out_intf.mtu:
print('%s: packet "%s" length greater then link mtu (%d)' % (self, pkt_S, self.out_intf.mtu))
return #return without transmitting if packet too big
#otherwise transmit the packet
try:
self.out_intf.put(pkt_S)
print('%s: transmitting packet "%s"' % (self, pkt_S))
except queue.Full:
print('%s: packet lost' % (self))
pass
## An abstraction of the link layer
class LinkLayer:
def __init__(self):
## list of links in the network
self.link_L = []
self.stop = False #for thread termination
##add a Link to the network
def add_link(self, link):
self.link_L.append(link)
##transfer a packet across all links
def transfer(self):
for link in self.link_L:
link.tx_pkt()
## thread target for the network to keep transmitting data across links
def run(self):
print (threading.currentThread().getName() + ': Starting')
while True:
#transfer one packet on all the links
self.transfer()
#terminate
if self.stop:
print (threading.currentThread().getName() + ': Ending')
return