Changeset 9baa6e7


Ignore:
Timestamp:
Oct 29, 2010 8:31:07 PM (9 years ago)
Author:
zzz <zzz@…>
Branches:
master
Children:
502cf72
Parents:
7efb0fa7
Message:

update requests after changing priorities

Location:
apps/i2psnark/java/src/org/klomp/snark
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • apps/i2psnark/java/src/org/klomp/snark/Peer.java

    r7efb0fa7 r9baa6e7  
    394394
    395395  /**
     396   * Tell the other side that we are no longer interested in any of
     397   * the outstanding requests (if any) for this piece.
     398   * @since 0.8.1
     399   */
     400  void cancel(int piece) {
     401    PeerState s = state;
     402    if (s != null)
     403      s.cancelPiece(piece);
     404  }
     405
     406  /**
     407   * Update the request queue.
     408   * Call after adding wanted pieces.
     409   * @since 0.8.1
     410   */
     411  void request() {
     412    PeerState s = state;
     413    if (s != null)
     414      s.addRequest();
     415  }
     416
     417  /**
    396418   * Whether or not the peer is interested in pieces we have. Returns
    397419   * false if not connected.
  • apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java

    r7efb0fa7 r9baa6e7  
    461461
    462462  /**
    463    * Returns true if we don't have the given piece yet.
     463   * @return true if we still want the given piece
    464464   */
    465465  public boolean gotHave(Peer peer, int piece)
     
    587587  public void updatePiecePriorities() {
    588588      int[] pri = storage.getPiecePriorities();
    589       if (pri == null)
     589      if (pri == null) {
     590          _log.debug("Updated piece priorities called but no priorities to set?");
    590591          return;
     592      }
    591593      synchronized(wantedPieces) {
    592594          // Add incomplete and previously unwanted pieces to the list
     
    620622          for (Iterator<Piece> iter = wantedPieces.iterator(); iter.hasNext(); ) {
    621623               Piece p = iter.next();
    622                int id = pri[p.getId()];
    623                if (id >= 0)
    624                    p.setPriority(pri[p.getId()]);
    625                else
     624               int priority = pri[p.getId()];
     625               if (priority >= 0) {
     626                   p.setPriority(priority);
     627               } else {
    626628                   iter.remove();
    627           }
     629                   // cancel all peers
     630                   synchronized(peers) {
     631                       for (Peer peer : peers) {
     632                           peer.cancel(p.getId());
     633                       }
     634                   }
     635               }
     636          }
     637          if (_log.shouldLog(Log.DEBUG))
     638              _log.debug("Updated piece priorities, now wanted: " + wantedPieces);
    628639          // if we added pieces, they will be in-order unless we shuffle
    629640          Collections.shuffle(wantedPieces, _random);
     641
     642          // update request queues, in case we added wanted pieces
     643          // and we were previously uninterested
     644          synchronized(peers) {
     645              for (Peer peer : peers) {
     646                  peer.request();
     647              }
     648          }
    630649      }
    631650  }
  • apps/i2psnark/java/src/org/klomp/snark/PeerState.java

    r7efb0fa7 r9baa6e7  
    5656
    5757  // Outstanding request
    58   private final List outstandingRequests = new ArrayList();
     58  private final List<Request> outstandingRequests = new ArrayList();
    5959  private Request lastRequest = null;
    6060
     
    275275  {
    276276    for (int i = 0; i < outstandingRequests.size(); i++)
    277       if (((Request)outstandingRequests.get(i)).piece == piece)
     277      if (outstandingRequests.get(i).piece == piece)
    278278        return i;
    279279    return -1;
     
    310310    synchronized(this)
    311311      {
    312         req = (Request)outstandingRequests.get(r);
     312        req = outstandingRequests.get(r);
    313313        while (req.piece == piece && req.off != begin
    314314               && r < outstandingRequests.size() - 1)
    315315          {
    316316            r++;
    317             req = (Request)outstandingRequests.get(r);
     317            req = outstandingRequests.get(r);
    318318          }
    319319       
     
    339339            for (int i = 0; i < r; i++)
    340340              {
    341                 Request dropReq = (Request)outstandingRequests.remove(0);
     341                Request dropReq = outstandingRequests.remove(0);
    342342                outstandingRequests.add(dropReq);
    343343                if (!choked)
     
    363363    Request req = null;
    364364    for (int i = 0; i < outstandingRequests.size(); i++) {
    365       Request r1 = (Request)outstandingRequests.get(i);
     365      Request r1 = outstandingRequests.get(i);
    366366      int j = getFirstOutstandingRequest(r1.piece);
    367367      if (j == -1)
    368368        continue;
    369       Request r2 = (Request)outstandingRequests.get(j);
     369      Request r2 = outstandingRequests.get(j);
    370370      if (r2.off > 0 && ((req == null) || (r2.off > req.off)))
    371371        req = r2;
     
    395395    Request req = null;
    396396    for (int i = 0; i < size; i++) {
    397       Request r1 = (Request)outstandingRequests.get(i);
     397      Request r1 = outstandingRequests.get(i);
    398398      if (pc != r1.piece) {
    399399        pc = r1.piece;
     
    420420  }
    421421
     422  /**
     423   *  We now have this piece.
     424   *  Tell the peer and cancel any requests for the piece.
     425   */
    422426  void havePiece(int piece)
    423427  {
     
    425429      _log.debug("Tell " + peer + " havePiece(" + piece + ")");
    426430
    427     synchronized(this)
    428       {
    429431        // Tell the other side that we are no longer interested in any of
    430432        // the outstanding requests for this piece.
     433    cancelPiece(piece);
     434
     435    // Tell the other side that we really have this piece.
     436    out.sendHave(piece);
     437   
     438    // Request something else if necessary.
     439    addRequest();
     440   
     441    synchronized(this)
     442      {
     443        // Is the peer still interesting?
     444        if (lastRequest == null)
     445          setInteresting(false);
     446      }
     447  }
     448
     449  /**
     450   * Tell the other side that we are no longer interested in any of
     451   * the outstanding requests (if any) for this piece.
     452   * @since 0.8.1
     453   */
     454  synchronized void cancelPiece(int piece) {
    431455        if (lastRequest != null && lastRequest.piece == piece)
    432456          lastRequest = null;
    433457       
    434         Iterator it = outstandingRequests.iterator();
     458        Iterator<Request> it = outstandingRequests.iterator();
    435459        while (it.hasNext())
    436460          {
    437             Request req = (Request)it.next();
     461            Request req = it.next();
    438462            if (req.piece == piece)
    439463              {
     
    444468              }
    445469          }
    446       }
    447    
    448     // Tell the other side that we really have this piece.
    449     out.sendHave(piece);
    450    
    451     // Request something else if necessary.
    452     addRequest();
    453    
    454     synchronized(this)
    455       {
    456         // Is the peer still interesting?
    457         if (lastRequest == null)
    458           setInteresting(false);
    459       }
    460   }
    461 
    462   // Starts or resumes requesting pieces.
     470  }
     471
     472  /**
     473   * Starts or resumes requesting pieces.
     474   * @param resend should we resend outstanding requests?
     475   */
    463476  private void request(boolean resend)
    464477  {
     
    477490  /**
    478491   * Adds a new request to the outstanding requests list.
    479    */
    480   synchronized private void addRequest()
     492   * Then send interested if we weren't.
     493   * Then send new requests if not choked.
     494   * If nothing to request, send not interested if we were.
     495   */
     496  synchronized void addRequest()
    481497  {
    482498    boolean more_pieces = true;
Note: See TracChangeset for help on using the changeset viewer.