Search in sources :

Example 11 with DiskManagerPiece

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

the class FilesItem method refresh.

@Override
public void refresh(TableCell cell) {
    PEPiece pePiece = (PEPiece) cell.getDataSource();
    String value = "";
    if (pePiece != null) {
        DiskManagerPiece dmp = pePiece.getDMPiece();
        if (dmp != null) {
            DMPieceList l = dmp.getManager().getPieceList(pePiece.getPieceNumber());
            for (int i = 0; i < l.size(); i++) {
                DMPieceMapEntry entry = l.get(i);
                String name = entry.getFile().getTorrentFile().getRelativePath();
                value += (value.isEmpty() ? "" : "; ") + name;
            }
        }
    }
    if (!cell.setSortValue(value) && cell.isValid())
        return;
    cell.setText(value);
}
Also used : PEPiece(com.biglybt.core.peer.PEPiece) DMPieceMapEntry(com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) DMPieceList(com.biglybt.core.disk.impl.piecemapper.DMPieceList)

Example 12 with DiskManagerPiece

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

the class PieceGraphView method getPercentDone.

/**
 * @param startNo
 * @param count
 * @param dm_pieces
 * @return
 *
 * @since 3.0.1.1
 */
private double getPercentDone(int startNo, int count, DiskManagerPiece[] dm_pieces) {
    int totalComplete = 0;
    int totalBlocks = 0;
    for (int i = startNo; i < startNo + count; i++) {
        DiskManagerPiece piece = dm_pieces[i];
        int numBlocks = piece.getNbBlocks();
        totalBlocks += numBlocks;
        if (piece.isDone()) {
            totalComplete += numBlocks;
        } else {
            // !done
            totalComplete += piece.getNbWritten();
        }
    }
    return (double) totalComplete / totalBlocks;
}
Also used : DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece)

Example 13 with DiskManagerPiece

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

the class PieceGraphView method buildImage.

