FileDocCategorySizeDatePackage
PlatformAdManager.javaAPI DocAzureus 3.0.3.411367Thu Sep 27 09:33:10 BST 2007com.aelitis.azureus.core.messenger.config

PlatformAdManager

public class PlatformAdManager extends Object
author
TuxPaper
created
May 31, 2007

Fields Summary
public static String
LISTENER_ID
public static String
OP_GETADS
public static String
OP_GETPLAYLIST
public static String
OP_STOREIMPRESSIONS
public static List
unsentImpressions
public static AEMonitor
mon_unsentImpressions
public static boolean
DEBUG_ADS
private static int
RESEND_DELAY
private static TimerEvent
resendEvent
Constructors Summary
Methods Summary
public static voiddebug(java.lang.String string)

		debug(string, null);
	
public static voiddebug(java.lang.String string, java.lang.Throwable e)

param
string
param
e
since
3.0.1.7

		try {
			AEDiagnosticsLogger diag_logger = AEDiagnostics.getLogger("v3.ads");
			diag_logger.log(string);
			if (e != null) {
				diag_logger.log(e);
				Debug.out(string, e);
			}
			if (Constants.DIAG_TO_STDOUT || DEBUG_ADS) {
				System.out.println(Thread.currentThread().getName() + "|ADS|"
						+ System.currentTimeMillis() + "] " + string);
				if (e != null) {
					e.printStackTrace();
				}
			}
		} catch (Throwable t) {
			// ignore
		}
	
