use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class UploadSessionPicker method pickBestDownloadSessions.
// this picks downloading sessions only
protected LinkedList<UploadSession> pickBestDownloadSessions(int max_sessions) {
// TODO factor download priority into best calculation?
ArrayList<PEPeer> all_peers = globalGetAllDownloadPeers();
if (all_peers.isEmpty())
return new LinkedList();
ArrayList<PEPeer> best = down_ranker.rankPeers(max_sessions, all_peers);
if (best.size() != max_sessions) {
Debug.outNoStack("best.size()[" + best.size() + "] != max_sessions[" + max_sessions + "]");
}
if (best.isEmpty()) {
return new LinkedList<>();
}
LinkedList<UploadSession> best_sessions = new LinkedList<>();
for (Iterator<PEPeer> it = best.iterator(); it.hasNext(); ) {
PEPeer peer = it.next();
UploadSession session = new UploadSession(peer, UploadSession.TYPE_DOWNLOAD);
best_sessions.add(session);
}
return best_sessions;
}
use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class NetworkAdminSpeedTesterBTImpl method start.
/**
* The downloads have been stopped just need to do the testing.
* @param tot - Torrent recieved from testing service.
*/
public synchronized void start(TOTorrent tot) {
if (test_started) {
Debug.out("Test already started!");
return;
}
test_started = true;
// OK lets start the test.
try {
TorrentUtils.setFlag(tot, TorrentUtils.TORRENT_FLAG_LOW_NOISE, true);
Torrent torrent = new TorrentImpl(tot);
String fileName = torrent.getName();
sendStageUpdateToListeners(MessageText.getString("SpeedTestWizard.stage.message.preparing"));
// create a blank file of specified size. (using the temporary name.)
File saveLocation = AETemporaryFileHandler.createTempFile();
File baseDir = saveLocation.getParentFile();
File blankFile = new File(baseDir, fileName);
File blankTorrentFile = new File(baseDir, "speedTestTorrent.torrent");
torrent.writeToFile(blankTorrentFile);
URL announce_url = torrent.getAnnounceURL();
if (announce_url.getProtocol().equalsIgnoreCase("https")) {
SESecurityManager.setCertificateHandler(announce_url, new SECertificateListener() {
@Override
public boolean trustCertificate(String resource, X509Certificate cert) {
return (true);
}
});
}
Download speed_download = plugin.getDownloadManager().addDownloadStopped(torrent, blankTorrentFile, blankFile);
speed_download.setBooleanAttribute(speedTestAttrib, true);
DownloadManager core_download = PluginCoreUtils.unwrap(speed_download);
core_download.setPieceCheckingEnabled(false);
// make sure we've got a bunch of upload slots
core_download.getDownloadState().setIntParameter(DownloadManagerState.PARAM_MAX_UPLOADS, 32);
core_download.getDownloadState().setIntParameter(DownloadManagerState.PARAM_MAX_UPLOADS_WHEN_SEEDING, 32);
if (use_crypto) {
core_download.setCryptoLevel(NetworkManager.CRYPTO_OVERRIDE_REQUIRED);
}
core_download.addPeerListener(new DownloadManagerPeerListener() {
@Override
public void peerManagerWillBeAdded(PEPeerManager peer_manager) {
DiskManager disk_manager = peer_manager.getDiskManager();
DiskManagerPiece[] pieces = disk_manager.getPieces();
int startPiece = setStartPieceBasedOnMode(testMode, pieces.length);
for (int i = startPiece; i < pieces.length; i++) {
pieces[i].setDone(true);
}
}
@Override
public void peerManagerAdded(PEPeerManager peer_manager) {
}
@Override
public void peerManagerRemoved(PEPeerManager manager) {
}
@Override
public void peerAdded(PEPeer peer) {
}
@Override
public void peerRemoved(PEPeer peer) {
}
});
speed_download.moveTo(1);
speed_download.setFlag(Download.FLAG_DISABLE_AUTO_FILE_MOVE, true);
core_download.initialize();
core_download.setForceStart(true);
TorrentSpeedTestMonitorThread monitor = new TorrentSpeedTestMonitorThread(speed_download);
monitor.start();
// The test has now started!!
} catch (Throwable e) {
test_completed = true;
abort("Could not start test", e);
}
}
use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class UnchokerUtil method doHighLatencyPeers.
public static void doHighLatencyPeers(ArrayList<PEPeer> peers_to_choke, ArrayList<PEPeer> peers_to_unchoke, boolean allow_snubbed) {
if (peers_to_choke.size() == 0) {
return;
}
// System.out.println( "doHLP: " + peers_to_choke + ", " + peers_to_unchoke );
Iterator<PEPeer> choke_it = peers_to_choke.iterator();
int to_remove = 0;
while (choke_it.hasNext()) {
PEPeer peer = choke_it.next();
if (AENetworkClassifier.categoriseAddress(peer.getIp()) != AENetworkClassifier.AT_PUBLIC) {
if (isUnchokable(peer, allow_snubbed)) {
// System.out.println( " removed " + peer );
choke_it.remove();
to_remove++;
} else {
// it isn't unchokable so we need to choke it whatever
}
}
}
if (to_remove > 0) {
ListIterator<PEPeer> unchoke_it = peers_to_unchoke.listIterator(peers_to_unchoke.size());
while (unchoke_it.hasPrevious()) {
PEPeer peer = unchoke_it.previous();
if (AENetworkClassifier.categoriseAddress(peer.getIp()) != AENetworkClassifier.AT_PUBLIC) {
// System.out.println( " balanced with " + peer );
unchoke_it.remove();
to_remove--;
if (to_remove == 0) {
return;
}
}
}
if (to_remove > 0) {
unchoke_it = peers_to_unchoke.listIterator(peers_to_unchoke.size());
while (unchoke_it.hasPrevious()) {
PEPeer peer = unchoke_it.previous();
unchoke_it.remove();
to_remove--;
if (to_remove == 0) {
return;
}
}
}
}
}
use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class StatsWriterImpl method writeSupport.
protected void writeSupport() {
writeLineRaw("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
boolean export_peer_stats = COConfigurationManager.getBooleanParameter("Stats Export Peer Details");
boolean export_file_stats = COConfigurationManager.getBooleanParameter("Stats Export File Details");
String xsl = COConfigurationManager.getStringParameter("Stats XSL File");
if (xsl.length() > 0) {
writeLineRaw("<?xml-stylesheet type=\"text/xsl\" href=\"" + xsl + "\"?>");
}
writeLineRaw("<STATS>");
GlobalManager global = core.getGlobalManager();
try {
indent();
writeTag("AZUREUS_VERSION", Constants.AZUREUS_VERSION);
writeLineRaw("<GLOBAL>");
try {
indent();
GlobalManagerStats gm_stats = global.getStats();
writeRawCookedAverageTag("DOWNLOAD_SPEED", gm_stats.getDataReceiveRate() + gm_stats.getProtocolReceiveRate());
writeRawCookedAverageTag("UPLOAD_SPEED", gm_stats.getDataSendRate() + gm_stats.getProtocolSendRate());
} finally {
exdent();
}
writeLineRaw("</GLOBAL>");
writeLineRaw("<DOWNLOADS>");
try {
indent();
List _dms = global.getDownloadManagers();
DownloadManager[] dms = new DownloadManager[_dms.size()];
// sort by position, downloads then seeders
_dms.toArray(dms);
Arrays.sort(dms, new Comparator() {
@Override
public int compare(Object o1, Object o2) {
DownloadManager d1 = (DownloadManager) o1;
DownloadManager d2 = (DownloadManager) o2;
int d1_index = d1.getPosition();
int d2_index = d2.getPosition();
if (d1.isDownloadComplete(false)) {
d1_index += 1000000;
}
if (d2.isDownloadComplete(false)) {
d2_index += 1000000;
}
return (d1_index - d2_index);
}
});
for (int i = 0; i < dms.length; i++) {
DownloadManager dm = (DownloadManager) dms[i];
DownloadManagerStats dm_stats = dm.getStats();
writeLineRaw("<DOWNLOAD>");
try {
indent();
writeLineRaw("<TORRENT>");
// torrent can be null if broken torrent!
TOTorrent torrent = dm.getTorrent();
try {
indent();
writeTag("NAME", dm.getDisplayName());
writeTag("TORRENT_FILE", dm.getTorrentFileName());
if (torrent != null) {
writeTag("HASH", TorrentUtils.nicePrintTorrentHash(torrent, true));
writeRawCookedTag("SIZE", torrent.getSize());
writeTag("PIECE_LENGTH", torrent.getPieceLength());
writeTag("PIECE_COUNT", torrent.getNumberOfPieces());
writeTag("FILE_COUNT", torrent.getFiles().length);
writeTag("COMMENT", dm.getTorrentComment());
writeTag("CREATED_BY", dm.getTorrentCreatedBy());
writeTag("CREATION_DATE", torrent.getCreationDate());
}
} finally {
exdent();
}
writeLineRaw("</TORRENT>");
writeTag("DOWNLOAD_STATUS", DisplayFormatters.formatDownloadStatusDefaultLocale(dm));
writeTag("DOWNLOAD_DIR", dm.getSaveLocation().toString());
if (torrent != null) {
if (torrent.isSimpleTorrent()) {
writeTag("TARGET_FILE", dm.getSaveLocation().toString());
} else {
writeTag("TARGET_DIR", dm.getSaveLocation().toString());
}
}
writeTag("TRACKER_STATUS", dm.getTrackerStatus());
writeTag("COMPLETED", dm_stats.getCompleted());
writeTag("NON_DND_COMPLETED", dm.isDownloadComplete(false));
writeRawCookedTag("DOWNLOADED", dm_stats.getTotalDataBytesReceived());
writeRawCookedTag("UPLOADED", dm_stats.getTotalDataBytesSent());
writeRawCookedTag("DISCARDED", dm_stats.getDiscarded());
writeRawCookedAverageTag("DOWNLOAD_SPEED", dm_stats.getDataReceiveRate());
writeRawCookedAverageTag("UPLOAD_SPEED", dm_stats.getDataSendRate());
writeRawCookedAverageTag("TOTAL_SPEED", dm_stats.getTotalAverage());
writeRawCookedAverageTag("DOWNLOAD_SPEED_SMOOTH", dm_stats.getSmoothedDataReceiveRate());
writeRawCookedAverageTag("UPLOAD_SPEED_SMOOTH", dm_stats.getSmoothedDataSendRate());
writeTag("ELAPSED", dm_stats.getElapsedTime());
writeTag("ETA", DisplayFormatters.formatETA(dm_stats.getSmoothedETA()));
writeTag("HASH_FAILS", dm_stats.getHashFailCount());
writeTag("SHARE_RATIO", dm_stats.getShareRatio());
writeTag("TOTAL_SEEDS", dm.getNbSeeds());
writeTag("TOTAL_LEECHERS", dm.getNbPeers());
if (export_file_stats) {
try {
writeLineRaw("<FILES>");
indent();
DiskManagerFileInfo[] files = dm.getDiskManagerFileInfo();
for (int j = 0; j < files.length; j++) {
DiskManagerFileInfo file = files[j];
try {
writeLineRaw("<FILE>");
indent();
writeTag("NAME", file.getTorrentFile().getRelativePath());
writeTag("DND", file.isSkipped());
writeRawCookedTag("SIZE", file.getLength());
writeRawCookedTag("DOWNLOADED", file.getDownloaded());
} finally {
exdent();
writeLineRaw("</FILE>");
}
}
} finally {
exdent();
writeLineRaw("</FILES>");
}
}
if (export_peer_stats) {
try {
writeLineRaw("<PEERS>");
indent();
PEPeerManager pm = dm.getPeerManager();
if (pm != null) {
List peers = pm.getPeers();
for (int j = 0; j < peers.size(); j++) {
PEPeer peer = (PEPeer) peers.get(j);
PEPeerStats peer_stats = peer.getStats();
byte[] id = peer.getId();
if (id == null) {
continue;
}
try {
String peer_id = PeerClassifier.getPrintablePeerID(id);
peer_id = escapeXML(peer_id);
String type = escapeXML(peer.getClient());
writeLineRaw("<PEER hex_id=\"" + ByteFormatter.encodeString(id) + "\" printable_id=\"" + peer_id + "\" type=\"" + type + "\">");
indent();
writeTag("IP", peer.getIp());
writeTag("IS_SEED", peer.isSeed());
writeRawCookedTag("DOWNLOADED", peer_stats.getTotalDataBytesReceived());
writeRawCookedTag("UPLOADED", peer_stats.getTotalDataBytesSent());
writeRawCookedAverageTag("DOWNLOAD_SPEED", peer_stats.getDataReceiveRate());
writeRawCookedAverageTag("UPLOAD_SPEED", peer_stats.getDataSendRate());
} catch (Throwable e) {
Debug.printStackTrace(e);
} finally {
exdent();
writeLineRaw("</PEER>");
}
}
}
} finally {
exdent();
writeLineRaw("</PEERS>");
}
}
} finally {
exdent();
}
writeLineRaw("</DOWNLOAD>");
}
} finally {
exdent();
}
writeLineRaw("</DOWNLOADS>");
} finally {
exdent();
}
writeLineRaw("</STATS>");
}
use of com.biglybt.core.peer.PEPeer in project BiglyBT by BiglySoftware.
the class PiecesItem method refresh.
@Override
public void refresh(final TableCell cell) {
/* Notes:
* We store our image and imageBufer in PEPeer using
* setData & getData.
*/
// Named infoObj so code can be copied easily to the other PiecesItem
final PEPeer infoObj = (PEPeer) cell.getDataSource();
long lCompleted = (infoObj == null) ? 0 : infoObj.getPercentDoneInThousandNotation();
if (!cell.setSortValue(lCompleted) && cell.isValid()) {
return;
}
if (infoObj == null)
return;
Utils.execSWTThread(new AERunnable() {
@Override
public void runSupport() {
if (cell.isDisposed()) {
return;
}
// Compute bounds ...
int newWidth = cell.getWidth();
if (newWidth <= 0)
return;
int newHeight = cell.getHeight();
int x0 = borderVerticalSize;
int x1 = newWidth - 1 - borderVerticalSize;
int y0 = completionHeight + borderHorizontalSize + borderSplit;
int y1 = newHeight - 1 - borderHorizontalSize;
int drawWidth = x1 - x0 + 1;
if (drawWidth < 10 || y1 < 3)
return;
int[] imageBuffer = (int[]) infoObj.getData("PiecesImageBuffer");
boolean bImageBufferValid = imageBuffer != null && imageBuffer.length == drawWidth;
Image image = (Image) infoObj.getData("PiecesImage");
GC gcImage;
boolean bImageChanged;
Rectangle imageBounds;
if (image == null || image.isDisposed()) {
bImageChanged = true;
} else {
imageBounds = image.getBounds();
bImageChanged = imageBounds.width != newWidth || imageBounds.height != newHeight;
}
if (bImageChanged) {
if (image != null && !image.isDisposed()) {
image.dispose();
}
image = new Image(Utils.getDisplay(), newWidth, newHeight);
imageBounds = image.getBounds();
bImageBufferValid = false;
// draw border
gcImage = new GC(image);
gcImage.setForeground(Colors.grey);
if (borderHorizontalSize > 0) {
if (borderVerticalSize > 0) {
gcImage.drawRectangle(0, 0, newWidth - 1, newHeight - 1);
} else {
gcImage.drawLine(0, 0, newWidth - 1, 0);
gcImage.drawLine(0, newHeight - 1, newWidth - 1, newHeight - 1);
}
} else if (borderVerticalSize > 0) {
gcImage.drawLine(0, 0, 0, newHeight - 1);
gcImage.drawLine(newWidth - 1, 0, newWidth - 1, newHeight - 1);
}
if (borderSplit > 0) {
gcImage.setForeground(Colors.white);
gcImage.drawLine(x0, completionHeight + borderHorizontalSize, x1, completionHeight + borderHorizontalSize);
}
} else {
gcImage = new GC(image);
}
final BitFlags peerHave = infoObj.getAvailable();
boolean established = ((PEPeerTransport) infoObj).getConnectionState() == PEPeerTransport.CONNECTION_FULLY_ESTABLISHED;
if (established && peerHave != null && peerHave.flags.length > 0) {
if (imageBuffer == null || imageBuffer.length != drawWidth) {
imageBuffer = new int[drawWidth];
}
final boolean[] available = peerHave.flags;
try {
int nbComplete = 0;
int nbPieces = available.length;
DiskManager disk_manager = infoObj.getManager().getDiskManager();
DiskManagerPiece[] pieces = disk_manager == null ? null : disk_manager.getPieces();
int a0;
int a1 = 0;
for (int i = 0; i < drawWidth; i++) {
if (i == 0) {
// always start out with one piece
a0 = 0;
a1 = nbPieces / drawWidth;
if (a1 == 0)
a1 = 1;
} else {
// the last iteration, a1 will be nbPieces
a0 = a1;
a1 = ((i + 1) * nbPieces) / (drawWidth);
}
int index;
int nbNeeded = 0;
if (a1 <= a0) {
index = imageBuffer[i - 1];
} else {
int nbAvailable = 0;
for (int j = a0; j < a1; j++) {
if (available[j]) {
if (pieces == null || !pieces[j].isDone()) {
nbNeeded++;
}
nbAvailable++;
}
}
nbComplete += nbAvailable;
index = (nbAvailable * Colors.BLUES_DARKEST) / (a1 - a0);
if (nbNeeded <= nbAvailable / 2)
index += INDEX_COLOR_FADEDSTARTS;
}
if (imageBuffer[i] != index) {
imageBuffer[i] = index;
if (bImageBufferValid) {
bImageChanged = true;
if (imageBuffer[i] >= INDEX_COLOR_FADEDSTARTS)
gcImage.setForeground(Colors.faded[index - INDEX_COLOR_FADEDSTARTS]);
else
gcImage.setForeground(Colors.blues[index]);
gcImage.drawLine(i + x0, y0, i + x0, y1);
}
}
}
if (!bImageBufferValid) {
if (established) {
int iLastIndex = imageBuffer[0];
int iWidth = 1;
for (int i = 1; i < drawWidth; i++) {
if (iLastIndex == imageBuffer[i]) {
iWidth++;
} else {
if (iLastIndex >= INDEX_COLOR_FADEDSTARTS) {
gcImage.setBackground(Colors.faded[iLastIndex - INDEX_COLOR_FADEDSTARTS]);
} else
gcImage.setBackground(Colors.blues[iLastIndex]);
gcImage.fillRectangle(i - iWidth + x0, y0, iWidth, y1 - y0 + 1);
iWidth = 1;
iLastIndex = imageBuffer[i];
}
}
if (iLastIndex >= INDEX_COLOR_FADEDSTARTS)
gcImage.setBackground(Colors.faded[iLastIndex - INDEX_COLOR_FADEDSTARTS]);
else
gcImage.setBackground(Colors.blues[iLastIndex]);
gcImage.fillRectangle(x1 - iWidth + 1, y0, iWidth, y1 - y0 + 1);
bImageChanged = true;
}
}
int limit = (drawWidth * nbComplete) / nbPieces;
if (limit < drawWidth) {
gcImage.setBackground(Colors.blues[Colors.BLUES_LIGHTEST]);
gcImage.fillRectangle(limit + x0, borderHorizontalSize, x1 - limit, completionHeight);
}
gcImage.setBackground(Colors.colorProgressBar);
gcImage.fillRectangle(x0, borderHorizontalSize, limit, completionHeight);
} catch (Exception e) {
System.out.println("Error Drawing PiecesItem");
Debug.printStackTrace(e);
}
} else {
gcImage.setForeground(Colors.grey);
gcImage.setBackground(Colors.grey);
gcImage.fillRectangle(x0, y0, newWidth, y1);
}
gcImage.dispose();
Image oldImage = null;
Graphic graphic = cell.getGraphic();
if (graphic instanceof UISWTGraphic) {
oldImage = ((UISWTGraphic) graphic).getImage();
}
if (bImageChanged || image != oldImage || !cell.isValid()) {
if (cell instanceof TableCellSWT) {
((TableCellSWT) cell).setGraphic(image);
} else {
cell.setGraphic(new UISWTGraphicImpl(image));
}
if (oldImage != null && image != oldImage && !oldImage.isDisposed()) {
oldImage.dispose();
}
if (bImageChanged || image != oldImage) {
cell.invalidate();
}
infoObj.setData("PiecesImage", image);
infoObj.setData("PiecesImageBuffer", imageBuffer);
}
}
});
}
Aggregations