Move dependency injection configuration closer to where it’s useful.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / FreenetInterface.java
index d525fc7..d4640f8 100644 (file)
@@ -21,6 +21,7 @@ import java.net.MalformedURLException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
@@ -34,14 +35,16 @@ import net.pterodactylus.sone.data.TemporaryImage;
 import net.pterodactylus.util.logging.Logging;
 
 import com.db4o.ObjectContainer;
+
+import com.google.common.base.Function;
 import com.google.common.eventbus.EventBus;
 import com.google.inject.Inject;
+import com.google.inject.Singleton;
 
 import freenet.client.ClientMetadata;
 import freenet.client.FetchException;
 import freenet.client.FetchResult;
 import freenet.client.HighLevelSimpleClient;
-import freenet.client.HighLevelSimpleClientImpl;
 import freenet.client.InsertBlock;
 import freenet.client.InsertContext;
 import freenet.client.InsertException;
@@ -54,6 +57,7 @@ import freenet.keys.FreenetURI;
 import freenet.keys.InsertableClientSSK;
 import freenet.keys.USK;
 import freenet.node.Node;
+import freenet.node.RequestClient;
 import freenet.node.RequestStarter;
 import freenet.support.api.Bucket;
 import freenet.support.io.ArrayBucket;
@@ -63,6 +67,7 @@ import freenet.support.io.ArrayBucket;
  *
  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
  */
+@Singleton
 public class FreenetInterface {
 
        /** The logger. */
@@ -110,11 +115,10 @@ public class FreenetInterface {
         * @return The result of the fetch, or {@code null} if an error occured
         */
        public Fetched fetchUri(FreenetURI uri) {
-               FetchResult fetchResult = null;
                FreenetURI currentUri = new FreenetURI(uri);
                while (true) {
                        try {
-                               fetchResult = client.fetch(currentUri);
+                               FetchResult fetchResult = client.fetch(currentUri);
                                return new Fetched(currentUri, fetchResult);
                        } catch (FetchException fe1) {
                                if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
@@ -128,16 +132,6 @@ public class FreenetInterface {
        }
 
        /**
-        * Creates a key pair.
-        *
-        * @return The request key at index 0, the insert key at index 1
-        */
-       public String[] generateKeyPair() {
-               FreenetURI[] keyPair = client.generateKeyPair("");
-               return new String[] { keyPair[1].toString(), keyPair[0].toString() };
-       }
-
-       /**
         * Inserts the image data of the given {@link TemporaryImage} and returns
         * the given insert token that can be used to add listeners or cancel the
         * insert.
@@ -229,7 +223,8 @@ public class FreenetInterface {
                                }
                        };
                        soneUskCallbacks.put(sone.getId(), uskCallback);
-                       node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, (System.currentTimeMillis() - sone.getTime()) < 7 * 24 * 60 * 60 * 1000, (HighLevelSimpleClientImpl) client);
+                       boolean runBackgroundFetch = (System.currentTimeMillis() - sone.getTime()) < TimeUnit.DAYS.toMillis(7);
+                       node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, runBackgroundFetch, (RequestClient) client);
                } catch (MalformedURLException mue1) {
                        logger.log(Level.WARNING, String.format("Could not subscribe USK “%s”!", sone.getRequestUri()), mue1);
                }
@@ -283,7 +278,7 @@ public class FreenetInterface {
 
                };
                try {
-                       node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (HighLevelSimpleClientImpl) client);
+                       node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (RequestClient) client);
                        uriUskCallbacks.put(uri, uskCallback);
                } catch (MalformedURLException mue1) {
                        logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
@@ -503,4 +498,13 @@ public class FreenetInterface {
 
        }
 
+       public class InsertTokenSupplier implements Function<Image, InsertToken> {
+
+               @Override
+               public InsertToken apply(Image image) {
+                       return new InsertToken(image);
+               }
+
+       }
+
 }