Search in sources :

Example 1 with RateLimiter

use of com.biglybt.pif.network.RateLimiter in project BiglyBT by BiglySoftware.

the class PeerImpl method getRateLimiters.

@Override
public RateLimiter[] getRateLimiters(boolean is_upload) {
    LimitedRateGroup[] limiters = delegate.getRateLimiters(is_upload);
    RateLimiter[] result = new RateLimiter[limiters.length];
    int pos = 0;
    for (LimitedRateGroup l : limiters) {
        if (l instanceof PluginLimitedRateGroup) {
            result[pos++] = UtilitiesImpl.unwrapLmiter((PluginLimitedRateGroup) l);
        }
    }
    if (pos == result.length) {
        return (result);
    }
    RateLimiter[] result_mod = new RateLimiter[pos];
    System.arraycopy(result, 0, result_mod, 0, pos);
    return (result_mod);
}
Also used : PluginLimitedRateGroup(com.biglybt.pifimpl.local.utils.UtilitiesImpl.PluginLimitedRateGroup) RateLimiter(com.biglybt.pif.network.RateLimiter) PluginLimitedRateGroup(com.biglybt.pifimpl.local.utils.UtilitiesImpl.PluginLimitedRateGroup) LimitedRateGroup(com.biglybt.core.networkmanager.LimitedRateGroup)

Example 2 with RateLimiter

use of com.biglybt.pif.network.RateLimiter in project BiglyBT by BiglySoftware.

the class SpeedLimitHandler method peersAdded.

