Search in sources :

Example 26 with PEPeer

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);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) PEPeerManager(com.biglybt.core.peer.PEPeerManager)

Example 27 with PEPeer

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);
    }
}
Also used : BitFlags(com.biglybt.core.peermanager.piecepicker.util.BitFlags) PEPeer(com.biglybt.core.peer.PEPeer) PEPeerListener(com.biglybt.core.peer.PEPeerListener)

Example 28 with PEPeer

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) }));
            }
        });
    }
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) PEPeer(com.biglybt.core.peer.PEPeer) Image(org.eclipse.swt.graphics.Image) DownloadManager(com.biglybt.core.download.DownloadManager) Proxy(java.net.Proxy) PEPeerManager(com.biglybt.core.peer.PEPeerManager)

Example 29 with PEPeer

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();
        }
    }
}
Also used : PEPeerManagerListenerAdapter(com.biglybt.core.peer.PEPeerManagerListenerAdapter) PEPeer(com.biglybt.core.peer.PEPeer) PEPeerManager(com.biglybt.core.peer.PEPeerManager)

Example 30 with PEPeer

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);
    }
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) ArrayList(java.util.ArrayList)

Aggregations

PEPeer (com.biglybt.core.peer.PEPeer)84 PEPeerManager (com.biglybt.core.peer.PEPeerManager)18 DownloadManager (com.biglybt.core.download.DownloadManager)11 ArrayList (java.util.ArrayList)11 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)5 AERunnable (com.biglybt.core.util.AERunnable)5 PEPiece (com.biglybt.core.peer.PEPiece)4 List (java.util.List)4 Image (org.eclipse.swt.graphics.Image)4 DiskManager (com.biglybt.core.disk.DiskManager)3 GlobalManager (com.biglybt.core.global.GlobalManager)3 PEPeerStats (com.biglybt.core.peer.PEPeerStats)3 PEPeerTransport (com.biglybt.core.peer.impl.PEPeerTransport)3 TOTorrent (com.biglybt.core.torrent.TOTorrent)3 Point (org.eclipse.swt.graphics.Point)3 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)2 DownloadManagerPeerListener (com.biglybt.core.download.DownloadManagerPeerListener)2 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)2 BitFlags (com.biglybt.core.peermanager.piecepicker.util.BitFlags)2 Tag (com.biglybt.core.tag.Tag)2