Search in sources :

Example 56 with PEPeerManager

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

the class FileInfoView method createFileInfoPanel.

private Composite createFileInfoPanel(Composite parent) {
    GridLayout layout;
    GridData gridData;
    // Peer Info section contains
    // - Peer's Block display
    // - Peer's Datarate
    fileInfoComposite = new Composite(parent, SWT.NONE);
    layout = new GridLayout();
    layout.numColumns = 2;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    fileInfoComposite.setLayout(layout);
    gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
    fileInfoComposite.setLayoutData(gridData);
    new Label(fileInfoComposite, SWT.NULL).setLayoutData(new GridData());
    topLabel = new Label(fileInfoComposite, SWT.NULL);
    gridData = new GridData(SWT.FILL, SWT.DEFAULT, false, false);
    topLabel.setLayoutData(gridData);
    sc = new ScrolledComposite(fileInfoComposite, SWT.V_SCROLL);
    sc.setExpandHorizontal(true);
    sc.setExpandVertical(true);
    layout = new GridLayout();
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    sc.setLayout(layout);
    gridData = new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1);
    sc.setLayoutData(gridData);
    fileInfoCanvas = new Canvas(sc, SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
    gridData = new GridData(GridData.FILL, SWT.DEFAULT, true, false);
    fileInfoCanvas.setLayoutData(gridData);
    fileInfoCanvas.addPaintListener(new PaintListener() {

        @Override
        public void paintControl(PaintEvent e) {
            if (e.width <= 0 || e.height <= 0)
                return;
            try {
                Rectangle bounds = (img == null) ? null : img.getBounds();
                if (bounds == null) {
                    e.gc.fillRectangle(e.x, e.y, e.width, e.height);
                } else {
                    if (e.x + e.width > bounds.width)
                        e.gc.fillRectangle(bounds.width, e.y, e.x + e.width - bounds.width + 1, e.height);
                    if (e.y + e.height > bounds.height)
                        e.gc.fillRectangle(e.x, bounds.height, e.width, e.y + e.height - bounds.height + 1);
                    int width = Math.min(e.width, bounds.width - e.x);
                    int height = Math.min(e.height, bounds.height - e.y);
                    e.gc.drawImage(img, e.x, e.y, width, height, e.x, e.y, width, height);
                }
            } catch (Exception ex) {
            }
        }
    });
    fileInfoCanvas.addMouseTrackListener(new MouseTrackAdapter() {

        @Override
        public void mouseHover(MouseEvent event) {
            showPieceDetails(event.x, event.y);
        }
    });
    Listener doNothingListener = new Listener() {

        @Override
        public void handleEvent(Event event) {
        }
    };
    fileInfoCanvas.addListener(SWT.KeyDown, doNothingListener);
    final Menu menu = new Menu(fileInfoCanvas.getShell(), SWT.POP_UP);
    fileInfoCanvas.setMenu(menu);
    fileInfoCanvas.addListener(SWT.MenuDetect, new Listener() {

        @Override
        public void handleEvent(Event event) {
            Point pt = fileInfoCanvas.toControl(event.x, event.y);
            int piece_number = getPieceNumber(pt.x, pt.y);
            menu.setData("pieceNumber", piece_number);
        }
    });
    MenuBuildUtils.addMaintenanceListenerForMenu(menu, new MenuBuildUtils.MenuBuilder() {

        @Override
        public void buildMenu(Menu menu, MenuEvent event) {
            Integer pn = (Integer) menu.getData("pieceNumber");
            if (pn != null && pn != -1) {
                DownloadManager download_manager = file.getDownloadManager();
                if (download_manager == null) {
                    return;
                }
                DiskManager disk_manager = download_manager.getDiskManager();
                PEPeerManager peer_manager = download_manager.getPeerManager();
                if (disk_manager == null || peer_manager == null) {
                    return;
                }
                final PiecePicker picker = peer_manager.getPiecePicker();
                DiskManagerPiece[] dm_pieces = disk_manager.getPieces();
                PEPiece[] pe_pieces = peer_manager.getPieces();
                final int piece_number = pn;
                final DiskManagerPiece dm_piece = dm_pieces[piece_number];
                final PEPiece pe_piece = pe_pieces[piece_number];
                final MenuItem force_piece = new MenuItem(menu, SWT.CHECK);
                Messages.setLanguageText(force_piece, "label.force.piece");
                boolean done = dm_piece.isDone();
                force_piece.setEnabled(!done);
                if (!done) {
                    force_piece.setSelection(picker.isForcePiece(piece_number));
                    force_piece.addSelectionListener(new SelectionAdapter() {

                        @Override
                        public void widgetSelected(SelectionEvent e) {
                            picker.setForcePiece(piece_number, force_piece.getSelection());
                        }
                    });
                }
                final MenuItem reset_piece = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(reset_piece, "label.reset.piece");
                boolean can_reset = dm_piece.isDone() || dm_piece.getNbWritten() > 0;
                reset_piece.setEnabled(can_reset);
                reset_piece.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        dm_piece.reset();
                        if (pe_piece != null) {
                            pe_piece.reset();
                        }
                    }
                });
            }
        }
    });
    fileInfoCanvas.addListener(SWT.Resize, new Listener() {

        @Override
        public void handleEvent(Event e) {
            if (refreshInfoCanvasQueued) {
                return;
            }
            refreshInfoCanvasQueued = true;
            // wrap in asyncexec because sc.setMinWidth (called later) doesn't work
            // too well inside a resize (the canvas won't size isn't always updated)
            Utils.execSWTThreadLater(0, new AERunnable() {

                @Override
                public void runSupport() {
                    if (img != null) {
                        int iOldColCount = img.getBounds().width / BLOCK_SIZE;
                        int iNewColCount = fileInfoCanvas.getClientArea().width / BLOCK_SIZE;
                        if (iOldColCount != iNewColCount)
                            refreshInfoCanvas();
                    }
                }
            });
        }
    });
    sc.setContent(fileInfoCanvas);
    Legend.createLegendComposite(fileInfoComposite, blockColors, new String[] { "FileView.BlockView.Done", "FileView.BlockView.Skipped", "FileView.BlockView.Active", "FileView.BlockView.Outstanding" }, new GridData(SWT.FILL, SWT.DEFAULT, true, false, 2, 1));
    int iFontPixelsHeight = 10;
    int iFontPointHeight = (iFontPixelsHeight * 72) / Utils.getDPIRaw(fileInfoCanvas.getDisplay()).y;
    Font f = fileInfoCanvas.getFont();
    FontData[] fontData = f.getFontData();
    fontData[0].setHeight(iFontPointHeight);
    font = new Font(fileInfoCanvas.getDisplay(), fontData);
    return fileInfoComposite;
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) UISWTViewCoreEventListener(com.biglybt.ui.swt.pifimpl.UISWTViewCoreEventListener) PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker) DiskManager(com.biglybt.core.disk.DiskManager) DownloadManager(com.biglybt.core.download.DownloadManager) GridLayout(org.eclipse.swt.layout.GridLayout) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) PEPiece(com.biglybt.core.peer.PEPiece) GridData(org.eclipse.swt.layout.GridData) UISWTViewEvent(com.biglybt.ui.swt.pif.UISWTViewEvent) LogEvent(com.biglybt.core.logging.LogEvent) PEPeerManager(com.biglybt.core.peer.PEPeerManager) MenuBuildUtils(com.biglybt.ui.swt.MenuBuildUtils)

