Author: toad
Date: 2006-09-05 15:48:40 +0000 (Tue, 05 Sep 2006)
New Revision: 10396

Added:
   trunk/freenet/src/freenet/node/ProbeCallback.java
Modified:
   trunk/freenet/src/freenet/node/NodeDispatcher.java
   trunk/freenet/src/freenet/node/TextModeClientInterface.java
Log:
Probe requests working, and producing interesting results...

Modified: trunk/freenet/src/freenet/node/NodeDispatcher.java
===================================================================
--- trunk/freenet/src/freenet/node/NodeDispatcher.java  2006-09-05 14:14:13 UTC 
(rev 10395)
+++ trunk/freenet/src/freenet/node/NodeDispatcher.java  2006-09-05 15:48:40 UTC 
(rev 10396)
@@ -408,18 +408,20 @@

        final PeerNode src; // FIXME make this a weak reference or something ? 
- Memory leak with high connection churn
        final HashSet visitedPeers;
+       final ProbeCallback cb;
        short counter;
        short htl;
        double nearest;
        double best;

-               public ProbeContext(long id, double target, double best, double 
nearest, short htl, short counter, PeerNode src) {
+               public ProbeContext(long id, double target, double best, double 
nearest, short htl, short counter, PeerNode src, ProbeCallback cb) {
                        visitedPeers = new HashSet();
                        this.counter = counter;
                        this.htl = htl;
                        this.nearest = nearest;
                        this.best = best;
                        this.src = src;
+                       this.cb = cb;
                }

     }
@@ -463,11 +465,11 @@
                                Logger.minor(this, "Probe request popped "+o);
                        }
                }
-               return innerHandleProbeRequest(src, id, lid, target, best, 
nearest, htl, counter, true, true);
+               return innerHandleProbeRequest(src, id, lid, target, best, 
nearest, htl, counter, true, true, null);
        }

     private boolean innerHandleProbeRequest(PeerNode src, long id, Long lid, 
double target, double best, 
-               double nearest, short htl, short counter, boolean checkRecent, 
boolean canReject) {
+               double nearest, short htl, short counter, boolean checkRecent, 
boolean canReject, ProbeCallback cb) {
        if(htl > Node.MAX_HTL) htl = Node.MAX_HTL;
        if(htl <= 1) htl = 1;
                ProbeContext ctx = null;
@@ -483,7 +485,7 @@
                        if(!rejected) {
                                ctx = (ProbeContext) 
recentProbeContexts.get(lid);
                                if(ctx == null) {
-                                       ctx = new ProbeContext(id, target, 
best, nearest, htl, counter, src);
+                                       ctx = new ProbeContext(id, target, 
best, nearest, htl, counter, src, cb);
                                }
                                recentProbeContexts.push(lid, ctx); // promote 
or add
                                while(recentProbeContexts.size() > 
MAX_PROBE_CONTEXTS)
@@ -568,7 +570,7 @@
                                }
                                return true;
                        } else {
-                               complete("success", src, target, best, id);
+                               complete("success", target, best, nearest, id, 
ctx, counter);
                        }
                }

@@ -591,7 +593,7 @@
                                                Logger.error(this, "Not 
connected rejecting a probe request from "+src);
                                        }
                                } else {
-                                       complete("RNF", src, target, best, id);
+                                       complete("RNF", target, best, nearest, 
id, ctx, counter);
                                }
                                return true;
                        }
@@ -611,10 +613,9 @@

        }

