Search in sources :

Example 41 with PEPeer

use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.

the class PeersGraphicView method doRefresh.

private void doRefresh() {
    synchronized (dm_data_lock) {
        if (panel == null || panel.isDisposed()) {
            return;
        }
        Point panelSize = panel.getSize();
        int pw = panelSize.x;
        int ph = panelSize.y;
        int num_dms = dm_data.length;
        if (num_dms == 0 || pw == 0 || ph == 0) {
            GC gcPanel = new GC(panel);
            gcPanel.setBackground(Colors.white);
            gcPanel.fillRectangle(panel.getBounds());
            gcPanel.dispose();
            return;
        }
        int h_cells;
        int v_cells;
        if (ph <= pw) {
            v_cells = 1;
            h_cells = pw / ph;
            double f = Math.sqrt(((double) num_dms) / (v_cells * h_cells));
            int factor = (int) Math.ceil(f);
            h_cells *= factor;
            v_cells = factor;
        } else {
            v_cells = ph / pw;
            h_cells = 1;
            double f = Math.sqrt(((double) num_dms) / (v_cells * h_cells));
            int factor = (int) Math.ceil(f);
            v_cells *= factor;
            h_cells = factor;
        }
        ph = h_cells == 1 ? (ph / num_dms) : (ph / v_cells);
        pw = v_cells == 1 ? (pw / num_dms) : (pw / h_cells);
        // System.out.println( h_cells + "*" + v_cells + ": " + pw + "*" + ph );
        Point mySize = new Point(pw, ph);
        int num = 0;
        Point lastOffset = null;
        for (ManagerData data : dm_data) {
            DownloadManager manager = data.manager;
            PEPeer[] sortedPeers;
            try {
                data.peers_mon.enter();
                List<PEPeerTransport> connectedPeers = new ArrayList<>();
                for (PEPeer peer : data.peers) {
                    if (peer_filter.acceptPeer(peer)) {
                        if (peer instanceof PEPeerTransport) {
                            PEPeerTransport peerTransport = (PEPeerTransport) peer;
                            if (peerTransport.getConnectionState() == PEPeerTransport.CONNECTION_FULLY_ESTABLISHED)
                                connectedPeers.add(peerTransport);
                        }
                    }
                }
                sortedPeers = connectedPeers.toArray(new PEPeer[connectedPeers.size()]);
            } finally {
                data.peers_mon.exit();
            }
            if (sortedPeers == null)
                return;
            for (int i = 0; i < 3; i++) {
                try {
                    Arrays.sort(sortedPeers, peerComparator);
                    break;
                } catch (IllegalArgumentException e) {
                // can happen as peer data can change during sort and result in 'comparison method violates its general contract' error
                }
            }
            int h = num % h_cells;
            int v = num / h_cells;
            Point myOffset = new Point(h * pw, v * ph);
            render(manager, data, sortedPeers, mySize, myOffset);
            num++;
            lastOffset = myOffset;
        }
        int rem_x = panelSize.x - (lastOffset.x + mySize.x);
        if (rem_x > 0) {
            GC gcPanel = new GC(panel);
            gcPanel.setBackground(Colors.white);
            gcPanel.fillRectangle(lastOffset.x + mySize.x, lastOffset.y, rem_x, mySize.y);
            gcPanel.dispose();
        }
        int rem_y = panelSize.y - (lastOffset.y + mySize.y);
        if (rem_y > 0) {
            GC gcPanel = new GC(panel);
            gcPanel.setBackground(Colors.white);
            gcPanel.fillRectangle(0, lastOffset.y + mySize.y, panelSize.x, rem_y);
            gcPanel.dispose();
        }
    }
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) PEPeerTransport(com.biglybt.core.peer.impl.PEPeerTransport) ArrayList(java.util.ArrayList) Point(org.eclipse.swt.graphics.Point) GC(org.eclipse.swt.graphics.GC) DownloadManager(com.biglybt.core.download.DownloadManager) Point(org.eclipse.swt.graphics.Point)

Example 42 with PEPeer

use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.

the class PeersViewBase method createSwarmsView.

