Author: toad
Date: 2007-04-13 20:50:38 +0000 (Fri, 13 Apr 2007)
New Revision: 12663

Modified:
   trunk/freenet/src/freenet/node/PeerNode.java
Log:
Make sendSync() actually synchronous. Maximum wait time of 1 minute.

Modified: trunk/freenet/src/freenet/node/PeerNode.java
===================================================================
--- trunk/freenet/src/freenet/node/PeerNode.java        2007-04-13 20:44:03 UTC 
(rev 12662)
+++ trunk/freenet/src/freenet/node/PeerNode.java        2007-04-13 20:50:38 UTC 
(rev 12663)
@@ -1254,9 +1254,57 @@
                 throw new NotConnectedException();
             }
                }
-        node.usm.send(this, req, ctr);
+       SyncMessageCallback cb = new SyncMessageCallback();
+       this.sendAsync(req, cb, 0, ctr);
+       cb.waitForSend(60*1000);
     }
+    
+    private class SyncMessageCallback implements AsyncMessageCallback {

+       private boolean done = false;
+       
+               public synchronized void waitForSend(long maxWaitInterval) {
+                       long now = System.currentTimeMillis();
+                       long end = now + maxWaitInterval;
+                       while((now = System.currentTimeMillis()) < end) {
+                               if(done) return;
+                               int waitTime = (int)(Math.min(end - now, 
Integer.MAX_VALUE));
+                               try {
+                                       wait(waitTime);
+                               } catch (InterruptedException e) {
+                                       // Ignore
+                               }
+                       }
+                       Logger.error(this, "Waited too long for a blocking send 
on "+PeerNode.this, new Exception("error"));
+               }
+               
+               public void acknowledged() {
+                       // Ignore, we only wait for it to be sent
+               }
+
+               public void disconnected() {
+                       synchronized(this) {
+                               done = true;
+                               notifyAll();
+                       }
+               }
+
+               public void fatalError() {
+                       synchronized(this) {
+                               done = true;
+                               notifyAll();
+                       }
+               }
+
+               public void sent() {
+                       synchronized(this) {
+                               done = true;
+                               notifyAll();
+                       }
+               }
+       
+    }
+
     /**
      * Update the Location to a new value.
      */


Reply via email to