public static voidgetAds(org.gudy.azureus2.core3.download.DownloadManager[] adEnabledDownloads, long maxDelayMS, com.aelitis.azureus.core.messenger.config.PlatformAdManager$GetAdsDataReplyListener replyListener)


	    
			     

		String[] hashes = new String[adEnabledDownloads.length];
		for (int i = 0; i < adEnabledDownloads.length; i++) {
			DownloadManager dm = adEnabledDownloads[i];
			try {
				hashes[i] = dm.getTorrent().getHashWrapper().toBase32String();
			} catch (TOTorrentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		DownloadManager[] existingAds = AdManager.getInstance().getAds(true);
		List ads = new ArrayList();
		for (int i = 0; i < existingAds.length; i++) {
			DownloadManager dm = existingAds[i];

			try {
				TOTorrent torrent = dm.getTorrent();
				String hash = torrent.getHashWrapper().toBase32String();
				String adid = PlatformTorrentUtils.getAdId(torrent);

				Map mapAd = new HashMap();
				mapAd.put("hash", hash);
				mapAd.put("id", adid);

				ads.add(mapAd);
			} catch (TOTorrentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		Map parameters = new HashMap();
		parameters.put("hashes", hashes);
		parameters.put("ads", ads);

		PlatformMessage message = new PlatformMessage("AZMSG", LISTENER_ID,
				OP_GETADS, parameters, maxDelayMS);

		PlatformMessenger.queueMessage(message, new PlatformMessengerListener() {
			public void replyReceived(PlatformMessage message, String replyType,
					Map reply) {
				if (!replyType.equals(PlatformMessenger.REPLY_RESULT)) {
					if (replyListener != null) {
						replyListener.replyReceived(replyType, reply);
					}
					return;
				}

				List adTorrents = new ArrayList();
				List torrentsList = (List) reply.get("torrents");
				if (torrentsList != null) {
					for (int i = 0; i < torrentsList.size(); i++) {
						byte[] torrentBEncoded = Base64.decode((String) torrentsList.get(i));
						try {
							TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedByteArray(torrentBEncoded);
							adTorrents.add(torrent);
						} catch (TOTorrentException e) {
							Debug.out(e);
						}
					}
				}
				if (replyListener != null) {
					replyListener.adsReceived(adTorrents);
				}
			}

			public void messageSent(PlatformMessage message) {
				if (replyListener != null) {
					replyListener.messageSent();
				}
			}

		});
	
public static voidgetPlayList(org.gudy.azureus2.core3.download.DownloadManager dmToPlay, java.lang.String URLToPlay, java.lang.String trackingURL, long maxDelayMS, com.aelitis.azureus.core.messenger.config.PlatformAdManager$GetPlaylistReplyListener replyListener)


		try {
			Map parameters = new HashMap();

			parameters.put("hash",
					dmToPlay.getTorrent().getHashWrapper().toBase32String());
			parameters.put("content-url", URLToPlay);
			//URLToPlay.replaceAll("^file:/([^/])","file:///$1"));
			parameters.put("tracking-urls", new String[] {
				trackingURL
			});

			DownloadManager[] existingAds = AdManager.getInstance().getAds(false);
			List ads = new ArrayList();
			for (int i = 0; i < existingAds.length; i++) {
				DownloadManager dm = existingAds[i];

				try {
					TOTorrent torrent = dm.getTorrent();
					String hash = torrent.getHashWrapper().toBase32String();
					String adid = PlatformTorrentUtils.getAdId(torrent);

					Map mapAd = new HashMap();
					mapAd.put("hash", hash);
					mapAd.put("id", adid);
					String sAdFile = dm.getDownloadState().getPrimaryFile();
					String url = new File(sAdFile).toURL().toString().replaceAll(
							"^file:/([^/])", "file:///$1");
					mapAd.put("local-url", url);

					ads.add(mapAd);
				} catch (TOTorrentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			parameters.put("ads", ads);

			PlatformMessage message = new PlatformMessage("AZMSG", LISTENER_ID,
					OP_GETPLAYLIST, parameters, maxDelayMS);

			PlatformMessenger.queueMessage(message, new PlatformMessengerListener() {
				public void replyReceived(PlatformMessage message, String replyType,
						Map reply) {
					if (!replyType.equals(PlatformMessenger.REPLY_RESULT)) {
						debug("getPlayList returned error of: " + reply.get("message"));
						replyListener.replyReceived(replyType, null);
						return;
					}

					String playlist = MapUtils.getMapString(reply, "playlist", null);
					if (replyListener != null) {
						replyListener.replyReceived(replyType, playlist);
					}
				}

				public void messageSent(PlatformMessage message) {
					if (replyListener != null) {
						replyListener.messageSent();
					}
				}

			});
		} catch (Exception e) {
			Debug.out(e);
		}
	
public static voidloadUnsentImpressions()

		try {
			mon_unsentImpressions.enter();

			Map map = BDecoder.decodeStrings(FileUtil.readResilientConfigFile("unsentdata.config"));
			Object value = map.get("unsent");
			if (value instanceof List) {
				unsentImpressions = (List) value;
			} else {
				unsentImpressions.clear();
			}
		} finally {
			mon_unsentImpressions.exit();
		}
	
private static voidsaveUnsentImpressions()

since
3.0.1.5

		try {
			mon_unsentImpressions.enter();

			Map map = new HashMap();
			map.put("unsent", unsentImpressions);
			FileUtil.writeResilientConfigFile("unsentdata.config", map);
		} finally {
			mon_unsentImpressions.exit();
		}
	
public static voidsendUnsentImpressions(long maxDelayMS)

		// clear unsentImpressions.  If storing fails, we'll add them back in
		List sendingImpressions;
		try {
			mon_unsentImpressions.enter();

			sendingImpressions = unsentImpressions;
			unsentImpressions = new ArrayList();
			saveUnsentImpressions();
		} finally {
			mon_unsentImpressions.exit();
		}

		if (sendingImpressions.size() == 0) {
			return;
		}

		final List fSendingImpressions = sendingImpressions;

		Map ads = new HashMap();
		ads.put("ads", fSendingImpressions);
		ads.put("rpc-version", new Long(2));

		try {
			debug("sending " + fSendingImpressions.size() + " impressions");
			PlatformMessage message = new PlatformMessage("AZMSG", LISTENER_ID,
					OP_STOREIMPRESSIONS, ads, maxDelayMS);

			PlatformMessenger.queueMessage(message, new PlatformMessengerListener() {
				public void replyReceived(PlatformMessage message, String replyType,
						Map reply) {
					if (!replyType.equals(PlatformMessenger.REPLY_RESULT)) {
						debug("sending " + fSendingImpressions + " impressions failed. "
								+ reply);
						try {
							mon_unsentImpressions.enter();

							unsentImpressions.addAll(fSendingImpressions);

							setupResendTimer();
						} finally {
							mon_unsentImpressions.exit();
						}
						saveUnsentImpressions();
						return;
					}
					// TODO: check result to see which ones succeeded
					debug("sending " + fSendingImpressions.size()
							+ " impressions completed");
				}

				public void messageSent(PlatformMessage message) {
				}

			});
		} catch (Exception e) {
			Debug.out(e);
		}
	
protected static voidsetupResendTimer()

since
3.0.1.5

		if (resendEvent != null) {
			resendEvent.cancel();
			resendEvent = null;
		}
		resendEvent = SimpleTimer.addEvent("resender",
				SystemTime.getOffsetTime(RESEND_DELAY), new TimerEventPerformer() {
					public void perform(TimerEvent event) {
						debug("resend impressions triggered");
						sendUnsentImpressions(5000);
					}
				});
	
public static voidstoreImpresssion(java.lang.String trackingID, long viewedOn, java.lang.String contentHash, java.lang.String torrentHash, java.lang.String adHash, java.lang.String adID, long maxDelayMS)

		// pass in contentHash instead of DownloadManager in case the user removed
		// the DM (and we are retrying)
		try {
			Map ad = new HashMap();

			ad.put("tracking-id", trackingID);
			ad.put("viewed-on", new Long(viewedOn));
			ad.put("content-hash", contentHash);
			if (torrentHash != null) {
				ad.put("torrent-hash", torrentHash);
			}
			if (adHash != null) {
				ad.put("hash", adHash);
			}
			if (adID != null) {
				ad.put("id", adID);
			}

			try {
				mon_unsentImpressions.enter();
				unsentImpressions.add(ad);
			} finally {
				mon_unsentImpressions.exit();
			}
			saveUnsentImpressions();
			sendUnsentImpressions(maxDelayMS);
		} catch (Exception e) {
			Debug.out(e);
		}