Throw an exception when the node is not connected.
[jSite.git] / src / de / todesbaum / jsite / application / Freenet7Interface.java
index 7aa2047..ae5c967 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * jSite - 
+ * jSite -
  * Copyright (C) 2006 David Roden
  *
  * This program is free software; you can redistribute it and/or modify
@@ -29,11 +29,14 @@ import de.todesbaum.util.freenet.fcp2.Node;
 
 /**
  * Interface for freenet-related operations.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class Freenet7Interface {
 
+       /** Random number to differentiate several jSites. */
+       private static final int number = (int) (Math.random() * Integer.MAX_VALUE);
+
        /** Counter. */
        private static int counter = 0;
 
@@ -46,18 +49,18 @@ public class Freenet7Interface {
        /**
         * Sets the hostname of the node. The default port for FCP2 connections ({@link Node#DEFAULT_PORT})
         * is used.
-        * 
+        *
         * @param hostname
         *            The hostname of the node
         */
        public void setNodeAddress(String hostname) {
                node = new Node(hostname);
-               connection = new Connection(node, "connection-" + counter++);
+               connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
        }
 
        /**
         * Sets the hostname and the port of the node.
-        * 
+        *
         * @param hostname
         *            The hostname of the node
         * @param port
@@ -65,19 +68,19 @@ public class Freenet7Interface {
         */
        public void setNodeAddress(String hostname, int port) {
                node = new Node(hostname, port);
-               connection = new Connection(node, "connection-" + counter++);
+               connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
        }
 
        /**
         * Sets hostname and port from the given node.
-        * 
+        *
         * @param node
         *            The node to get the hostname and port from
         */
        public void setNode(de.todesbaum.jsite.application.Node node) {
                if (node != null) {
                        this.node = new Node(node.getHostname(), node.getPort());
-                       connection = new Connection(node, "connection-" + counter++);
+                       connection = new Connection(node, "jSite-" + number + "-connection-" + counter++);
                } else {
                        this.node = null;
                        connection = null;
@@ -94,7 +97,7 @@ public class Freenet7Interface {
 
        /**
         * Returns the node this interface is connecting to.
-        * 
+        *
         * @return The node
         */
        public Node getNode() {
@@ -103,7 +106,7 @@ public class Freenet7Interface {
 
        /**
         * Creates a new connection to the current node with the given identifier.
-        * 
+        *
         * @param identifier
         *            The identifier of the connection
         * @return The connection to the node
@@ -115,7 +118,7 @@ public class Freenet7Interface {
        /**
         * Checks whether the current node is connected. If the node is not
         * connected, a connection will be tried.
-        * 
+        *
         * @return <code>true</code> if the node is connected, <code>false</code>
         *         otherwise
         * @throws IOException
@@ -130,7 +133,7 @@ public class Freenet7Interface {
 
        /**
         * Generates an SSK key pair.
-        * 
+        *
         * @return An array of strings, the first one being the generated private
         *         (insert) URI and the second one being the generated public
         *         (request) URI
@@ -139,7 +142,7 @@ public class Freenet7Interface {
         */
        public String[] generateKeyPair() throws IOException {
                if (!isNodePresent()) {
-                       return null;
+                       throw new IOException("Node is offline.");
                }
                GenerateSSK generateSSK = new GenerateSSK();
                Client client = new Client(connection, generateSSK);
@@ -149,7 +152,7 @@ public class Freenet7Interface {
 
        /**
         * Checks whether the interface has already been configured with a node.
-        * 
+        *
         * @return <code>true</code> if this interface already has a node set,
         *         <code>false</code> otherwise
         */