private void buildImage() {
    if (canvas == null || canvas.isDisposed()) {
        return;
    }
    // canvas.setBackground(ColorCache.getColor(canvas.getDisplay(), "#1b1b1b"));
    Rectangle bounds = canvas.getClientArea();
    if (bounds.isEmpty()) {
        return;
    }
    if (dlm == null) {
        canvas.redraw();
        return;
    }
    PEPeerManager pm = dlm.getPeerManager();
    DiskManager dm = dlm.getDiskManager();
    if (pm == null || dm == null) {
        canvas.redraw();
        return;
    }
    final DiskManagerPiece[] dm_pieces = dm.getPieces();
    if (dm_pieces == null || dm_pieces.length == 0) {
        canvas.redraw();
        return;
    }
    int numPieces = dm_pieces.length;
    if (imgHaveAll == null || imgHaveAll.isDisposed()) {
        imgHaveAll = new Image(canvas.getDisplay(), BLOCK_SIZE, BLOCK_SIZE);
        GC gc = new GC(imgHaveAll);
        try {
            try {
                gc.setAntialias(SWT.ON);
            } catch (Exception e) {
            // ignore
            }
            gc.setBackground(canvas.getBackground());
            gc.fillRectangle(imgHaveAll.getBounds());
            gc.setBackground(blockColors[BLOCKCOLOR_HAVEALL]);
            gc.fillRoundRectangle(1, 1, BLOCK_FILLSIZE, BLOCK_FILLSIZE, BLOCK_FILLSIZE, BLOCK_FILLSIZE);
        } finally {
            gc.dispose();
        }
    }
    if (imgNoHave == null || imgNoHave.isDisposed()) {
        imgNoHave = new Image(canvas.getDisplay(), BLOCK_SIZE, BLOCK_SIZE);
        GC gc = new GC(imgNoHave);
        try {
            try {
                gc.setAntialias(SWT.ON);
            } catch (Exception e) {
            // ignore
            }
            gc.setBackground(canvas.getBackground());
            gc.fillRectangle(imgNoHave.getBounds());
            gc.setBackground(blockColors[BLOCKCOLOR_NOHAVE]);
            gc.fillRoundRectangle(1, 1, BLOCK_FILLSIZE, BLOCK_FILLSIZE, BLOCK_FILLSIZE, BLOCK_FILLSIZE);
        } finally {
            gc.dispose();
        }
    }
    boolean clearImage = img == null || img.isDisposed() || img.getBounds().width != bounds.width || img.getBounds().height != bounds.height;
    if (clearImage) {
        if (img != null && !img.isDisposed()) {
            img.dispose();
        }
        // System.out.println("clear " + img);
        img = new Image(canvas.getDisplay(), bounds.width, bounds.height);
        squareCache = null;
    }
    PEPiece[] currentDLPieces = dlm.getCurrentPieces();
    Arrays.sort(currentDLPieces, compFindPEPiece);
    // find upload pieces
    ArrayList currentULPieces = new ArrayList();
    ArrayList futureULPieces = new ArrayList();
    PEPeer[] peers = (PEPeer[]) pm.getPeers().toArray(new PEPeer[0]);
    for (int i = 0; i < peers.length; i++) {
        PEPeer peer = peers[i];
        int[] peerRequestedPieces = peer.getIncomingRequestedPieceNumbers();
        if (peerRequestedPieces != null && peerRequestedPieces.length > 0) {
            currentULPieces.add(new Long(peerRequestedPieces[0]));
            for (int j = 1; j < peerRequestedPieces.length; j++) {
                futureULPieces.add(new Long(peerRequestedPieces[j]));
            }
        }
        // we'll have duplicates
        Collections.sort(currentULPieces);
        Collections.sort(futureULPieces);
    }
    int iNumCols = bounds.width / BLOCK_SIZE;
    int iNumRows = bounds.height / BLOCK_SIZE;
    int numSquares = onePiecePerBlock ? numPieces : iNumCols * iNumRows;
    double numPiecesPerSquare = numPieces / (double) numSquares;
    if (squareCache == null || squareCache.length != numSquares) {
        squareCache = new double[numSquares];
        Arrays.fill(squareCache, -1);
    }
    int[] availability = pm.getAvailability();
    int numRedraws = 0;
    GC gc = new GC(img);
    try {
        int iRow = 0;
        if (clearImage) {
            gc.setBackground(canvas.getBackground());
            gc.fillRectangle(bounds);
        }
        try {
            gc.setAdvanced(true);
            gc.setAntialias(SWT.ON);
            gc.setInterpolation(SWT.HIGH);
        } catch (Exception e) {
        // ignore
        }
        int iCol = 0;
        for (int squareNo = 0; squareNo < numSquares; squareNo++) {
            if (iCol >= iNumCols) {
                iCol = 0;
                iRow++;
            }
            int startNo = (int) (squareNo * numPiecesPerSquare);
            int count = (int) ((squareNo + 1) * numPiecesPerSquare) - startNo;
            if (count == 0) {
                count = 1;
            }
            // if (count > 1) System.out.println("!!! " + startNo);
            // System.out.println(startNo + ";" + count);
            double pctDone = getPercentDone(startNo, count, dm_pieces);
            // System.out.print(pctDone + ";");
            int colorIndex;
            int iXPos = iCol * BLOCK_SIZE;
            int iYPos = iRow * BLOCK_SIZE;
            if (pctDone == 1) {
                if (squareCache[squareNo] != pctDone) {
                    squareCache[squareNo] = pctDone;
                    gc.drawImage(imgHaveAll, iXPos, iYPos);
                    if (!clearImage) {
                        numRedraws++;
                        canvas.redraw(iXPos, iYPos, BLOCK_SIZE, BLOCK_SIZE, false);
                    }
                }
            } else if (pctDone == 0) {
                if (squareCache[squareNo] != pctDone) {
                    squareCache[squareNo] = pctDone;
                    gc.drawImage(imgNoHave, iXPos, iYPos);
                    if (!clearImage) {
                        numRedraws++;
                        canvas.redraw(iXPos, iYPos, BLOCK_SIZE, BLOCK_SIZE, false);
                    }
                }
            } else {
                // !done
                boolean isDownloading = false;
                for (int i = startNo; i < startNo + count; i++) {
                    if (Arrays.binarySearch(currentDLPieces, new Long(i), compFindPEPiece) >= 0) {
                        isDownloading = true;
                        break;
                    }
                }
                double val = pctDone + (isDownloading ? 0 : 1);
                if (squareCache[squareNo] != val) {
                    squareCache[squareNo] = val;
                    gc.drawImage(imgNoHave, iXPos, iYPos);
                    int size = (int) (BLOCK_FILLSIZE * pctDone);
                    if (size == 0) {
                        size = 1;
                    }
                    int q = (int) ((BLOCK_FILLSIZE - size) / 2.0 + 0.5) + 1;
                    colorIndex = isDownloading ? BLOCKCOLOR_DOWNLOADING : BLOCKCOLOR_HAVESOME;
                    gc.setBackground(blockColors[colorIndex]);
                    gc.fillOval(iXPos + q, iYPos + q, size, size);
                    // gc.fillRoundRectangle(iXPos + q, iYPos + q, size, size, size, size);
                    if (!clearImage) {
                        numRedraws++;
                        canvas.redraw(iXPos, iYPos, BLOCK_SIZE, BLOCK_SIZE, false);
                    }
                }
            }
            for (int i = startNo; i < startNo + count; i++) {
                if (Collections.binarySearch(currentULPieces, new Long(i)) >= 0) {
                    colorIndex = BLOCKCOLOR_UPLOADING;
                    int size = BLOCK_FILLSIZE + 1;
                    gc.setForeground(blockColors[colorIndex]);
                    gc.drawRoundRectangle(iXPos, iYPos, size, size, size, size);
                    if (!clearImage) {
                        numRedraws++;
                        canvas.redraw(iXPos, iYPos, BLOCK_SIZE, BLOCK_SIZE, false);
                    }
                    squareCache[squareNo] = -1;
                    break;
                } else if (Collections.binarySearch(futureULPieces, new Long(i)) >= 0) {
                    colorIndex = BLOCKCOLOR_UPLOADING;
                    int size = BLOCK_FILLSIZE + 1;
                    gc.setForeground(blockColors[colorIndex]);
                    gc.setLineStyle(SWT.LINE_DOT);
                    gc.drawRoundRectangle(iXPos, iYPos, size, size, size, size);
                    if (!clearImage) {
                        numRedraws++;
                        canvas.redraw(iXPos, iYPos, BLOCK_SIZE, BLOCK_SIZE, false);
                    }
                    gc.setLineStyle(SWT.LINE_SOLID);
                    squareCache[squareNo] = -1;
                    break;
                }
            }
            if (availability != null) {
                boolean hasNoAvail = false;
                for (int i = startNo; i < startNo + count; i++) {
                    if (availability[i] == 0) {
                        hasNoAvail = true;
                        squareCache[squareNo] = -1;
                        break;
                    }
                }
                if (hasNoAvail) {
                    gc.setForeground(blockColors[BLOCKCOLOR_NOAVAIL]);
                    gc.drawRectangle(iXPos, iYPos, BLOCK_FILLSIZE + 1, BLOCK_FILLSIZE + 1);
                    if (!clearImage) {
                        numRedraws++;
                        canvas.redraw(iXPos, iYPos, BLOCK_SIZE, BLOCK_SIZE, false);
                    }
                }
            }
            iCol++;
        }
    // System.out.println("redraws " + numRedraws);
    } catch (Exception e) {
        Debug.out(e);
    } finally {
        gc.dispose();
    }
// canvas.redraw();
}
Also used : PEPeer(com.biglybt.core.peer.PEPeer) DiskManager(com.biglybt.core.disk.DiskManager) PEPiece(com.biglybt.core.peer.PEPiece) PEPeerManager(com.biglybt.core.peer.PEPeerManager) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece)

Example 14 with DiskManagerPiece

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

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

Aggregations

DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)18 DiskManager (com.biglybt.core.disk.DiskManager)13 PEPeerManager (com.biglybt.core.peer.PEPeerManager)8 PEPiece (com.biglybt.core.peer.PEPiece)8 DownloadManager (com.biglybt.core.download.DownloadManager)6 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)5 LogEvent (com.biglybt.core.logging.LogEvent)5 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)5 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)3 DMPieceMapEntry (com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry)3 GC (org.eclipse.swt.graphics.GC)3 Image (org.eclipse.swt.graphics.Image)3 Rectangle (org.eclipse.swt.graphics.Rectangle)3 PEPeer (com.biglybt.core.peer.PEPeer)2 TOTorrent (com.biglybt.core.torrent.TOTorrent)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