Author: nextgens
Date: 2007-03-04 18:48:36 +0000 (Sun, 04 Mar 2007)
New Revision: 11960

Modified:
   trunk/freenet/src/freenet/node/LocationManager.java
Log:
Some synchronization fix on LocationManager to avoid:
Caught in SimpleToadletServer: java.util.ConcurrentModificationException
java.util.ConcurrentModificationException
        at java.util.HashMap$HashIterator.nextEntry(Unknown Source)
        at java.util.HashMap$ValueIterator.next(Unknown Source)
        at 
freenet.node.LocationManager.getNetworkSizeEstimate(LocationManager.java:1058)
        at freenet.node.Node.getNetworkSizeEstimate(Node.java:3228)
        at 
freenet.clients.http.DarknetConnectionsToadlet.handleGet(DarknetConnectionsToadlet.java:153)
        at 
freenet.clients.http.ToadletContextImpl.handle(ToadletContextImpl.java:292)
        at 
freenet.clients.http.SimpleToadletServer$SocketHandler.run(SimpleToadletServer.java:402)
        at java.lang.Thread.run(Unknown Source)

Modified: trunk/freenet/src/freenet/node/LocationManager.java
===================================================================
--- trunk/freenet/src/freenet/node/LocationManager.java 2007-03-04 18:44:26 UTC 
(rev 11959)
+++ trunk/freenet/src/freenet/node/LocationManager.java 2007-03-04 18:48:36 UTC 
(rev 11960)
@@ -1034,17 +1034,18 @@
     void registerKnownLocation(double d) {
        if(logMINOR) Logger.minor(this, "Known Location: "+d);
         Double dd = new Double(d);
+        Date timestamp = new Date();
+        Long longTime = new Long(timestamp.getTime());
+        
         synchronized(knownLocs) {
-            Date timestamp = new Date();
-            Long longTime = new Long(timestamp.getTime());
                        //If the location is already recorded, remove it from 
the hashmap
                        if (knownLocs.containsKey(dd)) {
                                knownLocs.remove(dd);
                        }
                        //Add the location to the map with the current 
timestamp as value
                        knownLocs.put(dd,longTime);
-                       if(logMINOR) Logger.minor(this, "Estimated net 
size(session): "+knownLocs.size());
         }
+               if(logMINOR) Logger.minor(this, "Estimated net size(session): 
"+knownLocs.size());
     }

     //Return the estimated network size based on locations seen after 
timestamp or for the whole session if -1
@@ -1052,17 +1053,19 @@
                int size = 0;
                if (timestamp == -1) {
                        size = knownLocs.size();
-               }
-               else if (timestamp > -1) {
-                       //TODO optimize some more if it is to be called a lot.
-                       Long locationTime = new Long(0);
+               }else if (timestamp > -1) {
+                               Long locationTime = new Long(0);
                                Iterator knownLocationsIterator = 
knownLocs.values().iterator();
-                               while (knownLocationsIterator.hasNext()) {
-                                       locationTime = (Long) 
knownLocationsIterator.next();
-                                       if (locationTime.longValue() > 
timestamp) {
-                                               size++;
-                                       }
-                               }
+                               
+                       synchronized (knownLocs) {
+                               //TODO optimize some more if it is to be called 
a lot.
+                               while (knownLocationsIterator.hasNext()) {
+                                       locationTime = (Long) 
knownLocationsIterator.next();
+                                       if (locationTime.longValue() > 
timestamp) {
+                                               size++;
+                                       }
+                               }
+                       }
                        }
                        return size;
        }
@@ -1075,15 +1078,17 @@
                        Double location = new Double(0.0);
                        Long locationTime = new Long(0);
                                Iterator knownLocationsIterator = 
knownLocs.keySet().iterator();
-                               while (knownLocationsIterator.hasNext()) {
-                                       location = (Double) 
knownLocationsIterator.next();
-                                       locationTime = (Long) 
knownLocs.get(location);
-                                       if (locationTime.longValue() > 
timestamp) {
-                                               //If the location is already 
recorded, remove it from the hashmap
-                                               if 
(knownLocsCopy.containsKey(location)) {
-                                                       
knownLocsCopy.remove(location);
+                               synchronized (knownLocs) {
+                                       while 
(knownLocationsIterator.hasNext()) {
+                                               location = (Double) 
knownLocationsIterator.next();
+                                               locationTime = (Long) 
knownLocs.get(location);
+                                               if (locationTime.longValue() > 
timestamp) {
+                                                       //If the location is 
already recorded, remove it from the hashmap
+                                                       if 
(knownLocsCopy.containsKey(location)) {
+                                                               
knownLocsCopy.remove(location);
+                                                       }
+                                                       
knownLocsCopy.put(location, locationTime);
                                                }
-                                               knownLocsCopy.put(location, 
locationTime);
                                        }
                                }
                                return knownLocsCopy;


Reply via email to