X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=9515876a868f78ee50c0d64f72b2c49fd1da1277;hb=036c6fa20ce11ce8093f46fc0fce1c1da1839789;hp=39f342c389a9564d1b0a07feb64f7b7e4537fe32;hpb=38cb6c5ec82298ee351d0eb15ddd8331db273af2;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java index 39f342c..9515876 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -1,5 +1,5 @@ /* - * Sone - FreenetInterface.java - Copyright © 2010 David Roden + * Sone - FreenetInterface.java - Copyright © 2010–2012 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 @@ -26,14 +26,13 @@ import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; -import net.pterodactylus.sone.core.SoneException.Type; import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.TemporaryImage; -import net.pterodactylus.util.collection.Pair; import net.pterodactylus.util.logging.Logging; import com.db4o.ObjectContainer; +import com.google.inject.Inject; import freenet.client.ClientMetadata; import freenet.client.FetchException; @@ -84,6 +83,7 @@ public class FreenetInterface { * @param node * The node to interact with */ + @Inject public FreenetInterface(Node node) { this.node = node; this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true); @@ -100,19 +100,19 @@ public class FreenetInterface { * The URI to fetch * @return The result of the fetch, or {@code null} if an error occured */ - public Pair fetchUri(FreenetURI uri) { + public Fetched fetchUri(FreenetURI uri) { FetchResult fetchResult = null; FreenetURI currentUri = new FreenetURI(uri); while (true) { try { fetchResult = client.fetch(currentUri); - return new Pair(currentUri, fetchResult); + return new Fetched(currentUri, fetchResult); } catch (FetchException fe1) { if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) { currentUri = fe1.newURI; continue; } - logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1); + logger.log(Level.WARNING, String.format("Could not fetch “%s”!", uri), fe1); return null; } } @@ -154,7 +154,7 @@ public class FreenetInterface { ClientPutter clientPutter = client.insert(insertBlock, false, null, false, insertContext, insertToken, RequestStarter.INTERACTIVE_PRIORITY_CLASS); insertToken.setClientPutter(clientPutter); } catch (InsertException ie1) { - throw new SoneException(Type.INSERT_FAILED, "Could not start image insert.", ie1); + throw new SoneInsertException("Could not start image insert.", ie1); } } @@ -175,7 +175,7 @@ public class FreenetInterface { try { return client.insertManifest(insertUri, manifestEntries, defaultFile); } catch (InsertException ie1) { - throw new SoneException(null, ie1); + throw new SoneException(ie1); } } @@ -190,13 +190,13 @@ public class FreenetInterface { */ 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" }) }); + logger.log(Level.FINE, String.format("Registering Sone “%s” for USK updates at %s…", 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 known good: %s, new slot too: %s.", new Object[] { sone, key, newKnownGood, newSlotToo }); + logger.log(Level.FINE, String.format("Found USK update for Sone “%s” at %s, new known good: %s, new slot too: %s.", sone, key, newKnownGood, newSlotToo)); if (edition > sone.getLatestEdition()) { sone.setLatestEdition(edition); new Thread(new Runnable() { @@ -222,7 +222,7 @@ 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); } catch (MalformedURLException mue1) { - logger.log(Level.WARNING, "Could not subscribe USK “" + sone.getRequestUri() + "”!", mue1); + logger.log(Level.WARNING, String.format("Could not subscribe USK “%s”!", sone.getRequestUri()), mue1); } } @@ -238,10 +238,10 @@ public class FreenetInterface { return; } try { - logger.log(Level.FINEST, "Unsubscribing from USK for %s…", new Object[] { sone }); + logger.log(Level.FINEST, String.format("Unsubscribing from USK for %s…", sone)); node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback); } catch (MalformedURLException mue1) { - logger.log(Level.FINE, "Could not unsubscribe USK “" + sone.getRequestUri() + "”!", mue1); + logger.log(Level.FINE, String.format("Could not unsubscribe USK “%s”!", sone.getRequestUri()), mue1); } } @@ -277,7 +277,7 @@ public class FreenetInterface { node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, (HighLevelSimpleClientImpl) client); uriUskCallbacks.put(uri, uskCallback); } catch (MalformedURLException mue1) { - logger.log(Level.WARNING, "Could not subscribe to USK: " + uri, uri); + logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1); } } @@ -290,17 +290,67 @@ public class FreenetInterface { public void unregisterUsk(FreenetURI uri) { USKCallback uskCallback = uriUskCallbacks.remove(uri); if (uskCallback == null) { - logger.log(Level.INFO, "Could not unregister unknown USK: " + uri); + logger.log(Level.INFO, String.format("Could not unregister unknown USK: %s", uri)); return; } try { node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback); } catch (MalformedURLException mue1) { - logger.log(Level.INFO, "Could not unregister invalid USK: " + uri); + logger.log(Level.INFO, String.format("Could not unregister invalid USK: %s", uri), mue1); } } /** + * Container for a fetched URI and the {@link FetchResult}. + * + * @author David Roden + */ + 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 David ‘Bombe’ Roden @@ -452,6 +502,14 @@ public class FreenetInterface { * {@inheritDoc} */ @Override + public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter, ObjectContainer objectContainer) { + /* ignore, we don’t care. */ + } + + /** + * {@inheritDoc} + */ + @Override public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter, ObjectContainer objectContainer) { resultingUri = generatedUri; }