X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=d823cc1ffb5cddd36b91cfa18c32c62664973115;hb=aa6e20dc73835b02bc434ca064b36838f19a67db;hp=490c2535c301da39da6721a714fb8653171f05c9;hpb=0ec4b5ec7155f86e0c96d290a246364bf3675d88;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 490c253..d823cc1 100644 --- a/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java +++ b/src/main/java/net/pterodactylus/sone/core/FreenetInterface.java @@ -17,9 +17,29 @@ 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. @@ -28,21 +48,145 @@ import freenet.node.Node; */ public class FreenetInterface extends AbstractService { + /** The logger. */ + private static final Logger logger = Logging.getLogger(FreenetInterface.class); + /** The node to interact with. */ private final Node node; /** The high-level client to use for requests. */ private final HighLevelSimpleClient client; + /** The USK callbacks. */ + private final Map soneUskCallbacks = new HashMap(); + /** * Creates a new Freenet interface. * * @param node * The node to interact with + * @param client + * The high-level client */ public FreenetInterface(Node node, HighLevelSimpleClient client) { + super("Sone Freenet Interface"); this.node = node; this.client = client; } + // + // ACTIONS + // + + /** + * Fetches the given URI. + * + * @param uri + * The URI to fetch + * @return The result of the fetch, or {@code null} if an error occured + */ + public FetchResult fetchUri(FreenetURI uri) { + logger.entering(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", uri); + FetchResult fetchResult = null; + try { + fetchResult = client.fetch(uri); + } catch (FetchException fe1) { + logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1); + } finally { + logger.exiting(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", fetchResult); + } + return fetchResult; + } + + /** + * 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 a directory into Freenet. + * + * @param insertUri + * The insert URI + * @param manifestEntries + * The directory entries + * @param defaultFile + * The name of the default file + * @return The generated URI + * @throws SoneException + * if an insert error occurs + */ + public FreenetURI insertDirectory(FreenetURI insertUri, HashMap manifestEntries, String defaultFile) throws SoneException { + try { + return client.insertManifest(insertUri, manifestEntries, defaultFile); + } catch (InsertException ie1) { + throw new SoneException(null, ie1); + } + } + + /** + * 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 known good: %s, new slot too: %s.", new Object[] { sone, key, newKnownGood, newSlotToo }); + 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 { + logger.log(Level.FINEST, "Unsubscribing from USK for %s…", new Object[] { sone }); + node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback); + } catch (MalformedURLException mue1) { + logger.log(Level.FINE, "Could not unsubscribe USK “" + sone.getRequestUri() + "”!", mue1); + } + } + }