+ /**
+ * Registers an arbitrary URI and calls the given callback if a new edition
+ * is found.
+ *
+ * @param uri
+ * The URI to watch
+ * @param callback
+ * The callback to call
+ */
+ public void registerUsk(FreenetURI uri, final Callback callback) {
+ USKCallback uskCallback = new USKCallback() {
+
+ @Override
+ public void onFoundEdition(long edition, USK key, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
+ callback.editionFound(key.getURI(), edition, newKnownGood, newSlotToo);
+ }
+
+ @Override
+ public short getPollingPriorityNormal() {
+ return RequestStarter.PREFETCH_PRIORITY_CLASS;
+ }
+
+ @Override
+ public short getPollingPriorityProgress() {
+ return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
+ }
+
+ };
+ try {
+ node.clientCore.uskManager.subscribe(USK.create(uri), uskCallback, true, requestClient);
+ uriUskCallbacks.put(uri, uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.WARNING, String.format("Could not subscribe to USK: %s", uri), mue1);
+ }
+ }
+
+ /**
+ * Unregisters the USK watcher for the given URI.
+ *
+ * @param uri
+ * The URI to unregister the USK watcher for
+ */
+ public void unregisterUsk(FreenetURI uri) {
+ USKCallback uskCallback = uriUskCallbacks.remove(uri);
+ if (uskCallback == null) {
+ 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, String.format("Could not unregister invalid USK: %s", uri), mue1);
+ }
+ }
+
+ /**
+ * Callback for USK watcher events.
+ */
+ 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
+ */
+ public class InsertToken implements ClientPutCallback {
+
+ /** The image being inserted. */
+ private final Image image;
+
+ /** The client putter. */
+ private ClientPutter clientPutter;
+ private Bucket bucket;
+
+ /** 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));
+ }
+
+ public void setBucket(Bucket bucket) {
+ this.bucket = bucket;
+ }
+
+ //
+ // ACTIONS
+ //
+
+ /**
+ * Cancels the running insert.
+ */
+ @SuppressWarnings("synthetic-access")
+ public void cancel() {
+ clientPutter.cancel(node.clientCore.clientContext);
+ eventBus.post(new ImageInsertAbortedEvent(image));
+ bucket.free();
+ }
+
+ //
+ // INTERFACE ClientPutCallback
+ //
+
+ @Override
+ public RequestClient getRequestClient() {
+ return imageInserts;
+ }
+
+ @Override
+ public void onResume(ClientContext context) throws ResumeFailedException {
+ /* ignore. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void onFailure(InsertException insertException, BaseClientPutter clientPutter) {
+ if ((insertException != null) && ("Cancelled by user".equals(insertException.getMessage()))) {
+ eventBus.post(new ImageInsertAbortedEvent(image));
+ } else {
+ eventBus.post(new ImageInsertFailedEvent(image, insertException));
+ }
+ bucket.free();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onFetchable(BaseClientPutter clientPutter) {
+ /* ignore, we donβt care. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onGeneratedMetadata(Bucket metadata, BaseClientPutter clientPutter) {
+ /* ignore, we donβt care. */
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void onGeneratedURI(FreenetURI generatedUri, BaseClientPutter clientPutter) {
+ resultingUri = generatedUri;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void onSuccess(BaseClientPutter clientPutter) {
+ eventBus.post(new ImageInsertFinishedEvent(image, resultingUri));
+ bucket.free();
+ }
+
+ }
+
+ public static class InsertTokenSupplier implements Function<Image, InsertToken> {
+
+ private final FreenetInterface freenetInterface;
+
+ @Inject
+ public InsertTokenSupplier(FreenetInterface freenetInterface) {
+ this.freenetInterface = freenetInterface;
+ }
+
+ @Override
+ public InsertToken apply(Image image) {
+ return freenetInterface.new InsertToken(image);
+ }
+
+ }
+