private void createSwarmsView(Composite parent) {
    if (swarm_view != null) {
        swarm_view.delete();
    }
    swarm_view = new PeersGraphicView(new PeersGraphicView.PeerFilter() {

        @Override
        public boolean acceptPeer(PEPeer peer) {
            return (swarm_peers.contains(peer));
        }
    });
    swarm_view.initialize(parent);
    swarm_view.getComposite().setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    parent.getParent().layout(true, true);
    swarm_view.setFocused(true);
    peers_changed = true;
    updateSwarmPeers();
    swarm_view.refresh();
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) GridData(org.eclipse.swt.layout.GridData)

Example 43 with PEPeer

use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.

the class PeersViewBase method updateSwarmPeers.

private void updateSwarmPeers() {
    if (peers_changed) {
        Utils.execSWTThread(new Runnable() {

            public void run() {
                peers_changed = false;
                GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
                List<PEPeer> peers = tv.getDataSources();
                swarm_peers = new HashSet<>(peers);
                final Map<PEPeerManager, int[]> done_pms = new HashMap<>();
                List<DownloadManager> dms = new ArrayList<>();
                for (PEPeer peer : peers) {
                    PEPeerManager pm = peer.getManager();
                    int[] count = done_pms.get(pm);
                    if (count == null) {
                        done_pms.put(pm, new int[] { 1 });
                        byte[] hash = pm.getHash();
                        DownloadManager dm = gm.getDownloadManager(new HashWrapper(hash));
                        if (dm != null) {
                            dms.add(dm);
                        }
                    } else {
                        count[0]++;
                    }
                }
                Collections.sort(dms, new Comparator<DownloadManager>() {

                    @Override
                    public int compare(DownloadManager o1, DownloadManager o2) {
                        PEPeerManager pm1 = o1.getPeerManager();
                        PEPeerManager pm2 = o2.getPeerManager();
                        int[] c1 = done_pms.get(pm1);
                        int[] c2 = done_pms.get(pm2);
                        int n1 = c1 == null ? 0 : c1[0];
                        int n2 = c2 == null ? 0 : c2[0];
                        return (n2 - n1);
                    }
                });
                swarm_view.dataSourceChanged(dms.toArray(new DownloadManager[dms.size()]));
            }
        });
    }
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) DownloadManager(com.biglybt.core.download.DownloadManager) Comparator(java.util.Comparator) GlobalManager(com.biglybt.core.global.GlobalManager) HashWrapper(com.biglybt.core.util.HashWrapper) ArrayList(java.util.ArrayList) List(java.util.List) PEPeerManager(com.biglybt.core.peer.PEPeerManager) Map(java.util.Map) HashMap(java.util.HashMap) IdentityHashSet(com.biglybt.core.util.IdentityHashSet) HashSet(java.util.HashSet)

Example 44 with PEPeer

use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.

the class PeersViewBase method fillMenu.