private void peersAdded(Download download, PeerManager peer_manager, Peer[] peers) {
    IPSet[] sets;
    long[][][] set_ranges;
    Set[] set_ccs;
    Set[] set_nets;
    boolean has_ccs = false;
    boolean has_nets = false;
    Set<String> category_or_tags = null;
    TagManager tm = TagManagerFactory.getTagManager();
    synchronized (this) {
        int len = current_ip_sets.size();
        sets = new IPSet[len];
        set_ranges = new long[len][][];
        set_ccs = new Set[len];
        set_nets = new Set[len];
        int pos = 0;
        for (IPSet set : current_ip_sets.values()) {
            sets[pos] = set;
            set_ranges[pos] = set.getRanges();
            set_ccs[pos] = set.getCountryCodes();
            set_nets[pos] = set.getNetworks();
            if (set_ccs[pos].size() > 0) {
                has_ccs = true;
            }
            if (set_nets[pos].size() > 0) {
                has_nets = true;
            }
            pos++;
            if (category_or_tags == null && set.getCategoriesOrTags() != null) {
                category_or_tags = new HashSet<>();
                String cat = download.getAttribute(category_attribute);
                if (cat != null && cat.length() > 0) {
                    category_or_tags.add(cat);
                }
                List<Tag> tags = tm.getTagsForTaggable(TagType.TT_DOWNLOAD_MANUAL, PluginCoreUtils.unwrap(download));
                for (Tag t : tags) {
                    category_or_tags.add(t.getTagName(true));
                }
            }
        }
    }
    if (sets.length == 0) {
        return;
    }
    for (Peer peer : peers) {
        List<RateLimiter> rlu_tbr;
        List<RateLimiter> rld_tbr;
        synchronized (RL_TO_BE_REMOVED_LOCK) {
            rlu_tbr = (List<RateLimiter>) peer.getUserData(RLU_TO_BE_REMOVED_KEY);
            rld_tbr = (List<RateLimiter>) peer.getUserData(RLD_TO_BE_REMOVED_KEY);
            if (rlu_tbr != null) {
                peer.setUserData(RLU_TO_BE_REMOVED_KEY, null);
            }
            if (rld_tbr != null) {
                peer.setUserData(RLD_TO_BE_REMOVED_KEY, null);
            }
        }
        try {
            long[] entry = (long[]) peer.getUserData(ip_set_peer_key);
            long l_address;
            if (entry == null) {
                l_address = 0;
                String ip = peer.getIp();
                if (!ip.contains(":")) {
                    byte[] bytes = HostNameToIPResolver.hostAddressToBytes(ip);
                    if (bytes != null) {
                        l_address = ((long) ((bytes[0] << 24) & 0xff000000 | (bytes[1] << 16) & 0x00ff0000 | (bytes[2] << 8) & 0x0000ff00 | bytes[3] & 0x000000ff)) & 0xffffffffL;
                    }
                }
                entry = new long[] { l_address };
                peer.setUserData(ip_set_peer_key, entry);
            } else {
                l_address = entry[0];
            }
            String peer_cc = null;
            String peer_net = null;
            if (has_ccs) {
                String[] details = PeerUtils.getCountryDetails(peer);
                if (details != null && details.length > 0) {
                    peer_cc = details[0];
                }
            }
            if (has_nets) {
                peer_net = AENetworkClassifier.categoriseAddress(peer.getIp());
            }
            Set<IPSet> added_to_sets = new HashSet<>();
            if (l_address != 0) {
                for (int i = 0; i < set_ranges.length; i++) {
                    long[][] ranges = set_ranges[i];
                    if (ranges.length == 0) {
                        continue;
                    }
                    IPSet set = sets[i];
                    boolean is_inverse = set.isInverse();
                    Set<String> set_cats_or_tags = set.getCategoriesOrTags();
                    if (set_cats_or_tags == null || new HashSet<>(set_cats_or_tags).removeAll(category_or_tags)) {
                        boolean hit = false;
                        for (long[] range : ranges) {
                            if (l_address >= range[0] && l_address <= range[1]) {
                                hit = true;
                                if (!is_inverse) {
                                    addLimiters(peer_manager, peer, set, rlu_tbr, rld_tbr);
                                    added_to_sets.add(set);
                                }
                                break;
                            }
                        }
                        if (is_inverse && !hit) {
                            addLimiters(peer_manager, peer, set, rlu_tbr, rld_tbr);
                            added_to_sets.add(set);
                        }
                    }
                }
            }
            if (peer_cc != null) {
                for (int i = 0; i < set_ccs.length; i++) {
                    IPSet set = sets[i];
                    if (added_to_sets.contains(set)) {
                        continue;
                    }
                    Set<String> ccs = set_ccs[i];
                    if (ccs.size() == 0) {
                        continue;
                    }
                    boolean not_inverse = !set.isInverse();
                    Set<String> set_cats_or_tags = set.getCategoriesOrTags();
                    if (set_cats_or_tags == null || new HashSet<>(set_cats_or_tags).removeAll(category_or_tags)) {
                        boolean hit = ccs.contains(peer_cc);
                        if (hit == not_inverse) {
                            addLimiters(peer_manager, peer, set, rlu_tbr, rld_tbr);
                            added_to_sets.add(set);
                        }
                    }
                }
            }
            if (peer_net != null) {
                String pub_peer_net = null;
                String pub_lan = null;
                if (peer_net == AENetworkClassifier.AT_PUBLIC) {
                    try {
                        byte[] address = InetAddress.getByName(peer.getIp()).getAddress();
                        pub_peer_net = address.length == 4 ? NET_IPV4 : NET_IPV6;
                    } catch (Throwable e) {
                    }
                    if (peer.isLANLocal()) {
                        pub_lan = NET_LAN;
                    } else {
                        pub_lan = NET_WAN;
                    }
                }
                for (int i = 0; i < set_nets.length; i++) {
                    IPSet set = sets[i];
                    if (added_to_sets.contains(set)) {
                        continue;
                    }
                    Set<String> nets = set_nets[i];
                    if (nets.size() == 0) {
                        continue;
                    }
                    boolean not_inverse = !set.isInverse();
                    Set<String> set_cats_or_tags = set.getCategoriesOrTags();
                    if (set_cats_or_tags == null || new HashSet<>(set_cats_or_tags).removeAll(category_or_tags)) {
                        boolean hit = nets.contains(peer_net);
                        if (!hit) {
                            if (pub_peer_net != null) {
                                hit = nets.contains(pub_peer_net);
                            }
                            if (!hit && pub_lan != null) {
                                hit = nets.contains(pub_lan);
                            }
                        }
                        if (hit == not_inverse) {
                            addLimiters(peer_manager, peer, set, rlu_tbr, rld_tbr);
                            added_to_sets.add(set);
                        }
                    }
                }
            }
        } finally {
            if (rlu_tbr != null) {
                for (RateLimiter l : rlu_tbr) {
                    peer.removeRateLimiter(l, true);
                }
            }
            if (rld_tbr != null) {
                for (RateLimiter l : rld_tbr) {
                    peer.removeRateLimiter(l, false);
                }
            }
        }
    }
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) Peer(com.biglybt.pif.peers.Peer) RateLimiter(com.biglybt.pif.network.RateLimiter)

Example 3 with RateLimiter

use of com.biglybt.pif.network.RateLimiter in project BiglyBT by BiglySoftware.

