FileDocCategorySizeDatePackage
GlobalRatingUtils.javaAPI DocAzureus 3.0.3.46788Thu Mar 01 23:47:30 GMT 2007com.aelitis.azureus.core.torrent

GlobalRatingUtils

public class GlobalRatingUtils extends Object
author
TuxPaper
created
Oct 24, 2006

Fields Summary
private static final int
DEF_EXPIRY_MINS
private static final long
RETRY_UPDATERATING
private static final String
TOR_AZ_PROP_GLOBAL_RATING
private static final String
GLOBAL_RATING_STRING
private static final String
GLOBAL_RATING_COLOR
private static final String
GLOBAL_RATING_COUNT
private static final String
GLOBAL_RATING_REFRESH_ON
Constructors Summary
Methods Summary
public static java.lang.StringgetColor(org.gudy.azureus2.core3.torrent.TOTorrent torrent)

		Map mapContent = getTempGlobalRatingContentMap(torrent);
		Object o = mapContent.get(GLOBAL_RATING_COLOR);
		if (o instanceof String) {
			return (String) o;
		} else if (o instanceof byte[]) {
			return new String((byte[]) o);
		}
		if (o != null) {
			Debug.out(GLOBAL_RATING_COLOR + " is not String - " + o.getClass() + "("
					+ o + ")");
		}
		return null;
	
public static longgetCount(org.gudy.azureus2.core3.torrent.TOTorrent torrent)

		Map mapContent = getTempGlobalRatingContentMap(torrent);
		Long l = (Long) mapContent.get(GLOBAL_RATING_COUNT);
		if (l == null) {
			return 0;
		}
		return l.longValue();
	
public static java.lang.StringgetRatingString(org.gudy.azureus2.core3.torrent.TOTorrent torrent)

param
torrent
return

		Map mapContent = getTempGlobalRatingContentMap(torrent);
		Object o = mapContent.get(GLOBAL_RATING_STRING);
		if (o instanceof String) {
			return (String) o;
		} else if (o instanceof byte[]) {
			return new String((byte[]) o);
		}

		if (o != null) {
			Debug.out(GLOBAL_RATING_STRING + " is not String - " + o.getClass() + "("
					+ o + ")");
		}
		return null;
	
public static longgetRefreshOn(org.gudy.azureus2.core3.torrent.TOTorrent torrent)

		Map mapContent = getTempGlobalRatingContentMap(torrent);
		Long l = (Long) mapContent.get(GLOBAL_RATING_REFRESH_ON);
		if (l == null) {
			return SystemTime.getCurrentTime();
		}
		return l.longValue();
	
private static java.util.MapgetTempGlobalRatingContentMap(org.gudy.azureus2.core3.torrent.TOTorrent torrent)


	     
		Map mapContent = PlatformTorrentUtils.getTempContentMap(torrent);
		Object o = mapContent.get(TOR_AZ_PROP_GLOBAL_RATING);

		if (o instanceof Map) {
			return (Map) o;
		}

		Map map = new HashMap();
		mapContent.put(TOR_AZ_PROP_GLOBAL_RATING, map);
		return map;
	
public static voidsetRating(org.gudy.azureus2.core3.torrent.TOTorrent torrent, java.lang.String rating, java.lang.String color, long count, long refreshOn)

		Map mapContent = getTempGlobalRatingContentMap(torrent);
		if (rating == null) {
			mapContent.remove(GLOBAL_RATING_STRING);
		} else {
			mapContent.put(GLOBAL_RATING_STRING, rating);
		}
		if (color == null) {
			mapContent.remove(GLOBAL_RATING_COLOR);
		} else {
			mapContent.put(GLOBAL_RATING_COLOR, color);
		}

		mapContent.put(GLOBAL_RATING_REFRESH_ON, new Long(refreshOn));
		mapContent.put(GLOBAL_RATING_COUNT, new Long(count));

		try {
			TorrentUtils.writeToFile(torrent);
		} catch (TOTorrentException e) {
			Debug.out(e);
		}

		if (PlatformTorrentUtils.DEBUG_CACHING) {
			PlatformTorrentUtils.log("v3.GR.caching: setRating to " + rating
					+ " for " + torrent + ".  Next refresh in "
					+ (refreshOn - SystemTime.getCurrentTime()));
		}
		SimpleTimer.addEvent("Update G.Rating", refreshOn,
				new TimerEventPerformer() {
					public void perform(TimerEvent event) {
						if (PlatformTorrentUtils.DEBUG_CACHING) {
							PlatformTorrentUtils.log("v3.GR.caching: refresh timer calling updateFromPlatform");
						}
						updateFromPlatform(torrent, 15000);
					}
				});
	
public static voidupdateFromPlatform(org.gudy.azureus2.core3.torrent.TOTorrent torrent, long maxDelayMS)

		try {
			final String hash = torrent.getHashWrapper().toBase32String();
			if (PlatformTorrentUtils.DEBUG_CACHING) {
				PlatformTorrentUtils.log("v3.GR.caching: updateFromPlatform for "
						+ torrent);
			}
			PlatformRatingMessenger.getGlobalRating(new String[] {
				PlatformRatingMessenger.RATE_TYPE_CONTENT
			}, new String[] {
				hash
			}, 5000, new GetRatingReplyListener() {
				public void replyReceived(String replyType,
						PlatformRatingMessenger.GetRatingReply reply) {

					if (PlatformTorrentUtils.DEBUG_CACHING) {
						PlatformTorrentUtils.log("v3.GR.caching: reply '" + replyType
								+ "' for " + torrent);
					}
					if (replyType.equals(PlatformMessenger.REPLY_RESULT)) {
						String type = PlatformRatingMessenger.RATE_TYPE_CONTENT;
						String rating = reply.getRatingString(hash, type);
						String color = reply.getRatingColor(hash, type);
						long count = reply.getRatingCount(hash, type);
						long expireyMins = reply.getRatingExpireyMins(hash, type);

						if (expireyMins <= 0) {
							expireyMins = DEF_EXPIRY_MINS;
						}

						long refreshOn = SystemTime.getCurrentTime()
								+ (expireyMins * 60 * 1000L);

						setRating(torrent, rating, color, count, refreshOn);
					} else if (replyType.equals(PlatformMessenger.REPLY_EXCEPTION)) {
						// try again in a bit
						SimpleTimer.addEvent("Update MD Retry", SystemTime.getCurrentTime()
								+ RETRY_UPDATERATING, new TimerEventPerformer() {
							public void perform(TimerEvent event) {
								if (PlatformTorrentUtils.DEBUG_CACHING) {
									PlatformTorrentUtils.log("v3.GR.caching: retrying..");
								}
								updateFromPlatform(torrent, 15000);
							}
						});

					}

				}

				public void messageSent() {
				}
			});
		} catch (TOTorrentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}