from .community import CommunityRouter

class BubbleRouter(CommunityRouter):
    def __call__(self, packet):
        if packet.destination in self.node.neighbours:
            return packet.destination, 'direct', True

        if self.local_community:
            best, lp = self.best_lp()
            if lp > self.lp():
                return best, 'lp', True

        elif self.not_local_community:
            best, gp = self.best_gp()
            if gp > self.gp():
                return best, 'gp', True

        return None, None, False

    def on_send_failure(self, target, packet):
        self.node.buffer.add(packet)