Author: toad
Date: 2008-02-02 23:38:43 +0000 (Sat, 02 Feb 2008)
New Revision: 17487

Modified:
   trunk/freenet/src/freenet/client/async/ClientRequestScheduler.java
   trunk/freenet/src/freenet/client/async/OfferedKeysList.java
   trunk/freenet/src/freenet/node/FailureTable.java
   trunk/freenet/src/freenet/node/NodeClientCore.java
Log:
Queue keys we want at the priority level we want them at.
Queue keys other people want at IMMEDIATE_SPLITFILE_PRIORITY_CLASS.

Modified: trunk/freenet/src/freenet/client/async/ClientRequestScheduler.java
===================================================================
--- trunk/freenet/src/freenet/client/async/ClientRequestScheduler.java  
2008-02-02 23:20:27 UTC (rev 17486)
+++ trunk/freenet/src/freenet/client/async/ClientRequestScheduler.java  
2008-02-02 23:38:43 UTC (rev 17487)
@@ -612,4 +612,30 @@
                        return pendingKeys.get(key) != null;
                }
        }
+
+       /** If we want the offered key, or if force is enabled, queue it */
+       public void maybeQueueOfferedKey(Key key, boolean force) {
+               short priority = Short.MAX_VALUE;
+               synchronized(this) {
+                       if(force) {
+                               // FIXME what priority???
+                               priority = 
RequestStarter.IMMEDIATE_SPLITFILE_PRIORITY_CLASS;
+                       }
+                       Object o = pendingKeys.get(key);
+                       if(o == null) {
+                               // Blah
+                       } else if(o instanceof SendableGet) {
+                               short p = ((SendableGet)o).getPriorityClass();
+                               if(p < priority) priority = p;
+                       } else { // if(o instanceof SendableGet[]) {
+                               SendableGet[] gets = (SendableGet[]) o;
+                               for(int i=0;i<gets.length;i++) {
+                                       short p = gets[i].getPriorityClass();
+                                       if(p < priority) priority = p;
+                               }
+                       }
+               }
+               if(priority == Short.MAX_VALUE) return;
+               offeredKeys[priority].queueKey(key);
+       }
 }

Modified: trunk/freenet/src/freenet/client/async/OfferedKeysList.java
===================================================================
--- trunk/freenet/src/freenet/client/async/OfferedKeysList.java 2008-02-02 
23:20:27 UTC (rev 17486)
+++ trunk/freenet/src/freenet/client/async/OfferedKeysList.java 2008-02-02 
23:38:43 UTC (rev 17487)
@@ -3,7 +3,6 @@
  * http://www.gnu.org/ for further details of the GPL. */
 package freenet.client.async;

-import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Vector;

@@ -31,7 +30,6 @@
  */
 public class OfferedKeysList extends SendableRequest {

-       private final HashMap clientKeysByKey;
        private final HashSet keys;
        // FIXME is there any way to avoid the O(n) shuffling penalty here?
        private final Vector keysList;
@@ -43,7 +41,6 @@
        OfferedKeysList(NodeClientCore core, RandomSource random, short 
priorityClass) {
                this.keys = new HashSet();
                this.keysList = new Vector();
-               clientKeysByKey = new HashMap();
                this.random = random;
                this.priorityClass = priorityClass;
                this.core = core;
@@ -53,11 +50,10 @@
        /** Called when a key is found, when it no longer belogns to this list 
etc. */
        public synchronized void remove(Key key) {
                logMINOR = Logger.shouldLog(Logger.MINOR, this);
-               ClientKey ck = (ClientKey) clientKeysByKey.remove(key);
-               if(ck == null) return;
-               if(logMINOR) Logger.minor(this, "Found "+key+" , removing it");
-               keys.remove(key);
-               keysList.remove(key);
+               if(keys.remove(key)) {
+                       if(logMINOR) Logger.minor(this, "Found "+key+" , 
removing it");
+                       keysList.remove(key);
+               }
        }

        /** Called when there are no more valid offers for a key */
@@ -67,7 +63,6 @@
                Key k = key.getNodeKey();
                keys.remove(k);
                keysList.remove(k);
-               clientKeysByKey.remove(k);
        }

        public synchronized boolean isEmpty() {
@@ -84,7 +79,6 @@
                if(keysList.isEmpty()) return null;
                Key k = (Key) keysList.remove(random.nextInt(keysList.size()));
                keys.remove(k);
-               clientKeysByKey.remove(k);
                return k;
        }

@@ -123,4 +117,10 @@
                return false;
        }

+       public synchronized void queueKey(Key key) {
+               if(keys.add(key)) {
+                       keysList.add(key);
+               }
+       }
+
 }

