Author: toad
Date: 2006-02-21 16:44:40 +0000 (Tue, 21 Feb 2006)
New Revision: 8083

Modified:
   trunk/freenet/src/freenet/config/FilePersistentConfig.java
   trunk/freenet/src/freenet/node/FNPPacketMangler.java
   trunk/freenet/src/freenet/node/KeyTracker.java
   trunk/freenet/src/freenet/node/NodeDispatcher.java
   trunk/freenet/src/freenet/node/PeerManager.java
   trunk/freenet/src/freenet/node/PeerNode.java
   trunk/freenet/src/freenet/node/TextModeClientInterface.java
   trunk/freenet/src/freenet/node/Version.java
   trunk/freenet/src/freenet/support/SimpleFieldSet.java
Log:
453:
We are now able to add node references without physical.udp.
(We can pick it up when they connect to us; this is for one-side-NATted 
support).
Can remove a node by its name as well as by its IP:port.
Eliminate errors on startup in FilePersistentConfig caused by us not removing 
consumed options.
Buffer initial load of config file.
Delete minor dead code.
Debug SimpleFieldSet.keyIterator().
Synchronization in FilePersistentConfig.


Modified: trunk/freenet/src/freenet/config/FilePersistentConfig.java
===================================================================
--- trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-02-21 
15:47:50 UTC (rev 8082)
+++ trunk/freenet/src/freenet/config/FilePersistentConfig.java  2006-02-21 
16:44:40 UTC (rev 8083)
@@ -1,5 +1,6 @@
 package freenet.config;

+import java.io.BufferedInputStream;
 import java.io.BufferedWriter;
 import java.io.File;
 import java.io.FileInputStream;
