Пример #1
0
  /**
   * Retrieve a SharingPeer object from the given peer specification.
   *
   * <p>This function tries to retrieve an existing peer object based on the provided peer
   * specification or otherwise instantiates a new one and adds it to our peer repository.
   *
   * @param search The {@link com.turn.ttorrent.common.Peer} specification.
   * @param hexInfoHash
   */
  private SharingPeer getOrCreatePeer(Peer search, String hexInfoHash) {
    SharingPeer peer;

    synchronized (this.peers) {
      logger.trace("Searching for {}...", search);

      List<SharingPeer> found = new ArrayList<SharingPeer>();
      for (SharingPeer p : this.peers) {
        if ((search.getHexPeerId() != null && search.getHexPeerId().equals(p.getHexPeerId()))
            || (search.getHostIdentifier() != null
                && search.getHostIdentifier().equals(p.getHostIdentifier()))) {
          found.add(p);
        }
      }

      for (SharingPeer f : found) {
        if (search.hasPeerId()) {
          f.setPeerId(search.getPeerId());
        }
      }

      for (SharingPeer f : found) {
        if (f.getTorrentHexInfoHash().equals(hexInfoHash)) {
          logger.trace("Found peer: {}.", f);
          return f;
        }
      }

      SharedTorrent torrent = this.torrents.get(hexInfoHash);

      peer = new SharingPeer(search.getIp(), search.getPort(), search.getPeerId(), torrent);
      logger.trace("Created new peer: {}.", peer);

      this.peers.add(peer);

      return peer;
    }
  }
Пример #2
0
  /** @param torrent */
  protected UDPTrackerClient(SharedTorrent torrent, Peer peer, URI tracker)
      throws UnknownHostException {
    super(torrent, peer, tracker);

    /**
     * The UDP announce request protocol only supports IPv4
     *
     * @see http://bittorrent.org/beps/bep_0015.html#ipv6
     */
    if (!(InetAddress.getByName(peer.getIp()) instanceof Inet4Address)) {
      throw new UnsupportedAddressTypeException();
    }

    this.address = new InetSocketAddress(tracker.getHost(), tracker.getPort());

    this.socket = null;
    this.random = new Random();
    this.connectionExpiration = null;
    this.stop = false;
  }