Skip to content
Snippets Groups Projects
hcbf.py 2.45 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jarrod Pas's avatar
    Jarrod Pas committed
    def hcbf(self, packet, state):
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        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
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        community = self.network.community
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        def send(to, reason):
            stats[f'hcbf_{reason}'] += 1
            self.send(to, packet, reason=reason)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        # case 1: direct delivery
        if dest in self.links:
            send(dest, 'direct')
            return True
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
        ui = community.get_ui
        lp = community.get_lp
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        cbc = lambda n: community.get_cbc(n.community, dest.community)
        ncf = lambda n: community.get_ncf(n, dest.community)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        local_community = [
            met for met in self.links if met in self.community
        ]
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        not_local_community = [
            met for met in self.links if met not in self.community
        ]
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        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')
    
    Jarrod Pas's avatar
    Jarrod Pas committed
                return True
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            elif ui(max_ui) < ui(self):
                return False
            # ui(max_ui) == ui(self)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            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)
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            return False
    
        if not_local_community:
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            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)
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        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)
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            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)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            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)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
        return False