Search in sources :

Example 1 with PiecePicker

use of com.biglybt.core.peermanager.piecepicker.PiecePicker in project BiglyBT by BiglySoftware.

the class PieceInfoView method createPeerInfoPanel.

private void createPeerInfoPanel(Composite parent) {
    GridLayout layout;
    GridData gridData;
    // Peer Info section contains
    // - Peer's Block display
    // - Peer's Datarate
    pieceInfoComposite = new Composite(parent, SWT.NONE);
    layout = new GridLayout();
    layout.numColumns = 2;
    layout.horizontalSpacing = 0;
    layout.verticalSpacing = 0;
    layout.marginHeight = 0;
    layout.marginWidth = 0;
    pieceInfoComposite.setLayout(layout);
    gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
    pieceInfoComposite.setLayoutData(gridData);
    imageLabel = new Label(pieceInfoComposite, SWT.NULL);
    gridData = new GridData();
    imageLabel.setLayoutData(gridData);
    topLabel = new Label(pieceInfoComposite, SWT.NULL);
    topLabel.setBackground(Colors.white);
    gridData = new GridData(SWT.FILL, SWT.DEFAULT, false, false);
    topLabel.setLayoutData(gridData);
    sc = new ScrolledComposite(pieceInfoComposite, 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);
    sc.getVerticalBar().setIncrement(BLOCK_SIZE);
    pieceInfoCanvas = new Canvas(sc, SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
    gridData = new GridData(GridData.FILL, SWT.DEFAULT, true, false);
    pieceInfoCanvas.setLayoutData(gridData);
    pieceInfoCanvas.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 || dlm == null || dlm.getPeerManager() == 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) {
            }
        }
    });
    pieceInfoCanvas.addListener(SWT.KeyDown, new DoNothingListener());
    pieceInfoCanvas.addListener(SWT.Resize, new Listener() {

        @Override
        public void handleEvent(Event e) {
            synchronized (PieceInfoView.this) {
                if (alreadyFilling) {
                    return;
                }
                alreadyFilling = 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 = pieceInfoCanvas.getClientArea().width / BLOCK_SIZE;
                        if (iOldColCount != iNewColCount)
                            refreshInfoCanvas();
                    }
                    synchronized (PieceInfoView.this) {
                        alreadyFilling = false;
                    }
                }
            });
        }
    });
    sc.setContent(pieceInfoCanvas);
    pieceInfoCanvas.addMouseMoveListener(new MouseMoveListener() {

        @Override
        public void mouseMove(MouseEvent event) {
            int piece_number = getPieceNumber(event.x, event.y);
            if (piece_number != selectedPiece) {
                selectedPieceShowFilePending = -1;
            }
        }
    });
    pieceInfoCanvas.addMouseTrackListener(new MouseTrackAdapter() {

        @Override
        public void mouseHover(MouseEvent event) {
            int piece_number = getPieceNumber(event.x, event.y);
            if (piece_number >= 0) {
                selectedPiece = piece_number;
                selectedPieceShowFilePending = piece_number;
                SimpleTimer.addEvent("ShowFile", SystemTime.getOffsetTime(1000), new TimerEventPerformer() {

                    @Override
                    public void perform(TimerEvent event) {
                        Utils.execSWTThread(new Runnable() {

                            @Override
                            public void run() {
                                if (selectedPieceShowFilePending == piece_number) {
                                    selectedPieceShowFile = true;
                                    refreshInfoCanvas();
                                }
                            }
                        });
                    }
                });
                refreshInfoCanvas();
                DiskManager disk_manager = dlm.getDiskManager();
                PEPeerManager pm = dlm.getPeerManager();
                DiskManagerPiece dm_piece = disk_manager.getPiece(piece_number);
                PEPiece pm_piece = pm.getPiece(piece_number);
                String text = "Piece " + piece_number + ": " + dm_piece.getString();
                if (pm_piece != null) {
                    text += ", active: " + pm_piece.getString();
                } else {
                    if (dm_piece.isNeeded() && !dm_piece.isDone()) {
                        text += ", inactive: " + pm.getPiecePicker().getPieceString(piece_number);
                    }
                }
                text += " - ";
                DMPieceList l = disk_manager.getPieceList(piece_number);
                for (int i = 0; i < l.size(); i++) {
                    DMPieceMapEntry entry = l.get(i);
                    DiskManagerFileInfo info = entry.getFile();
                    text += (i == 0 ? "" : "; ") + info.getFile(true).getName();
                }
                topLabelRHS = text;
            } else {
                topLabelRHS = "";
            }
            updateTopLabel();
        }

        @Override
        public void mouseExit(MouseEvent e) {
            selectedPiece = -1;
            selectedPieceShowFile = false;
            refreshInfoCanvas();
        }
    });
    final Menu menu = new Menu(pieceInfoCanvas.getShell(), SWT.POP_UP);
    pieceInfoCanvas.setMenu(menu);
    pieceInfoCanvas.addListener(SWT.MenuDetect, new Listener() {

        @Override
        public void handleEvent(Event event) {
            Point pt = pieceInfoCanvas.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 = dlm;
                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 pm_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 SelectionListenerForcePiece(picker, piece_number, force_piece));
                }
                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 SelectionListenerResetPiece(dm_piece, pm_piece));
                new MenuItem(menu, SWT.SEPARATOR);
                final MenuItem seq_asc = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(seq_asc, "label.seq.asc.from", new String[] { String.valueOf(piece_number) });
                seq_asc.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
                        picker.setReverseBlockOrder(false);
                        picker.setSequentialAscendingFrom(piece_number);
                    }
                });
                final MenuItem seq_desc = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(seq_desc, "label.seq.desc.from", new String[] { String.valueOf(piece_number) });
                seq_desc.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
                        picker.setReverseBlockOrder(true);
                        picker.setSequentialDescendingFrom(piece_number);
                    }
                });
                final MenuItem seq_clear = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(seq_clear, "label.seq.clear", new String[] { String.valueOf(piece_number) });
                seq_clear.addSelectionListener(new SelectionAdapter() {

                    @Override
                    public void widgetSelected(SelectionEvent e) {
                        download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
                        picker.setReverseBlockOrder(false);
                        picker.clearSequential();
                    }
                });
            }
        }
    });
    Legend.createLegendComposite(pieceInfoComposite, blockColors, new String[] { "PiecesView.BlockView.Have", "PiecesView.BlockView.NoHave", "PeersView.BlockView.Transfer", "PeersView.BlockView.NextRequest", "PeersView.BlockView.AvailCount", "PeersView.BlockView.ShowFile" }, new GridData(SWT.FILL, SWT.DEFAULT, true, false, 2, 1));
    int iFontPixelsHeight = 10;
    int iFontPointHeight = (iFontPixelsHeight * 72) / Utils.getDPIRaw(pieceInfoCanvas.getDisplay()).y;
    Font f = pieceInfoCanvas.getFont();
    FontData[] fontData = f.getFontData();
    fontData[0].setHeight(iFontPointHeight);
    font = new Font(pieceInfoCanvas.getDisplay(), fontData);
}
Also used : AERunnable(com.biglybt.core.util.AERunnable) DownloadManagerPieceListener(com.biglybt.core.download.DownloadManagerPieceListener) 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) TimerEventPerformer(com.biglybt.core.util.TimerEventPerformer) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) TimerEvent(com.biglybt.core.util.TimerEvent) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) ScrolledComposite(org.eclipse.swt.custom.ScrolledComposite) DMPieceList(com.biglybt.core.disk.impl.piecemapper.DMPieceList) PEPiece(com.biglybt.core.peer.PEPiece) AERunnable(com.biglybt.core.util.AERunnable) GridData(org.eclipse.swt.layout.GridData) DMPieceMapEntry(com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry) UISWTViewEvent(com.biglybt.ui.swt.pif.UISWTViewEvent) LogEvent(com.biglybt.core.logging.LogEvent) TimerEvent(com.biglybt.core.util.TimerEvent) PEPeerManager(com.biglybt.core.peer.PEPeerManager) MenuBuildUtils(com.biglybt.ui.swt.MenuBuildUtils)

