Skip to content
Snippets Groups Projects
hcbf.py 1.99 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
    
    
    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
        c_of = lambda n: community[n]
        same_community = lambda a, b: c_of(a) == c_of(b)
    
        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)
    
    Jarrod Pas's avatar
    Jarrod Pas committed
    
        max = lambda a, b: a if a[0] >= b[0] else b
    
    
    Jarrod Pas's avatar
    Jarrod Pas committed
        if len(c_of(self)) == 0:
    
    Jarrod Pas's avatar
    Jarrod Pas committed
            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