Modified: trunk/freenet/src/freenet/node/FailureTable.java
===================================================================
--- trunk/freenet/src/freenet/node/FailureTable.java    2008-02-02 23:20:27 UTC 
(rev 17486)
+++ trunk/freenet/src/freenet/node/FailureTable.java    2008-02-02 23:38:43 UTC 
(rev 17487)
@@ -6,6 +6,7 @@
 import java.lang.ref.WeakReference;
 import java.util.Vector;

+import freenet.client.async.ClientRequestScheduler;
 import freenet.io.comm.DMT;
 import freenet.io.comm.Message;
 import freenet.io.comm.NotConnectedException;
@@ -238,22 +239,12 @@
         * @param peer The node offering it.
         * @param authenticator 
         */
-       public void onOffer(Key key, PeerNode peer, byte[] authenticator) {
-               if(wantOffer(key, peer, authenticator)) {
-                       // Okay, we want the offer. Now what?
-                       // Two ClientRequestScheduler's? Then you'd have to 
remove the key from two different RGA's :(
-                       // Anyway, we don't want a key to be requested just 
because another key in the same group has an offer.
-                       // So what we want is a list of keys at each level 
which have been offered.
-                       // These would be considered before the other keys at 
that level, but only if the offer is still valid.
-               }
-       }
-       
-       boolean wantOffer(Key key, PeerNode peer, byte[] authenticator) {
+       void onOffer(Key key, PeerNode peer, byte[] authenticator) {
                FailureTableEntry entry;
                long now = System.currentTimeMillis();
                synchronized(this) {
                        entry = (FailureTableEntry) entriesByKey.get(key);
-                       if(entry == null) return false; // we haven't asked for 
it
+                       if(entry == null) return; // we haven't asked for it

                        /*
                         * Accept (subject to later checks) if we asked for it.
@@ -287,7 +278,7 @@
                        if(!(entry.askedFromPeer(peer, now) || 
                                        ((key instanceof NodeCHK) && 
entry.askedByPeer(peer, now)))) {
                                if(entry.isEmpty(now)) 
entriesByKey.removeKey(key);
-                               return false;
+                               return;
                        }
                        if(entry.isEmpty(now)) entriesByKey.removeKey(key);

@@ -307,10 +298,8 @@
                }

                // Now, does anyone want it?
-               // Firstly, do we want it?
-               if(!node.clientCore.clientWantKey(key)) return true;
-               if(entry.othersWant(peer)) return true;
-               return false;
+               
+               node.clientCore.maybeQueueOfferedKey(key, 
entry.othersWant(peer));
        }

        private synchronized void trimOffersList(long now) {

Modified: trunk/freenet/src/freenet/node/NodeClientCore.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeClientCore.java  2008-02-02 23:20:27 UTC 
(rev 17486)
+++ trunk/freenet/src/freenet/node/NodeClientCore.java  2008-02-02 23:38:43 UTC 
(rev 17487)
@@ -9,6 +9,7 @@
 import freenet.client.HighLevelSimpleClientImpl;
 import freenet.client.InsertContext;
 import freenet.client.async.BackgroundBlockEncoder;
+import freenet.client.async.ClientRequestScheduler;
 import freenet.client.async.HealingQueue;
 import freenet.client.async.SimpleHealingQueue;
 import freenet.client.async.USKManager;
@@ -1096,4 +1097,14 @@
        public boolean hasLoadedQueue() {
                return fcpServer.hasFinishedStart();
        }
+
+       /** Pass the offered key down to the client layer.
+        * If the client layer wants it, or force is enabled, queue it. */
+       public void maybeQueueOfferedKey(Key key, boolean force) {
+               ClientRequestScheduler sched =
+                       key instanceof NodeSSK ?
+                                       requestStarters.sskFetchScheduler :
+                                               
requestStarters.chkFetchScheduler;
+               sched.maybeQueueOfferedKey(key, force);
+       }
 }


Reply via email to