the class SpeedLimitHandler method checkIPSetsSupport.

private synchronized void checkIPSetsSupport() {
    final com.biglybt.pif.download.DownloadManager download_manager = plugin_interface.getDownloadManager();
    if (current_dml != null) {
        current_dml.destroy();
        current_dml = null;
    }
    Download[] downloads = download_manager.getDownloads();
    for (Download dm : downloads) {
        PeerManager pm = dm.getPeerManager();
        if (pm != null) {
            Peer[] peers = pm.getPeers();
            for (Peer peer : peers) {
                RateLimiter[] lims = peer.getRateLimiters(false);
                for (RateLimiter l : lims) {
                    if (ip_set_rate_limiters_down.containsValue(l)) {
                        synchronized (RL_TO_BE_REMOVED_LOCK) {
                            List<RateLimiter> to_be_removed = (List<RateLimiter>) peer.getUserData(RLD_TO_BE_REMOVED_KEY);
                            if (to_be_removed == null) {
                                to_be_removed = new ArrayList<>();
                                peer.setUserData(RLD_TO_BE_REMOVED_KEY, to_be_removed);
                            }
                            to_be_removed.add(l);
                        }
                    // defer as removing the rate limiter and then re-adding it gives time for
                    // quite a lot to happen in between
                    // peer.removeRateLimiter( l , false );
                    }
                }
                lims = peer.getRateLimiters(true);
                for (RateLimiter l : lims) {
                    if (ip_set_rate_limiters_up.containsValue(l)) {
                        synchronized (RL_TO_BE_REMOVED_LOCK) {
                            List<RateLimiter> to_be_removed = (List<RateLimiter>) peer.getUserData(RLU_TO_BE_REMOVED_KEY);
                            if (to_be_removed == null) {
                                to_be_removed = new ArrayList<>();
                                peer.setUserData(RLU_TO_BE_REMOVED_KEY, to_be_removed);
                            }
                            to_be_removed.add(l);
                        }
                    // peer.removeRateLimiter( l , true );
                    }
                }
            }
        }
    }
    ip_set_rate_limiters_down.clear();
    ip_set_rate_limiters_up.clear();
    boolean has_cats_or_tags = false;
    for (IPSet set : current_ip_sets.values()) {
        ip_set_rate_limiters_down.put(set.getName(), set.getDownLimiter());
        ip_set_rate_limiters_up.put(set.getName(), set.getUpLimiter());
        if (set.getCategoriesOrTags() != null) {
            has_cats_or_tags = true;
        }
        set.removeAllPeers();
    }
    if (current_ip_sets.size() == 0) {
        if (ip_set_event != null) {
            ip_set_event.cancel();
            ip_set_event = null;
        }
    } else {
        if (ip_set_event == null) {
            ip_set_event = SimpleTimer.addPeriodicEvent("speed handler ip set scheduler", 1000, new TimerEventPerformer() {

                private int tick_count;

                @Override
                public void perform(TimerEvent event) {
                    tick_count++;
                    synchronized (SpeedLimitHandler.this) {
                        for (IPSet set : current_ip_sets.values()) {
                            set.updateStats(tick_count);
                        }
                    /*
									if ( tick_count % 30 == 0 ){

										String str = "";

										for ( IPSet set: current_ip_sets.values()){

											str += (str.length()==0?"":", ") + set.getString();
										}

										logger.log( str );
									}
									*/
                    }
                }
            });
        }
        current_dml = new DML(download_manager, has_cats_or_tags);
    }
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) Peer(com.biglybt.pif.peers.Peer) RateLimiter(com.biglybt.pif.network.RateLimiter) PEPeerManager(com.biglybt.core.peer.PEPeerManager) PeerManager(com.biglybt.pif.peers.PeerManager) Download(com.biglybt.pif.download.Download)

Aggregations

RateLimiter (com.biglybt.pif.network.RateLimiter)3 PEPeer (com.biglybt.core.peer.PEPeer)2 Peer (com.biglybt.pif.peers.Peer)2 LimitedRateGroup (com.biglybt.core.networkmanager.LimitedRateGroup)1 PEPeerManager (com.biglybt.core.peer.PEPeerManager)1 Download (com.biglybt.pif.download.Download)1 PeerManager (com.biglybt.pif.peers.PeerManager)1 PluginLimitedRateGroup (com.biglybt.pifimpl.local.utils.UtilitiesImpl.PluginLimitedRateGroup)1