Search in sources :

Example 56 with Download

use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.

the class DDBaseTTTorrent method read.

// server side read
@Override
public DistributedDatabaseValue read(DistributedDatabaseContact contact, DistributedDatabaseTransferType type, DistributedDatabaseKey key) throws DistributedDatabaseException {
    try {
        byte[] search_key = ((DDBaseKeyImpl) key).getBytes();
        Download download = null;
        PluginInterface pi = PluginInitializer.getDefaultInterface();
        String search_sha1 = pi.getUtilities().getFormatters().encodeBytesToString(search_key);
        if (ta_sha1 == null) {
            ta_sha1 = pi.getTorrentManager().getPluginAttribute("DDBaseTTTorrent::sha1");
        }
        // gotta look for the sha1(hash)
        Download[] downloads = pi.getDownloadManager().getDownloads();
        for (int i = 0; i < downloads.length; i++) {
            Download dl = downloads[i];
            if (dl.getTorrent() == null) {
                continue;
            }
            String sha1 = dl.getAttribute(ta_sha1);
            if (sha1 == null) {
                sha1 = pi.getUtilities().getFormatters().encodeBytesToString(new SHA1Simple().calculateHash(dl.getTorrent().getHash()));
                dl.setAttribute(ta_sha1, sha1);
            }
            if (sha1.equals(search_sha1)) {
                download = dl;
                break;
            }
        }
        if (download == null) {
            synchronized (this) {
                if (external_downloads != null) {
                    for (int i = 0; i < external_downloads.size(); i++) {
                        Download dl = (Download) external_downloads.get(i);
                        if (dl.getTorrent() == null) {
                            continue;
                        }
                        String sha1 = dl.getAttribute(ta_sha1);
                        if (sha1 == null) {
                            sha1 = pi.getUtilities().getFormatters().encodeBytesToString(new SHA1Simple().calculateHash(dl.getTorrent().getHash()));
                            dl.setAttribute(ta_sha1, sha1);
                        }
                        if (sha1.equals(search_sha1)) {
                            download = dl;
                            break;
                        }
                    }
                }
            }
        }
        String originator = contact.getName();
        if (download == null) {
            String msg = "TorrentDownload: request from " + originator + " for '" + pi.getUtilities().getFormatters().encodeBytesToString(search_key) + "' not found";
            if (TRACE) {
                System.out.println(msg);
            }
            ddb.log(msg);
            return (null);
        }
        if (!ddb.isTorrentXferEnabled()) {
            ddb.log("TorrentDownload: request from " + originator + "  for '" + download.getName() + "' denied as torrent transfer is disabled");
            return (null);
        }
        Torrent torrent = download.getTorrent();
        if (torrent.isPrivate()) {
            Debug.out("Attempt to download private torrent");
            ddb.log("TorrentDownload: request from " + originator + "  for '" + download.getName() + "' denied as it is private");
            return (null);
        }
        try {
            // apparently there are some trackers using non-private torrents with passkeys. Crazy, however to give users at
            // least the opportunity to prevent .torrent transfer for these torrents we deny this if the DHT peer source has
            // been disabled by the user
            DownloadManager dm = PluginCoreUtils.unwrapIfPossible(download);
            if (dm != null) {
                DownloadManagerState dms = dm.getDownloadState();
                if (!dms.isPeerSourceEnabled(PEPeerSource.PS_DHT)) {
                    ddb.log("TorrentDownload: request from " + originator + "  for '" + download.getName() + "' denied as DHT peer source disabled");
                    return (null);
                }
                if (dms.getFlag(DownloadManagerState.FLAG_METADATA_DOWNLOAD)) {
                    return (null);
                }
            }
        } catch (Throwable e) {
            Debug.out(e);
        }
        String msg = "TorrentDownload: request from " + originator + "  for '" + download.getName() + "' OK";
        if (TRACE) {
            System.out.println(msg);
        }
        ddb.log(msg);
        HashWrapper hw = new HashWrapper(torrent.getHash());
        synchronized (data_cache) {
            Object[] data = (Object[]) data_cache.get(hw);
            if (data != null) {
                data[1] = new Long(SystemTime.getCurrentTime());
                return (ddb.createValue((byte[]) data[0]));
            }
        }
        torrent = torrent.removeAdditionalProperties();
        // when clients get a torrent from the DHT they take on
        // responsibility for tracking it too
        torrent.setDecentralisedBackupRequested(true);
        byte[] data = torrent.writeToBEncodedData();
        data = encrypt(torrent.getHash(), data);
        if (data == null) {
            return (null);
        }
        synchronized (data_cache) {
            if (data_cache.size() == 0) {
                final TimerEventPeriodic[] pe = { null };
                pe[0] = SimpleTimer.addPeriodicEvent("DDBTorrent:timeout", 30 * 1000, new TimerEventPerformer() {

                    @Override
                    public void perform(TimerEvent event) {
                        long now = SystemTime.getCurrentTime();
                        synchronized (data_cache) {
                            Iterator it = data_cache.values().iterator();
                            while (it.hasNext()) {
                                long time = ((Long) ((Object[]) it.next())[1]).longValue();
                                if (now < time || now - time > 120 * 1000) {
                                    it.remove();
                                }
                            }
                            if (data_cache.size() == 0) {
                                pe[0].cancel();
                            }
                        }
                    }
                });
            }
            data_cache.put(hw, new Object[] { data, new Long(SystemTime.getCurrentTime()) });
        }
        return (ddb.createValue(data));
    } catch (Throwable e) {
        throw (new DistributedDatabaseException("Torrent write fails", e));
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) PluginInterface(com.biglybt.pif.PluginInterface) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) Download(com.biglybt.pif.download.Download)

