Set status of Sone when downloading, prepare for fetch result being null.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 19 Oct 2010 07:01:37 +0000 (09:01 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Tue, 19 Oct 2010 07:01:37 +0000 (09:01 +0200)
src/main/java/net/pterodactylus/sone/core/Core.java
src/main/java/net/pterodactylus/sone/core/SoneDownloader.java

index 7a0ec9c..76881ef 100644 (file)
@@ -399,6 +399,9 @@ public class Core extends AbstractService {
                                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) {
@@ -428,10 +431,19 @@ public class Core extends AbstractService {
                        @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, SoneStatus.idle);
                                }
                        }
                }, "Sone Downloader").start();
index 04fc37f..07a3755 100644 (file)
@@ -24,6 +24,7 @@ import java.util.Set;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.core.Core.SoneStatus;
 import net.pterodactylus.sone.data.Post;
 import net.pterodactylus.sone.data.Profile;
 import net.pterodactylus.sone.data.Reply;
@@ -111,11 +112,20 @@ public class SoneDownloader extends AbstractService {
        public void fetchSone(Sone sone) {
                logger.log(Level.FINE, "Starting fetch for Sone “%s” from %s…", new Object[] { sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" }) });
                FreenetURI requestUri = sone.getRequestUri().setMetaString(new String[] { "sone.xml" });
-               FetchResult fetchResult = freenetInterface.fetchUri(requestUri);
-               logger.log(Level.FINEST, "Got %d bytes back.", fetchResult.size());
-               Sone parsedSone = parseSone(sone, fetchResult, requestUri);
-               if (parsedSone != null) {
-                       core.addSone(parsedSone);
+               core.setSoneStatus(sone, SoneStatus.downloading);
+               try {
+                       FetchResult fetchResult = freenetInterface.fetchUri(requestUri);
+                       if (fetchResult == null) {
+                               /* TODO - mark Sone as bad. */
+                               return;
+                       }
+                       logger.log(Level.FINEST, "Got %d bytes back.", fetchResult.size());
+                       Sone parsedSone = parseSone(sone, fetchResult, requestUri);
+                       if (parsedSone != null) {
+                               core.addSone(parsedSone);
+                       }
+               } finally {
+                       core.setSoneStatus(sone, SoneStatus.idle);
                }
        }