def hcbf(self, packet, state): if 'hcbf_ui' not in packet.stats: 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 def send(to, reason): stats[f'hcbf_{reason}'] += 1 self.send(to, packet, reason=reason) # case 1: direct delivery if dest in self.links: send(dest, 'direct') return True ui = community.get_ui lp = community.get_lp cbc = lambda n: community.get_cbc(n.community, dest.community) ncf = lambda n: community.get_ncf(n, dest.community) local_community = [ met for met in self.links if met in self.community ] not_local_community = [ met for met in self.links if met not in self.community ] if self.community is dest.community and local_community: max_ui = max(local_community, key=ui) if ui(max_ui) > ui(self): send(max_ui, 'ui') return True elif ui(max_ui) < ui(self): return False # ui(max_ui) == ui(self) max_lp = max(local_community, key=lp) if lp(max_lp) > lp(self): send(max_lp, 'lp') return True elif lp(max_lp) < lp(self): return False # lp(max_lp) == lp(self) return False if not_local_community: max_cbc = max(not_local_community, key=cbc) if cbc(max_cbc) > cbc(self): send(max_cbc, 'cbc') return True elif cbc(max_cbc) < cbc(self): return False # cbc(max_cbc) == cbc(self) if local_community: max_ncf = max(local_community, key=ncf) if ncf(max_ncf) > ncf(self): send(max_ncf, 'ncf') return True elif ncf(max_ncf) < ncf(self): return False # ncf(max_ncf) == ncf(self) 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) max_lp = max(local_community, key=lp) if lp(max_lp) > lp(self): send(max_lp, 'lp_lonely') return True elif lp(max_lp) < lp(self): return False # lp(max_lp) == lp(self) return False