Example 57 with Download

use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.

the class ColumnTorrentSpeed method refresh.

@Override
public void refresh(TableCell cell) {
    Object ds = cell.getDataSource();
    if (!(ds instanceof Download)) {
        return;
    }
    Download dm = (Download) ds;
    long value;
    long sortValue;
    String prefix = "";
    int iState;
    iState = dm.getState();
    if (iState == Download.ST_DOWNLOADING) {
        value = dm.getStats().getDownloadAverage();
        ((TableCellSWT) cell).setIcon(imgDown);
    } else if (iState == Download.ST_SEEDING) {
        value = dm.getStats().getUploadAverage();
        ((TableCellSWT) cell).setIcon(imgUp);
    } else {
        ((TableCellSWT) cell).setIcon(null);
        value = 0;
    }
    if (cell.isSecondarySortEnabled()) {
        sortValue = (value << 4) | iState;
    } else {
        sortValue = value;
    }
    if (cell.setSortValue(sortValue) || !cell.isValid()) {
        cell.setText(value > 0 ? prefix + DisplayFormatters.formatByteCountToKiBEtcPerSec(value) : "");
    }
}
Also used : TableCellSWT(com.biglybt.ui.swt.views.table.TableCellSWT) Download(com.biglybt.pif.download.Download)

Example 58 with Download

use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.

the class DeviceMediaRendererImpl method updateStatus.

@Override
protected void updateStatus(int tick_count) {
    super.updateStatus(tick_count);
    if (tick_count > 0 && tick_count % TAG_SHARE_CHECK_TICKS == 0) {
        long tag_id = getAutoShareToTagID();
        if (tag_id != -1) {
            synchronized (DeviceMediaRendererImpl.class) {
                if (share_ta == null) {
                    share_ta = PluginInitializer.getDefaultInterface().getTorrentManager().getPluginAttribute("DeviceMediaRendererImpl:tag_share");
                }
            }
            TagManager tm = TagManagerFactory.getTagManager();
            Tag assigned_tag = tm.lookupTagByUID(tag_id);
            if (assigned_tag != null) {
                // not going to want this to be public
                assigned_tag.setPublic(false);
                synchronized (share_requests) {
                    if (share_requests.size() == 0) {
                        Set<Taggable> taggables = assigned_tag.getTagged();
                        Set<String> done_files = new HashSet<>();
                        for (Taggable temp : taggables) {
                            if (!(temp instanceof DownloadManager)) {
                                continue;
                            }
                            DownloadManager dm = (DownloadManager) temp;
                            Download download = PluginCoreUtils.wrap(dm);
                            String attr = download.getAttribute(share_ta);
                            if (attr != null) {
                                done_files.add(attr);
                            }
                        }
                        TranscodeFileImpl[] files = getFiles();
                        for (TranscodeFileImpl file : files) {
                            if (file.isComplete()) {
                                try {
                                    File target_file = file.getTargetFile().getFile(true);
                                    long size = target_file.length();
                                    if (target_file.exists() && size > 0) {
                                        String suffix = " (" + file.getProfileName() + " - " + DisplayFormatters.formatByteCountToKiBEtc(size) + ")";
                                        String share_name = file.getName() + suffix;
                                        String key = target_file.getName() + suffix;
                                        if (!done_files.contains(key)) {
                                            share_requests.add(new Object[] { key, target_file, share_name, assigned_tag });
                                        }
                                    }
                                } catch (Throwable e) {
                                }
                            }
                        }
                        if (share_requests.size() > 0) {
                            shareRequestAdded();
                        }
                    }
                }
            }
        }
    }
}
Also used : DownloadManager(com.biglybt.core.download.DownloadManager) TagManager(com.biglybt.core.tag.TagManager) Tag(com.biglybt.core.tag.Tag) Taggable(com.biglybt.core.tag.Taggable) Download(com.biglybt.pif.download.Download) ShareResourceFile(com.biglybt.pif.sharing.ShareResourceFile) File(java.io.File)

