Search in sources :

Example 36 with TOTorrent

use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.

the class LWSPeerManagerAdapter method getTorrentInfoDict.

@Override
public byte[] getTorrentInfoDict(PEPeer peer) {
    try {
        byte[] data = md_info_dict_ref.get();
        if (data == null) {
            TOTorrent torrent = PluginCoreUtils.unwrap(lws.getTorrent());
            data = BEncoder.encode((Map) torrent.serialiseToMap().get("info"));
            md_info_dict_ref = new WeakReference<>(data);
        }
        return (data);
    } catch (Throwable e) {
        return (null);
    }
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent) Map(java.util.Map)

Example 37 with TOTorrent

use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.

the class TRTrackerAnnouncerMuxer method split.

protected void split(boolean first_time) throws TRTrackerAnnouncerException {
    String[] networks = f_provider == null ? null : f_provider.getNetworks();
    boolean force_recreate = false;
    if (!first_time) {
        if (current_networks != networks) {
            if (current_networks == null || networks == null) {
                force_recreate = true;
            } else {
                if (networks.length != current_networks.length) {
                    force_recreate = true;
                } else {
                    for (String net1 : current_networks) {
                        boolean match = false;
                        for (String net2 : networks) {
                            if (net1 == net2) {
                                match = true;
                            }
                        }
                        if (!match) {
                            force_recreate = true;
                            break;
                        }
                    }
                }
            }
        }
    }
    current_networks = networks;
    TRTrackerAnnouncerHelper to_activate = null;
    synchronized (this) {
        if (stopped || destroyed) {
            return;
        }
        TOTorrent torrent = getTorrent();
        TOTorrentAnnounceURLSet[] sets = torrent.getAnnounceURLGroup().getAnnounceURLSets();
        if (sets.length == 0) {
            sets = new TOTorrentAnnounceURLSet[] { torrent.getAnnounceURLGroup().createAnnounceURLSet(new URL[] { torrent.getAnnounceURL() }) };
        } else {
            boolean found_decentralised = false;
            boolean modified = false;
            for (int i = 0; i < sets.length; i++) {
                TOTorrentAnnounceURLSet set = sets[i];
                URL[] urls = set.getAnnounceURLs().clone();
                for (int j = 0; j < urls.length; j++) {
                    URL u = urls[j];
                    if (u != null && TorrentUtils.isDecentralised(u)) {
                        if (found_decentralised) {
                            modified = true;
                            urls[j] = null;
                        } else {
                            found_decentralised = true;
                        }
                    }
                }
            }
            if (modified) {
                List<TOTorrentAnnounceURLSet> s_list = new ArrayList<>();
                for (TOTorrentAnnounceURLSet set : sets) {
                    URL[] urls = set.getAnnounceURLs();
                    List<URL> u_list = new ArrayList<>(urls.length);
                    for (URL u : urls) {
                        if (u != null) {
                            u_list.add(u);
                        }
                    }
                    if (u_list.size() > 0) {
                        s_list.add(torrent.getAnnounceURLGroup().createAnnounceURLSet(u_list.toArray(new URL[u_list.size()])));
                    }
                }
                sets = s_list.toArray(new TOTorrentAnnounceURLSet[s_list.size()]);
            }
        }
        List<TOTorrentAnnounceURLSet[]> new_sets = new ArrayList<>();
        if (is_manual || sets.length < 2) {
            new_sets.add(sets);
        } else {
            List<TOTorrentAnnounceURLSet> list = new ArrayList<>(Arrays.asList(sets));
            while (list.size() > 0) {
                TOTorrentAnnounceURLSet set1 = list.remove(0);
                boolean done = false;
                URL[] urls1 = set1.getAnnounceURLs();
                if (urls1.length == 1) {
                    URL url1 = urls1[0];
                    String prot1 = url1.getProtocol().toLowerCase();
                    String host1 = url1.getHost();
                    for (int i = 0; i < list.size(); i++) {
                        TOTorrentAnnounceURLSet set2 = list.get(i);
                        URL[] urls2 = set2.getAnnounceURLs();
                        if (urls2.length == 1) {
                            URL url2 = urls2[0];
                            String prot2 = url2.getProtocol().toLowerCase();
                            String host2 = url2.getHost();
                            if (host1.equals(host2)) {
                                if ((prot1.equals("udp") && prot2.startsWith("http")) || (prot2.equals("udp") && prot1.startsWith("http"))) {
                                    list.remove(i);
                                    new_sets.add(new TOTorrentAnnounceURLSet[] { set1, set2 });
                                    done = true;
                                }
                            }
                        }
                    }
                }
                if (!done) {
                    new_sets.add(new TOTorrentAnnounceURLSet[] { set1 });
                }
            }
        }
        // work out the difference
        Iterator<TOTorrentAnnounceURLSet[]> ns_it = new_sets.iterator();
        // need to copy list as we modify it and returned list ain't thread safe
        List<TRTrackerAnnouncerHelper> existing_announcers = new ArrayList<>(announcers.getList());
        List<TRTrackerAnnouncerHelper> new_announcers = new ArrayList<>();
        if (!force_recreate) {
            while (ns_it.hasNext()) {
                TOTorrentAnnounceURLSet[] ns = ns_it.next();
                Iterator<TRTrackerAnnouncerHelper> a_it = existing_announcers.iterator();
                while (a_it.hasNext()) {
                    TRTrackerAnnouncerHelper a = a_it.next();
                    TOTorrentAnnounceURLSet[] os = a.getAnnounceSets();
                    if (same(ns, os)) {
                        ns_it.remove();
                        a_it.remove();
                        new_announcers.add(a);
                        break;
                    }
                }
            }
        }
        // first remove dht ones from the equation
        TRTrackerAnnouncerHelper existing_dht_announcer = null;
        TOTorrentAnnounceURLSet[] new_dht_set = null;
        ns_it = new_sets.iterator();
        while (ns_it.hasNext()) {
            TOTorrentAnnounceURLSet[] x = ns_it.next();
            if (TorrentUtils.isDecentralised(x[0].getAnnounceURLs()[0])) {
                new_dht_set = x;
                ns_it.remove();
                break;
            }
        }
        Iterator<TRTrackerAnnouncerHelper> an_it = existing_announcers.iterator();
        while (an_it.hasNext()) {
            TRTrackerAnnouncerHelper a = an_it.next();
            TOTorrentAnnounceURLSet[] x = a.getAnnounceSets();
            if (TorrentUtils.isDecentralised(x[0].getAnnounceURLs()[0])) {
                existing_dht_announcer = a;
                an_it.remove();
                break;
            }
        }
        if (existing_dht_announcer != null && new_dht_set != null) {
            new_announcers.add(existing_dht_announcer);
        } else if (existing_dht_announcer != null) {
            activated.remove(existing_dht_announcer);
            existing_dht_announcer.destroy();
        } else if (new_dht_set != null) {
            TRTrackerAnnouncerHelper a = create(torrent, networks, new_dht_set);
            new_announcers.add(a);
        }
        // create any new ones required
        ns_it = new_sets.iterator();
        while (ns_it.hasNext()) {
            TOTorrentAnnounceURLSet[] s = ns_it.next();
            TRTrackerAnnouncerHelper a = create(torrent, networks, s);
            new_announcers.add(a);
        }
        // finally fix up the announcer list to represent the new state
        Iterator<TRTrackerAnnouncerHelper> a_it = announcers.iterator();
        while (a_it.hasNext()) {
            TRTrackerAnnouncerHelper a = a_it.next();
            if (!new_announcers.contains(a)) {
                a_it.remove();
                try {
                    if (activated.contains(a) && torrent.getPrivate() && a instanceof TRTrackerBTAnnouncerImpl) {
                        URL url = a.getTrackerURL();
                        if (url != null) {
                            forceStop((TRTrackerBTAnnouncerImpl) a, networks, url);
                        }
                    }
                } finally {
                    if (Logger.isEnabled()) {
                        Logger.log(new LogEvent(getTorrent(), LOGID, "Deactivating " + getString(a.getAnnounceSets())));
                    }
                    activated.remove(a);
                    a.destroy();
                }
            }
        }
        a_it = new_announcers.iterator();
        while (a_it.hasNext()) {
            TRTrackerAnnouncerHelper a = a_it.next();
            if (!announcers.contains(a)) {
                announcers.add(a);
            }
        }
        if (!is_manual && announcers.size() > 0) {
            if (activated.size() == 0) {
                TRTrackerAnnouncerHelper a = announcers.get(0);
                if (Logger.isEnabled()) {
                    Logger.log(new LogEvent(getTorrent(), LOGID, "Activating " + getString(a.getAnnounceSets())));
                }
                activated.add(a);
                last_activation_time = SystemTime.getMonotonousTime();
                if (provider != null) {
                    to_activate = a;
                }
            }
            setupActivationCheck(ACT_CHECK_INIT_DELAY);
        }
    }
    if (to_activate != null) {
        if (complete) {
            to_activate.complete(true);
        } else {
            to_activate.update(false);
        }
    }
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) URL(java.net.URL) TRTrackerBTAnnouncerImpl(com.biglybt.core.tracker.client.impl.bt.TRTrackerBTAnnouncerImpl) TOTorrent(com.biglybt.core.torrent.TOTorrent) TOTorrentAnnounceURLSet(com.biglybt.core.torrent.TOTorrentAnnounceURLSet)