Example 57 with PEPeerManager

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

the class FileInfoView method refreshInfoCanvas.

protected void refreshInfoCanvas() {
    if (fileInfoCanvas == null || fileInfoCanvas.isDisposed()) {
        return;
    }
    refreshInfoCanvasQueued = false;
    Rectangle bounds = fileInfoCanvas.getClientArea();
    if (bounds.width <= 0 || bounds.height <= 0)
        return;
    if (img != null && !img.isDisposed()) {
        img.dispose();
        img = null;
    }
    DownloadManager download_manager = file == null ? null : file.getDownloadManager();
    DiskManager disk_manager = download_manager == null ? null : download_manager.getDiskManager();
    PEPeerManager peer_manager = download_manager == null ? null : download_manager.getPeerManager();
    if (file == null || disk_manager == null || peer_manager == null) {
        GC gc = new GC(fileInfoCanvas);
        gc.fillRectangle(bounds);
        gc.dispose();
        return;
    }
    int first_piece = file.getFirstPieceNumber();
    int num_pieces = file.getNbPieces();
    int iNumCols = bounds.width / BLOCK_SIZE;
    int iNeededHeight = (((num_pieces - 1) / iNumCols) + 1) * BLOCK_SIZE;
    if (sc.getMinHeight() != iNeededHeight) {
        sc.setMinHeight(iNeededHeight);
        sc.layout(true, true);
        bounds = fileInfoCanvas.getClientArea();
    }
    img = new Image(fileInfoCanvas.getDisplay(), bounds.width, iNeededHeight);
    GC gcImg = new GC(img);
    try {
        gcImg.setBackground(fileInfoCanvas.getBackground());
        gcImg.fillRectangle(0, 0, bounds.width, bounds.height);
        DiskManagerPiece[] dm_pieces = disk_manager.getPieces();
        PEPiece[] pe_pieces = peer_manager.getPieces();
        int iRow = 0;
        int iCol = 0;
        for (int i = first_piece; i < first_piece + num_pieces; i++) {
            DiskManagerPiece dm_piece = dm_pieces[i];
            PEPiece pe_piece = pe_pieces[i];
            int colorIndex;
            int iXPos = iCol * BLOCK_SIZE;
            int iYPos = iRow * BLOCK_SIZE;
            if (dm_piece.isDone()) {
                colorIndex = BLOCKCOLOR_DONE;
            } else if (!dm_piece.isNeeded()) {
                colorIndex = BLOCKCOLOR_SKIPPED;
            } else if (pe_piece != null) {
                colorIndex = BLOCKCOLOR_ACTIVE;
            } else {
                colorIndex = BLOCKCOLOR_NEEDED;
            }
            gcImg.setBackground(blockColors[colorIndex]);
            gcImg.fillRectangle(iXPos, iYPos, BLOCK_FILLSIZE, BLOCK_FILLSIZE);
            iCol++;
            if (iCol >= iNumCols) {
                iCol = 0;
                iRow++;
            }
        }
    } catch (Exception e) {
        Logger.log(new LogEvent(LogIDs.GUI, "drawing piece map", e));
    } finally {
        gcImg.dispose();
    }
    fileInfoCanvas.redraw();
}
Also used : LogEvent(com.biglybt.core.logging.LogEvent) DiskManager(com.biglybt.core.disk.DiskManager) DownloadManager(com.biglybt.core.download.DownloadManager) PEPiece(com.biglybt.core.peer.PEPiece) PEPeerManager(com.biglybt.core.peer.PEPeerManager) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece)

