Search in sources :

Example 26 with DiskManager

use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.

the class GeneralView method updatePiecesInfo.

private void updatePiecesInfo(boolean bForce) {
    if (manager == null)
        return;
    try {
        this_mon.enter();
        if (display == null || display.isDisposed())
            return;
        if (piecesImage == null || piecesImage.isDisposed())
            return;
        if (piecesImageRefreshNeeded) {
            bForce = true;
            piecesImageRefreshNeeded = false;
        }
        DiskManager dm = manager.getDiskManager();
        int nbPieces = manager.getNbPieces();
        boolean valid;
        int[] oldPiecesState = piecesStateCache;
        if (oldPiecesState == null || oldPiecesState.length != nbPieces) {
            valid = false;
        } else {
            valid = !bForce;
        }
        int[] newPiecesState = new int[nbPieces];
        final int PS_NONE = 0x00000000;
        final int PS_DONE = 0x00000001;
        final int PS_SKIPPED = 0x00000002;
        final int PS_FILE_BOUNDARY = 0x00000004;
        if (dm != null) {
            DiskManagerPiece[] dm_pieces = dm.getPieces();
            boolean update_skipped;
            boolean update_boundaries;
            // ensure disk manager is in a decent state before we start poking about as during
            // allocation the checking of skipped status is not reliable
            int dm_state = dm.getState();
            if (dm_state == DiskManager.CHECKING || dm_state == DiskManager.READY) {
                if (!valid) {
                    update_skipped = true;
                    update_boundaries = true;
                } else {
                    if (piecesStateFileBoundariesDone) {
                        update_boundaries = false;
                    } else {
                        piecesStateFileBoundariesDone = true;
                        update_boundaries = true;
                    }
                    long marker = dm.getPriorityChangeMarker();
                    if (marker == piecesStateSkippedMarker) {
                        update_skipped = false;
                    } else {
                        piecesStateSkippedMarker = marker;
                        update_skipped = true;
                    }
                }
            } else {
                update_skipped = false;
                update_boundaries = false;
            }
            for (int i = 0; i < nbPieces; i++) {
                DiskManagerPiece piece = dm_pieces[i];
                int state = piece.isDone() ? PS_DONE : PS_NONE;
                if (update_skipped) {
                    if (piece.isSkipped()) {
                        state |= PS_SKIPPED;
                    }
                } else {
                    state |= oldPiecesState[i] & PS_SKIPPED;
                }
                if (update_boundaries) {
                    if (piece.spansFiles()) {
                        state |= PS_FILE_BOUNDARY;
                    }
                } else {
                    state |= oldPiecesState[i] & PS_FILE_BOUNDARY;
                }
                newPiecesState[i] = state;
                if (valid) {
                    if (oldPiecesState[i] != state) {
                        valid = false;
                    }
                }
            }
        } else if (valid) {
            for (int i = 0; i < nbPieces; i++) {
                if (oldPiecesState[i] != 0) {
                    valid = false;
                    break;
                }
            }
        }
        piecesStateCache = newPiecesState;
        if (!valid) {
            Rectangle bounds = piecesImage.getClientArea();
            int xMax = bounds.width - 2;
            int yMax = bounds.height - 2 - 6;
            if (xMax < 10 || yMax < 5) {
                return;
            }
            int total = manager.getStats().getDownloadCompleted(true);
            if (pImage != null && !pImage.isDisposed()) {
                pImage.dispose();
            }
            pImage = new Image(display, bounds.width, bounds.height);
            GC gcImage = new GC(pImage);
            try {
                gcImage.setForeground(Colors.grey);
                gcImage.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
                gcImage.drawLine(1, 6, xMax, 6);
                if (newPiecesState != null && newPiecesState.length != 0) {
                    int[] boundariesHandled = new int[newPiecesState.length];
                    for (int i = 0; i < xMax; i++) {
                        int a0 = (i * nbPieces) / xMax;
                        int a1 = ((i + 1) * nbPieces) / xMax;
                        if (a1 == a0)
                            a1++;
                        if (a1 > nbPieces)
                            a1 = nbPieces;
                        int nbAvailable = 0;
                        int nbSkipped = 0;
                        boolean hasFileBoundary = false;
                        for (int j = a0; j < a1; j++) {
                            int ps = newPiecesState[j];
                            if ((ps & PS_DONE) != 0) {
                                nbAvailable++;
                            }
                            if ((ps & PS_SKIPPED) != 0) {
                                nbSkipped++;
                            }
                            if ((ps & PS_FILE_BOUNDARY) != 0) {
                                if (boundariesHandled[j] < 2) {
                                    boundariesHandled[j]++;
                                    hasFileBoundary = true;
                                }
                            }
                        }
                        if (nbAvailable == 0 && nbSkipped > 0) {
                            gcImage.setBackground(Colors.grey);
                            gcImage.fillRectangle(i + 1, 7, 1, yMax);
                        } else {
                            int index = (nbAvailable * Colors.BLUES_DARKEST) / (a1 - a0);
                            gcImage.setBackground(Colors.blues[index]);
                            gcImage.fillRectangle(i + 1, 7, 1, yMax);
                        }
                        if (hasFileBoundary) {
                            gcImage.setBackground(Colors.green);
                            gcImage.fillRectangle(i + 1, 7 + yMax - 6, 1, 6);
                        }
                    }
                }
                // draw file % bar above
                int limit = (xMax * total) / 1000;
                gcImage.setBackground(Colors.colorProgressBar);
                gcImage.fillRectangle(1, 1, limit, 5);
                if (limit < xMax) {
                    gcImage.setBackground(Colors.blues[Colors.BLUES_LIGHTEST]);
                    gcImage.fillRectangle(limit + 1, 1, xMax - limit, 5);
                }
            } finally {
                gcImage.dispose();
            }
            if (piecesPercent != null && !piecesPercent.isDisposed())
                piecesPercent.setText(DisplayFormatters.formatPercentFromThousands(total));
            if (pImage == null || pImage.isDisposed()) {
                return;
            }
        }
    } finally {
        this_mon.exit();
    }
}
Also used : Rectangle(org.eclipse.swt.graphics.Rectangle) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DiskManager(com.biglybt.core.disk.DiskManager) Image(org.eclipse.swt.graphics.Image) GC(org.eclipse.swt.graphics.GC)

