2 * FreenetSone - FreenetInterface.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.net.MalformedURLException;
21 import java.util.HashMap;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
26 import net.pterodactylus.sone.data.Sone;
27 import net.pterodactylus.util.collection.Pair;
28 import net.pterodactylus.util.logging.Logging;
30 import com.db4o.ObjectContainer;
32 import freenet.client.FetchException;
33 import freenet.client.FetchResult;
34 import freenet.client.HighLevelSimpleClient;
35 import freenet.client.HighLevelSimpleClientImpl;
36 import freenet.client.InsertException;
37 import freenet.client.async.ClientContext;
38 import freenet.client.async.USKCallback;
39 import freenet.keys.FreenetURI;
40 import freenet.keys.USK;
41 import freenet.node.Node;
42 import freenet.node.RequestStarter;
45 * Contains all necessary functionality for interacting with the Freenet node.
47 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
49 public class FreenetInterface {
52 private static final Logger logger = Logging.getLogger(FreenetInterface.class);
54 /** The node to interact with. */
55 private final Node node;
57 /** The high-level client to use for requests. */
58 private final HighLevelSimpleClient client;
60 /** The USK callbacks. */
61 private final Map<String, USKCallback> soneUskCallbacks = new HashMap<String, USKCallback>();
64 * Creates a new Freenet interface.
67 * The node to interact with
69 public FreenetInterface(Node node) {
71 this.client = node.clientCore.makeClient(RequestStarter.INTERACTIVE_PRIORITY_CLASS, false, true);
79 * Fetches the given URI.
83 * @return The result of the fetch, or {@code null} if an error occured
85 public Pair<FreenetURI, FetchResult> fetchUri(FreenetURI uri) {
86 FetchResult fetchResult = null;
87 FreenetURI currentUri = new FreenetURI(uri);
90 fetchResult = client.fetch(currentUri);
91 return new Pair<FreenetURI, FetchResult>(currentUri, fetchResult);
92 } catch (FetchException fe1) {
93 if (fe1.getMode() == FetchException.PERMANENT_REDIRECT) {
94 currentUri = fe1.newURI;
97 logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1);
104 * Creates a key pair.
106 * @return The request key at index 0, the insert key at index 1
108 public String[] generateKeyPair() {
109 FreenetURI[] keyPair = client.generateKeyPair("");
110 return new String[] { keyPair[1].toString(), keyPair[0].toString() };
114 * Inserts a directory into Freenet.
118 * @param manifestEntries
119 * The directory entries
121 * The name of the default file
122 * @return The generated URI
123 * @throws SoneException
124 * if an insert error occurs
126 public FreenetURI insertDirectory(FreenetURI insertUri, HashMap<String, Object> manifestEntries, String defaultFile) throws SoneException {
128 return client.insertManifest(insertUri, manifestEntries, defaultFile);
129 } catch (InsertException ie1) {
130 throw new SoneException(null, ie1);
135 * Registers the USK for the given Sone and notifies the given
136 * {@link SoneDownloader} if an update was found.
140 * @param soneDownloader
141 * The Sone download to notify on updates
143 public void registerUsk(final Sone sone, final SoneDownloader soneDownloader) {
145 logger.log(Level.FINE, "Registering Sone “%s” for USK updates at %s…", new Object[] { sone, sone.getRequestUri().setMetaString(new String[] { "sone.xml" }) });
146 USKCallback uskCallback = new USKCallback() {
149 @SuppressWarnings("synthetic-access")
150 public void onFoundEdition(long edition, USK key, ObjectContainer objectContainer, ClientContext clientContext, boolean metadata, short codec, byte[] data, boolean newKnownGood, boolean newSlotToo) {
151 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 });
153 sone.setLatestEdition(key.suggestedEdition);
154 new Thread(new Runnable() {
158 soneDownloader.fetchSone(sone);
160 }, "Sone Downloader").start();
165 public short getPollingPriorityProgress() {
166 return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
170 public short getPollingPriorityNormal() {
171 return RequestStarter.INTERACTIVE_PRIORITY_CLASS;
174 soneUskCallbacks.put(sone.getId(), uskCallback);
175 node.clientCore.uskManager.subscribe(USK.create(sone.getRequestUri()), uskCallback, true, (HighLevelSimpleClientImpl) client);
176 } catch (MalformedURLException mue1) {
177 logger.log(Level.WARNING, "Could not subscribe USK “" + sone.getRequestUri() + "”!", mue1);
182 * Unsubscribes the request URI of the given Sone.
185 * The Sone to unregister
187 public void unregisterUsk(Sone sone) {
188 USKCallback uskCallback = soneUskCallbacks.remove(sone.getId());
189 if (uskCallback == null) {
193 logger.log(Level.FINEST, "Unsubscribing from USK for %s…", new Object[] { sone });
194 node.clientCore.uskManager.unsubscribe(USK.create(sone.getRequestUri()), uskCallback);
195 } catch (MalformedURLException mue1) {
196 logger.log(Level.FINE, "Could not unsubscribe USK “" + sone.getRequestUri() + "”!", mue1);