2bdff65baf57dbfce82137c66b756b57a0f90551
[jSite.git] / src / de / todesbaum / jsite / application / Freenet7Interface.java
1 /*
2  * jSite - 
3  * Copyright (C) 2006 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 package de.todesbaum.jsite.application;
21
22 import java.io.IOException;
23
24 import de.todesbaum.util.freenet.fcp2.Client;
25 import de.todesbaum.util.freenet.fcp2.Connection;
26 import de.todesbaum.util.freenet.fcp2.GenerateSSK;
27 import de.todesbaum.util.freenet.fcp2.Message;
28 import de.todesbaum.util.freenet.fcp2.Node;
29
30 /**
31  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
32  */
33 public class Freenet7Interface {
34
35         private static int counter = 0;
36
37         private Node node;
38         private Connection connection;
39
40         public void setNodeAddress(String hostname) {
41                 node = new Node(hostname);
42                 connection = new Connection(node, "connection-" + counter++);
43         }
44
45         public void setNodeAddress(String hostname, int port) {
46                 node = new Node(hostname, port);
47                 connection = new Connection(node, "connection-" + counter++);
48         }
49         
50         public void setNode(de.todesbaum.jsite.application.Node node) {
51                 if (node != null) {
52                         this.node = new Node(node.getHostname(), node.getPort());
53                         connection = new Connection(node, "connection-" + counter++);
54                 } else {
55                         this.node = null;
56                         connection = null;
57                 }
58         }
59         
60         public void removeNode() {
61                 node = null;
62                 connection = null;
63         }
64
65         /**
66          * @return Returns the node.
67          */
68         public Node getNode() {
69                 return node;
70         }
71
72         /**
73          * @return Returns the connection.
74          */
75         public Connection getConnection(String identifier) {
76                 return new Connection(node, identifier);
77         }
78
79         public boolean isNodePresent() throws IOException {
80                 if (!connection.isConnected()) {
81                         return connection.connect();
82                 }
83                 return true;
84         }
85
86         public String[] generateKeyPair() throws IOException {
87                 if (!isNodePresent()) {
88                         return null;
89                 }
90                 GenerateSSK generateSSK = new GenerateSSK();
91                 Client client = new Client(connection, generateSSK);
92                 Message keypairMessage = client.readMessage();
93                 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
94         }
95
96         /**
97          * @return <code>true</code> if this interface already has a node set,
98          *         <code>false</code> otherwise
99          */
100         public boolean hasNode() {
101                 return (node != null) && (connection != null);
102         }
103
104 }