*/
public class Core extends AbstractService {
+ /**
+ * Enumeration for the possible states of a {@link Sone}.
+ *
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ public enum SoneStatus {
+
+ /** The Sone is unknown, i.e. not yet downloaded. */
+ unknown,
+
+ /** The Sone is idle, i.e. not being downloaded or inserted. */
+ idle,
+
+ /** The Sone is currently being inserted. */
+ inserting,
+
+ /** The Sone is currently being downloaded. */
+ downloading,
+ }
+
/** The logger. */
private static final Logger logger = Logging.getLogger(Core.class);
/** Sone inserters. */
private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
+ /** The Sones’ statuses. */
+ private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
+
/* various caches follow here. */
/** Cache for all known Sones. */
if (!soneCache.containsKey(soneId)) {
Sone sone = new Sone(soneId);
soneCache.put(soneId, sone);
+ setSoneStatus(sone, SoneStatus.unknown);
}
return soneCache.get(soneId);
}
}
/**
+ * Returns the status of the given Sone.
+ *
+ * @param sone
+ * The Sone to get the status for
+ * @return The status of the Sone
+ */
+ public SoneStatus getSoneStatus(Sone sone) {
+ return soneStatuses.get(sone);
+ }
+
+ /**
+ * Sets the status of the Sone.
+ *
+ * @param sone
+ * The Sone to set the status for
+ * @param soneStatus
+ * The status of the Sone
+ */
+ public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
+ soneStatuses.put(sone, soneStatus);
+ }
+
+ /**
* Creates a new post and adds it to the given Sone.
*
* @param sone
*/
public void addLocalSone(Sone sone) {
if (localSones.add(sone)) {
+ setSoneStatus(sone, SoneStatus.idle);
SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
soneInserter.start();
soneInserters.put(sone, soneInserter);
try {
FreenetURI realRequestUri = new FreenetURI(requestUri).setMetaString(new String[] { "sone.xml" });
FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
+ if (fetchResult == null) {
+ return;
+ }
Sone parsedSone = soneDownloader.parseSone(null, fetchResult, realRequestUri);
if (parsedSone != null) {
if (insertUri != null) {
@SuppressWarnings("synthetic-access")
public void run() {
FreenetURI realRequestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
- FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
- Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
- if (parsedSone != null) {
- addSone(parsedSone);
+ setSoneStatus(sone, SoneStatus.downloading);
+ try {
+ FetchResult fetchResult = freenetInterface.fetchUri(realRequestUri);
+ if (fetchResult == null) {
+ /* TODO - mark Sone as bad. */
+ return;
+ }
+ Sone parsedSone = soneDownloader.parseSone(sone, fetchResult, realRequestUri);
+ if (parsedSone != null) {
+ addSone(parsedSone);
+ }
+ } finally {
+ setSoneStatus(sone, (sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle);
}
}
}, "Sone Downloader").start();
return replies;
}
+ /**
+ * Deletes the given reply. It is removed from its Sone and from the reply
+ * cache.
+ *
+ * @param reply
+ * The reply to remove
+ */
+ public void deleteReply(Reply reply) {
+ reply.getSone().removeReply(reply);
+ replyCache.remove(reply.getId());
+ }
+
//
// SERVICE METHODS
//