Author: toad
Date: 2008-05-20 15:32:20 +0000 (Tue, 20 May 2008)
New Revision: 19969

Added:
   branches/db4o/freenet/src/freenet/node/HandlePortTuple.java
Modified:
   branches/db4o/freenet/src/freenet/node/Node.java
   branches/db4o/freenet/src/freenet/node/NodeCrypto.java
Log:
Create, store, and retrieve, a handle (random long value) which will be used to 
find all other values related to a specific node.

Added: branches/db4o/freenet/src/freenet/node/HandlePortTuple.java
===================================================================
--- branches/db4o/freenet/src/freenet/node/HandlePortTuple.java                 
        (rev 0)
+++ branches/db4o/freenet/src/freenet/node/HandlePortTuple.java 2008-05-20 
15:32:20 UTC (rev 19969)
@@ -0,0 +1,10 @@
+/* This code is part of Freenet. It is distributed under the GNU General
+ * Public License, version 2 (or at your option any later version). See
+ * http://www.gnu.org/ for further details of the GPL. */
+package freenet.node;
+
+/** Used to associate a port with a node database handle */
+class HandlePortTuple {
+       long handle;
+       int portNumber;
+}

Modified: branches/db4o/freenet/src/freenet/node/Node.java
===================================================================
--- branches/db4o/freenet/src/freenet/node/Node.java    2008-05-20 14:56:44 UTC 
(rev 19968)
+++ branches/db4o/freenet/src/freenet/node/Node.java    2008-05-20 15:32:20 UTC 
(rev 19969)
@@ -230,6 +230,10 @@
         * ObjectContainer's from it. Be careful to refresh objects on any 
         * long-lived container! */
        public final ObjectServer dbServer;
+       /** A fixed random number which identifies the top-level objects 
belonging to
+        * this node, as opposed to any others that might be stored in the same 
database
+        * (e.g. because of many-nodes-in-one-VM). */
+       public final long nodeDBHandle;

        /** Stats */
        public final NodeStats nodeStats;
@@ -889,6 +893,13 @@
                sortOrder += NodeCryptoConfig.OPTION_COUNT;

                darknetCrypto = new NodeCrypto(this, false, darknetConfig, 
startupTime, enableARKs);
+               
+               ObjectContainer setupContainer = dbServer.openClient();
+               
+               nodeDBHandle = darknetCrypto.getNodeHandle(setupContainer);
+               
+               setupContainer.commit();
+               setupContainer = null; // Don't reuse.

                // Must be created after darknetCrypto
                dnsr = new DNSRequester(this);

Modified: branches/db4o/freenet/src/freenet/node/NodeCrypto.java
===================================================================
--- branches/db4o/freenet/src/freenet/node/NodeCrypto.java      2008-05-20 
14:56:44 UTC (rev 19968)
+++ branches/db4o/freenet/src/freenet/node/NodeCrypto.java      2008-05-20 
15:32:20 UTC (rev 19969)
@@ -12,6 +12,10 @@
 import java.util.Vector;
 import java.util.zip.DeflaterOutputStream;

+import com.db4o.ObjectContainer;
+import com.db4o.ObjectSet;
+import com.db4o.query.Predicate;
+
 import net.i2p.util.NativeBigInteger;
 import freenet.crypt.BlockCipher;
 import freenet.crypt.DSA;
@@ -512,4 +516,36 @@
        public FreenetInetAddress getBindTo() {
                return config.getBindTo();
        }
+
+       public long getNodeHandle(ObjectContainer setupContainer) {
+               // Ignore warnings, this is db4o magic.
+               ObjectSet result = setupContainer.query(new Predicate() {
+                       public boolean match(HandlePortTuple tuple) {
+                               return tuple.portNumber == portNumber;
+                       }
+               });
+               long handle;
+               if(result.hasNext()) {
+                       handle = ((HandlePortTuple)result.next()).handle;
+                       System.err.println("Retrieved database handle for node 
on port "+portNumber+": "+handle);
+                       return handle;
+               } else {
+                       while(true) {
+                               handle = random.nextLong();
+                               HandlePortTuple tuple = new HandlePortTuple();
+                               tuple.handle = handle;
+                               // Double-check with QBE, just in case the RNG 
is broken (similar things have happened before!)
+                               ObjectSet os = setupContainer.get(tuple);
+                               if(os.hasNext()) {
+                                       System.err.println("Generating database 
handle for node: already taken: "+handle);
+                                       continue;
+                               }
+                               tuple.portNumber = portNumber;
+                               setupContainer.set(tuple);
+                               System.err.println("Generated and stored 
database handle for node on port "+portNumber+": "+handle);
+                               return handle;
+                       }
+               }
+       }
 }
+


Reply via email to