Example 38 with TOTorrent

use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.

the class TOTorrentXMLDeserialiser method deserialise.

public TOTorrent deserialise(File file) throws TOTorrentException {
    try {
        SimpleXMLParserDocument doc = SimpleXMLParserDocumentFactory.create(file);
        TOTorrent res = decodeRoot(doc);
        return (res);
    } catch (SimpleXMLParserDocumentException e) {
        throw (new TOTorrentException("XML Parse Fails: " + e.getMessage(), TOTorrentException.RT_DECODE_FAILS));
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) SimpleXMLParserDocumentException(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentException) SimpleXMLParserDocument(com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocument) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 39 with TOTorrent

use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.

the class TRHostConfigImpl method loadConfig.

protected void loadConfig(TRHostTorrentFinder finder) {
    try {
        this_mon.enter();
        loading = true;
        Map map = FileUtil.readResilientConfigFile("tracker.config");
        List torrents = (List) map.get("torrents");
        if (torrents == null) {
            return;
        }
        Iterator iter = torrents.iterator();
        while (iter.hasNext()) {
            Map t_map = (Map) iter.next();
            Long persistent_l = (Long) t_map.get("persistent");
            boolean persistent = persistent_l == null || persistent_l.longValue() == 1;
            Long passive_l = (Long) t_map.get("passive");
            boolean passive = passive_l != null && passive_l.longValue() == 1;
            Long dateadded_l = (Long) t_map.get("dateadded");
            long date_added = dateadded_l == null ? SystemTime.getCurrentTime() : dateadded_l.longValue();
            byte[] hash = (byte[]) t_map.get("hash");
            if (persistent) {
                int state = ((Long) t_map.get("status")).intValue();
                if (state == TRHostTorrent.TS_FAILED) {
                    state = TRHostTorrent.TS_STOPPED;
                }
                TOTorrent torrent = finder.lookupTorrent(hash);
                if (torrent == null && passive) {
                    byte[] file_b = (byte[]) t_map.get("torrent_file");
                    if (file_b != null) {
                        try {
                            File file = new File(new String(file_b, Constants.BYTE_ENCODING));
                            torrent = TorrentUtils.readFromFile(file, true, true);
                        } catch (Throwable e) {
                        // torrent might have been deleted, don't barf out errors
                        // Debug.printStackTrace( e );
                        }
                    }
                }
                if (torrent != null) {
                    TRHostTorrent ht = host.addTorrent(torrent, state, true, passive, date_added);
                    if (ht instanceof TRHostTorrentHostImpl) {
                        TRHostTorrentHostImpl hth = (TRHostTorrentHostImpl) ht;
                        recoverStats(hth, t_map);
                    }
                } else {
                    if (COConfigurationManager.getBooleanParameter("Tracker Public Enable")) {
                        host.addExternalTorrent(hash, state, date_added);
                    }
                }
            } else {
                // store stats for later
                saved_stats.put(new HashWrapper(hash), t_map);
            }
        }
    } catch (Exception e) {
        Debug.printStackTrace(e);
    } finally {
        loading = false;
        this_mon.exit();
    }
}
Also used : TRHostTorrent(com.biglybt.core.tracker.host.TRHostTorrent) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent) File(java.io.File)

