use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class TagDownloadWithState method setRateLimit.
private void setRateLimit(DownloadManager manager, boolean added) {
synchronized (rate_lock) {
if (added) {
if (manager.getUserData(rate_lock) == null) {
manager.setUserData(rate_lock, "");
manager.addPeerListener(peer_listener, true);
manager.addRateLimiter(upload_limiter, true);
manager.addRateLimiter(download_limiter, false);
}
} else {
if (manager.getUserData(rate_lock) != null) {
manager.setUserData(rate_lock, null);
manager.removeRateLimiter(upload_limiter, true);
manager.removeRateLimiter(download_limiter, false);
manager.removePeerListener(peer_listener);
PEPeerManager pm = manager.getPeerManager();
if (pm != null) {
List<PEPeer> peers = pm.getPeers();
if (upload_rate_limit < 0 || download_rate_limit < 0) {
for (PEPeer peer : peers) {
if (upload_rate_limit < 0) {
peer.setUploadDisabled(peer_listener, false);
}
if (download_rate_limit < 0) {
peer.setDownloadDisabled(peer_listener, false);
}
}
}
}
}
}
}
}
use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class PeerImpl method addListener.
@Override
public void addListener(final PeerListener2 l) {
PEPeerListener core_listener = new PEPeerListener() {
@Override
public void stateChanged(// seems don't need this here
final PEPeer peer, int new_state) {
fireEvent(PeerEvent.ET_STATE_CHANGED, new Integer(new_state));
}
@Override
public void sentBadChunk(// seems don't need this here
final PEPeer peer, int piece_num, int total_bad_chunks) {
fireEvent(PeerEvent.ET_BAD_CHUNK, new Integer[] { new Integer(piece_num), new Integer(total_bad_chunks) });
}
@Override
public void addAvailability(final PEPeer peer, BitFlags peerHavePieces) {
fireEvent(PeerEvent.ET_ADD_AVAILABILITY, peerHavePieces.flags);
}
@Override
public void removeAvailability(final PEPeer peer, BitFlags peerHavePieces) {
fireEvent(PeerEvent.ET_REMOVE_AVAILABILITY, peerHavePieces.flags);
}
protected void fireEvent(final int type, final Object data) {
try {
l.eventOccurred(new PeerEvent() {
@Override
public int getType() {
return (type);
}
@Override
public Object getData() {
return (data);
}
});
} catch (Throwable e) {
Debug.printStackTrace(e);
}
}
};
delegate.addListener(core_listener);
synchronized (this) {
if (peer_listeners == null) {
peer_listeners = new HashMap<>();
}
peer_listeners.put(l, core_listener);
}
}
use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class AEProxySelectorSWTImpl method updateStatus.
private void updateStatus() {
if (!is_visible) {
return;
}
Image icon;
String tip_key;
Proxy active_proxy = proxy_selector.getActiveProxy();
long now = SystemTime.getMonotonousTime();
if (active_proxy == null) {
icon = icon_grey;
tip_key = "label.inactive";
} else {
long last_con = proxy_selector.getLastConnectionTime();
long last_fail = proxy_selector.getLastFailTime();
long con_ago = now - last_con;
long fail_ago = now - last_fail;
if (last_fail < 0) {
icon = icon_green;
tip_key = "PeerManager.status.ok";
} else {
if (fail_ago > 60 * 1000) {
if (con_ago < fail_ago) {
icon = icon_green;
tip_key = "PeerManager.status.ok";
} else {
icon = icon_grey;
tip_key = "PeersView.state.pending";
}
} else {
icon = icon_yellow;
tip_key = "label.con_prob";
}
}
}
if (flag_incoming) {
boolean bad_incoming = false;
if (now - last_bad_peer_update > 15 * 1000) {
last_bad_peer_update = now;
List<DownloadManager> dms = core.getGlobalManager().getDownloadManagers();
for (DownloadManager dm : dms) {
PEPeerManager pm = dm.getPeerManager();
if (pm != null) {
if (pm.getNbRemoteTCPConnections() + pm.getNbRemoteUDPConnections() + pm.getNbRemoteUTPConnections() > 0) {
List<PEPeer> peers = pm.getPeers();
for (PEPeer peer : peers) {
if (peer.isIncoming()) {
if (!peer.isLANLocal()) {
try {
if (InetAddress.getByAddress(HostNameToIPResolver.hostAddressToBytes(peer.getIp())).isLoopbackAddress()) {
continue;
}
} catch (Throwable e) {
}
bad_incoming = true;
break;
}
}
}
}
}
if (bad_incoming) {
break;
}
}
} else if (last_icon == icon_red) {
bad_incoming = true;
}
if (bad_incoming) {
icon = icon_red;
tip_key = "proxy.socks.bad.incoming";
}
}
if (last_icon != icon) {
final Image f_icon = icon;
final String f_key = tip_key;
Utils.execSWTThread(new AERunnable() {
@Override
public void runSupport() {
last_icon = f_icon;
status.setImage(f_icon);
status.setTooltipText(MessageText.getString("proxy.socks.ui.icon.tip", new String[] { MessageText.getString(f_key) }));
}
});
}
}
use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class LightWeightSeed method activate.
protected synchronized void activate(String reason_str, byte activation_reason) {
log("Activate: " + activation_reason + "/" + reason_str);
if (activation_state != ACT_NONE) {
return;
}
try {
disk_manager = new LWSDiskManager(this, data_location);
disk_manager.start();
if (disk_manager.getState() != DiskManager.READY) {
log("Start of '" + getString() + "' failed, disk manager failed = " + disk_manager.getErrorMessage());
} else {
peer_manager = PEPeerManagerFactory.create(announcer.getPeerId(), new LWSPeerManagerAdapter(this, peer_manager_registration), disk_manager);
peer_manager.addListener(new PEPeerManagerListenerAdapter() {
@Override
public void peerAdded(final PEPeerManager manager, final PEPeer peer) {
last_activity_time = SystemTime.getMonotonousTime();
}
@Override
public void peerRemoved(PEPeerManager manager, PEPeer peer) {
last_activity_time = SystemTime.getMonotonousTime();
}
});
peer_manager.start();
announcer.update(true);
activation_state = activation_reason;
last_activity_time = SystemTime.getMonotonousTime();
}
} catch (Throwable e) {
log("Activation of '" + getString() + "' failed", e);
} finally {
if (activation_state != ACT_NONE) {
} else {
deactivate();
}
}
}
use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class DownloadingUnchoker method calculateUnchokes.
@Override
public void calculateUnchokes(int max_to_unchoke, ArrayList<PEPeer> all_peers, boolean force_refresh, boolean check_priority_connections, boolean do_high_latency_peers) {
// one optimistic unchoke for every 10 upload slots
int max_optimistic = ((max_to_unchoke - 1) / 10) + 1;
ArrayList<PEPeer> optimistic_unchokes = new ArrayList<>();
ArrayList<PEPeer> best_peers = new ArrayList<>();
// ensure we never pick more slots than allowed to unchoke
long[] bests = new long[max_to_unchoke];
// get all the currently unchoked peers
for (int i = 0; i < all_peers.size(); i++) {
PEPeer peer = all_peers.get(i);
if (!peer.isChokedByMe()) {
if (UnchokerUtil.isUnchokable(peer, true)) {
unchokes.add(peer);
if (peer.isOptimisticUnchoke()) {
optimistic_unchokes.add(peer);
}
} else {
// should be immediately choked
chokes.add(peer);
}
}
}
if (!force_refresh) {
// ensure current optimistic unchokes remain unchoked
for (int i = 0; i < optimistic_unchokes.size(); i++) {
PEPeer peer = optimistic_unchokes.get(i);
if (i < max_optimistic) {
// add them to the front of the "best" list
best_peers.add(peer);
} else {
// too many optimistics
peer.setOptimisticUnchoke(false);
}
}
}
// fill slots with peers who we are currently downloading the fastest from
int start_pos = best_peers.size();
for (int i = 0; i < all_peers.size(); i++) {
PEPeer peer = all_peers.get(i);
if (peer.isInteresting() && UnchokerUtil.isUnchokable(peer, false) && !best_peers.contains(peer)) {
// viable peer found
long rate = peer.getStats().getSmoothDataReceiveRate();
if (rate > 256) {
// filter out really slow peers
UnchokerUtil.updateLargestValueFirstSort(rate, bests, peer, best_peers, start_pos);
}
}
}
// if we havent yet picked enough slots
if (best_peers.size() < max_to_unchoke) {
start_pos = best_peers.size();
// fill the remaining slots with peers that we have downloaded from in the past
for (int i = 0; i < all_peers.size(); i++) {
PEPeer peer = all_peers.get(i);
if (peer.isInteresting() && UnchokerUtil.isUnchokable(peer, false) && !best_peers.contains(peer)) {
// viable peer found
long uploaded_ratio = peer.getStats().getTotalDataBytesSent() / (peer.getStats().getTotalDataBytesReceived() + (DiskManager.BLOCK_SIZE - 1));
// make sure we haven't already uploaded several times as much data as they've sent us
if (uploaded_ratio < 3) {
UnchokerUtil.updateLargestValueFirstSort(peer.getStats().getTotalDataBytesReceived(), bests, peer, best_peers, start_pos);
}
}
}
}
if (force_refresh) {
// make space for new optimistic unchokes
while (best_peers.size() > max_to_unchoke - max_optimistic) {
best_peers.remove(best_peers.size() - 1);
}
}
// if we still have remaining slots
while (best_peers.size() < max_to_unchoke) {
// just pick one optimistically
PEPeer peer = UnchokerUtil.getNextOptimisticPeer(all_peers, true, true);
// no more new unchokes avail
if (peer == null)
break;
if (!best_peers.contains(peer)) {
best_peers.add(peer);
peer.setOptimisticUnchoke(true);
} else {
// we're here because the given optimistic peer is already "best", but is choked still,
// which means it will continually get picked by the getNextOptimisticPeer() method,
// and we'll loop forever if there are no other peers to choose from
// send unchoke immediately, so it won't get picked optimistically anymore
peer.sendUnChoke();
// all_peers.remove( peer ); //remove from all_peers list, so it won't get picked optimistically anymore //TODO
}
}
// update chokes
for (Iterator<PEPeer> it = unchokes.iterator(); it.hasNext(); ) {
PEPeer peer = it.next();
if (!best_peers.contains(peer)) {
// should be choked
if (best_peers.size() < max_to_unchoke) {
// but there are still slots needed (no optimistics avail), so don't bother choking them
best_peers.add(peer);
} else {
chokes.add(peer);
it.remove();
}
}
}
// update unchokes
for (int i = 0; i < best_peers.size(); i++) {
PEPeer peer = best_peers.get(i);
if (!unchokes.contains(peer)) {
unchokes.add(peer);
}
}
if (do_high_latency_peers) {
UnchokerUtil.doHighLatencyPeers(chokes, unchokes, true);
}
}
Aggregations