Search in sources :

Example 66 with Download

use of com.biglybt.pif.download.Download 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) {
        if (dm.getFlag(Download.FLAG_METADATA_DOWNLOAD)) {
            continue;
        }
        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();
    Set<String> has_cats_or_tags = new HashSet<>();
    for (PeerSet 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());
        Set<String> cot = set.getCategoriesOrTags();
        if (cot != null) {
            has_cats_or_tags.addAll(cot);
        }
        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 (PeerSet 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)

Example 67 with Download

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

the class PeerManagerImpl method checkIfPrivate.

protected void checkIfPrivate() {
    Download dl;
    try {
        dl = getDownload();
    } catch (Throwable e) {
        return;
    }
    Torrent t = dl.getTorrent();
    if (t != null) {
        if (TorrentUtils.isReallyPrivate(PluginCoreUtils.unwrap(t))) {
            throw (new RuntimeException("Torrent is private, peer addition not permitted"));
        }
    }
}
Also used : Torrent(com.biglybt.pif.torrent.Torrent) Download(com.biglybt.pif.download.Download)

Example 68 with Download

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

the class BuddyPluginViewBetaChat method dropDownloadFile.

private void dropDownloadFile(DiskManagerFileInfo file, DropAccepter accepter) {
    try {
        Download download = file.getDownload();
        if (download.getTorrent().isSimpleTorrent()) {
            dropDownload(download, accepter);
            return;
        }
        File target = file.getFile(true);
        if (target.exists() && (file.getDownloaded() == file.getLength() || (download.isComplete() && !file.isSkipped()))) {
            // just in case cached file completion is borked
            dropFile(target, accepter);
        } else {
            throw (new Exception("File is incomplete or missing"));
        }
    } catch (Throwable e) {
        dropFailed(file.getFile(true).getName(), e);
    }
}
Also used : Download(com.biglybt.pif.download.Download) ShareResourceFile(com.biglybt.pif.sharing.ShareResourceFile) File(java.io.File)

Example 69 with Download

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

the class BuddyPluginView method addBetaSubviews.

private void addBetaSubviews(boolean enable) {
    Class[] datasourceTypes = { // TODO: Use com.biglybt.pif.tag.Tag?
    Tag.class, Download.class };
    if (enable) {
        taggableLifecycleAdapter = new TaggableLifecycleAdapter() {

            @Override
            public void taggableTagged(TagType tag_type, Tag tag, Taggable taggable) {
                if (tag_type.getTagType() == TagType.TT_DOWNLOAD_MANUAL) {
                    DownloadManager dm = (DownloadManager) taggable;
                    for (BetaSubViewHolder h : beta_subviews.values()) {
                        h.tagsUpdated(dm);
                    }
                }
            }

            @Override
            public void taggableUntagged(TagType tag_type, Tag tag, Taggable taggable) {
                if (tag_type.getTagType() == TagType.TT_DOWNLOAD_MANUAL) {
                    DownloadManager dm = (DownloadManager) taggable;
                    for (BetaSubViewHolder h : beta_subviews.values()) {
                        h.tagsUpdated(dm);
                    }
                }
            }
        };
        TagManagerFactory.getTagManager().addTaggableLifecycleListener(Taggable.TT_DOWNLOAD, taggableLifecycleAdapter);
        UISWTViewEventListener listener = new UISWTViewEventListener() {

            @Override
            public boolean eventOccurred(UISWTViewEvent event) {
                UISWTView currentView = event.getView();
                switch(event.getType()) {
                    case UISWTViewEvent.TYPE_CREATE:
                        {
                            beta_subviews.put(currentView, new BetaSubViewHolder());
                            currentView.setDestroyOnDeactivate(false);
                            break;
                        }
                    case UISWTViewEvent.TYPE_INITIALIZE:
                        {
                            BetaSubViewHolder subview = beta_subviews.get(currentView);
                            if (subview != null) {
                                subview.initialise(event.getView(), (Composite) event.getData());
                            }
                            break;
                        }
                    case UISWTViewEvent.TYPE_DATASOURCE_CHANGED:
                        {
                            BetaSubViewHolder subview = beta_subviews.get(currentView);
                            if (subview != null) {
                                subview.setDataSource(event.getData());
                            }
                            break;
                        }
                    case UISWTViewEvent.TYPE_FOCUSGAINED:
                        {
                            BetaSubViewHolder subview = beta_subviews.get(currentView);
                            if (subview != null) {
                                subview.gotFocus();
                            }
                            break;
                        }
                    case UISWTViewEvent.TYPE_FOCUSLOST:
                        {
                            BetaSubViewHolder subview = beta_subviews.get(currentView);
                            if (subview != null) {
                                subview.lostFocus();
                            }
                            break;
                        }
                    case UISWTViewEvent.TYPE_DESTROY:
                        {
                            BetaSubViewHolder subview = beta_subviews.remove(currentView);
                            if (subview != null) {
                                subview.destroy();
                            }
                            break;
                        }
                }
                return true;
            }
        };
        // Our one listener instance can handle multiple views, so we
        // return same listener for every instantiation
        UISWTViewBuilder viewBuilder = ui_instance.createViewBuilder(VIEWID_CHAT).setListenerInstantiator(true, (Builder, view) -> listener);
        for (Class datasourceType : datasourceTypes) {
            ui_instance.registerView(datasourceType, viewBuilder);
        }
        TableManager table_manager = plugin.getPluginInterface().getUIManager().getTableManager();
        TableCellRefreshListener msg_refresh_listener = new TableCellRefreshListener() {

            @Override
            public void refresh(TableCell _cell) {
                TableCellSWT cell = (TableCellSWT) _cell;
                Download dl = (Download) cell.getDataSource();
                if (dl == null) {
                    return;
                }
                List<ChatInstance> instances = BuddyPluginUtils.peekChatInstances(dl);
                boolean is_pending = false;
                boolean is_sm = false;
                String msg_history = "";
                for (ChatInstance instance : instances) {
                    if (instance.getMessageOutstanding()) {
                        is_pending = true;
                        List<ChatMessage> messages = instance.getMessages();
                        for (ChatMessage msg : messages) {
                            if (msg.getFlagOrigin() == BuddyPluginBeta.FLAGS_MSG_ORIGIN_RATINGS) {
                                if (msg.getMessage().contains("Swarm_Merging")) {
                                    is_sm = true;
                                }
                            }
                        }
                    }
                    if (plugin.getBeta().getFTUXAccepted()) {
                        Object[] entry = (Object[]) dl.getUserData(TT_KEY);
                        long now = SystemTime.getCurrentTime();
                        if (entry == null || now - (Long) entry[0] > 30 * 1000) {
                            List<ChatMessage> msgs = instance.getMessages();
                            int num = msgs.size();
                            if (num > 0) {
                                String msgs_str = "";
                                int added = 0;
                                for (int i = msgs.size() - 1; i >= 0; i--) {
                                    ChatMessage msg = msgs.get(i);
                                    if (msg.isIgnored()) {
                                        continue;
                                    } else if (msg.getMessageType() == ChatMessage.MT_NORMAL) {
                                        String str = msg.getMessage();
                                        if (str.length() > 50) {
                                            str = str.substring(0, 47) + "...";
                                        }
                                        msgs_str = str + "\n" + msgs_str;
                                        added++;
                                        if (added == 5) {
                                            break;
                                        }
                                    }
                                }
                                if (!msgs_str.isEmpty()) {
                                    if (!msg_history.isEmpty()) {
                                        msg_history += "\n\n";
                                    }
                                    msg_history += msgs_str;
                                }
                            }
                            dl.setUserData(TT_KEY, new Object[] { now, msg_history });
                        } else {
                            msg_history = (String) entry[1];
                        }
                    }
                }
                Image graphic;
                String tooltip;
                int sort_order;
                if (is_pending) {
                    if (is_sm) {
                        graphic = bs_chat_swarm_merge;
                        tooltip = MessageText.getString("label.swarm.merge.available");
                        sort_order = 2;
                    } else {
                        graphic = bs_chat_gray_text;
                        tooltip = MessageText.getString("TableColumn.header.chat.msg.out");
                        sort_order = 1;
                    }
                } else {
                    graphic = null;
                    tooltip = MessageText.getString("label.no.messages");
                    sort_order = 0;
                }
                if (!msg_history.isEmpty()) {
                    tooltip += "\n\n---- " + MessageText.getString("label.history") + " ----\n" + msg_history.trim();
                }
                cell.setMarginHeight(0);
                cell.setGraphic(graphic);
                cell.setToolTip(tooltip);
                cell.setSortValue(sort_order);
                cell.setCursorID(graphic == null ? SWT.CURSOR_ARROW : SWT.CURSOR_HAND);
            }
        };
        TableCellMouseListener msg_mouse_listener = new TableCellMouseListener() {

            @Override
            public void cellMouseTrigger(TableCellMouseEvent event) {
                if (event.eventType == TableCellMouseEvent.EVENT_MOUSEUP) {
                    TableCell cell = event.cell;
                    Download dl = (Download) cell.getDataSource();
                    if (dl != null) {
                        List<ChatInstance> instances = BuddyPluginUtils.peekChatInstances(dl);
                        for (ChatInstance instance : instances) {
                            if (instance.getMessageOutstanding()) {
                                try {
                                    BuddyPluginUtils.getBetaPlugin().showChat(instance);
                                } catch (Throwable e) {
                                    Debug.out(e);
                                }
                            }
                        }
                    }
                } else if (event.eventType == TableCellMouseEvent.EVENT_MOUSEDOUBLECLICK) {
                    TableCell cell = event.cell;
                    Download dl = (Download) cell.getDataSource();
                    if (dl != null) {
                        List<ChatInstance> instances = BuddyPluginUtils.peekChatInstances(dl);
                        for (ChatInstance instance : instances) {
                            try {
                                BuddyPluginUtils.getBetaPlugin().showChat(instance);
                            } catch (Throwable e) {
                                Debug.out(e);
                            }
                        }
                    }
                    event.skipCoreFunctionality = true;
                }
            }
        };
        columnMessagePending = new TableColumnCreationListener() {

            @Override
            public void tableColumnCreated(TableColumn result) {
                result.setAlignment(TableColumn.ALIGN_CENTER);
                result.setPosition(TableColumn.POSITION_LAST);
                result.setWidth(32);
                result.setRefreshInterval(TableColumn.INTERVAL_INVALID_ONLY);
                result.setType(TableColumn.TYPE_GRAPHIC);
                result.addCellRefreshListener(msg_refresh_listener);
                result.addCellMouseListener(msg_mouse_listener);
                result.setIconReference("dchat_gray", true);
                synchronized (columns) {
                    columns.add(result);
                }
            }
        };
        table_manager.registerColumn(Download.class, "azbuddy.ui.column.msgpending", columnMessagePending);
    } else {
        for (Class datasourceType : datasourceTypes) {
            ui_instance.unregisterView(datasourceType, VIEWID_CHAT);
        }
        for (UISWTView entry : new ArrayList<>(beta_subviews.keySet())) {
            entry.closeView();
        }
        if (taggableLifecycleAdapter != null) {
            TagManagerFactory.getTagManager().removeTaggableLifecycleListener(Taggable.TT_DOWNLOAD, taggableLifecycleAdapter);
            taggableLifecycleAdapter = null;
        }
        beta_subviews.clear();
        if (columnMessagePending != null) {
            TableManager table_manager = plugin.getPluginInterface().getUIManager().getTableManager();
            table_manager.unregisterColumn(Download.class, "azbuddy.ui.column.msgpending", columnMessagePending);
            columnMessagePending = null;
            synchronized (columns) {
                columns.clear();
            }
        }
    }
}
Also used : ChatInstance(com.biglybt.plugin.net.buddy.BuddyPluginBeta.ChatInstance) TableCellSWT(com.biglybt.ui.swt.views.table.TableCellSWT) Image(org.eclipse.swt.graphics.Image) DownloadManager(com.biglybt.core.download.DownloadManager) List(java.util.List) Download(com.biglybt.pif.download.Download) ChatMessage(com.biglybt.plugin.net.buddy.BuddyPluginBeta.ChatMessage) TableColumn(com.biglybt.pif.ui.tables.TableColumn) Point(org.eclipse.swt.graphics.Point)

Example 70 with Download

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

the class ColumnChatMessageCount method refresh.

@Override
public void refresh(TableCell cell) {
    Object dataSource = cell.getDataSource();
    int num = -1;
    if (dataSource instanceof Download) {
        Download dl = (Download) dataSource;
        BuddyPluginBeta beta = BuddyPluginUtils.getBetaPlugin();
        if (beta != null && beta.getEnableAutoDownloadChats()) {
            ChatInstance chat = beta.peekChatInstance(dl);
            if (chat != null) {
                num = chat.getMessageCount(true);
            } else {
                Map<String, Object> peek_data = beta.peekChat(dl, true);
                if (peek_data != null) {
                    Number message_count = (Number) peek_data.get("m");
                    if (message_count != null) {
                        num = message_count.intValue();
                    }
                }
            }
        }
    } else {
        ChatInstance chat = (ChatInstance) cell.getDataSource();
        if (chat != null) {
            num = chat.getMessageCount(true);
        }
    }
    if (!cell.setSortValue(num) && cell.isValid()) {
        return;
    }
    if (!cell.isShown()) {
        return;
    }
    cell.setText(num == -1 ? "" : (num < 100 ? String.valueOf(num) : "100+"));
}
Also used : BuddyPluginBeta(com.biglybt.plugin.net.buddy.BuddyPluginBeta) 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