private static void fillMenu(final Menu menu, final PEPeer[] peers, final Shell shell, DownloadManager download_specific) {
    boolean hasSelection = (peers.length > 0);
    boolean downSpeedDisabled = false;
    boolean downSpeedUnlimited = false;
    long totalDownSpeed = 0;
    long downSpeedSetMax = 0;
    long maxDown = 0;
    boolean upSpeedDisabled = false;
    boolean upSpeedUnlimited = false;
    long totalUpSpeed = 0;
    long upSpeedSetMax = 0;
    long maxUp = 0;
    GlobalManager gm = CoreFactory.getSingleton().getGlobalManager();
    final IdentityHashSet<DownloadManager> download_managers = new IdentityHashSet<>();
    Map<PEPeer, DownloadManager> peer_dm_map = new HashMap<>();
    if (hasSelection) {
        for (int i = 0; i < peers.length; i++) {
            PEPeer peer = peers[i];
            PEPeerManager m = peer.getManager();
            if (m != null) {
                if (gm != null) {
                    DownloadManager dm = gm.getDownloadManager(new HashWrapper(m.getHash()));
                    if (dm != null) {
                        peer_dm_map.put(peer, dm);
                        download_managers.add(dm);
                    }
                }
            }
            try {
                int maxul = peer.getStats().getUploadRateLimitBytesPerSecond();
                maxUp += maxul * 4;
                if (maxul == 0) {
                    upSpeedUnlimited = true;
                } else {
                    if (maxul > upSpeedSetMax) {
                        upSpeedSetMax = maxul;
                    }
                }
                if (maxul == -1) {
                    maxul = 0;
                    upSpeedDisabled = true;
                }
                totalUpSpeed += maxul;
                int maxdl = peer.getStats().getDownloadRateLimitBytesPerSecond();
                maxDown += maxdl * 4;
                if (maxdl == 0) {
                    downSpeedUnlimited = true;
                } else {
                    if (maxdl > downSpeedSetMax) {
                        downSpeedSetMax = maxdl;
                    }
                }
                if (maxdl == -1) {
                    maxdl = 0;
                    downSpeedDisabled = true;
                }
                totalDownSpeed += maxdl;
            } catch (Exception ex) {
                Debug.printStackTrace(ex);
            }
        }
    }
    if (download_specific != null) {
        final MenuItem block_item = new MenuItem(menu, SWT.CHECK);
        PEPeer peer = peers.length == 0 ? null : peers[0];
        if (peer == null || peer.getManager().getDiskManager().getRemainingExcludingDND() > 0) {
            // disallow peer upload blocking when downloading
            block_item.setSelection(false);
            block_item.setEnabled(false);
        } else {
            block_item.setEnabled(true);
            block_item.setSelection(peer.isSnubbed());
        }
        if (peer != null) {
            final boolean newSnubbedValue = !peer.isSnubbed();
            Messages.setLanguageText(block_item, "PeersView.menu.blockupload");
            block_item.addListener(SWT.Selection, new PeersRunner(peers) {

                @Override
                public void run(PEPeer peer) {
                    peer.setSnubbed(newSnubbedValue);
                }
            });
        }
    } else {
        if (download_managers.size() > 0) {
            MenuItem itemDetails = new MenuItem(menu, SWT.PUSH);
            Messages.setLanguageText(itemDetails, "PeersView.menu.showdownload");
            Utils.setMenuItemImage(itemDetails, "details");
            itemDetails.addListener(SWT.Selection, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
                    if (uiFunctions != null) {
                        for (DownloadManager dm : download_managers) {
                            uiFunctions.getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_DETAILS, dm);
                        }
                    }
                }
            });
            new MenuItem(menu, SWT.SEPARATOR);
        }
    }
    BuddyPlugin bp = BuddyPluginUtils.getPlugin();
    if (bp != null) {
        boolean has_pb = false;
        boolean has_public = false;
        for (PEPeer peer : peers) {
            if (AENetworkClassifier.categoriseAddress(peer.getIp()) == AENetworkClassifier.AT_PUBLIC) {
                has_public = true;
                DownloadManager dm = peer_dm_map.get(peer);
                if (dm != null && bp.isPartialBuddy(PluginCoreUtils.wrap(dm), PluginCoreUtils.wrap(peer))) {
                    has_pb = true;
                }
            }
        }
        MenuItem boost_item = new MenuItem(menu, SWT.CHECK);
        Messages.setLanguageText(boost_item, "PeersView.menu.boost");
        boost_item.setSelection(has_pb);
        boost_item.setEnabled(has_public);
        boost_item.addListener(SWT.Selection, new PeersRunner(peers) {

            @Override
            public void run(PEPeer peer) {
                boolean sel = boost_item.getSelection();
                if (AENetworkClassifier.categoriseAddress(peer.getIp()) == AENetworkClassifier.AT_PUBLIC) {
                    DownloadManager dm = peer_dm_map.get(peer);
                    if (dm != null) {
                        bp.setPartialBuddy(PluginCoreUtils.wrap(dm), PluginCoreUtils.wrap(peer), sel);
                    }
                }
            }
        });
    }
    final MenuItem kick_item = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(kick_item, "PeersView.menu.kick");
    kick_item.addListener(SWT.Selection, new PeersRunner(peers) {

        @Override
        public void run(PEPeer peer) {
            peer.getManager().removePeer(peer, "Peer kicked");
        }
    });
    final MenuItem ban_item = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(ban_item, "PeersView.menu.kickandban");
    ban_item.addListener(SWT.Selection, new PeersRunner(peers) {

        @Override
        public void run(PEPeer peer) {
            String msg = MessageText.getString("PeersView.menu.kickandban.reason");
            IpFilterManagerFactory.getSingleton().getIPFilter().ban(peer.getIp(), msg, true);
            peer.getManager().removePeer(peer, "Peer kicked and banned");
        }
    });
    final MenuItem ban_for_item = new MenuItem(menu, SWT.PUSH);
    Messages.setLanguageText(ban_for_item, "PeersView.menu.kickandbanfor");
    ban_for_item.addListener(SWT.Selection, new PeersRunner(peers) {

        @Override
        public boolean run(final PEPeer[] peers) {
            String text = MessageText.getString("dialog.ban.for.period.text");
            SimpleTextEntryWindow entryWindow = new SimpleTextEntryWindow("dialog.ban.for.period.title", "!" + text + "!");
            int def = COConfigurationManager.getIntParameter("ban.for.period.default", 60);
            entryWindow.setPreenteredText(String.valueOf(def), false);
            entryWindow.prompt(new UIInputReceiverListener() {

                @Override
                public void UIInputReceiverClosed(UIInputReceiver entryWindow) {
                    if (!entryWindow.hasSubmittedInput()) {
                        return;
                    }
                    String sReturn = entryWindow.getSubmittedInput();
                    if (sReturn == null) {
                        return;
                    }
                    int mins = -1;
                    try {
                        mins = Integer.valueOf(sReturn).intValue();
                    } catch (NumberFormatException er) {
                    // Ignore
                    }
                    if (mins <= 0) {
                        MessageBox mb = new MessageBox(Utils.findAnyShell(), SWT.ICON_ERROR | SWT.OK);
                        mb.setText(MessageText.getString("MyTorrentsView.dialog.NumberError.title"));
                        mb.setMessage(MessageText.getString("MyTorrentsView.dialog.NumberError.text"));
                        mb.open();
                        return;
                    }
                    COConfigurationManager.setParameter("ban.for.period.default", mins);
                    IpFilter filter = IpFilterManagerFactory.getSingleton().getIPFilter();
                    for (PEPeer peer : peers) {
                        String msg = MessageText.getString("PeersView.menu.kickandbanfor.reason", new String[] { String.valueOf(mins) });
                        filter.ban(peer.getIp(), msg, true, mins);
                        peer.getManager().removePeer(peer, "Peer kicked and banned");
                    }
                }
            });
            return (true);
        }
    });
    // === advanced menu ===
    final MenuItem itemAdvanced = new MenuItem(menu, SWT.CASCADE);
    Messages.setLanguageText(itemAdvanced, "MyTorrentsView.menu.advancedmenu");
    itemAdvanced.setEnabled(hasSelection);
    final Menu menuAdvanced = new Menu(shell, SWT.DROP_DOWN);
    itemAdvanced.setMenu(menuAdvanced);
    // advanced > Download Speed Menu //
    Map<String, Object> menu_properties = new HashMap<>();
    menu_properties.put(ViewUtils.SM_PROP_PERMIT_UPLOAD_DISABLE, true);
    menu_properties.put(ViewUtils.SM_PROP_PERMIT_DOWNLOAD_DISABLE, true);
    ViewUtils.addSpeedMenu(shell, menuAdvanced, true, true, false, hasSelection, downSpeedDisabled, downSpeedUnlimited, totalDownSpeed, downSpeedSetMax, maxDown, upSpeedDisabled, upSpeedUnlimited, totalUpSpeed, upSpeedSetMax, maxUp, peers.length, menu_properties, new ViewUtils.SpeedAdapter() {

        @Override
        public void setDownSpeed(int speed) {
            if (peers.length > 0) {
                for (int i = 0; i < peers.length; i++) {
                    try {
                        PEPeer peer = (PEPeer) peers[i];
                        peer.getStats().setDownloadRateLimitBytesPerSecond(speed);
                    } catch (Exception e) {
                        Debug.printStackTrace(e);
                    }
                }
            }
        }

        @Override
        public void setUpSpeed(int speed) {
            if (peers.length > 0) {
                for (int i = 0; i < peers.length; i++) {
                    try {
                        PEPeer peer = (PEPeer) peers[i];
                        peer.getStats().setUploadRateLimitBytesPerSecond(speed);
                    } catch (Exception e) {
                        Debug.printStackTrace(e);
                    }
                }
            }
        }
    });
    addPeersMenu(download_specific, "", menu);
}
Also used : IdentityHashSet(com.biglybt.core.util.IdentityHashSet) PEPeer(com.biglybt.core.peer.PEPeer) IpFilter(com.biglybt.core.ipfilter.IpFilter) SelectionListener(org.eclipse.swt.events.SelectionListener) TableViewSWTMenuFillListener(com.biglybt.ui.swt.views.table.TableViewSWTMenuFillListener) Listener(org.eclipse.swt.widgets.Listener) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) TableSelectionListener(com.biglybt.ui.common.table.TableSelectionListener) UIManagerListener(com.biglybt.pif.ui.UIManagerListener) TableLifeCycleListener(com.biglybt.ui.common.table.TableLifeCycleListener) HashMap(java.util.HashMap) DownloadManager(com.biglybt.core.download.DownloadManager) BuddyPlugin(com.biglybt.plugin.net.buddy.BuddyPlugin) GlobalManager(com.biglybt.core.global.GlobalManager) UIFunctions(com.biglybt.ui.UIFunctions) SimpleTextEntryWindow(com.biglybt.ui.swt.SimpleTextEntryWindow) Menu(org.eclipse.swt.widgets.Menu) MenuItem(org.eclipse.swt.widgets.MenuItem) MessageBox(org.eclipse.swt.widgets.MessageBox) HashWrapper(com.biglybt.core.util.HashWrapper) UIInputReceiver(com.biglybt.pif.ui.UIInputReceiver) UISWTViewEvent(com.biglybt.ui.swt.pif.UISWTViewEvent) Event(org.eclipse.swt.widgets.Event) SelectionEvent(org.eclipse.swt.events.SelectionEvent) PEPeerManager(com.biglybt.core.peer.PEPeerManager) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener)