@@ -54,8 +55,9 @@
         * @throws IOException */
        private void initialLoad() throws IOException {
                FileInputStream fis = new FileInputStream(filename);
+               BufferedInputStream bis = new BufferedInputStream(fis);
                try {
-                       LineReadingInputStream lis = new 
LineReadingInputStream(fis);
+                       LineReadingInputStream lis = new 
LineReadingInputStream(bis);
                        origConfigFileContents = new SimpleFieldSet(lis, 4096, 
256, true);
                } finally {
                        try {
@@ -69,24 +71,17 @@

        public void register(SubConfig sc) {
                super.register(sc);
-               if(origConfigFileContents != null) {
-                       SimpleFieldSet sfs = 
origConfigFileContents.subset(sc.prefix);
-                       Logger.minor(this, "Registering "+sc+": "+sfs);
-                       // Set all the options
-                       if(sfs != null)
-                               sc.setOptions(sfs);
-               }
        }

        /**
         * Finished initialization. So any remaining options must be invalid.
         */
-       public void finishedInit() {
+       public synchronized void finishedInit() {
                if(origConfigFileContents == null) return;
                Iterator i = origConfigFileContents.keyIterator();
                while(i.hasNext()) {
                        String key = (String) i.next();
-                       Logger.error(this, "Unknown option: "+key+" 
(value="+origConfigFileContents.get(key));
+                       Logger.error(this, "Unknown option: "+key+" 
(value="+origConfigFileContents.get(key)+")");
                }
        }

@@ -106,12 +101,14 @@
                Logger.minor(this, "fs = "+fs);
                FileOutputStream fos = new FileOutputStream(tempFilename);
                BufferedWriter bw = new BufferedWriter(new 
OutputStreamWriter(fos));
-               fs.writeTo(bw);
+               synchronized(this) {
+                       fs.writeTo(bw);
+               }
                bw.close();
                tempFilename.renameTo(filename);
        }

-       private SimpleFieldSet exportFieldSet() {
+       private synchronized SimpleFieldSet exportFieldSet() {
                SimpleFieldSet fs = new SimpleFieldSet(true);
                SubConfig[] configs;
                synchronized(this) {
@@ -125,10 +122,14 @@
        }

        public void onRegister(SubConfig config, Option o) {
-               if(origConfigFileContents == null) return;
-               String name = 
config.prefix+SimpleFieldSet.MULTI_LEVEL_CHAR+o.name;
-               String val = origConfigFileContents.get(name);
-               if(val == null) return;
+               String val, name;
+               synchronized(this) {
+                       if(origConfigFileContents == null) return;
+                       name = 
config.prefix+SimpleFieldSet.MULTI_LEVEL_CHAR+o.name;
+                       val = origConfigFileContents.get(name);
+                       origConfigFileContents.remove(name);
+                       if(val == null) return;
+               }
                try {
                        o.setInitialValue(val);
                } catch (InvalidConfigValueException e) {

Modified: trunk/freenet/src/freenet/node/FNPPacketMangler.java
===================================================================
--- trunk/freenet/src/freenet/node/FNPPacketMangler.java        2006-02-21 
15:47:50 UTC (rev 8082)
+++ trunk/freenet/src/freenet/node/FNPPacketMangler.java        2006-02-21 
16:44:40 UTC (rev 8083)
@@ -1262,6 +1262,7 @@

         Logger.minor(this,"Sending packet of length "+output.length+" to 
"+kt.pn);

+        // pn.getPeer() cannot be null
         usm.sendPacket(output, kt.pn.getPeer());
         kt.pn.sentPacket();
     }

Modified: trunk/freenet/src/freenet/node/KeyTracker.java
===================================================================
--- trunk/freenet/src/freenet/node/KeyTracker.java      2006-02-21 15:47:50 UTC 
(rev 8082)
+++ trunk/freenet/src/freenet/node/KeyTracker.java      2006-02-21 16:44:40 UTC 
(rev 8083)
@@ -483,6 +483,7 @@
     }

     private PacketThrottle getThrottle() {
+       // pn.getPeer() cannot be null as it has already connected.
        return PacketThrottle.getThrottle(pn.getPeer(), Node.PACKET_SIZE);
        }


Modified: trunk/freenet/src/freenet/node/NodeDispatcher.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeDispatcher.java  2006-02-21 15:47:50 UTC 
(rev 8082)
+++ trunk/freenet/src/freenet/node/NodeDispatcher.java  2006-02-21 16:44:40 UTC 
(rev 8083)
@@ -308,6 +308,7 @@
             PeerNode next = node.peers.closerPeer(pn, ctx.routedTo, 
ctx.notIgnored, target, true);
             Logger.minor(this, "Next: "+next+" message: "+m);
             if(next != null) {
+               // next is connected, or at least has been => next.getPeer() 
CANNOT be null.
                 Logger.minor(this, "Forwarding "+m.getSpec()+" to 
"+next.getPeer().getPort());
                 ctx.addSent(next);
                 try {

Modified: trunk/freenet/src/freenet/node/PeerManager.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerManager.java     2006-02-21 15:47:50 UTC 
(rev 8082)
+++ trunk/freenet/src/freenet/node/PeerManager.java     2006-02-21 16:44:40 UTC 
(rev 8083)
@@ -191,7 +191,7 @@
      */
     public PeerNode getByPeer(Peer peer) {
         for(int i=0;i<myPeers.length;i++) {
-            if(myPeers[i].getPeer().equals(peer))
+            if(peer.equals(myPeers[i].getPeer()))
                 return myPeers[i];
         }
         return null;

Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java        2006-02-21 15:47:50 UTC 
(rev 8082)
+++ trunk/freenet/src/freenet/node/PeerNode.java        2006-02-21 16:44:40 UTC 
(rev 8083)
@@ -210,7 +210,8 @@
                String physical[]=fs.getAll("physical.udp");
                if(physical==null){
                        Peer p = new Peer(fs.get("physical.udp"));
-                       nominalPeer.addElement(p);
+                       if(p != null)
+                               nominalPeer.addElement(p);
                }else{
                        for(int i=0;i<physical.length;i++){             
                                        Peer p = new Peer(physical[i]);
@@ -221,8 +222,11 @@
         } catch (Exception e1) {
                 throw new FSParseException(e1);
         }
-        if(nominalPeer.isEmpty()) throw new FSParseException("No 
physical.udp");
-        detectedPeer=(Peer) nominalPeer.firstElement();
+        if(nominalPeer.isEmpty()) {
+               Logger.normal(this, "No IP addresses found");
+               detectedPeer = null;
+        } else
+               detectedPeer=(Peer) nominalPeer.firstElement();

         String name = fs.get("myName");
         if(name == null) throw new FSParseException("No name");
@@ -311,6 +315,8 @@
     public Peer[] getHandshakeIPs(){
        Peer[] p=null;

+       if(detectedPeer == null && nominalPeer.size() == 0) return new Peer[0];
+       
        if( ! nominalPeer.contains(detectedPeer)){
                p= new Peer[1+nominalPeer.size()];
                p[0]=detectedPeer;
@@ -614,7 +620,7 @@
      * @return short version of toString()
      */
     public String shortToString() {
-        return 
super.toString()+"@"+detectedPeer.toString()+"@"+HexUtil.bytesToHex(identity);
+        return 
super.toString()+"@"+detectedPeer+"@"+HexUtil.bytesToHex(identity);
     }

     public String toString() {
@@ -885,10 +891,13 @@
         if(!Arrays.equals(oldPeers, nominalPeer.toArray(new 
Peer[nominalPeer.size()])))
                changedAnything = true;

-        if(nominalPeer.isEmpty()) throw new FSParseException("No 
physical.udp");
-        /* yes, we pick up a random one : it will be updated on handshake */
-        detectedPeer=(Peer) nominalPeer.firstElement();
-        
+        if(nominalPeer.isEmpty()) {
+               Logger.normal(this, "No physical.udp");
+               // detectedPeer stays as it is
+        } else {
+            /* yes, we pick up a random one : it will be updated on handshake 
*/
+            detectedPeer=(Peer) nominalPeer.firstElement();
+        }
         String name = fs.get("myName");
         if(name == null) throw new FSParseException("No name");
         if(!name.equals(myName)) changedAnything = true;
@@ -944,7 +953,7 @@

     public String getStatus() {
         return 
-               (isConnected ? "CONNECTED   " : "DISCONNECTED") + " " + 
getPeer().toString()+" "+myName+" "+currentLocation.getValue()+" 
"+getVersion()+" backoff: "+backoffLength+" ("+(Math.max(backedOffUntil - 
System.currentTimeMillis(),0))+")";
+               (isConnected ? "CONNECTED   " : "DISCONNECTED") + " " + 
getPeer()+" "+myName+" "+currentLocation.getValue()+" "+getVersion()+" backoff: 
"+backoffLength+" ("+(Math.max(backedOffUntil - 
System.currentTimeMillis(),0))+")";
     }

     public String getFreevizOutput() {

Modified: trunk/freenet/src/freenet/node/TextModeClientInterface.java
===================================================================
--- trunk/freenet/src/freenet/node/TextModeClientInterface.java 2006-02-21 
15:47:50 UTC (rev 8082)
+++ trunk/freenet/src/freenet/node/TextModeClientInterface.java 2006-02-21 
16:44:40 UTC (rev 8083)
@@ -29,6 +29,7 @@
 import freenet.config.InvalidConfigValueException;
 import freenet.config.SubConfig;
 import freenet.crypt.RandomSource;
+import freenet.io.comm.Peer;
 import freenet.io.comm.PeerParseException;
 import freenet.keys.FreenetURI;
 import freenet.keys.InsertableClientSSK;
@@ -110,7 +111,7 @@
 //        System.out.println("SUBSCRIBE:<key> - subscribe to a 
publish/subscribe stream by key");
         System.out.println("CONNECT:<filename|URL> - connect to a node from 
its ref in a file/url.");
         System.out.println("CONNECT:\n<noderef including an End on a line by 
itself> - enter a noderef directly.");
-        System.out.println("DISCONNECT:<ip:port> - disconnect from a node by 
providing it's ip+port");
+        System.out.println("DISCONNECT:<ip:port> - disconnect from a node by 
providing it's ip+port or name");
         System.out.println("NAME:<new node name> - change the node's name.");
 //        System.out.println("SUBFILE:<filename> - append all data received 
from subscriptions to a file, rather than sending it to stdout.");
 //        System.out.println("SAY:<text> - send text to the last 
created/pushed stream");
@@ -658,21 +659,26 @@
     }

     /**
-     * Disconnect from a node, given its ip and port as a String
+     * Disconnect from a node, given its ip and port, or name, as a String
      */
-    private void disconnect(String ipAndPort) {
-       System.out.println("Disconnecting from node at: "+ipAndPort);
+    private void disconnect(String nodeIdentifier) {
+       System.out.println("Disconnecting from node at: "+nodeIdentifier);
        PeerNode[] pn = n.peers.myPeers;
        for(int i=0;i<pn.length;i++)
        {
-               String nodeIpAndPort = 
pn[i].getDetectedPeer().getAddress().getHostAddress()+":"+pn[i].getDetectedPeer().getPort();
-               if(nodeIpAndPort.equals(ipAndPort))
+               Peer peer = pn[i].getDetectedPeer();
+               String nodeIpAndPort = "";
+               if(peer != null) {
+                       nodeIpAndPort = 
peer.getAddress().getHostAddress()+":"+pn[i].getDetectedPeer().getPort();
+               }
+               String name = pn[i].myName;
+               if(nodeIpAndPort.equals(nodeIdentifier) || 
name.equals(nodeIdentifier))
                {
                        n.peers.disconnect(pn[i]);
                        return;
                }
        }
-       System.out.println("No node in peers list at: "+ipAndPort);
+       System.out.println("No node in peers list at: "+nodeIdentifier);
     }

     private String sanitize(String fnam) {

Modified: trunk/freenet/src/freenet/node/Version.java
===================================================================
--- trunk/freenet/src/freenet/node/Version.java 2006-02-21 15:47:50 UTC (rev 
8082)
+++ trunk/freenet/src/freenet/node/Version.java 2006-02-21 16:44:40 UTC (rev 
8083)
@@ -20,7 +20,7 @@
        public static final String protocolVersion = "1.0";

        /** The build number of the current revision */
-       private static final int buildNumber = 452;
+       private static final int buildNumber = 453;

        /** Oldest build of Fred we will talk to */
        private static final int lastGoodBuild = 403;

Modified: trunk/freenet/src/freenet/support/SimpleFieldSet.java
===================================================================
--- trunk/freenet/src/freenet/support/SimpleFieldSet.java       2006-02-21 
15:47:50 UTC (rev 8082)
+++ trunk/freenet/src/freenet/support/SimpleFieldSet.java       2006-02-21 
16:44:40 UTC (rev 8083)
@@ -155,6 +155,7 @@
     }

     private static final String[] split(String string) {
+       if(string == null) return new String[0];
        return string.split(";"); // slower???
 //     int index = string.indexOf(';');
 //     if(index == -1) return null;
@@ -257,16 +258,22 @@
        }

        public Iterator keyIterator() {
-               return new KeyIterator();
+               return new KeyIterator("");
        }
+
+       KeyIterator keyIterator(String prefix) {
+               return new KeyIterator(prefix);
+       }

     public class KeyIterator implements Iterator {

        final Iterator mapIterator;
        KeyIterator subIterator;
+       String prefix;

-       public KeyIterator() {
+       public KeyIterator(String prefix) {
                mapIterator = map.keySet().iterator();
+               this.prefix = prefix;
        }

                public boolean hasNext() {
@@ -277,17 +284,18 @@

                public Object next() {
                        while(true) { // tail-recurse so we get infinite loop 
instead of OOM in case of a loop...
-                               if(subIterator != null && subIterator.hasNext())
+                               if(subIterator != null && 
subIterator.hasNext()) {
                                        return subIterator.next();
+                               }
                                if(subIterator != null) subIterator = null;
                                if(mapIterator.hasNext()) {
                                        String key = (String) 
mapIterator.next();
                                        Object value = map.get(key);
                                        if(value instanceof String)
-                                               return value;
+                                               return prefix + 
MULTI_LEVEL_CHAR + key;
                                        else {
                                                SimpleFieldSet fs = 
(SimpleFieldSet) value;
-                                               subIterator = (KeyIterator) 
fs.keyIterator();
+                                               subIterator = 
fs.keyIterator((prefix.length() == 0) ? key : (prefix+MULTI_LEVEL_CHAR+key));
                                                continue;
                                        }
                                }
@@ -302,6 +310,8 @@
        }

        public void put(String key, SimpleFieldSet fs) {
+               if(fs.isEmpty())
+                       throw new IllegalArgumentException("Empty");
                if(!multiLevel)
                        throw new IllegalArgumentException("Not multi-level");
                if(!fs.multiLevel)
@@ -311,4 +321,26 @@
                map.put(key, fs);
        }

+       public void remove(String key) {
+               int idx;
+               if((!multiLevel) || (idx = key.indexOf(MULTI_LEVEL_CHAR)) == 
-1) {
+                       map.remove(key);
+               } else {
+                       String before = key.substring(0, idx);
+                       String after = key.substring(idx+1);
+                       SimpleFieldSet fs = (SimpleFieldSet) (map.get(before));
+                       if(fs == null) {
+                               return;
+                       }
+                       fs.remove(after);
+                       if(fs.isEmpty())
+                               map.remove(before);
+               }
+       }
+
+       /** Is this SimpleFieldSet empty? */
+       public boolean isEmpty() {
+               return map.isEmpty();
+       }
+
 }


Reply via email to