+ this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
+ }
+
+ //
+ // 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 Pair<FreenetURI, FetchResult> fetchUri(FreenetURI uri) {
+ FetchResult fetchResult = null;
+ FreenetURI currentUri = new FreenetURI(uri);
+ while (true) {
+ try {
+ fetchResult = client.fetch(currentUri);
+ return new Pair<FreenetURI, FetchResult>(currentUri, fetchResult);
+ } catch (FetchException fe1) {
+ if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
+ currentUri = fe1.newURI;
+ continue;
+ }
+ logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1);
+ return null;
+ }
+ }
+ }
+
+ /**
+ * 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<String, Object> 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 (edition > sone.getLatestEdition()) {
+ sone.setLatestEdition(edition);
+ new Thread(new Runnable() {
+
+ @Override
+ public void run() {
+ soneDownloader.fetchSone(sone);
+ }
+ }, "Sone Downloader").start();
+ }
+ }
+
+ @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, (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);
+ }
+ }
+
+ /**
+ * 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);
+ }
+ }
+
+ /**
+ * 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, ObjectContainer objectContainer, 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, (HighLevelSimpleClientImpl) client);
+ uriUskCallbacks.put(uri, uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.WARNING, "Could not subscribe to USK: " + uri, uri);
+ }
+ }
+
+ /**
+ * 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, "Could not unregister unknown USK: " + uri);
+ return;
+ }
+ try {
+ node.clientCore.uskManager.unsubscribe(USK.create(uri), uskCallback);
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.INFO, "Could not unregister invalid USK: " + uri);
+ }
+ }
+
+ /**
+ * 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);
+