-       private void complete(String msg, PeerNode src, double target, double 
best, long id) {
-               Logger.error(this, "Completed Probe request # "+id+" - RNF - 
"+msg+": "+best);
-               if(src == null)
-                       System.out.println("Completed probe from "+target+": 
"+best+" ("+id+")");
+       private void complete(String msg, double target, double best, double 
nearest, long id, ProbeContext ctx, short counter) {
+               Logger.normal(this, "Completed Probe request # "+id+" - RNF - 
"+msg+": "+best);
+               ctx.cb.onCompleted(msg, target, best, nearest, id, counter);
        }

        private boolean handleProbeReply(Message m, PeerNode src) {
@@ -639,11 +640,16 @@
                                recentProbeContexts.popValue();
                }

-               Message complete = DMT.createFNPProbeReply(id, target, nearest, 
best, counter++);
-               try {
-                       ctx.src.sendAsync(complete, null, 0, null);
-               } catch (NotConnectedException e) {
-                       Logger.error(this, "Not connected completing a probe 
request from "+ctx.src+" (forwarding completion from "+src+")");
+               if(ctx.src != null) {
+                       Message complete = DMT.createFNPProbeReply(id, target, 
nearest, best, counter++);
+                       try {
+                               ctx.src.sendAsync(complete, null, 0, null);
+                       } catch (NotConnectedException e) {
+                               Logger.error(this, "Not connected completing a 
probe request from "+ctx.src+" (forwarding completion from "+src+")");
+                       }
+               } else {
+                       if(ctx.cb != null)
+                               complete("Completed", target, best, nearest, 
id, ctx, counter);
                }
                return true;
        }
@@ -672,16 +678,16 @@
                                recentProbeContexts.popValue();
                }

-               return innerHandleProbeRequest(src, id, lid, target, best, 
nearest, htl, counter, false, false);
+               return innerHandleProbeRequest(src, id, lid, target, best, 
nearest, htl, counter, false, false, null);
     }

-       public void startProbe(double d) {
+       public void startProbe(double d, ProbeCallback cb) {
                long l = node.random.nextLong();
                Long ll = new Long(l);
                synchronized(recentProbeRequestIDs) {
                        recentProbeRequestIDs.push(ll);
                }
-               innerHandleProbeRequest(null, l, ll, d, 2.0, 2.0, Node.MAX_HTL, 
(short)0, false, false);
+               innerHandleProbeRequest(null, l, ll, d, 2.0, 2.0, Node.MAX_HTL, 
(short)0, false, false, cb);
        }

 }

Added: trunk/freenet/src/freenet/node/ProbeCallback.java
===================================================================
--- trunk/freenet/src/freenet/node/ProbeCallback.java   2006-09-05 14:14:13 UTC 
(rev 10395)
+++ trunk/freenet/src/freenet/node/ProbeCallback.java   2006-09-05 15:48:40 UTC 
(rev 10396)
@@ -0,0 +1,8 @@
+package freenet.node;
+
+/** Callback for a locally initiated probe request */
+public interface ProbeCallback {
+
+       void onCompleted(String reason, double target, double best, double 
nearest, long id, short counter);
+
+}

Modified: trunk/freenet/src/freenet/node/TextModeClientInterface.java
===================================================================
--- trunk/freenet/src/freenet/node/TextModeClientInterface.java 2006-09-05 
14:14:13 UTC (rev 10395)
+++ trunk/freenet/src/freenet/node/TextModeClientInterface.java 2006-09-05 
15:48:40 UTC (rev 10396)
@@ -58,6 +58,7 @@
     final File downloadsDir;
     final InputStream in;
     final OutputStream out;
+    private boolean doneSomething;

     public TextModeClientInterface(TextModeClientInterfaceServer server, 
InputStream in, OutputStream out) {
        this.n = server.n;
@@ -185,7 +186,7 @@
      * Process a single command.
      * @throws IOException If we could not write the data to stdout.
      */
-    private boolean processLine(BufferedReader reader, OutputStream out) 
throws IOException {
+    private boolean processLine(BufferedReader reader, final OutputStream out) 
throws IOException {
         String line;
         StringBuffer outsb = new StringBuffer();
         try {
@@ -741,8 +742,32 @@
         } else if(uline.startsWith("PROBE:")) {
                String s = uline.substring("PROBE:".length()).trim();
                double d = Double.parseDouble(s);
-               System.err.println("Starting probe request for location "+d);
-               n.dispatcher.startProbe(d);
+               ProbeCallback cb = new ProbeCallback() {
+                               public void onCompleted(String reason, double 
target, double best, double nearest, long id, short counter) {
+                                       String msg = "Completed probe request: 
"+target+" -> "+best+"\r\nNearest actually hit "+nearest+", "+counter+" hops, 
id "+id+"\r\n";
+                                       try {
+                                               out.write(msg.getBytes());
+                                               out.flush();
+                                       } catch (IOException e) {
+                                               // Already closed. :(
+                                       }
+                                       
synchronized(TextModeClientInterface.this) {
+                                               doneSomething = true;
+                                               
TextModeClientInterface.this.notifyAll();
+                                       }
+                               }
+               };
+               n.dispatcher.startProbe(d, cb);
+               synchronized(this) {
+                       while(!doneSomething) {
+                               try {
+                                       wait(5000);
+                               } catch (InterruptedException e) {
+                                       // Ignore
+                               }
+                       }
+                       doneSomething = false;
+               }
         } else if(uline.startsWith("PLUGLOAD:")) {
                if (line.substring("PLUGLOAD:".length()).trim().equals("?")) {
                        outsb.append("  PLUGLOAD: pkg.Class                  - 
Load plugin from current classpath");                    


Reply via email to