Example 58 with PEPeerManager

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

the class PieceInfoView method getPieceNumber.

private int getPieceNumber(int x, int y) {
    DownloadManager manager = dlm;
    if (manager == null) {
        return (-1);
    }
    PEPeerManager pm = manager.getPeerManager();
    if (pm == null) {
        return (-1);
    }
    Rectangle bounds = pieceInfoCanvas.getClientArea();
    if (bounds.width <= 0 || bounds.height <= 0) {
        return (-1);
    }
    int iNumCols = bounds.width / BLOCK_SIZE;
    int x_block = x / BLOCK_SIZE;
    int y_block = y / BLOCK_SIZE;
    int piece_number = y_block * iNumCols + x_block;
    if (piece_number >= pm.getPiecePicker().getNumberOfPieces()) {
        return (-1);
    } else {
        return (piece_number);
    }
}
Also used : PEPeerManager(com.biglybt.core.peer.PEPeerManager) DownloadManager(com.biglybt.core.download.DownloadManager)

Example 59 with PEPeerManager

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

the class PieceInfoView method refreshInfoCanvas.

protected void refreshInfoCanvas() {
    synchronized (PieceInfoView.this) {
        alreadyFilling = false;
    }
    if (pieceInfoCanvas == null || pieceInfoCanvas.isDisposed() || !pieceInfoCanvas.isVisible()) {
        return;
    }
    pieceInfoCanvas.layout(true);
    Rectangle bounds = pieceInfoCanvas.getClientArea();
    if (bounds.width <= 0 || bounds.height <= 0) {
        topLabelLHS = "";
        updateTopLabel();
        return;
    }
    if (dlm == null) {
        GC gc = new GC(pieceInfoCanvas);
        gc.fillRectangle(bounds);
        gc.dispose();
        topLabelLHS = MessageText.getString("view.one.download.only");
        topLabelRHS = "";
        updateTopLabel();
        return;
    }
    PEPeerManager pm = dlm.getPeerManager();
    DiskManager dm = dlm.getDiskManager();
    if (pm == null || dm == null) {
        GC gc = new GC(pieceInfoCanvas);
        gc.fillRectangle(bounds);
        gc.dispose();
        topLabelLHS = "";
        updateTopLabel();
        return;
    }
    int iNumCols = bounds.width / BLOCK_SIZE;
    int iNeededHeight = (((dm.getNbPieces() - 1) / iNumCols) + 1) * BLOCK_SIZE;
    if (img != null && !img.isDisposed()) {
        Rectangle imgBounds = img.getBounds();
        if (imgBounds.width != bounds.width || imgBounds.height != iNeededHeight) {
            oldBlockInfo = null;
            img.dispose();
            img = null;
        }
    }
    DiskManagerPiece[] dm_pieces = dm.getPieces();
    PEPiece[] currentDLPieces = pm.getPieces();
    byte[] uploadingPieces = new byte[dm_pieces.length];
    // find upload pieces
    for (PEPeer peer : pm.getPeers()) {
        int[] peerRequestedPieces = peer.getIncomingRequestedPieceNumbers();
        if (peerRequestedPieces != null && peerRequestedPieces.length > 0) {
            int pieceNum = peerRequestedPieces[0];
            if (uploadingPieces[pieceNum] < 2)
                uploadingPieces[pieceNum] = 2;
            for (int j = 1; j < peerRequestedPieces.length; j++) {
                pieceNum = peerRequestedPieces[j];
                if (uploadingPieces[pieceNum] < 1)
                    uploadingPieces[pieceNum] = 1;
            }
        }
    }
    if (sc.getMinHeight() != iNeededHeight) {
        sc.setMinHeight(iNeededHeight);
        sc.layout(true, true);
        bounds = pieceInfoCanvas.getClientArea();
    }
    int[] availability = pm.getAvailability();
    int minAvailability = Integer.MAX_VALUE;
    int minAvailability2 = Integer.MAX_VALUE;
    if (availability != null && availability.length > 0) {
        for (int anAvailability : availability) {
            if (anAvailability != 0 && anAvailability < minAvailability) {
                minAvailability2 = minAvailability;
                minAvailability = anAvailability;
                if (minAvailability == 1) {
                    break;
                }
            }
        }
    }
    if (img == null) {
        img = new Image(pieceInfoCanvas.getDisplay(), bounds.width, iNeededHeight);
        oldBlockInfo = null;
    }
    GC gcImg = new GC(img);
    BlockInfo[] newBlockInfo = new BlockInfo[dm_pieces.length];
    int iRow = 0;
    try {
        // use advanced capabilities for faster drawText
        gcImg.setAdvanced(true);
        if (oldBlockInfo == null) {
            gcImg.setBackground(pieceInfoCanvas.getBackground());
            gcImg.fillRectangle(0, 0, bounds.width, iNeededHeight);
        }
        int selectionStart = Integer.MAX_VALUE;
        int selectionEnd = Integer.MIN_VALUE;
        if (selectedPiece != -1) {
            if (selectedPieceShowFile) {
                DMPieceList l = dm.getPieceList(selectedPiece);
                for (int i = 0; i < l.size(); i++) {
                    DMPieceMapEntry entry = l.get(i);
                    DiskManagerFileInfo info = entry.getFile();
                    int first = info.getFirstPieceNumber();
                    int last = info.getLastPieceNumber();
                    if (first < selectionStart) {
                        selectionStart = first;
                    }
                    if (last > selectionEnd) {
                        selectionEnd = last;
                    }
                }
            }
        }
        gcImg.setFont(font);
        int iCol = 0;
        for (int i = 0; i < dm_pieces.length; i++) {
            if (iCol >= iNumCols) {
                iCol = 0;
                iRow++;
            }
            BlockInfo newInfo = newBlockInfo[i] = new BlockInfo();
            if (i >= selectionStart && i <= selectionEnd) {
                newInfo.selected = true;
            }
            boolean done = dm_pieces[i].isDone();
            int iXPos = iCol * BLOCK_SIZE + 1;
            int iYPos = iRow * BLOCK_SIZE + 1;
            if (done) {
                newInfo.haveWidth = BLOCK_FILLSIZE;
            } else {
                // !done
                boolean partiallyDone = dm_pieces[i].getNbWritten() > 0;
                int width = BLOCK_FILLSIZE;
                if (partiallyDone) {
                    int iNewWidth = (int) (((float) dm_pieces[i].getNbWritten() / dm_pieces[i].getNbBlocks()) * width);
                    if (iNewWidth >= width)
                        iNewWidth = width - 1;
                    else if (iNewWidth <= 0)
                        iNewWidth = 1;
                    newInfo.haveWidth = iNewWidth;
                }
            }
            if (currentDLPieces[i] != null && currentDLPieces[i].hasUndownloadedBlock()) {
                newInfo.showDown = currentDLPieces[i].getNbRequests() == 0 ? SHOW_SMALL : SHOW_BIG;
            }
            if (uploadingPieces[i] > 0) {
                newInfo.showUp = uploadingPieces[i] < 2 ? SHOW_SMALL : SHOW_BIG;
            }
            if (availability != null) {
                newInfo.availNum = availability[i];
                if (minAvailability2 == availability[i]) {
                    newInfo.availDotted = true;
                }
            } else {
                newInfo.availNum = -1;
            }
            if (oldBlockInfo != null && i < oldBlockInfo.length && oldBlockInfo[i].sameAs(newInfo)) {
                iCol++;
                continue;
            }
            if (newInfo.selected) {
                Color fc = blockColors[BLOCKCOLOR_SHOWFILE];
                gcImg.setBackground(fc);
                gcImg.fillRectangle(iCol * BLOCK_SIZE, iRow * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
                if (fc != file_color) {
                    file_color = fc;
                    file_color_faded = Colors.getInstance().getLighterColor(fc, 75);
                }
                gcImg.setBackground(file_color_faded);
                gcImg.fillRectangle(iXPos + newInfo.haveWidth, iYPos, BLOCK_FILLSIZE - newInfo.haveWidth, BLOCK_FILLSIZE);
            } else {
                gcImg.setBackground(pieceInfoCanvas.getBackground());
                gcImg.fillRectangle(iCol * BLOCK_SIZE, iRow * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE);
                gcImg.setBackground(blockColors[BLOCKCOLOR_HAVE]);
                gcImg.fillRectangle(iXPos, iYPos, newInfo.haveWidth, BLOCK_FILLSIZE);
                gcImg.setBackground(blockColors[BLOCKCOLORL_NOHAVE]);
                gcImg.fillRectangle(iXPos + newInfo.haveWidth, iYPos, BLOCK_FILLSIZE - newInfo.haveWidth, BLOCK_FILLSIZE);
            }
            if (newInfo.showDown > 0) {
                drawDownloadIndicator(gcImg, iXPos, iYPos, newInfo.showDown == SHOW_SMALL);
            }
            if (newInfo.showUp > 0) {
                drawUploadIndicator(gcImg, iXPos, iYPos, newInfo.showUp == SHOW_SMALL);
            }
            if (newInfo.availNum != -1) {
                if (minAvailability == newInfo.availNum) {
                    gcImg.setForeground(blockColors[BLOCKCOLOR_AVAILCOUNT]);
                    gcImg.drawRectangle(iXPos - 1, iYPos - 1, BLOCK_FILLSIZE + 1, BLOCK_FILLSIZE + 1);
                }
                if (minAvailability2 == newInfo.availNum) {
                    gcImg.setLineStyle(SWT.LINE_DOT);
                    gcImg.setForeground(blockColors[BLOCKCOLOR_AVAILCOUNT]);
                    gcImg.drawRectangle(iXPos - 1, iYPos - 1, BLOCK_FILLSIZE + 1, BLOCK_FILLSIZE + 1);
                    gcImg.setLineStyle(SWT.LINE_SOLID);
                }
                String sNumber = String.valueOf(newInfo.availNum);
                Point size = gcImg.stringExtent(sNumber);
                if (newInfo.availNum < 100) {
                    int x = iXPos + (BLOCK_FILLSIZE / 2) - (size.x / 2);
                    int y = iYPos + (BLOCK_FILLSIZE / 2) - (size.y / 2);
                    gcImg.setForeground(blockColors[BLOCKCOLOR_AVAILCOUNT]);
                    gcImg.drawText(sNumber, x, y, true);
                }
            }
            iCol++;
        }
        oldBlockInfo = newBlockInfo;
    } catch (Exception e) {
        Logger.log(new LogEvent(LogIDs.GUI, "drawing piece map", e));
    } finally {
        gcImg.dispose();
    }
    topLabelLHS = MessageText.getString("PiecesView.BlockView.Header", new String[] { "" + iNumCols, "" + (iRow + 1), "" + dm_pieces.length });
    PiecePicker picker = pm.getPiecePicker();
    int seq_info = picker.getSequentialInfo();
    if (seq_info != 0) {
        topLabelLHS += "; seq=" + seq_info;
    }
    String egm_info = picker.getEGMInfo();
    if (egm_info != null) {
        topLabelLHS += "; EGM=" + egm_info;
    }
    updateTopLabel();
    pieceInfoCanvas.redraw();
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker) DiskManager(com.biglybt.core.disk.DiskManager) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) LogEvent(com.biglybt.core.logging.LogEvent) DMPieceList(com.biglybt.core.disk.impl.piecemapper.DMPieceList) PEPiece(com.biglybt.core.peer.PEPiece) DMPieceMapEntry(com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry) PEPeerManager(com.biglybt.core.peer.PEPeerManager)