Example 27 with DiskManager

use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.

the class PiecesDoneAndCountItem method refresh.

@Override
public void refresh(TableCell cell) {
    DiskManagerFileInfo fileInfo = (DiskManagerFileInfo) cell.getDataSource();
    int total = -1;
    int done = -1;
    if (fileInfo != null) {
        total = fileInfo.getNbPieces();
        DiskManager dm = fileInfo == null ? null : fileInfo.getDiskManager();
        if (dm != null) {
            done = 0;
            int start = fileInfo.getFirstPieceNumber();
            int end = start + total;
            DiskManagerPiece[] pieces = dm.getPieces();
            for (int i = start; i < end; i++) {
                if (pieces[i].isDone()) {
                    done++;
                }
            }
        }
    }
    if (!cell.setSortValue(done) && cell.isValid()) {
        return;
    }
    cell.setText(done < 0 || total < 0 ? "" : MessageText.getString("v3.MainWindow.xofx", new String[] { String.valueOf(done), String.valueOf(total) }));
}
Also used : DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DiskManager(com.biglybt.core.disk.DiskManager)

Example 28 with DiskManager

use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.

the class DisplayFormatters method formatDownloadStatusDefaultLocale.

public static String formatDownloadStatusDefaultLocale(DownloadManager manager) {
    int state = manager.getState();
    String tmp = "";
    DiskManager dm = manager.getDiskManager();
    switch(state) {
        case DownloadManager.STATE_WAITING:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.waiting");
            break;
        case DownloadManager.STATE_INITIALIZING:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.initializing");
            break;
        case DownloadManager.STATE_INITIALIZED:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.initializing");
            break;
        case DownloadManager.STATE_ALLOCATING:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.allocating");
            break;
        case DownloadManager.STATE_CHECKING:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.checking");
            break;
        case DownloadManager.STATE_FINISHING:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.finishing");
            break;
        case DownloadManager.STATE_READY:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.ready");
            break;
        case DownloadManager.STATE_DOWNLOADING:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.downloading");
            if (manager.isSwarmMerging() != null) {
                tmp += " + " + MessageText.getDefaultLocaleString("TableColumn.header.mergeddata");
            }
            break;
        case DownloadManager.STATE_SEEDING:
            if (dm != null && dm.getCompleteRecheckStatus() != -1) {
                int done = dm.getCompleteRecheckStatus();
                if (done == -1) {
                    done = 1000;
                }
                tmp = MessageText.getDefaultLocaleString("ManagerItem.seeding") + " + " + MessageText.getDefaultLocaleString("ManagerItem.checking") + ": " + formatPercentFromThousands(done);
            } else if (manager.getPeerManager() != null && manager.getPeerManager().isSuperSeedMode()) {
                tmp = MessageText.getDefaultLocaleString("ManagerItem.superseeding");
            } else {
                tmp = MessageText.getDefaultLocaleString("ManagerItem.seeding");
            }
            break;
        case DownloadManager.STATE_STOPPING:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.stopping");
            break;
        case DownloadManager.STATE_STOPPED:
            tmp = MessageText.getDefaultLocaleString(manager.isPaused() ? "ManagerItem.paused" : "ManagerItem.stopped");
            break;
        case DownloadManager.STATE_QUEUED:
            tmp = MessageText.getDefaultLocaleString("ManagerItem.queued");
            break;
        case DownloadManager.STATE_ERROR:
            // $NON-NLS-1$ //$NON-NLS-2$
            tmp = MessageText.getDefaultLocaleString("ManagerItem.error").concat(": ").concat(manager.getErrorDetails());
            break;
        default:
            tmp = String.valueOf(state);
    }
    return (tmp);
}
Also used : DiskManager(com.biglybt.core.disk.DiskManager)

Example 29 with DiskManager

use of com.biglybt.core.disk.DiskManager 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 30 with DiskManager

use of com.biglybt.core.disk.DiskManager 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)

Aggregations

DiskManager (com.biglybt.core.disk.DiskManager)31 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)13 PEPeerManager (com.biglybt.core.peer.PEPeerManager)13 DownloadManager (com.biglybt.core.download.DownloadManager)12 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)7 PEPiece (com.biglybt.core.peer.PEPiece)6 LogEvent (com.biglybt.core.logging.LogEvent)5 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)4 TOTorrent (com.biglybt.core.torrent.TOTorrent)4 IOException (java.io.IOException)4 PEPeer (com.biglybt.core.peer.PEPeer)3 File (java.io.File)3 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)2 DMPieceMapEntry (com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry)2 DownloadManagerStats (com.biglybt.core.download.DownloadManagerStats)2 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)2 AERunnable (com.biglybt.core.util.AERunnable)2 Download (com.biglybt.pif.download.Download)2 MenuBuildUtils (com.biglybt.ui.swt.MenuBuildUtils)2 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)2