Skip to content
Snippets Groups Projects
hcbf.py 2.03 KiB
Newer Older
  • Learn to ignore specific revisions
  • Jarrod Pas's avatar
    Jarrod Pas committed
    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