Example 45 with PEPeer

use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.

the class UploadSessionPicker method pickNextOptimisticSession.

// this picks both downloading and seeding sessions
protected UploadSession pickNextOptimisticSession() {
    try {
        next_optimistics_mon.enter();
        HashSet failed_helpers = null;
        int loops_allowed = next_optimistics.size();
        while (loops_allowed > 0) {
            // take from front
            UploadHelper helper = (UploadHelper) next_optimistics.removeFirst();
            // add back at end
            next_optimistics.addLast(helper);
            if (failed_helpers == null || !failed_helpers.contains(helper)) {
                // pre-emptive check to see if we've already tried this helper
                PEPeer peer;
                if (helper.isSeeding()) {
                    peer = seed_ranker.getNextOptimisticPeer(helper.getAllPeers());
                } else {
                    peer = down_ranker.getNextOptimisticPeer(helper.getAllPeers());
                }
                if (peer == null) {
                    // lazy alloc
                    if (failed_helpers == null)
                        failed_helpers = new HashSet();
                    // remember this helper in case we get it again in another loop round
                    failed_helpers.add(helper);
                } else {
                    return new UploadSession(peer, helper.isSeeding() ? UploadSession.TYPE_SEED : UploadSession.TYPE_DOWNLOAD);
                }
            }
            loops_allowed--;
        }
        // no optimistic peer found
        return null;
    } finally {
        next_optimistics_mon.exit();
    }
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer)

Aggregations

PEPeer (com.biglybt.core.peer.PEPeer)84 PEPeerManager (com.biglybt.core.peer.PEPeerManager)18 DownloadManager (com.biglybt.core.download.DownloadManager)11 ArrayList (java.util.ArrayList)11 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)5 AERunnable (com.biglybt.core.util.AERunnable)5 PEPiece (com.biglybt.core.peer.PEPiece)4 List (java.util.List)4 Image (org.eclipse.swt.graphics.Image)4 DiskManager (com.biglybt.core.disk.DiskManager)3 GlobalManager (com.biglybt.core.global.GlobalManager)3 PEPeerStats (com.biglybt.core.peer.PEPeerStats)3 PEPeerTransport (com.biglybt.core.peer.impl.PEPeerTransport)3 TOTorrent (com.biglybt.core.torrent.TOTorrent)3 Point (org.eclipse.swt.graphics.Point)3 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)2 DownloadManagerPeerListener (com.biglybt.core.download.DownloadManagerPeerListener)2 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)2 BitFlags (com.biglybt.core.peermanager.piecepicker.util.BitFlags)2 Tag (com.biglybt.core.tag.Tag)2