Skip to content
Snippets Groups Projects
Commit ec3b7460 authored by Jarrod Pas's avatar Jarrod Pas
Browse files

Rewrite HCBF

parent fe9091b5
No related branches found
No related tags found
1 merge request!3Version 0.2
def hcbf(self, packet, state): def hcbf(self, packet, state):
if len(self.community) == 0: if 'hcbf_ui' not in packet.stats:
return False packet.stats = {
f'hcbf_{reason}': 0
for reason in ['direct', 'ui', 'lp','ui_lonely', 'lp_lonely',
'cbc', 'ncf' ]
}
stats = packet.stats
dest = packet.destination
community = self.network.community community = self.network.community
c_of = lambda n: community[n] def send(to, reason):
same_community = lambda a, b: c_of(a) == c_of(b) stats[f'hcbf_{reason}'] += 1
self.send(to, packet, reason=reason)
cbc_of = lambda n, d: community.get_cbc(c_of(n), c_of(d))
lp_of = lambda n: community.get_lp(n)
ncf_of = lambda n, d: community.get_ncf(n, c_of(d))
ui_of = lambda n: community.get_ui(n)
max = lambda a, b: a if a[0] >= b[0] else b # case 1: direct delivery
if dest in self.links:
send(dest, 'direct')
return True
if len(c_of(self)) == 0: ui = lambda n: community.get_ui(n)
return False lp = lambda n: community.get_lp(n)
cbc = lambda n: community.get_cbc(n.community, dest.community)
ncf = lambda n: community.get_ncf(n, dest.community)
dest = packet.destination local_community = [
max_cbc = (cbc_of(self, dest), self) met for met in self.links if met in self.community
max_lp = (-1, None) ]
max_ncf= (ncf_of(self, dest), self)
max_ui = (-1, None)
my_lp = lp_of(self) not_local_community = [
my_ui = ui_of(self) met for met in self.links if met not in self.community
]
for met in self.links: if self.community is dest.community and local_community:
if met == dest: max_ui = max(local_community, key=ui)
# direct transfer if ui(max_ui) > ui(self):
self.send(met, packet) send(max_ui, 'ui')
return True return True
elif ui(max_ui) < ui(self):
return False
# ui(max_ui) == ui(self)
if same_community(met, dest): max_lp = max(local_community, key=lp)
met_ui = ui_of(met) if lp(max_lp) > lp(self):
met_lp = lp_of(met) send(max_lp, 'lp')
if met_ui > my_ui: return True
max_ui = max((met_ui, met), max_ui) elif lp(max_lp) < lp(self):
max_lp = max((met_lp, met), max_lp) return False
elif same_community(met, self): # lp(max_lp) == lp(self)
met_cbc = cbc_of(met, dest)
max_cbc = max((met_cbc, met), max_cbc) elif not_local_community:
else: max_ncf = max(not_local_community, key=ncf)
met_ncf = ncf_of(met, dest) if ncf(max_ncf) > ncf(self):
max_ncf = max((met_ncf, met), max_ncf) send(max_ncf, 'ncf')
return True
if max_ui[1] is not None and same_community(max_ui[1], dest): elif ncf(max_ncf) < ncf(self):
self.send(max_ui[1], packet) return False
return True # ncf(max_ncf) == ncf(self)
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: max_cbc = max(not_local_community, key=cbc)
self.send(max_ncf[1], packet) if cbc(max_cbc) > cbc(self):
self.send(max_ui[1], packet) send(max_cbc, 'cbc')
return True return True
elif cbc(max_cbc) < cbc(self):
return False
# cbc(max_cbc) == cbc(self)
elif local_community:
max_ui = max(local_community, key=ui)
if ui(max_ui) > ui(self):
send(max_ui, 'ui_lonely')
return True
elif ui(max_ui) < ui(self):
return False
# ui(max_ui) == ui(self)
if max_lp[1] is not None and max_lp[0] > my_lp: max_lp = max(local_community, key=lp)
self.send(max_lp[1], packet) if lp(max_lp) > lp(self):
return True send(max_lp, 'lp_lonely')
return True
elif lp(max_lp) < lp(self):
return False
# lp(max_lp) == lp(self)
return False return False
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment