FileDocCategorySizeDatePackage
SeedingListener.javaAPI DocAzureus 3.0.3.47834Thu Aug 02 17:32:28 BST 2007com.aelitis.azureus.ui.swt.browser.listener.publish

SeedingListener

public class SeedingListener extends com.aelitis.azureus.ui.swt.browser.msg.AbstractMessageListener

Fields Summary
private static final long
INFINITE_ETA
private static final long
STOPPED_ETA
private static final long
ERROR_ETA
private static final String
JS_UPLOAD_PROGRESS_MSG_KEY
private static final String
JS_INDIVI_UPDATE_MSG_OP
private static final String
JS_GLOBAL_UPDATE_MSG_OP
public static final String
DEFAULT_LISTENER_ID
public static final String
OP_SEND_UPDATE
public static final String
OP_REMOVE
public static final String
OP_STOP
public static final String
OP_START
Constructors Summary
public SeedingListener()


	  
		this(DEFAULT_LISTENER_ID);
	
public SeedingListener(String id)

		super(id);
	
Methods Summary
private java.util.MapconstructJSGlobalProgress(int percent, long _eta)

		String eta = formatETA(_eta);

		Map global = new HashMap();
		global.put("percent", new Long(percent));
		global.put("eta", eta);

		return global;
	
private java.util.MapconstructJSTorrentProgress(byte[] infohash, java.lang.String name, int percent, long _eta)

		String hash = infohash == null ? "<null>" : Base32.encode(infohash);
		String eta = formatETA(_eta);

		Map torrent = new HashMap();
		torrent.put("hash", hash);
		torrent.put("name", name);
		torrent.put("percent", new Long(percent));
		torrent.put("eta", eta);

		return torrent;
	
private java.lang.StringformatETA(long eta)

		if (eta == INFINITE_ETA) {
			return "";
		} else if (eta == STOPPED_ETA) {
			return "x";
		} else if (eta == ERROR_ETA) {
			return "e";
		}

		return TimeFormatter.format( eta );
	
private org.gudy.azureus2.core3.download.DownloadManagergetDM(java.lang.String magnet)

		AzureusCore core = AzureusCoreFactory.getSingleton();
		return core.getGlobalManager().getDownloadManager(
				new HashWrapper(Base32.decode(magnet)));
	
public voidhandleMessage(com.aelitis.azureus.ui.swt.browser.msg.BrowserMessage message)

		if (OP_SEND_UPDATE.equals(message.getOperationId())) {
			sendUpdate();
		} else if (OP_REMOVE.equals(message.getOperationId())) {
			String id = MapUtils.getMapString(message.getDecodedMap(), "id", null);
			if (id != null) {
				removeTorrent(id);
			}
		} else if (OP_START.equals(message.getOperationId())) {
			String id = MapUtils.getMapString(message.getDecodedMap(), "id", null);
			if (id != null) {
				startTorrent(id);
			}
		} else if (OP_STOP.equals(message.getOperationId())) {
			String id = MapUtils.getMapString(message.getDecodedMap(), "id", null);
			if (id != null) {
				stopTorrent(id);
			}
		} else {
			throw new IllegalArgumentException("Unknown operation: "
					+ message.getOperationId());
		}
	
private voidremoveTorrent(java.lang.String id)

		final DownloadManager dm = getDM(id);

		if (PublishUtils.isPublished(dm)) {
			PublishUtils.setPublished(dm, false);
			ManagerUtils.remove(dm, null, false, false, new AERunnable() {
				public void runSupport() {
					PublishUtils.setPublished(dm);
				}
			});
		}
	
private voidsendUpdate()

		try {
			GlobalManager gm = AzureusCoreFactory.getSingleton().getGlobalManager();
			if (gm == null) {
				return;
			}
			List dmsArray = gm.getDownloadManagers();
			if (dmsArray == null) {
				return;
			}

			Object[] dms = dmsArray.toArray();

			boolean hasIncompletePublish = false;
			long totSeedingBytes = 0;
			long totSeedingBytesRemaining = 0;
			long total_up_rate_bps = 0;

			int num_actually_uploading = 0;

			ArrayList indiv_torrents = new ArrayList();

			for (int i = dms.length - 1; i >= 0; i--) { //loop through all the running torrents

				DownloadManager d = (DownloadManager) dms[i];

				if (PublishUtils.isPublished(d)) { //this one we're uploading as published

					//TODO use something more explicit than swarm availability / uploaded bytes

					long download_size = d.getTorrent().getSize();

					int percent_done = (int) ((d.getStats().getTotalDataBytesSent() * 100) / download_size);

					if (percent_done > 99) { //100% uploaded
						indiv_torrents.add(new IndividualProgress(d.getTorrent().getHash(),
								d.getDisplayName(), 100, 0));
					} else { //upload still in progress
						hasIncompletePublish = true;

						long eta = INFINITE_ETA; //so it shows infinity						

						if (d.getState() == DownloadManager.STATE_STOPPED) {
							eta = STOPPED_ETA;
						} else if (d.getState() == DownloadManager.STATE_ERROR) {
							eta = ERROR_ETA;
						} else {
							num_actually_uploading++; //running and upload still needed

							totSeedingBytes += download_size;

							long remaining = (long) (download_size * ((float) (100 - percent_done) / 100)); //rough

							totSeedingBytesRemaining += remaining; //global stats

							long up_rate_bps = d.getStats().getDataSendRate();

							if (up_rate_bps > 0) {
								total_up_rate_bps += up_rate_bps; //global stats

								eta = remaining / up_rate_bps; //seconds remaining
							}
						}

						indiv_torrents.add(new IndividualProgress(d.getTorrent().getHash(),
								d.getDisplayName(), percent_done, eta));
					}
				}
			}

			if (!indiv_torrents.isEmpty()) { //there is something to update				

				long g_percent = 100;
				long g_eta = INFINITE_ETA;

				if (totSeedingBytes > 0) { //there is still torrent data to upload
					g_percent = ((totSeedingBytes - totSeedingBytesRemaining) * 100)
							/ totSeedingBytes;

					if (total_up_rate_bps > 0) {
						g_eta = totSeedingBytesRemaining / total_up_rate_bps; //seconds remaining					
					}

					if (num_actually_uploading < 1) { //all in stopped state
						g_eta = STOPPED_ETA;
						g_percent = 0;
					}
				} else if (!hasIncompletePublish) { //done uploading
					g_eta = 0;
				}

				List torrents = new ArrayList();

				for (Iterator it = indiv_torrents.iterator(); it.hasNext();) {
					IndividualProgress ind = (IndividualProgress) it.next();

					long mod_eta = ind.eta;

					if (g_eta > 0 && ind.eta > g_eta) { //for user display purposes, limit indiv eta to max what the global eta is showing
						mod_eta = g_eta;
					}

					torrents.add(constructJSTorrentProgress(ind.infohash, ind.name,
							ind.percent, mod_eta));
				}

				context.sendBrowserMessage(JS_UPLOAD_PROGRESS_MSG_KEY,
						JS_GLOBAL_UPDATE_MSG_OP, constructJSGlobalProgress((int) g_percent,
								g_eta));

				context.sendBrowserMessage(JS_UPLOAD_PROGRESS_MSG_KEY,
						JS_INDIVI_UPDATE_MSG_OP, torrents);
			}

		} catch (Throwable tt) {
			tt.printStackTrace();
		}
	
private voidstartTorrent(java.lang.String id)

		DownloadManager dm = getDM(id);

		if (dm != null) {
			try {
				dm.setForceStart(true);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	
private voidstop(org.gudy.azureus2.core3.download.DownloadManager dm)

		try {
			ManagerUtils.stop(dm, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	
private voidstopTorrent(java.lang.String id)

		DownloadManager dm = getDM(id);
		stop(dm);