Package com.turn.ttorrent.client.peer

Examples of com.turn.ttorrent.client.peer.SharingPeer


   * </p>
   *
   * @param search The {@link Peer} specification.
   */
  private SharingPeer getOrCreatePeer(Peer search) {
    SharingPeer peer;

    synchronized (this.peers) {
      logger.trace("Searching for {}...", search);
      if (search.hasPeerId()) {
        peer = this.peers.get(search.getHexPeerId());
        if (peer != null) {
          logger.trace("Found peer (by peer ID): {}.", peer);
          this.peers.put(peer.getHostIdentifier(), peer);
          this.peers.put(search.getHostIdentifier(), peer);
          return peer;
        }
      }

      peer = this.peers.get(search.getHostIdentifier());
      if (peer != null) {
        if (search.hasPeerId()) {
          logger.trace("Recording peer ID {} for {}.",
            search.getHexPeerId(), peer);
          peer.setPeerId(search.getPeerId());
          this.peers.put(search.getHexPeerId(), peer);
        }

        logger.debug("Found peer (by host ID): {}.", peer);
        return peer;
      }

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

      this.peers.put(peer.getHostIdentifier(), peer);
      if (peer.hasPeerId()) {
        this.peers.put(peer.getHexPeerId(), peer);
      }

      return peer;
    }
  }
View Full Code Here


    }

    // Actually choke all chosen peers (if any), except the eventual
    // optimistic unchoke.
    if (choked.size() > 0) {
      SharingPeer randomPeer = choked.toArray(
          new SharingPeer[0])[this.random.nextInt(choked.size())];

      for (SharingPeer peer : choked) {
        if (optimistic && peer == randomPeer) {
          logger.debug("Optimistic unchoke of {}.", peer);
View Full Code Here

      // Attempt to connect to the peer if and only if:
      //   - We're not already connected or connecting to it;
      //   - We're not a seeder (we leave the responsibility
      //     of connecting to peers that need to download
      //     something).
      SharingPeer match = this.getOrCreatePeer(peer);
      if (this.isSeed()) {
        continue;
      }

      synchronized (match) {
        if (!match.isConnected()) {
          this.service.connect(match);
        }
      }
    }
  }
View Full Code Here

      (peerId != null
        ? ByteBuffer.wrap(peerId)
        : (ByteBuffer)null));

    logger.info("Handling new peer connection with {}...", search);
    SharingPeer peer = this.getOrCreatePeer(search);

    try {
      synchronized (peer) {
        if (peer.isConnected()) {
          logger.info("Already connected with {}, closing link.",
            peer);
          channel.close();
          return;
        }

        peer.register(this);
        peer.bind(channel);
      }

      this.connected.put(peer.getHexPeerId(), peer);
      peer.register(this.torrent);
      logger.debug("New peer connection with {} [{}/{}].",
        new Object[] {
          peer,
          this.connected.size(),
          this.peers.size()
        });
    } catch (Exception e) {
      this.connected.remove(peer.getHexPeerId());
      logger.warn("Could not handle new peer connection " +
          "with {}: {}", peer, e.getMessage());
    }
  }
View Full Code Here

TOP

Related Classes of com.turn.ttorrent.client.peer.SharingPeer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.