X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FFreenetInterface.java;h=39f342c389a9564d1b0a07feb64f7b7e4537fe32;hb=5e30dc1ca805f1a2d87b6c752da68a57275cb159;hp=e22f407488ee7ca6e468671381031d4ca7132ead;hpb=13b6e1517335f9e1bcf883ddd6f27fde95ce551b;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 e22f407..39f342c 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 @@
/*
- * FreenetSone - FreenetInterface.java - Copyright © 2010 David Roden
+ * Sone - FreenetInterface.java - Copyright © 2010 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
@@ -18,29 +18,43 @@
package net.pterodactylus.sone.core;
import java.net.MalformedURLException;
+import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
+import java.util.List;
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 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;
+import freenet.client.async.BaseClientPutter;
import freenet.client.async.ClientContext;
+import freenet.client.async.ClientPutCallback;
+import freenet.client.async.ClientPutter;
import freenet.client.async.USKCallback;
import freenet.keys.FreenetURI;
+import freenet.keys.InsertableClientSSK;
import freenet.keys.USK;
import freenet.node.Node;
import freenet.node.RequestStarter;
+import freenet.support.api.Bucket;
+import freenet.support.io.ArrayBucket;
/**
* Contains all necessary functionality for interacting with the Freenet node.
@@ -115,6 +129,36 @@ public class FreenetInterface {
}
/**
+ * 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.
+ *
+ * @param temporaryImage
+ * The temporary image data
+ * @param image
+ * The image
+ * @param insertToken
+ * The insert token
+ * @throws SoneException
+ * if the insert could not be started
+ */
+ public void insertImage(TemporaryImage temporaryImage, Image image, InsertToken insertToken) throws SoneException {
+ String filenameHint = image.getId() + "." + temporaryImage.getMimeType().substring(temporaryImage.getMimeType().lastIndexOf("/") + 1);
+ InsertableClientSSK key = InsertableClientSSK.createRandom(node.random, "");
+ FreenetURI targetUri = key.getInsertURI().setDocName(filenameHint);
+ InsertContext insertContext = client.getInsertContext(true);
+ Bucket bucket = new ArrayBucket(temporaryImage.getImageData());
+ ClientMetadata metadata = new ClientMetadata(temporaryImage.getMimeType());
+ InsertBlock insertBlock = new InsertBlock(bucket, metadata, targetUri);
+ try {
+ 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);
+ }
+ }
+
+ /**
* Inserts a directory into Freenet.
*
* @param insertUri
@@ -280,4 +324,148 @@ public class FreenetInterface {
}
+ /**
+ * Insert token that can be used to add {@link ImageInsertListener}s and
+ * cancel a running insert.
+ *
+ * @author David âBombeâ Roden
+ */
+ public class InsertToken implements ClientPutCallback {
+
+ /** The image being inserted. */
+ private final Image image;
+
+ /** The list of registered image insert listeners. */
+ private final List imageInsertListeners = Collections.synchronizedList(new ArrayList());
+
+ /** The client putter. */
+ private ClientPutter clientPutter;
+
+ /** The final URI. */
+ private volatile FreenetURI resultingUri;
+
+ /**
+ * Creates a new insert token for the given image.
+ *
+ * @param image
+ * The image being inserted
+ */
+ public InsertToken(Image image) {
+ this.image = image;
+ }
+
+ //
+ // LISTENER MANAGEMENT
+ //
+
+ /**
+ * Adds the given listener to the list of registered listener.
+ *
+ * @param imageInsertListener
+ * The listener to add
+ */
+ public void addImageInsertListener(ImageInsertListener imageInsertListener) {
+ imageInsertListeners.add(imageInsertListener);
+ }
+
+ /**
+ * Removes the given listener from the list of registered listener.
+ *
+ * @param imageInsertListener
+ * The listener to remove
+ */
+ public void removeImageInsertListener(ImageInsertListener imageInsertListener) {
+ imageInsertListeners.remove(imageInsertListener);
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Sets the client putter that is inserting the image. This will also
+ * signal all registered listeners that the image has started.
+ *
+ * @see ImageInsertListener#imageInsertStarted(Image)
+ * @param clientPutter
+ * The client putter
+ */
+ public void setClientPutter(ClientPutter clientPutter) {
+ this.clientPutter = clientPutter;
+ for (ImageInsertListener imageInsertListener : imageInsertListeners) {
+ imageInsertListener.imageInsertStarted(image);
+ }
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Cancels the running insert.
+ *
+ * @see ImageInsertListener#imageInsertAborted(Image)
+ */
+ @SuppressWarnings("synthetic-access")
+ public void cancel() {
+ clientPutter.cancel(null, node.clientCore.clientContext);
+ for (ImageInsertListener imageInsertListener : imageInsertListeners) {
+ imageInsertListener.imageInsertAborted(image);
+ }
+ }
+
+ //
+ // INTERFACE ClientPutCallback
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onMajorProgress(ObjectContainer objectContainer) {
+ /* ignore, we donât care. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onFailure(InsertException insertException, BaseClientPutter clientPutter, ObjectContainer objectContainer) {
+ for (ImageInsertListener imageInsertListener : imageInsertListeners) {
+ if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
+ imageInsertListener.imageInsertAborted(image);
+ } else {
+ imageInsertListener.imageInsertFailed(image, insertException);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onFetchable(BaseClientPutter clientPutter, ObjectContainer objectContainer) {
+ /* ignore, we donât care. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter, ObjectContainer objectContainer) {
+ resultingUri = generatedUri;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onSuccess(BaseClientPutter clientPutter, ObjectContainer objectContainer) {
+ for (ImageInsertListener imageInsertListener : imageInsertListeners) {
+ imageInsertListener.imageInsertFinished(image, resultingUri);
+ }
+ }
+
+ }
+
}