Example 2 with PiecePicker

use of com.biglybt.core.peermanager.piecepicker.PiecePicker in project BiglyBT by BiglySoftware.

the class EnhancedDownloadManager method updateProgressiveStats.

protected boolean updateProgressiveStats(int tick_count) {
    if (!progressive_active) {
        return (false);
    }
    synchronized (this) {
        if (!progressive_active || progressive_stats == null) {
            return (false);
        }
        if (tick_count % REACTIVATE_PROVIDER_PERIOD_TICKS == 0) {
            PiecePicker piece_picker = current_piece_pickler;
            if (piece_picker != null) {
                buffer_provider.checkActivation(piece_picker);
            }
        }
        progressive_stats.update(tick_count);
        long current_max = progressive_stats.getStreamBytesPerSecondMax();
        if (RealTimeInfo.getProgressiveActiveBytesPerSec() != current_max) {
            RealTimeInfo.setProgressiveActive(current_max);
        }
    }
    return (true);
}
Also used : PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker)

Example 3 with PiecePicker

use of com.biglybt.core.peermanager.piecepicker.PiecePicker in project BiglyBT by BiglySoftware.

the class DiskManagerRandomReadController method executeRequest.

private void executeRequest() {
    DiskManagerRandomReadRequestImpl request;
    synchronized (requests) {
        if (requests.isEmpty()) {
            return;
        }
        request = requests.remove(0);
    }
    if (request.isCancelled()) {
        return;
    }
    DiskManagerFileInfoListener info_listener = null;
    com.biglybt.core.disk.DiskManagerFileInfo core_file = request.getFile().getCore();
    DownloadManager core_download = core_file.getDownloadManager();
    int prev_hint_piece = -1;
    int curr_hint_piece = -1;
    try {
        if (core_download.getTorrent() == null) {
            throw (new DownloadException("Torrent invalid"));
        }
        if (core_download.isDestroyed()) {
            Debug.out("Download has been removed");
            throw (new DownloadException("Download has been removed"));
        }
        TOTorrentFile tf = core_file.getTorrentFile();
        TOTorrent torrent = tf.getTorrent();
        TOTorrentFile[] tfs = torrent.getFiles();
        long core_file_start_byte = 0;
        for (int i = 0; i < core_file.getIndex(); i++) {
            core_file_start_byte += tfs[i].getLength();
        }
        long download_byte_start = core_file_start_byte + request.getOffset();
        long download_byte_end = download_byte_start + request.getLength();
        int piece_size = (int) tf.getTorrent().getPieceLength();
        if (core_file.getDownloaded() != core_file.getLength()) {
            if (core_file.isSkipped()) {
                core_file.setSkipped(false);
            }
            boolean force_start = download.isForceStart();
            if (!force_start) {
                download.setForceStart(true);
                set_force_start = true;
                final AESemaphore running_sem = new AESemaphore("rs");
                DownloadListener dl_listener = new DownloadListener() {

                    @Override
                    public void stateChanged(Download download, int old_state, int new_state) {
                        if (new_state == Download.ST_DOWNLOADING || new_state == Download.ST_SEEDING) {
                            running_sem.release();
                        }
                    }

                    @Override
                    public void positionChanged(Download download, int oldPosition, int newPosition) {
                    }
                };
                download.addListener(dl_listener);
                try {
                    if (download.getState() != Download.ST_DOWNLOADING && download.getState() != Download.ST_SEEDING) {
                        if (!running_sem.reserve(10 * 1000)) {
                            throw (new DownloadException("timeout waiting for download to start"));
                        }
                    }
                } finally {
                    download.removeListener(dl_listener);
                }
            }
        }
        boolean is_reverse = request.isReverse();
        final AESemaphore wait_sem = new AESemaphore("rr:waiter");
        info_listener = new DiskManagerFileInfoListener() {

            @Override
            public void dataWritten(long offset, long length) {
                wait_sem.release();
            }

            @Override
            public void dataChecked(long offset, long length) {
            }
        };
        long start_time = SystemTime.getMonotonousTime();
        boolean has_started = false;
        core_file.addListener(info_listener);
        while (download_byte_start < download_byte_end) {
            if (request.isCancelled()) {
                throw (new Exception("request cancelled"));
            }
            // System.out.println( "Request current: " + download_byte_start + " -> " + download_byte_end );
            long now = SystemTime.getMonotonousTime();
            int piece_start = (int) (download_byte_start / piece_size);
            int piece_start_offset = (int) (download_byte_start % piece_size);
            int piece_end = (int) ((download_byte_end - 1) / piece_size);
            int piece_end_offset = (int) ((download_byte_end - 1) % piece_size) + 1;
            // System.out.println( "    piece details: " + piece_start + "/" + piece_start_offset + " -> " + piece_end + "/" + piece_end_offset );
            DiskManagerPiece[] pieces = null;
            DiskManager disk_manager = core_download.getDiskManager();
            if (disk_manager != null) {
                pieces = disk_manager.getPieces();
            }
            long avail_start;
            long avail_end;
            if (pieces == null) {
                if (core_file.getDownloaded() == core_file.getLength()) {
                    avail_start = download_byte_start;
                    avail_end = download_byte_end;
                } else {
                    if (now - start_time < 10000 && !has_started) {
                        wait_sem.reserve(250);
                        continue;
                    }
                    throw (new Exception("download stopped"));
                }
            } else {
                has_started = true;
                if (is_reverse) {
                    long min_done = download_byte_end;
                    for (int i = piece_end; i >= piece_start; i--) {
                        int p_start = i == piece_start ? piece_start_offset : 0;
                        int p_end = i == piece_end ? piece_end_offset : piece_size;
                        DiskManagerPiece piece = pieces[i];
                        boolean[] done = piece.getWritten();
                        if (done == null) {
                            if (piece.isDone()) {
                                min_done = i * (long) piece_size;
                                continue;
                            } else {
                                break;
                            }
                        }
                        int block_size = piece.getBlockSize(0);
                        int first_block = p_start / block_size;
                        int last_block = (p_end - 1) / block_size;
                        for (int j = last_block; j >= first_block; j--) {
                            if (done[j]) {
                                min_done = i * (long) piece_size + j * block_size;
                            } else {
                                break;
                            }
                        }
                    }
                    avail_start = Math.max(download_byte_start, min_done);
                    avail_end = download_byte_end;
                } else {
                    long max_done = download_byte_start;
                    for (int i = piece_start; i <= piece_end; i++) {
                        int p_start = i == piece_start ? piece_start_offset : 0;
                        int p_end = i == piece_end ? piece_end_offset : piece_size;
                        DiskManagerPiece piece = pieces[i];
                        boolean[] done = piece.getWritten();
                        if (done == null) {
                            if (piece.isDone()) {
                                max_done = (i + 1) * (long) piece_size;
                                continue;
                            } else {
                                break;
                            }
                        }
                        int block_size = piece.getBlockSize(0);
                        int first_block = p_start / block_size;
                        int last_block = (p_end - 1) / block_size;
                        for (int j = first_block; j <= last_block; j++) {
                            if (done[j]) {
                                max_done = i * (long) piece_size + (j + 1) * block_size;
                            } else {
                                break;
                            }
                        }
                    }
                    avail_start = download_byte_start;
                    avail_end = Math.min(download_byte_end, max_done);
                }
            }
            // System.out.println( "    avail: " + avail_start + " -> " + avail_end );
            int max_chunk = 128 * 1024;
            if (avail_end > avail_start) {
                long length = avail_end - avail_start;
                if (length > max_chunk) {
                    if (is_reverse) {
                        avail_start = avail_end - max_chunk;
                    } else {
                        avail_end = avail_start + max_chunk;
                    }
                }
                // System.out.println( "got data: " + avail_start + " -> " + avail_end );
                long read_offset = avail_start - core_file_start_byte;
                int read_length = (int) (avail_end - avail_start);
                DirectByteBuffer buffer = core_file.read(read_offset, read_length);
                request.dataAvailable(buffer, read_offset, read_length);
                if (is_reverse) {
                    download_byte_end = avail_start;
                } else {
                    download_byte_start = avail_end;
                }
                continue;
            }
            PEPeerManager pm = core_download.getPeerManager();
            if (pm == null) {
                if (now - start_time < 10000 && !has_started) {
                    wait_sem.reserve(250);
                    continue;
                }
                throw (new Exception("download stopped"));
            } else {
                has_started = true;
            }
            PiecePicker picker = pm.getPiecePicker();
            picker.setReverseBlockOrder(is_reverse);
            int hint_piece;
            int hint_offset;
            int hint_length;
            if (piece_start == piece_end) {
                hint_piece = piece_start;
                hint_offset = piece_start_offset;
                hint_length = piece_end_offset - piece_start_offset;
            } else {
                if (is_reverse) {
                    hint_piece = piece_end;
                    hint_offset = 0;
                    hint_length = piece_end_offset;
                } else {
                    hint_piece = piece_start;
                    hint_offset = piece_start_offset;
                    hint_length = piece_size - piece_start_offset;
                }
            }
            if (curr_hint_piece == -1) {
                int[] existing = picker.getGlobalRequestHint();
                if (existing != null) {
                    curr_hint_piece = existing[0];
                }
            }
            // System.out.println( "hint: " + hint_piece + "/" + hint_offset + "/" + hint_length + ": curr=" + curr_hint_piece + ", prev=" + prev_hint_piece );
            picker.setGlobalRequestHint(hint_piece, hint_offset, hint_length);
            if (hint_piece != curr_hint_piece) {
                prev_hint_piece = curr_hint_piece;
                curr_hint_piece = hint_piece;
            }
            if (prev_hint_piece != -1) {
                clearHint(pm, prev_hint_piece);
            }
            wait_sem.reserve(250);
        }
    } catch (Throwable e) {
        request.failed(e);
    } finally {
        PEPeerManager pm = core_download.getPeerManager();
        if (pm != null) {
            PiecePicker picker = pm.getPiecePicker();
            if (picker != null) {
                picker.setReverseBlockOrder(false);
                picker.setGlobalRequestHint(-1, 0, 0);
                if (curr_hint_piece != -1) {
                    clearHint(pm, curr_hint_piece);
                }
            }
        }
        if (info_listener != null) {
            core_file.removeListener(info_listener);
        }
    }
}
Also used : PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker) DiskManager(com.biglybt.core.disk.DiskManager) DownloadManager(com.biglybt.core.download.DownloadManager) DiskManagerFileInfoListener(com.biglybt.core.disk.DiskManagerFileInfoListener) DownloadException(com.biglybt.pif.download.DownloadException) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) Download(com.biglybt.pif.download.Download) DownloadException(com.biglybt.pif.download.DownloadException) TOTorrentFile(com.biglybt.core.torrent.TOTorrentFile) DownloadListener(com.biglybt.pif.download.DownloadListener) TOTorrent(com.biglybt.core.torrent.TOTorrent) PEPeerManager(com.biglybt.core.peer.PEPeerManager)

