+
+ /**
+ * 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 {
+
+ /**
+ * Notifies a listener that a new edition was found for a URI.
+ *
+ * @param uri
+ * The URI that a new edition was found for
+ * @param edition
+ * The found edition
+ * @param newKnownGood
+ * Whether the found edition was actually fetched
+ * @param newSlot
+ * Whether the found edition is higher than all previously
+ * found editions
+ */
+ public void editionFound(FreenetURI uri, long edition, boolean newKnownGood, boolean newSlot);
+
+ }
+
+ /**
+ * Insert token that can cancel a running insert and sends events.
+ *
+ * @see ImageInsertAbortedEvent
+ * @see ImageInsertStartedEvent
+ * @see ImageInsertFailedEvent
+ * @see ImageInsertFinishedEvent
+ * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+ public class InsertToken implements ClientPutCallback {
+
+ /** The image being inserted. */
+ private final Image image;
+
+ /** 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;
+ }
+
+ //
+ // ACCESSORS
+ //
+
+ /**
+ * Sets the client putter that is inserting the image. This will also
+ * signal all registered listeners that the image has started.
+ *
+ * @param clientPutter
+ * The client putter
+ */
+ @SuppressWarnings("synthetic-access")
+ public void setClientPutter(ClientPutter clientPutter) {
+ this.clientPutter = clientPutter;
+ eventBus.post(new ImageInsertStartedEvent(image));
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Cancels the running insert.
+ */
+ @SuppressWarnings("synthetic-access")
+ public void cancel() {
+ clientPutter.cancel(null, node.clientCore.clientContext);
+ eventBus.post(new ImageInsertAbortedEvent(image));
+ }
+
+ //
+ // INTERFACE ClientPutCallback
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onMajorProgress(ObjectContainer objectContainer) {
+ /* ignore, we don’t care. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void onFailure(InsertException insertException, BaseClientPutter clientPutter, ObjectContainer objectContainer) {
+ if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
+ eventBus.post(new ImageInsertAbortedEvent(image));
+ } else {
+ eventBus.post(new ImageInsertFailedEvent(image, insertException));
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onFetchable(BaseClientPutter clientPutter, ObjectContainer objectContainer) {
+ /* ignore, we don’t care. */
+ }
+
+ /**
+ * {@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;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void onSuccess(BaseClientPutter clientPutter, ObjectContainer objectContainer) {
+ eventBus.post(new ImageInsertFinishedEvent(image, resultingUri));
+ }
+
+ }
+
+ public class InsertTokenSupplier implements Function<Image, InsertToken> {
+
+ @Override
+ public InsertToken apply(Image image) {
+ return new InsertToken(image);
+ }
+