Newer
Older
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
def hcbf(self, packet, state):
if len(self.community) == 0:
return False
community_of = lambda n: self.network.community[n]
same_community = lambda a, b: community_of(a) == community_of(b)
cbc_of = lambda n, d: self.network.community.get_cbc(n, d)
lp_of = lambda n: self.network.community.get_lp(n)
ncf_of = lambda n, d: self.network.community.get_ncf(n, d)
ui_of = lambda n: self.network.community.get_ui(n)
max = lambda a, b: a if a[0] >= b[0] else b
if len(community_of(self)) == 0:
return False
dest = packet.destination
max_cbc = (cbc_of(self, dest), self)
max_lp = (-1, None)
max_ncf= (ncf_of(self, dest), self)
max_ui = (-1, None)
my_lp = lp_of(self)
my_ui = ui_of(self)
for met in self.links:
if met == dest:
# direct transfer
self.send(met, packet)
return True
if same_community(met, dest):
met_ui = ui_of(met)
met_lp = lp_of(met)
if met_ui > my_ui:
max_ui = max((met_ui, met), max_ui)
max_lp = max((met_lp, met), max_lp)
elif same_community(met, self):
met_cbc = cbc_of(met, dest)
max_cbc = max((met_cbc, met), max_cbc)
else:
met_ncf = ncf_of(met, dest)
max_ncf = max((met_ncf, met), max_ncf)
if max_ui[1] is not None and same_community(max_ui[1], dest):
self.send(max_ui[1], packet)
return True
if max_lp[1] is not None and same_community(max_lp[1], dest):
self.send(max_lp[1], packet)
return True
if max_cbc[1] is not self:
self.send(max_cbc[1], packet)
return True
if max_ncf[1] is not self:
self.send(max_ncf[1], packet)
return True
if max_ui[1] is not None and max_ui[0] > my_ui:
self.send(max_ncf[1], packet)
self.send(max_ui[1], packet)
return True
if max_lp[1] is not None and max_lp[0] > my_lp:
self.send(max_lp[1], packet)
return True
return False