Example 60 with PEPeerManager

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

the class TransferStatsView method refreshConnectionPanel.

private void refreshConnectionPanel() {
    if (global_manager == null) {
        return;
    }
    int total_connections = 0;
    int total_con_queued = 0;
    int total_con_blocked = 0;
    int total_con_unchoked = 0;
    int total_data_queued = 0;
    int total_in = 0;
    List<DownloadManager> dms = global_manager.getDownloadManagers();
    for (DownloadManager dm : dms) {
        PEPeerManager pm = dm.getPeerManager();
        if (pm != null) {
            total_data_queued += pm.getBytesQueuedForUpload();
            total_connections += pm.getNbPeers() + pm.getNbSeeds();
            total_con_queued += pm.getNbPeersWithUploadQueued();
            total_con_blocked += pm.getNbPeersWithUploadBlocked();
            total_con_unchoked += pm.getNbPeersUnchoked();
            total_in += pm.getNbRemoteTCPConnections() + pm.getNbRemoteUDPConnections() + pm.getNbRemoteUTPConnections();
        }
    }
    connection_label.setText(MessageText.getString("SpeedView.stats.con_details", new String[] { String.valueOf(total_connections) + "[" + MessageText.getString("label.in").toLowerCase() + ":" + total_in + "]", String.valueOf(total_con_unchoked), String.valueOf(total_con_queued), String.valueOf(total_con_blocked) }));
    connection_graphic.addIntsValue(new int[] { total_connections, total_con_unchoked, total_con_queued, total_con_blocked });
    upload_label.setText(MessageText.getString("SpeedView.stats.upload_details", new String[] { DisplayFormatters.formatByteCountToKiBEtc(total_data_queued) }));
    upload_graphic.addIntValue(total_data_queued);
    upload_graphic.refresh(false);
    connection_graphic.refresh(false);
    if (con_folder.getSelectionIndex() == 1) {
        long now = SystemTime.getMonotonousTime();
        if (now - last_route_update >= 2 * 1000) {
            last_route_update = now;
            NetworkAdmin na = NetworkAdmin.getSingleton();
            Map<InetAddress, String> ip_to_name_map = new HashMap<>();
            Map<String, RouteInfo> name_to_route_map = new HashMap<>();
            RouteInfo udp_info = null;
            RouteInfo unknown_info = null;
            List<PRUDPPacketHandler> udp_handlers = PRUDPPacketHandlerFactory.getHandlers();
            InetAddress udp_bind_ip = null;
            for (PRUDPPacketHandler handler : udp_handlers) {
                if (handler.hasPrimordialHandler()) {
                    udp_bind_ip = handler.getBindIP();
                    if (udp_bind_ip != null) {
                        if (udp_bind_ip.isAnyLocalAddress()) {
                            udp_bind_ip = null;
                        }
                    }
                }
            }
            for (DownloadManager dm : dms) {
                PEPeerManager pm = dm.getPeerManager();
                if (pm != null) {
                    List<PEPeer> peers = pm.getPeers();
                    for (PEPeer p : peers) {
                        NetworkConnection nc = PluginCoreUtils.unwrap(p.getPluginConnection());
                        boolean done = false;
                        if (nc != null) {
                            Transport transport = nc.getTransport();
                            if (transport != null) {
                                InetSocketAddress notional_address = nc.getEndpoint().getNotionalAddress();
                                String ip = AddressUtils.getHostAddress(notional_address);
                                String network = AENetworkClassifier.categoriseAddress(ip);
                                if (network == AENetworkClassifier.AT_PUBLIC) {
                                    if (transport.isTCP()) {
                                        TransportStartpoint start = transport.getTransportStartpoint();
                                        if (start != null) {
                                            InetSocketAddress socket_address = start.getProtocolStartpoint().getAddress();
                                            if (socket_address != null) {
                                                InetAddress address = socket_address.getAddress();
                                                String name;
                                                if (address.isAnyLocalAddress()) {
                                                    name = "* (TCP)";
                                                } else {
                                                    name = ip_to_name_map.get(address);
                                                }
                                                if (name == null) {
                                                    name = na.classifyRoute(address);
                                                    ip_to_name_map.put(address, name);
                                                }
                                                if (transport.isSOCKS()) {
                                                    name += " (SOCKS)";
                                                }
                                                RouteInfo info = name_to_route_map.get(name);
                                                if (info == null) {
                                                    info = new RouteInfo(name);
                                                    name_to_route_map.put(name, info);
                                                    route_last_seen.put(name, now);
                                                }
                                                info.update(p);
                                                done = true;
                                            }
                                        }
                                    } else {
                                        if (udp_bind_ip != null) {
                                            RouteInfo info;
                                            String name = ip_to_name_map.get(udp_bind_ip);
                                            if (name == null) {
                                                name = na.classifyRoute(udp_bind_ip);
                                                ip_to_name_map.put(udp_bind_ip, name);
                                                info = name_to_route_map.get(name);
                                                route_last_seen.put(name, now);
                                                if (info == null) {
                                                    info = new RouteInfo(name);
                                                    name_to_route_map.put(name, info);
                                                }
                                            } else {
                                                info = name_to_route_map.get(name);
                                            }
                                            info.update(p);
                                            done = true;
                                        } else {
                                            if (udp_info == null) {
                                                udp_info = new RouteInfo("* (UDP)");
                                                name_to_route_map.put(udp_info.getName(), udp_info);
                                                route_last_seen.put(udp_info.getName(), now);
                                            }
                                            udp_info.update(p);
                                            done = true;
                                        }
                                    }
                                } else {
                                    RouteInfo info = name_to_route_map.get(network);
                                    if (info == null) {
                                        info = new RouteInfo(network);
                                        name_to_route_map.put(network, info);
                                        route_last_seen.put(network, now);
                                    }
                                    info.update(p);
                                    done = true;
                                }
                            }
                        }
                        if (!done) {
                            if (unknown_info == null) {
                                unknown_info = new RouteInfo("Pending");
                                name_to_route_map.put(unknown_info.getName(), unknown_info);
                                route_last_seen.put(unknown_info.getName(), now);
                            }
                            unknown_info.update(p);
                        }
                    }
                }
            }
            List<RouteInfo> rows = new ArrayList<>();
            Iterator<Map.Entry<String, Long>> it = route_last_seen.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry<String, Long> entry = it.next();
                long when = entry.getValue();
                if (now - when > 60 * 1000) {
                    it.remove();
                } else if (when != now) {
                    rows.add(new RouteInfo(entry.getKey()));
                }
            }
            rows.addAll(name_to_route_map.values());
            Collections.sort(rows, new Comparator<RouteInfo>() {

                @Override
                public int compare(RouteInfo o1, RouteInfo o2) {
                    String n1 = o1.getName();
                    String n2 = o2.getName();
                    // wildcard and pending to the bottom
                    if (n1.startsWith("*") || n1.equals("Pending")) {
                        n1 = "zzzz" + n1;
                    }
                    if (n2.startsWith("*") || n2.equals("Pending")) {
                        n2 = "zzzz" + n2;
                    }
                    return (n1.compareTo(n2));
                }
            });
            buildRouteComponent(rows.size());
            for (int i = 0; i < rows.size(); i++) {
                RouteInfo info = rows.get(i);
                route_labels[i][0].setText(info.getName());
                route_labels[i][1].setText(info.getIncomingString());
                route_labels[i][2].setText(info.getOutgoingString());
            }
            buildRouteComponent(rows.size());
        }
    }
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) DownloadManager(com.biglybt.core.download.DownloadManager) NetworkAdmin(com.biglybt.core.networkmanager.admin.NetworkAdmin) TransportStartpoint(com.biglybt.core.networkmanager.TransportStartpoint) NetworkConnection(com.biglybt.core.networkmanager.NetworkConnection) Point(org.eclipse.swt.graphics.Point) TransportStartpoint(com.biglybt.core.networkmanager.TransportStartpoint) PRUDPPacketHandler(com.biglybt.net.udp.uc.PRUDPPacketHandler) PEPeerManager(com.biglybt.core.peer.PEPeerManager) Transport(com.biglybt.core.networkmanager.Transport) InetAddress(java.net.InetAddress) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

PEPeerManager (com.biglybt.core.peer.PEPeerManager)60 DownloadManager (com.biglybt.core.download.DownloadManager)29 PEPeer (com.biglybt.core.peer.PEPeer)18 DiskManager (com.biglybt.core.disk.DiskManager)13 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)9 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)8 PEPiece (com.biglybt.core.peer.PEPiece)8 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)7 List (java.util.List)7 LogEvent (com.biglybt.core.logging.LogEvent)6 Download (com.biglybt.pif.download.Download)6 File (java.io.File)6 ArrayList (java.util.ArrayList)6 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)5 TOTorrent (com.biglybt.core.torrent.TOTorrent)5 URL (java.net.URL)5 Map (java.util.Map)5 GlobalManager (com.biglybt.core.global.GlobalManager)4 Tag (com.biglybt.core.tag.Tag)4 AERunnable (com.biglybt.core.util.AERunnable)4