Search in sources :

Example 26 with TOTorrentException

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

the class ActivitiesEntry method loadCommonFromMap.

public void loadCommonFromMap(Map<?, ?> map) {
    if (!playable) {
        setPlayable(MapUtils.getMapBoolean(map, "playable", false));
    }
    setID(MapUtils.getMapString(map, "id", null));
    setText(MapUtils.getMapString(map, "text", null));
    Map<?, ?> torrentMap = MapUtils.getMapMap(map, "torrent", null);
    if (torrentMap != null) {
        TOTorrent torrent = null;
        try {
            torrent = TOTorrentFactory.deserialiseFromMap(torrentMap);
            setTorrent(torrent);
        } catch (TOTorrentException e) {
        }
    }
    if (dm == null && torrentName == null) {
        setTorrentName(MapUtils.getMapString(map, "torrent-name", null));
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 27 with TOTorrentException

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

the class DownloadManagerController method setInitialState.

protected void setInitialState(int initial_state) {
    // only take note if there's been no errors
    bInitialized = true;
    if (getState() == DownloadManager.STATE_START_OF_DAY) {
        setState(initial_state, true);
    }
    DownloadManagerState state = download_manager.getDownloadState();
    TOTorrent torrent = download_manager.getTorrent();
    if (torrent != null) {
        try {
            peer_manager_registration = PeerManager.getSingleton().registerLegacyManager(torrent.getHashWrapper(), this);
            md_info_dict_size = state.getIntAttribute(DownloadManagerState.AT_MD_INFO_DICT_SIZE);
            if (md_info_dict_size == 0) {
                try {
                    md_info_dict_size = BEncoder.encode((Map) torrent.serialiseToMap().get("info")).length;
                } catch (Throwable e) {
                    md_info_dict_size = -1;
                }
                state.setIntAttribute(DownloadManagerState.AT_MD_INFO_DICT_SIZE, md_info_dict_size);
            }
        } catch (TOTorrentException e) {
            Debug.printStackTrace(e);
        }
    }
    if (state.parameterExists(DownloadManagerState.PARAM_DND_FLAGS)) {
        long flags = state.getLongParameter(DownloadManagerState.PARAM_DND_FLAGS);
        cached_complete_excluding_dnd = (flags & STATE_FLAG_COMPLETE_NO_DND) != 0;
        cached_has_dnd_files = (flags & STATE_FLAG_HASDND) != 0;
        cached_values_set = true;
    }
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 28 with TOTorrentException

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

the class GlobalManagerImpl method removeDownloadManager.

@Override
public void removeDownloadManager(DownloadManager manager, boolean remove_torrent, boolean remove_data) throws GlobalManagerDownloadRemovalVetoException {
    if (!managers_cow.contains(manager)) {
        return;
    }
    canDownloadManagerBeRemoved(manager, remove_torrent, remove_data);
    manager.stopIt(DownloadManager.STATE_STOPPED, remove_torrent, remove_data, true);
    try {
        managers_mon.enter();
        List new_download_managers = new ArrayList(managers_cow);
        new_download_managers.remove(manager);
        managers_cow = new_download_managers;
        TOTorrent torrent = manager.getTorrent();
        if (torrent != null) {
            try {
                manager_map.remove(new HashWrapper(torrent.getHash()));
            } catch (TOTorrentException e) {
                Debug.printStackTrace(e);
            }
        }
    } finally {
        managers_mon.exit();
    }
    // when we remove a download manager from the client this is the time to remove it from the record of
    // created torrents if present
    TOTorrent torrent = manager.getTorrent();
    if (torrent != null) {
        TorrentUtils.removeCreatedTorrent(torrent);
    }
    manager.destroy(false);
    fixUpDownloadManagerPositions();
    listeners_and_event_listeners.dispatch(LDT_MANAGER_REMOVED, manager);
    TorrentUtils.setTorrentDeleted();
    taggable_life_manager.taggableDestroyed(manager);
    manager.removeListener(this);
    saveDownloads(false);
    DownloadManagerState dms = manager.getDownloadState();
    if (dms.getCategory() != null) {
        dms.setCategory(null);
    }
    if (manager.getTorrent() != null) {
        trackerScraper.remove(manager.getTorrent());
    }
    if (host_support != null) {
        host_support.torrentRemoved(manager.getTorrentFileName(), manager.getTorrent());
    }
    // delete the state last as passivating a hosted torrent may require access to
    // the existing torrent state
    dms.delete();
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 29 with TOTorrentException

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

the class GlobalManagerImpl method addDownloadManager.

protected DownloadManager addDownloadManager(DownloadManager download_manager, boolean save, boolean notifyListeners) {
    if (!isStopping) {
        // make sure we have existing ones loaded so that existing check works
        loadExistingTorrentsNow(false);
        try {
            managers_mon.enter();
            int existing_index = managers_cow.indexOf(download_manager);
            if (existing_index != -1) {
                DownloadManager existing = managers_cow.get(existing_index);
                download_manager.destroy(true);
                return (existing);
            }
            DownloadManagerStats dm_stats = download_manager.getStats();
            HashWrapper hashwrapper = null;
            try {
                TOTorrent torrent = download_manager.getTorrent();
                if (torrent != null) {
                    hashwrapper = torrent.getHashWrapper();
                }
            } catch (Exception e1) {
            }
            Map save_download_state = (Map) saved_download_manager_state.remove(hashwrapper);
            long saved_data_bytes_downloaded = 0;
            long saved_data_bytes_uploaded = 0;
            long saved_discarded = 0;
            long saved_hashfails = 0;
            long saved_SecondsDownloading = 0;
            long saved_SecondsOnlySeeding = 0;
            if (save_download_state != null) {
                int maxDL = save_download_state.get("maxdl") == null ? 0 : ((Long) save_download_state.get("maxdl")).intValue();
                int maxUL = save_download_state.get("maxul") == null ? 0 : ((Long) save_download_state.get("maxul")).intValue();
                Long lDownloaded = (Long) save_download_state.get("downloaded");
                Long lUploaded = (Long) save_download_state.get("uploaded");
                Long lCompletedBytes = (Long) save_download_state.get("completedbytes");
                Long lDiscarded = (Long) save_download_state.get("discarded");
                // old method, number of fails
                Long lHashFailsCount = (Long) save_download_state.get("hashfails");
                // new method, bytes failed
                Long lHashFailsBytes = (Long) save_download_state.get("hashfailbytes");
                // migrated to downloadstate in 2403
                Long nbUploads = (Long) save_download_state.get("uploads");
                if (nbUploads != null) {
                    // migrate anything other than the default value of 4
                    int maxUploads = nbUploads.intValue();
                    if (maxUploads != 4) {
                        // value if the stored value is non-default and the state one is
                        if (download_manager.getMaxUploads() == 4) {
                            download_manager.setMaxUploads(maxUploads);
                        }
                    }
                }
                dm_stats.setDownloadRateLimitBytesPerSecond(maxDL);
                dm_stats.setUploadRateLimitBytesPerSecond(maxUL);
                if (lCompletedBytes != null) {
                    dm_stats.setDownloadCompletedBytes(lCompletedBytes.longValue());
                }
                if (lDiscarded != null) {
                    saved_discarded = lDiscarded.longValue();
                }
                if (lHashFailsBytes != null) {
                    saved_hashfails = lHashFailsBytes.longValue();
                } else if (lHashFailsCount != null) {
                    TOTorrent torrent = download_manager.getTorrent();
                    if (torrent != null) {
                        saved_hashfails = lHashFailsCount.longValue() * torrent.getPieceLength();
                    }
                }
                Long lPosition = (Long) save_download_state.get("position");
                // 2.2.0.1 - category moved to downloadstate - this here for
                // migration purposes
                String sCategory = null;
                if (save_download_state.containsKey("category")) {
                    try {
                        sCategory = new String((byte[]) save_download_state.get("category"), Constants.DEFAULT_ENCODING);
                    } catch (UnsupportedEncodingException e) {
                        Debug.printStackTrace(e);
                    }
                }
                if (sCategory != null) {
                    Category cat = CategoryManager.getCategory(sCategory);
                    if (cat != null)
                        download_manager.getDownloadState().setCategory(cat);
                }
                download_manager.requestAssumedCompleteMode();
                if (lDownloaded != null && lUploaded != null) {
                    boolean bCompleted = download_manager.isDownloadComplete(false);
                    long lUploadedValue = lUploaded.longValue();
                    long lDownloadedValue = lDownloaded.longValue();
                    if (bCompleted && (lDownloadedValue == 0)) {
                        // Gudy : I say if the torrent is complete, let's simply set downloaded
                        // to size in order to see a meaningfull share-ratio
                        // Gudy : Bypass this horrible hack, and I don't care of first priority seeding...
                        /*
		            if (lDownloadedValue != 0 && ((lUploadedValue * 1000) / lDownloadedValue < minQueueingShareRatio) )
		              lUploadedValue = ( download_manager.getSize()+999) * minQueueingShareRatio / 1000;
	                */
                        // Parg: quite a few users have complained that they want "open-for-seeding" torrents to
                        // have an infinite share ratio for seeding rules (i.e. so they're not first priority)
                        int dl_copies = COConfigurationManager.getIntParameter("StartStopManager_iAddForSeedingDLCopyCount");
                        lDownloadedValue = download_manager.getSize() * dl_copies;
                        download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_ONLY_EVER_SEEDED, true);
                    }
                    saved_data_bytes_downloaded = lDownloadedValue;
                    saved_data_bytes_uploaded = lUploadedValue;
                }
                if (lPosition != null)
                    download_manager.setPosition(lPosition.intValue());
                // no longer needed code
                // else if (dm_stats.getDownloadCompleted(false) < 1000)
                // dm.setPosition(bCompleted ? numCompleted : numDownloading);
                Long lSecondsDLing = (Long) save_download_state.get("secondsDownloading");
                if (lSecondsDLing != null) {
                    saved_SecondsDownloading = lSecondsDLing.longValue();
                }
                Long lSecondsOnlySeeding = (Long) save_download_state.get("secondsOnlySeeding");
                if (lSecondsOnlySeeding != null) {
                    saved_SecondsOnlySeeding = lSecondsOnlySeeding.longValue();
                }
                Long already_allocated = (Long) save_download_state.get("allocated");
                if (already_allocated != null && already_allocated.intValue() == 1) {
                    download_manager.setDataAlreadyAllocated(true);
                }
                Long creation_time = (Long) save_download_state.get("creationTime");
                if (creation_time != null) {
                    long ct = creation_time.longValue();
                    if (ct < SystemTime.getCurrentTime()) {
                        download_manager.setCreationTime(ct);
                    }
                }
            } else {
                if (dm_stats.getDownloadCompleted(false) == 1000) {
                    int dl_copies = COConfigurationManager.getIntParameter("StartStopManager_iAddForSeedingDLCopyCount");
                    saved_data_bytes_downloaded = download_manager.getSize() * dl_copies;
                }
            }
            dm_stats.restoreSessionTotals(saved_data_bytes_downloaded, saved_data_bytes_uploaded, saved_discarded, saved_hashfails, saved_SecondsDownloading, saved_SecondsOnlySeeding);
            boolean isCompleted = download_manager.isDownloadComplete(false);
            if (download_manager.getPosition() == -1) {
                int endPosition = 0;
                for (int i = 0; i < managers_cow.size(); i++) {
                    DownloadManager dm = managers_cow.get(i);
                    boolean dmIsCompleted = dm.isDownloadComplete(false);
                    if (dmIsCompleted == isCompleted)
                        endPosition++;
                }
                download_manager.setPosition(endPosition + 1);
            }
            // Even though when the DownloadManager was created, onlySeeding was
            // most likely set to true for completed torrents (via the Initializer +
            // readTorrent), there's a chance that the torrent file didn't have the
            // resume data.  If it didn't, but we marked it as complete in our
            // downloads config file, we should set to onlySeeding
            download_manager.requestAssumedCompleteMode();
            List<DownloadManager> new_download_managers = new ArrayList<>(managers_cow);
            new_download_managers.add(download_manager);
            managers_cow = new_download_managers;
            TOTorrent torrent = download_manager.getTorrent();
            if (torrent != null) {
                try {
                    manager_map.put(new HashWrapper(torrent.getHash()), download_manager);
                } catch (TOTorrentException e) {
                    Debug.printStackTrace(e);
                }
            }
            // flag set, to prevent them being moved.
            if (COConfigurationManager.getBooleanParameter("Set Completion Flag For Completed Downloads On Start")) {
                // ones yet.
                if (download_manager.isDownloadComplete(true)) {
                    download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_MOVE_ON_COMPLETION_DONE, true);
                }
            }
            if (notifyListeners) {
                listeners_and_event_listeners.dispatch(LDT_MANAGER_ADDED, download_manager);
                taggable_life_manager.taggableCreated(download_manager);
                if (host_support != null) {
                    host_support.torrentAdded(download_manager.getTorrentFileName(), download_manager.getTorrent());
                }
            }
            download_manager.addListener(this);
            if (save_download_state != null) {
                Long lForceStart = (Long) save_download_state.get("forceStart");
                if (lForceStart == null) {
                    Long lStartStopLocked = (Long) save_download_state.get("startStopLocked");
                    if (lStartStopLocked != null) {
                        lForceStart = lStartStopLocked;
                    }
                }
                if (lForceStart != null) {
                    if (lForceStart.intValue() == 1) {
                        download_manager.setForceStart(true);
                    }
                }
            }
        } finally {
            managers_mon.exit();
        }
        if (save) {
            saveDownloads(false);
        }
        return (download_manager);
    } else {
        Logger.log(new LogEvent(LOGID, LogEvent.LT_ERROR, "Tried to add a DownloadManager after shutdown of GlobalManager."));
        return (null);
    }
}
Also used : Category(com.biglybt.core.category.Category) LogEvent(com.biglybt.core.logging.LogEvent) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 30 with TOTorrentException

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

the class TOTorrentDeserialiseImpl method construct.

protected void construct(InputStream is) throws TOTorrentException {
    ByteArrayOutputStream metaInfo = new ByteArrayOutputStream(64 * 1024);
    try {
        // raised this limit as 2k was rather too small
        byte[] buf = new byte[32 * 1024];
        // do a check to see if it's a BEncode file.
        int iFirstByte = is.read();
        if (iFirstByte != 'd' && iFirstByte != 'e' && iFirstByte != 'i' && !(iFirstByte >= '0' && iFirstByte <= '9')) {
            try {
                metaInfo.write(iFirstByte);
                int nbRead;
                while ((nbRead = is.read(buf)) > 0 && metaInfo.size() < 32000) {
                    metaInfo.write(buf, 0, nbRead);
                }
                String char_data = new String(metaInfo.toByteArray());
                if (char_data.toLowerCase().contains("html")) {
                    char_data = HTMLUtils.convertHTMLToText2(char_data);
                    char_data = HTMLUtils.splitWithLineLength(char_data, 80);
                    if (char_data.length() > 400) {
                        char_data = char_data.substring(0, 400) + "...";
                    }
                    throw (new TOTorrentException("Contents maybe HTML:\n" + char_data, TOTorrentException.RT_DECODE_FAILS));
                }
            } catch (Throwable e) {
                if (e instanceof TOTorrentException) {
                    throw ((TOTorrentException) e);
                }
            // ignore this
            }
            throw (new TOTorrentException("Contents invalid - bad header", TOTorrentException.RT_DECODE_FAILS));
        }
        metaInfo.write(iFirstByte);
        int nbRead;
        while ((nbRead = is.read(buf)) > 0) {
            metaInfo.write(buf, 0, nbRead);
        }
    } catch (Throwable e) {
        throw (new TOTorrentException("Error reading torrent: " + Debug.getNestedExceptionMessage(e), TOTorrentException.RT_READ_FAILS));
    }
    construct(metaInfo.toByteArray());
}
Also used : TOTorrentException(com.biglybt.core.torrent.TOTorrentException)

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