Mark node as unused for now.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / FreenetInterface.java
1 /*
2  * FreenetSone - FreenetInterface.java - Copyright © 2010 David Roden
3  *
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.pterodactylus.sone.core;
19
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22
23 import net.pterodactylus.util.logging.Logging;
24 import net.pterodactylus.util.service.AbstractService;
25 import freenet.client.FetchException;
26 import freenet.client.FetchResult;
27 import freenet.client.HighLevelSimpleClient;
28 import freenet.keys.FreenetURI;
29 import freenet.node.Node;
30
31 /**
32  * Contains all necessary functionality for interacting with the Freenet node.
33  *
34  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
35  */
36 public class FreenetInterface extends AbstractService {
37
38         /** The logger. */
39         private static final Logger logger = Logging.getLogger(FreenetInterface.class);
40
41         /** The node to interact with. */
42         @SuppressWarnings("unused")
43         private final Node node;
44
45         /** The high-level client to use for requests. */
46         private final HighLevelSimpleClient client;
47
48         /**
49          * Creates a new Freenet interface.
50          *
51          * @param node
52          *            The node to interact with
53          * @param client
54          *            The high-level client
55          */
56         public FreenetInterface(Node node, HighLevelSimpleClient client) {
57                 this.node = node;
58                 this.client = client;
59         }
60
61         //
62         // ACTIONS
63         //
64
65         /**
66          * Fetches the given URI.
67          *
68          * @param uri
69          *            The URI to fetch
70          * @return The result of the fetch, or {@code null} if an error occured
71          */
72         public FetchResult fetchUri(FreenetURI uri) {
73                 logger.entering(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", uri);
74                 FetchResult fetchResult = null;
75                 try {
76                         fetchResult = client.fetch(uri);
77                 } catch (FetchException fe1) {
78                         logger.log(Level.WARNING, "Could not fetch “" + uri + "”!", fe1);
79                 } finally {
80                         logger.exiting(FreenetInterface.class.getName(), "fetchUri(FreenetURI)", fetchResult);
81                 }
82                 return fetchResult;
83         }
84
85         /**
86          * Creates a key pair.
87          *
88          * @return The request key at index 0, the insert key at index 1
89          */
90         public String[] generateKeyPair() {
91                 FreenetURI[] keyPair = client.generateKeyPair("/");
92                 return new String[] { keyPair[1].toString(), keyPair[0].toString() };
93         }
94
95 }