Example 40 with TOTorrent

use of com.biglybt.core.torrent.TOTorrent in project BiglyBT by BiglySoftware.

the class TRHostImpl method hostTorrentStateChange.

protected void hostTorrentStateChange(TRHostTorrent host_torrent) {
    try {
        this_mon.enter();
        TOTorrent torrent = host_torrent.getTorrent();
        TRTrackerAnnouncer tc = (TRTrackerAnnouncer) tracker_client_map.get(torrent);
        if (tc != null) {
            tc.refreshListeners();
        }
    // config will get saved soon anyway (periodic or on closedown) - perf issues
    // here with multiple torrent removal if we save each time
    // config.saveConfig();
    } finally {
        this_mon.exit();
    }
}
Also used : TOTorrent(com.biglybt.core.torrent.TOTorrent)

Aggregations

TOTorrent (com.biglybt.core.torrent.TOTorrent)123 DownloadManager (com.biglybt.core.download.DownloadManager)34 File (java.io.File)32 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)29 URL (java.net.URL)25 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)15 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)13 IOException (java.io.IOException)13 GlobalManager (com.biglybt.core.global.GlobalManager)10 VuzeFile (com.biglybt.core.vuzefile.VuzeFile)10 Torrent (com.biglybt.pif.torrent.Torrent)9 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)8 TOTorrentAnnounceURLSet (com.biglybt.core.torrent.TOTorrentAnnounceURLSet)8 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)8 List (java.util.List)8 Map (java.util.Map)7 Core (com.biglybt.core.Core)6 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)6 Download (com.biglybt.pif.download.Download)6 TorrentImpl (com.biglybt.pifimpl.local.torrent.TorrentImpl)6