Example 59 with Download

use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.

the class TranscodeFileImpl method getSourceFile.

@Override
public DiskManagerFileInfo getSourceFile() throws TranscodeException {
    checkDeleted();
    // options are either a download file or a link to an existing non-torrent based file
    String hash = getString(KEY_SOURCE_FILE_HASH);
    if (hash != null) {
        try {
            Download download = PluginInitializer.getDefaultInterface().getDownloadManager().getDownload(Base32.decode(hash));
            if (download != null) {
                int index = (int) getLong(KEY_SOURCE_FILE_INDEX);
                return (download.getDiskManagerFileInfo(index));
            }
        } catch (Throwable e) {
        }
    }
    String link = getString(KEY_SOURCE_FILE_LINK);
    if (link != null) {
        File link_file = new File(link);
        if (link_file.exists() || getBoolean(KEY_NO_XCODE)) {
            return (new DiskManagerFileInfoFile(link_file));
        }
    }
    return (new DiskManagerFileInfoFile(getCacheFile()));
}
Also used : DiskManagerFileInfoFile(com.biglybt.core.download.DiskManagerFileInfoFile) Download(com.biglybt.pif.download.Download) File(java.io.File) DiskManagerFileInfoFile(com.biglybt.core.download.DiskManagerFileInfoFile)

Example 60 with Download

use of com.biglybt.pif.download.Download in project BiglyBT by BiglySoftware.

the class TranscodeManagerImpl method processTag.

void processTag(Tag tag, Object[] details, DownloadManager dm) {
    Download download = PluginCoreUtils.wrap(dm);
    if (download == null) {
        return;
    }
    if (download.getFlag(Download.FLAG_LOW_NOISE)) {
        return;
    }
    String str = download.getAttribute(tag_ta);
    String tag_name = tag.getTagName(true);
    String tag_tag = tag.getTagType().getTagType() + "." + tag.getTagID() + ";";
    if (str != null && str.contains(tag_tag)) {
        return;
    }
    try {
        DeviceMediaRenderer device = (DeviceMediaRenderer) details[0];
        TranscodeProfile profile = (TranscodeProfile) details[1];
        log("Tag " + tag_name + " - adding " + download.getName() + " to " + device.getName() + "/" + profile.getName());
        DiskManagerFileInfo[] dm_files = download.getDiskManagerFileInfo();
        int num_added = 0;
        for (DiskManagerFileInfo dm_file : dm_files) {
            if (num_added > 64) {
                break;
            }
            if (dm_files.length == 1 || dm_file.getLength() >= 128 * 1024) {
                try {
                    queue.add(device, profile, dm_file, false);
                    num_added++;
                } catch (Throwable e) {
                    log("    add failed", e);
                }
            }
        }
    } finally {
        download.setAttribute(tag_ta, str == null ? tag_tag : (str + tag_tag));
    }
}
Also used : DiskManagerFileInfo(com.biglybt.pif.disk.DiskManagerFileInfo) Download(com.biglybt.pif.download.Download)

Aggregations

Download (com.biglybt.pif.download.Download)80 DownloadManager (com.biglybt.core.download.DownloadManager)22 Torrent (com.biglybt.pif.torrent.Torrent)17 DiskManagerFileInfo (com.biglybt.pif.disk.DiskManagerFileInfo)12 File (java.io.File)12 TOTorrent (com.biglybt.core.torrent.TOTorrent)11 PluginInterface (com.biglybt.pif.PluginInterface)11 URL (java.net.URL)10 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)8 PEPeerManager (com.biglybt.core.peer.PEPeerManager)8 List (java.util.List)7 Tag (com.biglybt.core.tag.Tag)6 MenuItem (com.biglybt.pif.ui.menus.MenuItem)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 DownloadException (com.biglybt.pif.download.DownloadException)5 DiskManager (com.biglybt.core.disk.DiskManager)4 DownloadManager (com.biglybt.pif.download.DownloadManager)4 DownloadScrapeResult (com.biglybt.pif.download.DownloadScrapeResult)4 Peer (com.biglybt.pif.peers.Peer)4