示例#1
0
  def OnAround(self, peer):
    nbNeighbours = len(self.state.neighbours)
    # last neighbour found
    last = self.state.neighbours[nbNeighbours-1]
    # best neighbour
    best = self.state.neighbours[0]

    peerPosition = Geometry.relativePosition(peer.position, self.node.position)
    # check if turn ends : we have at least 3 neighbours and either we got back
    # to the first peer (=> turn completed) or our last neighbour was in the left
    # half plane and this peer is in the right half plane
    if ( nbNeighbours > 2 ) and (peer.id == best.id) or (
      not Geometry.inHalfPlane(best.position, self.node.position, last.position)
      and Geometry.inHalfPlane(best.position, self.node.position, peerPosition) ):
        self.state.name = "CONNECTING"      
        # Our awarenesse radius is the distance between us and our closest neighbour,
        # because we are sure to know all the entities between us and the best.
        bestRelativePos = Geometry.relativePosition(best.position,
                                                    self.node.position)
        minDistance = Geometry.distance(bestRelativePos, self.node.position)
        self.node.updateAr(minDistance)

        # register these peers with the peerManager and connect !
        manager = self.node.getPeersManager()
        for p in self.state.neighbours:
          manager.addPeer(p)
          msg = self.HelloMsg()
          self.node.send(p, msg)
      
    else:
      # add this peer to our list of neighbours
      self.state.neighbours.append(peer)
      bestDist = Geometry.distance(best.position, self.node.position)
      msg = self.QueryaroundMsg(best.id, bestDist)
      # send this peer a queryaround message
      self.node.send(peer, msg)