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

Rewrite bubble code

parent 2a26b709
No related branches found
No related tags found
1 merge request!3Version 0.2
def bubble(self, packet, state):
''''''
if len(self.community) == 0:
return False
if 'bubble_lp' not in packet.stats:
packet.stats = {
f'bubble_{reason}': 0
for reason in ['direct', 'lp', 'gp']
}
stats = packet.stats
dest = packet.destination
community = self.network.community
community_of = lambda n: self.network.community[n]
same_community = lambda a, b: community_of(a) == community_of(b)
def send(to, reason):
stats[f'bubble_{reason}'] += 1
self.send(to, packet, reason=reason)
gp_of = lambda n: self.network.community.get_gp(n)
lp_of = lambda n: self.network.community.get_lp(n)
if dest in self.links:
send(dest, 'direct')
return True
max = lambda a, b: a if a[0] >= b[0] else b
lp = lambda n: community.get_lp(n)
gp = lambda n: community.get_gp(n)
max_gp = (-1, None)
local_community = [
met for met in self.links if met in self.community
]
if same_community(self, dest):
max_lp = (lp_of(self), self)
for met in self.links:
if same_community(met, dest):
max_lp = max((lp_of(met), met), max_lp)
not_local_community = [
met for met in self.links if met not in self.community
]
if max_lp[1] is not self:
self.send(max_lp[1], packet)
return True
else:
max_gp = (-1, None)
max_lp = (-1, None)
for met in self.links:
if same_community(met, dest):
max_lp = max((lp_of(met), met), max_lp)
max_gp = max((gp_of(met), met), max_gp)
if max_lp[1] is not None:
self.send(max_lp[1], packet)
if local_community:
max_lp = max(local_community, key=lp)
if lp(max_lp) > lp(self):
send(max_lp, 'lp')
return True
if max_gp[1] is not None:
self.send(max_gp[1], packet)
elif not_local_community:
max_gp = max(not_local_community, key=gp)
if gp(max_gp) > gp(self):
send(max_gp, 'gp')
return True
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