Search in sources :

Example 36 with TOTorrentException

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

the class TRHostImpl method addTorrent.

protected TRHostTorrent addTorrent(TOTorrent torrent, int state, boolean persistent, boolean passive, long date_added) throws TRHostException {
    try {
        this_mon.enter();
        if (persistent && state != TRHostTorrent.TS_PUBLISHED) {
            if (host_add_announce_urls) {
                addTrackerAnnounce(torrent);
            }
        }
        TRHostTorrent ht = lookupHostTorrent(torrent);
        if (ht != null) {
            try {
                ht = lookupHostTorrentViaHash(torrent.getHash());
                if (ht instanceof TRHostTorrentHostImpl) {
                    TRHostTorrentHostImpl hti = (TRHostTorrentHostImpl) ht;
                    if (hti.getTorrent() != torrent) {
                        hti.setTorrentInternal(torrent);
                        if (persistent && !hti.isPersistent()) {
                            hti.setPersistent(true);
                        }
                        if (passive && !hti.isPassive()) {
                            hti.setPassive(true);
                        }
                        if (state != TRHostTorrent.TS_PUBLISHED) {
                            startHosting(hti);
                            if (state == TRHostTorrent.TS_STARTED) {
                                hti.start();
                            }
                        }
                        listeners.dispatch(LDT_TORRENT_CHANGED, ht);
                    }
                }
            } catch (TOTorrentException e) {
                Debug.printStackTrace(e);
            }
            return (ht);
        }
        int port;
        boolean ssl;
        int protocol = TRTrackerServerFactory.PR_TCP;
        if (state == TRHostTorrent.TS_PUBLISHED) {
            port = COConfigurationManager.getIntParameter("Tracker Port", TRHost.DEFAULT_PORT);
            ssl = false;
        } else {
            URL announce_url = torrent.getAnnounceURL();
            String protocol_str = announce_url.getProtocol();
            ssl = protocol_str.equalsIgnoreCase("https");
            if (protocol_str.equalsIgnoreCase("udp")) {
                protocol = TRTrackerServerFactory.PR_UDP;
            } else if (TorrentUtils.isDecentralised(torrent)) {
                protocol = TRTrackerServerFactory.PR_DHT;
            }
            boolean force_external = COConfigurationManager.getBooleanParameter("Tracker Port Force External");
            port = announce_url.getPort();
            if (force_external) {
                String tracker_ip = COConfigurationManager.getStringParameter("Tracker IP", "");
                if (tracker_ip.length() > 0 && !announce_url.getHost().equalsIgnoreCase(tracker_ip)) {
                    if (ssl) {
                        port = COConfigurationManager.getIntParameter("Tracker Port SSL", TRHost.DEFAULT_PORT_SSL);
                    } else {
                        port = COConfigurationManager.getIntParameter("Tracker Port", TRHost.DEFAULT_PORT);
                    }
                }
            }
            if (port == -1) {
                port = ssl ? URL_DEFAULT_PORT_SSL : URL_DEFAULT_PORT;
            }
        }
        TRTrackerServer server = startServer(protocol, port, ssl);
        TRHostTorrent host_torrent;
        if (state == TRHostTorrent.TS_PUBLISHED) {
            TRHostTorrentPublishImpl new_torrent = new TRHostTorrentPublishImpl(this, torrent, date_added);
            new_torrent.setPersistent(persistent);
            host_torrent = new_torrent;
        } else {
            TRHostTorrentHostImpl new_torrent = new TRHostTorrentHostImpl(this, server, torrent, port, date_added);
            new_torrent.setPersistent(persistent);
            new_torrent.setPassive(passive);
            host_torrent = new_torrent;
        }
        host_torrents.add(host_torrent);
        try {
            host_torrent_hash_map.put(new HashWrapper(torrent.getHash()), host_torrent);
        } catch (TOTorrentException e) {
            Debug.printStackTrace(e);
        }
        host_torrent_map.put(torrent, host_torrent);
        if (state != TRHostTorrent.TS_PUBLISHED) {
            startHosting((TRHostTorrentHostImpl) host_torrent);
            if (state == TRHostTorrent.TS_STARTED) {
                host_torrent.start();
            }
            if (!persistent) {
                config.recoverStats((TRHostTorrentHostImpl) host_torrent);
            }
        }
        listeners.dispatch(LDT_TORRENT_ADDED, host_torrent);
        config.saveRequired();
        return (host_torrent);
    } finally {
        this_mon.exit();
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) URL(java.net.URL)

Example 37 with TOTorrentException

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

the class TOTorrentCreateImpl method constructFixed.

private int constructFixed(File _torrent_base, long _piece_length) throws TOTorrentException {
    setIgnoreList();
    setCreationDate(SystemTime.getCurrentTime() / 1000);
    setCreatedBy(Constants.AZUREUS_NAME + "/" + Constants.AZUREUS_VERSION);
    setPieceLength(_piece_length);
    report("Torrent.create.progress.piecelength", _piece_length);
    piece_count = calculateNumberOfPieces(_torrent_base, _piece_length);
    if (piece_count == 0) {
        throw (new TOTorrentException("TOTorrentCreate: specified files have zero total length", TOTorrentException.RT_ZERO_LENGTH));
    }
    report("Torrent.create.progress.hashing");
    for (int i = 0; i < progress_listeners.size(); i++) {
        ((TOTorrentProgressListener) progress_listeners.get(i)).reportProgress(0);
    }
    boolean add_other_per_file_hashes = add_other_hashes && !getSimpleTorrent();
    file_hasher = new TOTorrentFileHasher(add_other_hashes, add_other_per_file_hashes, (int) _piece_length, progress_listeners.size() == 0 ? null : this);
    int ignored = 0;
    try {
        if (cancelled) {
            throw (new TOTorrentException("TOTorrentCreate: operation cancelled", TOTorrentException.RT_CANCELLED));
        }
        if (getSimpleTorrent()) {
            File link = linkage_map.get(_torrent_base.getName());
            if (link != null) {
                linked_tf_map.put("0", link.getAbsolutePath());
            }
            long length = file_hasher.add(link == null ? _torrent_base : link);
            setFiles(new TOTorrentFileImpl[] { new TOTorrentFileImpl(this, 0, 0, length, new byte[][] { getName() }) });
            setPieces(file_hasher.getPieces());
        } else {
            List<TOTorrentFileImpl> encoded = new ArrayList<>();
            ignored = processDir(file_hasher, _torrent_base, encoded, _torrent_base.getName(), "");
            TOTorrentFileImpl[] files = new TOTorrentFileImpl[encoded.size()];
            encoded.toArray(files);
            setFiles(files);
        }
        setPieces(file_hasher.getPieces());
        if (add_other_hashes) {
            byte[] sha1_digest = file_hasher.getSHA1Digest();
            byte[] ed2k_digest = file_hasher.getED2KDigest();
            addAdditionalInfoProperty("sha1", sha1_digest);
            addAdditionalInfoProperty("ed2k", ed2k_digest);
        // System.out.println( "overall:sha1 = " + ByteFormatter.nicePrint( sha1_digest, true));
        // System.out.println( "overall:ed2k = " + ByteFormatter.nicePrint( ed2k_digest, true));
        }
        return (ignored);
    } finally {
        file_hasher = null;
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrentProgressListener(com.biglybt.core.torrent.TOTorrentProgressListener) File(java.io.File)

Example 38 with TOTorrentException

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

the class TOTorrentCreateImpl method create.

protected void create() throws TOTorrentException {
    int ignored = constructFixed(torrent_base, piece_length);
    if (linkage_map.size() > 0 && linkage_map.size() != (linked_tf_map.size() + ignored)) {
        throw (new TOTorrentException("TOTorrentCreate: unresolved linkages: required=" + linkage_map + ", resolved=" + linked_tf_map, TOTorrentException.RT_DECODE_FAILS));
    }
    if (linked_tf_map.size() > 0) {
        Map m = getAdditionalMapProperty(TOTorrent.AZUREUS_PRIVATE_PROPERTIES);
        if (m == null) {
            m = new HashMap();
            setAdditionalMapProperty(TOTorrent.AZUREUS_PRIVATE_PROPERTIES, m);
        }
        if (linked_tf_map.size() < 100) {
            m.put(TorrentUtils.TORRENT_AZ_PROP_INITIAL_LINKAGE, linked_tf_map);
        } else {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(100 * 1024);
            try {
                GZIPOutputStream gos = new GZIPOutputStream(baos);
                gos.write(BEncoder.encode(linked_tf_map));
                gos.close();
                m.put(TorrentUtils.TORRENT_AZ_PROP_INITIAL_LINKAGE2, baos.toByteArray());
            } catch (Throwable e) {
                throw (new TOTorrentException("Failed to serialise linkage", TOTorrentException.RT_WRITE_FAILS));
            }
        }
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) GZIPOutputStream(java.util.zip.GZIPOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 39 with TOTorrentException

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

the class TOTorrentCreatorImpl method readDescriptor.

private List<DescEntry> readDescriptor() throws TOTorrentException {
    try {
        int top_files = 0;
        int top_entries = 0;
        String top_component = null;
        Map map = BDecoder.decode(FileUtil.readFileAsByteArray(torrent_base));
        List<Map> file_map = (List<Map>) map.get("file_map");
        if (file_map == null) {
            throw (new TOTorrentException("Invalid descriptor file", TOTorrentException.RT_READ_FAILS));
        }
        List<DescEntry> desc_entries = new ArrayList<>();
        BDecoder.decodeStrings(file_map);
        for (Map m : file_map) {
            List<String> logical_path = (List<String>) m.get("logical_path");
            String target = (String) m.get("target");
            if (logical_path == null || target == null) {
                throw (new TOTorrentException("Invalid descriptor file: entry=" + m, TOTorrentException.RT_READ_FAILS));
            }
            if (logical_path.size() == 0) {
                throw (new TOTorrentException("Logical path must have at least one entry: " + m, TOTorrentException.RT_READ_FAILS));
            }
            for (int i = 0; i < logical_path.size(); i++) {
                logical_path.set(i, FileUtil.convertOSSpecificChars(logical_path.get(i), i < logical_path.size() - 1));
            }
            File tf = new File(target);
            if (!tf.exists()) {
                throw (new TOTorrentException("Invalid descriptor file: file '" + tf + "' not found" + m, TOTorrentException.RT_READ_FAILS));
            } else {
                String str = logical_path.get(0);
                if (logical_path.size() == 1) {
                    top_entries++;
                }
                if (top_component != null && !top_component.equals(str)) {
                    throw (new TOTorrentException("Invalid descriptor file: multiple top level elements specified", TOTorrentException.RT_READ_FAILS));
                }
                top_component = str;
            }
            desc_entries.add(new DescEntry(logical_path, tf));
        }
        if (top_entries > 1) {
            throw (new TOTorrentException("Invalid descriptor file: exactly one top level entry required", TOTorrentException.RT_READ_FAILS));
        }
        if (desc_entries.isEmpty()) {
            throw (new TOTorrentException("Invalid descriptor file: no mapping entries found", TOTorrentException.RT_READ_FAILS));
        }
        return (desc_entries);
    } catch (IOException e) {
        throw (new TOTorrentException("Invalid descriptor file: " + Debug.getNestedExceptionMessage(e), TOTorrentException.RT_READ_FAILS));
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map) File(java.io.File)

Example 40 with TOTorrentException

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

the class TorrentImpl method removeAdditionalProperties.

@Override
public Torrent removeAdditionalProperties() {
    try {
        TOTorrent t = TOTorrentFactory.deserialiseFromMap(torrent.serialiseToMap());
        t.removeAdditionalProperties();
        return (new TorrentImpl(t));
    } catch (TOTorrentException e) {
        Debug.printStackTrace(e);
        return (this);
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Aggregations

TOTorrentException (com.biglybt.core.torrent.TOTorrentException)45 TOTorrent (com.biglybt.core.torrent.TOTorrent)23 File (java.io.File)15 IOException (java.io.IOException)9 URL (java.net.URL)8 DownloadManager (com.biglybt.core.download.DownloadManager)7 Core (com.biglybt.core.Core)3 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)3 GlobalManager (com.biglybt.core.global.GlobalManager)3 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)3 SimpleXMLParserDocumentNode (com.biglybt.pif.utils.xml.simpleparser.SimpleXMLParserDocumentNode)3 List (java.util.List)3 Map (java.util.Map)3 CoreRunningListener (com.biglybt.core.CoreRunningListener)2 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)2 LogEvent (com.biglybt.core.logging.LogEvent)2 TOTorrentProgressListener (com.biglybt.core.torrent.TOTorrentProgressListener)2 TRHostTorrent (com.biglybt.core.tracker.host.TRHostTorrent)2 TorrentImpl (com.biglybt.pifimpl.local.torrent.TorrentImpl)2 TrackerEditorListener (com.biglybt.ui.swt.maketorrent.TrackerEditorListener)2