📄 Update year in file headers
[Sone.git] / src / main / java / net / pterodactylus / sone / core / FreenetInterface.java
index 7d1b007..a01a2bb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Sone - FreenetInterface.java - Copyright © 2010–2016 David Roden
+ * Sone - FreenetInterface.java - Copyright © 2010–2020 David Roden
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -21,7 +21,6 @@ import static freenet.keys.USK.create;
 import static java.lang.String.format;
 import static java.util.logging.Level.WARNING;
 import static java.util.logging.Logger.getLogger;
-import static net.pterodactylus.sone.freenet.Key.routingKey;
 
 import java.io.IOException;
 import java.net.MalformedURLException;
@@ -75,11 +74,10 @@ import freenet.support.api.Bucket;
 import freenet.support.api.RandomAccessBucket;
 import freenet.support.io.ArrayBucket;
 import freenet.support.io.ResumeFailedException;
+import net.pterodactylus.sone.freenet.*;
 
 /**
  * Contains all necessary functionality for interacting with the Freenet node.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
 @Singleton
 public class FreenetInterface {
@@ -95,9 +93,10 @@ public class FreenetInterface {
 
        /** The high-level client to use for requests. */
        private final HighLevelSimpleClient client;
+       private final RequestClient requestClient = new RequestClientBuilder().realTime().build();
 
        /** The USK callbacks. */
-       private final Map<String, USKCallback> soneUskCallbacks = new HashMap<String, USKCallback>();
+       private final Map<String, USKCallback> soneUskCallbacks = new HashMap<>();
 
        /** The not-Sone-related USK callbacks. */
        private final Map<FreenetURI, USKCallback> uriUskCallbacks = Collections.synchronizedMap(new HashMap<FreenetURI, USKCallback>());
@@ -256,9 +255,9 @@ public class FreenetInterface {
        public void registerActiveUsk(FreenetURI requestUri,
                        USKCallback uskCallback) {
                try {
-                       soneUskCallbacks.put(routingKey(requestUri), uskCallback);
+                       soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback);
                        node.clientCore.uskManager.subscribe(create(requestUri),
-                                       uskCallback, true, (RequestClient) client);
+                                       uskCallback, true, requestClient);
                } catch (MalformedURLException mue1) {
                        logger.log(WARNING, format("Could not subscribe USK “%s”!",
                                        requestUri), mue1);
@@ -268,11 +267,10 @@ public class FreenetInterface {
        public void registerPassiveUsk(FreenetURI requestUri,
                        USKCallback uskCallback) {
                try {
-                       soneUskCallbacks.put(routingKey(requestUri), uskCallback);
+                       soneUskCallbacks.put(FreenetURIsKt.getRoutingKeyString(requestUri), uskCallback);
                        node.clientCore
                                        .uskManager
-                                       .subscribe(create(requestUri), uskCallback, false,
-                                                       (RequestClient) client);
+                                       .subscribe(create(requestUri), uskCallback, false, requestClient);
                } catch (MalformedURLException mue1) {
                        logger.log(WARNING,
                                        format("Could not subscribe USK “%s”!", requestUri),
@@ -328,8 +326,8 @@ public class FreenetInterface {
 
                };
                try {
-                       node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (RequestClient) client);
-                       uriUskCallbacks.put(uri, uskCallback);
+                       node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient);
+                       uriUskCallbacks.put(USK.create(uri).clearCopy().getURI(), uskCallback);
                } catch (MalformedURLException mue1) {
                        logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
                }
@@ -342,12 +340,12 @@ public class FreenetInterface {
         *            The URI to unregister the USK watcher for
         */
        public void unregisterUsk(FreenetURI uri) {
-               USKCallback uskCallback = uriUskCallbacks.remove(uri);
-               if (uskCallback == null) {
-                       logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri));
-                       return;
-               }
                try {
+                       USKCallback uskCallback = uriUskCallbacks.remove(USK.create(uri).clearCopy().getURI());
+                       if (uskCallback == null) {
+                               logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri));
+                               return;
+                       }
                        node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback);
                } catch (MalformedURLException mue1) {
                        logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1);
@@ -355,59 +353,7 @@ public class FreenetInterface {
        }
 
        /**
-        * Container for a fetched URI and the {@link FetchResult}.
-        *
-        * @author <a href="mailto:d.roden@xplosion.de">David Roden</a>
-        */
-       public static class Fetched {
-
-               /** The fetched URI. */
-               private final FreenetURI freenetUri;
-
-               /** The fetch result. */
-               private final FetchResult fetchResult;
-
-               /**
-                * Creates a new fetched URI.
-                *
-                * @param freenetUri
-                *            The URI that was fetched
-                * @param fetchResult
-                *            The fetch result
-                */
-               public Fetched(FreenetURI freenetUri, FetchResult fetchResult) {
-                       this.freenetUri = freenetUri;
-                       this.fetchResult = fetchResult;
-               }
-
-               //
-               // ACCESSORS
-               //
-
-               /**
-                * Returns the fetched URI.
-                *
-                * @return The fetched URI
-                */
-               public FreenetURI getFreenetUri() {
-                       return freenetUri;
-               }
-
-               /**
-                * Returns the fetch result.
-                *
-                * @return The fetch result
-                */
-               public FetchResult getFetchResult() {
-                       return fetchResult;
-               }
-
-       }
-
-       /**
         * Callback for USK watcher events.
-        *
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
        public static interface Callback {
 
@@ -435,7 +381,6 @@ public class FreenetInterface {
         * @see ImageInsertStartedEvent
         * @see ImageInsertFailedEvent
         * @see ImageInsertFinishedEvent
-        * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
         */
        public class InsertToken implements ClientPutCallback {