Add Sone downloader.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / FreenetInterface.java
index 05b1fba..dae6f68 100644 (file)
 
 package net.pterodactylus.sone.core;
 
+import java.net.MalformedURLException;
 import java.util.HashMap;
+import java.util.Map;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.sone.data.Sone;
 import net.pterodactylus.util.logging.Logging;
 import net.pterodactylus.util.service.AbstractService;
+
+import com.db4o.ObjectContainer;
+
 import freenet.client.FetchException;
 import freenet.client.FetchResult;
 import freenet.client.HighLevelSimpleClient;
+import freenet.client.HighLevelSimpleClientImpl;
 import freenet.client.InsertException;
+import freenet.client.async.ClientContext;
+import freenet.client.async.USKCallback;
 import freenet.keys.FreenetURI;
+import freenet.keys.USK;
 import freenet.node.Node;
+import freenet.node.RequestStarter;
 
 /**
  * Contains all necessary functionality for interacting with the Freenet node.
@@ -41,12 +52,14 @@ public class FreenetInterface extends AbstractService {
        private static final Logger logger = Logging.getLogger(FreenetInterface.class);
 
        /** The node to interact with. */
-       @SuppressWarnings("unused")
        private final Node node;
 
        /** The high-level client to use for requests. */
        private final HighLevelSimpleClient client;
 
+       /** The USK callbacks. */
+       private final Map<String, USKCallback> soneUskCallbacks = new HashMap<String, USKCallback>();
+
        /**
         * Creates a new Freenet interface.
         *
@@ -116,4 +129,63 @@ public class FreenetInterface extends AbstractService {
                }
        }
 
+       /**
+        * Registers the USK for the given Sone and notifies the given
+        * {@link SoneDownloader} if an update was found.
+        *
+        * @param sone
+        *            The Sone to watch
+        * @param soneDownloader
+        *            The Sone download to notify on updates
+        */
+       public void registerUsk(final Sone sone, final SoneDownloader soneDownloader) {
+               try {
+                       logger.log(Level.FINE, "Registering Sone “%s” for USK updates at %s…", new Object[] { sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" }) });
+                       USKCallback uskCallback = new USKCallback() {
+
+                               @Override
+                               @SuppressWarnings("synthetic-access")
+                               public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
+                                       logger.log(Level.FINE, "Found USK update for Sone “%s” at %s.", new Object[] { sone, key });
+                                       if (newKnownGood) {
+                                               sone.updateUris(key.getURI());
+                                       }
+                                       soneDownloader.fetchSone(sone);
+                               }
+
+                               @Override
+                               public short getPollingPriorityProgress() {
+                                       return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
+                               }
+
+                               @Override
+                               public short getPollingPriorityNormal() {
+                                       return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
+                               }
+                       };
+                       soneUskCallbacks.put(sone.getId(), uskCallback);
+                       node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, true, (HighLevelSimpleClientImpl) client);
+               } catch (MalformedURLException mue1) {
+                       logger.log(Level.WARNING, "Could not subscribe USK “" + sone.getRequestUri() + "”!", mue1);
+               }
+       }
+
+       /**
+        * Unsubscribes the request URI of the given Sone.
+        *
+        * @param sone
+        *            The Sone to unregister
+        */
+       public void unregisterUsk(Sone sone) {
+               USKCallback uskCallback = soneUskCallbacks.remove(sone.getId());
+               if (uskCallback == null) {
+                       return;
+               }
+               try {
+                       node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback);
+               } catch (MalformedURLException mue1) {
+                       logger.log(Level.FINE, "Could not unsubscribe USK “" + sone.getRequestUri() + "”!", mue1);
+               }
+       }
+
 }