* i2psnark: Refactor request tracking to prevent possible deadlocks

This commit is contained in:
zzz
2011-01-05 20:21:29 +00:00
parent 6822bf7978
commit e826ce723a
4 changed files with 49 additions and 50 deletions

View File

@ -12,13 +12,15 @@ class Piece implements Comparable {
private int id;
private Set<PeerID> peers;
private boolean requested;
/** @since 0.8.3 */
private Set<PeerID> requests;
/** @since 0.8.1 */
private int priority;
public Piece(int id) {
this.id = id;
this.peers = new ConcurrentHashSet();
this.peers = new ConcurrentHashSet(I2PSnarkUtil.MAX_CONNECTIONS);
this.requests = new ConcurrentHashSet(2);
}
/**
@ -49,12 +51,36 @@ class Piece implements Comparable {
}
public int getId() { return this.id; }
/** @deprecated unused */
public Set<PeerID> getPeers() { return this.peers; }
public boolean addPeer(Peer peer) { return this.peers.add(peer.getPeerID()); }
public boolean removePeer(Peer peer) { return this.peers.remove(peer.getPeerID()); }
public boolean isRequested() { return this.requested; }
public void setRequested(boolean requested) { this.requested = requested; }
public boolean isRequested() { return !this.requests.isEmpty(); }
/**
* Since 0.8.3, keep track of who is requesting here,
* to avoid deadlocks from querying each peer.
*/
public void setRequested(Peer peer, boolean requested) {
if (requested)
this.requests.add(peer.getPeerID());
else
this.requests.remove(peer.getPeerID());
}
/**
* Is peer requesting this piece?
* @since 0.8.3
*/
public boolean isRequestedBy(Peer peer) {
return this.requests.contains(peer.getPeerID());
}
/**
* How many peers are requesting this piece?
* @since 0.8.3
*/
public int getRequestCount() {
return this.requests.size();
}
/** @return default 0 @since 0.8.1 */
public int getPriority() { return this.priority; }