Example 4 with PiecePicker

use of com.biglybt.core.peermanager.piecepicker.PiecePicker in project BiglyBT by BiglySoftware.

the class ExternalSeedReaderGetRight method setActiveSupport.

@Override
protected void setActiveSupport(PeerManager peer_manager, boolean active) {
    if (linear_download) {
        if (peer_manager != null) {
            PiecePicker picker = PluginCoreUtils.unwrap(peer_manager).getPiecePicker();
            if (active) {
                piece_priorities = new long[peer_manager.getPieces().length];
                for (int i = 0; i < piece_priorities.length; i++) {
                    piece_priorities[i] = 10 * 1000 + i;
                }
                picker.addPriorityProvider(this);
            } else {
                piece_priorities = null;
                picker.removePriorityProvider(this);
            }
        }
        if (!active) {
            boolean downloaders_set;
            synchronized (this) {
                downloaders_set = http_downloaders != null;
            }
            if (downloaders_set) {
                for (ExternalSeedHTTPDownloader d : http_downloaders) {
                    d.deactivate();
                }
            }
        }
    }
}
Also used : ExternalSeedHTTPDownloader(com.biglybt.plugin.extseed.util.ExternalSeedHTTPDownloader) PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker)

Example 5 with PiecePicker

use of com.biglybt.core.peermanager.piecepicker.PiecePicker in project BiglyBT by BiglySoftware.

