jSite: First commit : verion 4.0 (written by Bombe)
[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 Roden <droden@gmail.com>
32  * @version $Id: Freenet7Interface.java 418 2006-03-29 17:49:16Z bombe $
33  */
34 public class Freenet7Interface {
35
36         private static int counter = 0;
37
38         private Node node;
39         private Connection connection;
40
41         public void setNodeAddress(String hostname) {
42                 node = new Node(hostname);
43                 connection = new Connection(node, "connection-" + counter++);
44         }
45
46         public void setNodeAddress(String hostname, int port) {
47                 node = new Node(hostname, port);
48                 connection = new Connection(node, "connection-" + counter++);
49         }
50         
51         public void setNode(de.todesbaum.jsite.application.Node node) {
52                 if (node != null) {
53                         this.node = new Node(node.getHostname(), node.getPort());
54                         connection = new Connection(node, "connection-" + counter++);
55                 } else {
56                         this.node = null;
57                         connection = null;
58                 }
59         }
60         
61         public void removeNode() {
62                 node = null;
63                 connection = null;
64         }
65
66         /**
67          * @return Returns the node.
68          */
69         public Node getNode() {
70                 return node;
71         }
72
73         /**
74          * @return Returns the connection.
75          */
76         public Connection getConnection(String identifier) {
77                 return new Connection(node, identifier);
78         }
79
80         public boolean isNodePresent() throws IOException {
81                 if (!connection.isConnected()) {
82                         return connection.connect();
83                 }
84                 return true;
85         }
86
87         public String[] generateKeyPair() throws IOException {
88                 if (!isNodePresent()) {
89                         return null;
90                 }
91                 GenerateSSK generateSSK = new GenerateSSK();
92                 Client client = new Client(connection, generateSSK);
93                 Message keypairMessage = client.readMessage();
94                 return new String[] { keypairMessage.get("InsertURI"), keypairMessage.get("RequestURI") };
95         }
96
97         /**
98          * @return <code>true</code> if this interface already has a node set,
99          *         <code>false</code> otherwise
100          */
101         public boolean hasNode() {
102                 return (node != null) && (connection != null);
103         }
104
105 }