the class PieceDistributionView method updateDistribution.

private final void updateDistribution() {
    if (!initialized || pem == null || comp == null || pem.getPiecePicker() == null || pem.getDiskManager() == null || !comp.isVisible())
        return;
    Rectangle rect = pieceDistCanvas.getBounds();
    if (rect.height <= 0 || rect.width <= 0)
        return;
    PiecePicker picker = pem.getPiecePicker();
    final int seeds = pem.getNbSeeds() + (pem.isSeeding() ? 1 : 0);
    final int connected = pem.getNbPeers() + seeds + (pem.isSeeding() ? 0 : 1);
    final int upperBound = 1 + (1 << (int) Math.ceil(Math.log(connected + 0.0) / Math.log(2.0)));
    // System.out.println("conn:"+connected+" bound:"+upperBound);
    final int minAvail = (int) picker.getMinAvailability();
    // final int maxAvail = picker.getMaxAvailability();
    final int nbPieces = picker.getNumberOfPieces();
    final int[] availabilties = picker.getAvailability();
    final DiskManagerPiece[] dmPieces = pem.getDiskManager().getPieces();
    final PEPiece[] pePieces = pem.getPieces();
    final int[] globalPiecesPerAvailability = new int[upperBound];
    final int[] datasourcePiecesPerAvailability = new int[upperBound];
    // me-only stuff
    final boolean[] downloading = new boolean[upperBound];
    int avlPeak = 0;
    for (int i = 0; i < nbPieces; i++) {
        if (availabilties[i] >= upperBound)
            // availability and peer lists are OOS, just wait for the next round
            return;
        final int newPeak;
        if (avlPeak < (newPeak = ++globalPiecesPerAvailability[availabilties[i]])) {
            avlPeak = newPeak;
        // avlPeakIdx = availabilties[i];
        }
        if ((isMe && dmPieces[i].isDone()) || (!isMe && hasPieces != null && hasPieces[i]))
            ++datasourcePiecesPerAvailability[availabilties[i]];
        if (isMe && pePieces[i] != null)
            downloading[availabilties[i]] = true;
    }
    Image img = new Image(comp.getDisplay(), pieceDistCanvas.getBounds());
    GC gc = new GC(img);
    try {
        int stepWidthX = rect.width / upperBound;
        int barGap = 1;
        int barWidth = stepWidthX - barGap - 1;
        int barFillingWidth = barWidth - 1;
        double stepWidthY = 1.0 * (rect.height - 1) / avlPeak;
        int offsetY = rect.height;
        gc.setForeground(Colors.green);
        for (int i = 0; i <= connected; i++) {
            Color curColor;
            if (i == 0)
                curColor = Colors.colorError;
            else if (i <= seeds)
                curColor = Colors.green;
            else
                curColor = Colors.blues[Colors.BLUES_DARKEST];
            gc.setBackground(curColor);
            gc.setForeground(curColor);
            if (globalPiecesPerAvailability[i] == 0) {
                gc.setLineWidth(2);
                gc.drawLine(stepWidthX * i, offsetY - 1, stepWidthX * (i + 1) - barGap, offsetY - 1);
            } else {
                gc.setLineWidth(1);
                if (downloading[i])
                    gc.setLineStyle(SWT.LINE_DASH);
                gc.fillRectangle(stepWidthX * i + 1, offsetY - 1, barFillingWidth, (int) (Math.ceil(stepWidthY * datasourcePiecesPerAvailability[i] - 1) * -1));
                gc.drawRectangle(stepWidthX * i, offsetY, barWidth, (int) (Math.ceil(stepWidthY * globalPiecesPerAvailability[i]) + 1) * -1);
            }
            if (i == minAvail) {
                gc.setForeground(Colors.blue);
                gc.drawRectangle(stepWidthX * i + 1, offsetY - 1, barWidth - 2, (int) (Math.ceil(stepWidthY * globalPiecesPerAvailability[i] - 1)) * -1);
            }
            gc.setLineStyle(SWT.LINE_SOLID);
        }
        gc.setLineWidth(1);
        String[] boxContent = new String[] { MessageText.getString("PiecesView.DistributionView.NoAvl"), MessageText.getString("PiecesView.DistributionView.SeedAvl"), MessageText.getString("PiecesView.DistributionView.PeerAvl"), MessageText.getString("PiecesView.DistributionView.RarestAvl", new String[] { globalPiecesPerAvailability[minAvail] + "", minAvail + "" }), MessageText.getString("PiecesView.DistributionView." + (isMe ? "weHave" : "theyHave")), MessageText.getString("PiecesView.DistributionView.weDownload") };
        int charWidth = gc.getFontMetrics().getAverageCharWidth();
        int charHeight = gc.getFontMetrics().getHeight();
        int maxBoxOffsetY = charHeight + 2;
        int maxBoxWidth = 0;
        int maxBoxOffsetX = 0;
        for (int i = 0; i < boxContent.length; i++) maxBoxWidth = Math.max(maxBoxWidth, boxContent[i].length());
        maxBoxOffsetX = (maxBoxWidth + 5) * charWidth;
        maxBoxWidth = ++maxBoxWidth * charWidth;
        int boxNum = 1;
        gc.setForeground(Colors.colorError);
        gc.setBackground(Colors.background);
        gc.drawRectangle(rect.width + (maxBoxOffsetX) * -1, maxBoxOffsetY * boxNum, maxBoxWidth, charHeight);
        gc.drawString(boxContent[boxNum - 1], rect.width + (maxBoxOffsetX - 5) * -1, maxBoxOffsetY * boxNum, true);
        boxNum++;
        gc.setForeground(Colors.green);
        gc.setBackground(Colors.background);
        gc.drawRectangle(rect.width + (maxBoxOffsetX) * -1, maxBoxOffsetY * boxNum, maxBoxWidth, charHeight);
        gc.drawString(boxContent[boxNum - 1], rect.width + (maxBoxOffsetX - 5) * -1, maxBoxOffsetY * boxNum, true);
        boxNum++;
        gc.setForeground(Colors.blues[Colors.BLUES_DARKEST]);
        gc.drawRectangle(rect.width + (maxBoxOffsetX) * -1, maxBoxOffsetY * boxNum, maxBoxWidth, charHeight);
        gc.drawString(boxContent[boxNum - 1], rect.width + (maxBoxOffsetX - 5) * -1, maxBoxOffsetY * boxNum, true);
        boxNum++;
        gc.setForeground(Colors.blue);
        gc.drawRectangle(rect.width + (maxBoxOffsetX) * -1, maxBoxOffsetY * boxNum, maxBoxWidth, charHeight);
        gc.drawString(boxContent[boxNum - 1], rect.width + (maxBoxOffsetX - 5) * -1, maxBoxOffsetY * boxNum, true);
        boxNum++;
        gc.setForeground(Colors.black);
        gc.setBackground(Colors.black);
        gc.drawRectangle(rect.width + (maxBoxOffsetX) * -1, maxBoxOffsetY * boxNum, maxBoxWidth, charHeight);
        gc.fillRectangle(rect.width + (maxBoxOffsetX) * -1, maxBoxOffsetY * boxNum, maxBoxWidth / 2, charHeight);
        gc.setForeground(Colors.grey);
        gc.setBackground(Colors.background);
        gc.drawString(boxContent[boxNum - 1], rect.width + (maxBoxOffsetX - 5) * -1, maxBoxOffsetY * boxNum, true);
        if (isMe) {
            boxNum++;
            gc.setForeground(Colors.black);
            gc.setLineStyle(SWT.LINE_DASH);
            gc.drawRectangle(rect.width + (maxBoxOffsetX) * -1, maxBoxOffsetY * boxNum, maxBoxWidth, charHeight);
            gc.drawString(boxContent[boxNum - 1], rect.width + (maxBoxOffsetX - 5) * -1, maxBoxOffsetY * boxNum, true);
        }
        gc.setLineStyle(SWT.LINE_SOLID);
    } finally {
        gc.dispose();
    }
    if (imgToPaint != null) {
        imgToPaint.dispose();
    }
    imgToPaint = img;
    pieceDistCanvas.redraw();
}
Also used : PiecePicker(com.biglybt.core.peermanager.piecepicker.PiecePicker) Color(org.eclipse.swt.graphics.Color) Rectangle(org.eclipse.swt.graphics.Rectangle) Image(org.eclipse.swt.graphics.Image) PEPiece(com.biglybt.core.peer.PEPiece) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) GC(org.eclipse.swt.graphics.GC)

Aggregations

PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)11 PEPeerManager (com.biglybt.core.peer.PEPeerManager)7 PEPiece (com.biglybt.core.peer.PEPiece)7 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)5 DiskManager (com.biglybt.core.disk.DiskManager)4 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)4 DownloadManager (com.biglybt.core.download.DownloadManager)4 LogEvent (com.biglybt.core.logging.LogEvent)3 AERunnable (com.biglybt.core.util.AERunnable)3 MenuBuildUtils (com.biglybt.ui.swt.MenuBuildUtils)3 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)2 DMPieceMapEntry (com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry)2 PEPeer (com.biglybt.core.peer.PEPeer)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)2 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)2 ScrolledComposite (org.eclipse.swt.custom.ScrolledComposite)2 Color (org.eclipse.swt.graphics.Color)2 GC (org.eclipse.swt.graphics.GC)2 Image (org.eclipse.swt.graphics.Image)2 GridData (org.eclipse.swt.layout.GridData)2