Update of /cvsroot/freenet/freenet/src/freenet/node/states/request
In directory sc8-pr-cvs1:/tmp/cvs-serv31429/src/freenet/node/states/request

Modified Files:
        DataPending.java FeedbackToken.java InsertPending.java 
        Pending.java ReceivingInsert.java ReceivingReply.java 
        RequestState.java TransferInsert.java 
        TransferInsertPending.java TransferReply.java 
Added Files:
        SendFinished.java 
Log Message:
add SendFinished, a new internal message used as a callback when sending messages. 
Make sending QueryRestarted's asynchronous. Logging.


--- NEW FILE: SendFinished.java ---
package freenet.node.states.request;

import freenet.Core;
import freenet.MessageSendCallback;
import freenet.support.Logger;
import freenet.node.Node;
import freenet.node.State;
import freenet.CommunicationException;

/** Message Object for async send callback 
 * @see RequestSendCallback */
public class SendFinished extends RequestObject implements MessageSendCallback {
    
    final long initTime;
    private long finishTime;
    private Exception finishException;
    private boolean succeeded;
    final Node n;
    
    public SendFinished(Node n, long id) {
        super(id, true);
        initTime = System.currentTimeMillis();
        finishTime = -1;
        finishException = null;
        succeeded = false;
        this.n = n;
    }
    
    public String toString() {
        return getClass().getName()+"@ "+finishTime+":"+initTime+":"+succeeded+":"+
            finishException;
    }
    
    public long startTime() {
        return initTime;
    }
    
    public long endTime() {
        return finishTime;
    }
    
    public boolean finished() {
        return finishTime != -1;
    }
    
    public boolean getSuccess() {
        return succeeded;
    }
    
    public Exception failCause() {
        return finishException;
    }
    
    public void succeeded() {
        finishTime = System.currentTimeMillis();
        succeeded = true;
        if(n.logger.shouldLog(Logger.DEBUG))
            n.logger.log(this, toString() + "succeeded",
                         Logger.DEBUG);
        n.schedule(this);
    }
    
    public void thrown(Exception e) {
        finishTime = System.currentTimeMillis();
        succeeded = false;
        finishException = e;
        if(e instanceof CommunicationException) {
            CommunicationException ce = (CommunicationException)e;
            n.logger.log(this,
                         "Failed to send back to peer " +ce.peer+
                         " ("+this+")", e, Logger.MINOR);
        } else {
            n.logger.log(this, "Unexpected exception sending for "+
                         this+": "+e, e, Logger.NORMAL);
        }
        n.schedule(this);
    }
}

Index: DataPending.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/states/request/DataPending.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- DataPending.java    30 Aug 2003 23:16:53 -0000      1.16
+++ DataPending.java    3 Sep 2003 18:10:13 -0000       1.17
@@ -133,6 +133,15 @@
         return new RequestDone(this);
     }
     
+    public State receivedMessage(Node n, SendFinished sf) throws StateException {
+       try {
+           super.receivedSendFinished(n, sf);
+       } catch (RequestAbortException rae) {
+           return rae.state;
+       }
+       return this;
+    }
+    
     private final void checkFailureTable(Node n) throws RequestAbortException {
         long toq = n.ft.shouldFail(searchKey, hopsToLive);
         if ((origPeer != null) && toq > 0) {

Index: FeedbackToken.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/states/request/FeedbackToken.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- FeedbackToken.java  14 Aug 2003 02:06:48 -0000      1.6
+++ FeedbackToken.java  3 Sep 2003 18:10:13 -0000       1.7
@@ -25,7 +25,8 @@
     /**
      * @param millis  the maximum time until the next callback
      */
-    void restarted(Node n, long millis)
+    void restarted(Node n, long millis,
+                  MessageSendCallback cb)
         throws CommunicationException;
     
     void dataNotFound(Node n, long timeOfQuery,

Index: InsertPending.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/states/request/InsertPending.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- InsertPending.java  30 Aug 2003 23:16:53 -0000      1.18
+++ InsertPending.java  3 Sep 2003 18:10:13 -0000       1.19
@@ -266,6 +266,15 @@
         return this;
     }
     
+    public State receivedMessage(Node n, SendFinished sf) throws StateException {
+       try {
+           super.receivedSendFinished(n, sf);
+       } catch (RequestAbortException rae) {
+           return rae.state;
+       }
+       return this;
+    }
+    
     
     //=== support methods ======================================================
     

Index: Pending.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/states/request/Pending.java,v
retrieving revision 1.51
retrieving revision 1.52
diff -u -r1.51 -r1.52
--- Pending.java        2 Sep 2003 21:37:32 -0000       1.51
+++ Pending.java        3 Sep 2003 18:10:13 -0000       1.52
@@ -57,6 +57,8 @@
     
     long searchDataRoutingTime = -1;
     
+    SendFinished feedbackSender = null;
+    
     public String toString() {
        return super.toString()+", routedTime="+routedTime+", replyTime="+replyTime;
     }
@@ -121,7 +123,7 @@
        
         cancelRestart();
         long timeout = Core.hopTime(hopsToLive);
-        relayRestarted(n, timeout);
+        relayRestarted(n, timeout, true);
         scheduleRestart(n, timeout);
     }
     
@@ -302,7 +304,7 @@
            
             if (--hopsToLive > 0) {
                 // send QueryRestarted to initiating chain
-                relayRestarted(n, Core.hopTime(hopsToLive + 1));
+                relayRestarted(n, Core.hopTime(hopsToLive + 1), true);
                 // +1 for Accepted
             }
            
@@ -613,11 +615,43 @@
         scheduleRestart(n, Core.hopTime(1));  // timeout to get the Accepted
     }
     
-    private final void relayRestarted(Node n, long timeout) throws 
RequestAbortException {
+    public void receivedSendFinished(Node n, SendFinished sf) 
+       throws BadStateException, RequestAbortException {
+       if(sf == feedbackSender) {
+           n.logger.log(this, "Got a SendFinished "+sf+" for "+this,
+                        Logger.NORMAL /* FIXME! */ );
+           feedbackSender = null;
+           // Our feedback sender
+           // If success, cool
+           // If failure, throw RAE to RequestDone
+           if(!sf.finished())
+               throw new BadStateException("Got a SendFinished that wasn't finished: 
"+sf);
+           if(!sf.getSuccess()) {
+               Exception e = sf.failCause();
+               if(e instanceof CommunicationException) {
+                   n.logger.log(this, "Couldn't send QueryRestarted back to "+
+                                ft+":"+origPeer+", killing request ("+sf+")",
+                                Logger.MINOR);
+               } else {
+                   n.logger.log(this, "Got strange Exception "+e+" sending "+
+                                "QueryRestarted to "+ft+":"+origPeer+" ("+sf+")",
+                                Logger.MINOR);
+               }
+               // Either way...
+               terminateRouting(false, false);
+               throw new RequestAbortException(new RequestDone(this));
+           }
+       }
+    }
+    
+    private final void relayRestarted(Node n, long timeout,
+                                     boolean sendAsync) throws RequestAbortException {
+       if(sendAsync)
+           feedbackSender = new SendFinished(n, id);
         try {
-            ft.restarted(n, timeout);
-        }
-        catch (CommunicationException e) {
+            ft.restarted(n, timeout, 
+                        sendAsync ? feedbackSender : null);
+        } catch (CommunicationException e) {
             n.logger.log(this,
                          "Couldn't restart because relaying QueryRestarted failed: "+
                         e+" for "+this, Logger.MINOR);

Index: ReceivingInsert.java
===================================================================
RCS file: 
/cvsroot/freenet/freenet/src/freenet/node/states/request/ReceivingInsert.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- ReceivingInsert.java        30 Aug 2003 23:16:53 -0000      1.19
+++ ReceivingInsert.java        3 Sep 2003 18:10:13 -0000       1.20
@@ -107,5 +107,11 @@
                 return new RequestDone(this);
         }
     }
+    
+    public State receivedMessage(Node n, SendFinished sf) throws StateException {
+       n.logger.log(this, "Received "+sf+" in "+this+" - WTF?",
+                    Logger.NORMAL);
+       return this;
+    }
 }
 

Index: ReceivingReply.java
===================================================================
RCS file: 
/cvsroot/freenet/freenet/src/freenet/node/states/request/ReceivingReply.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- ReceivingReply.java 30 Aug 2003 23:16:53 -0000      1.8
+++ ReceivingReply.java 3 Sep 2003 18:10:13 -0000       1.9
@@ -159,6 +159,12 @@
        terminateRouting(false, true); // routing will ignore if we succeeded already
         return new RequestDone(this);
     }
+    
+    public State receivedMessage(Node n, SendFinished sf) throws StateException {
+       // Too important - we are transferring, we don't care
+       n.logger.log(this, "Got "+sf+" in "+this, Logger.MINOR);
+       return this;
+    }
 }
 
 

Index: RequestState.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/states/request/RequestState.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- RequestState.java   30 Aug 2003 23:16:53 -0000      1.12
+++ RequestState.java   3 Sep 2003 18:10:13 -0000       1.13
@@ -131,6 +131,9 @@
      * This also cancels the restart if possible.
      */
     final void fail(Node n, String reason, FieldSet otherFields) {
+       if(logDEBUG)
+           n.logger.log(this, "failing: "+reason+" for "+this,
+                        Logger.DEBUG);
         cancelRestart();
         try {
             if (reason == null)

Index: TransferInsert.java
===================================================================
RCS file: 
/cvsroot/freenet/freenet/src/freenet/node/states/request/TransferInsert.java,v
retrieving revision 1.12
retrieving revision 1.13
diff -u -r1.12 -r1.13
--- TransferInsert.java 2 Sep 2003 17:05:22 -0000       1.12
+++ TransferInsert.java 3 Sep 2003 18:10:13 -0000       1.13
@@ -73,6 +73,19 @@
         return this;
     }
 
+    public State receivedMessage(Node n, SendFinished sf) throws StateException {
+       try {
+           super.receivedSendFinished(n, sf);
+       } catch (RequestAbortException rae) {
+            // going to RequestDone with SendFailedException
+            receivingData.cancel();
+            sendingData.abort(Presentation.CB_ABORTED);
+            queryAborted(n);
+            return transition(rae.state, false);  // drop queue
+       }
+       return this;
+    }
+    
     // must have timed out waiting for InsertReply/DataReply
     public State receivedMessage(Node n, RequestInitiator ri) throws StateException {
        if(this.ri == null || this.ri != ri) {

Index: TransferInsertPending.java
===================================================================
RCS file: 
/cvsroot/freenet/freenet/src/freenet/node/states/request/TransferInsertPending.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- TransferInsertPending.java  30 Aug 2003 23:16:53 -0000      1.15
+++ TransferInsertPending.java  3 Sep 2003 18:10:13 -0000       1.16
@@ -115,7 +115,18 @@
         }
         return this;
     }
-
+    
+    public State receivedMessage(Node n, SendFinished sf) throws StateException {
+       try {
+           super.receivedSendFinished(n, sf);
+       } catch (RequestAbortException rae) {
+            // going to RequestDone with SendFailedException
+            receivingData.cancel();
+           return rae.state;
+       }
+       return this;
+    }
+    
     // if we didn't make it to TransferInsert yet...
     public State receivedMessage(Node n, DataReceived dr) throws StateException {
        // Routing does not care

Index: TransferReply.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/node/states/request/TransferReply.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- TransferReply.java  30 Aug 2003 23:16:53 -0000      1.14
+++ TransferReply.java  3 Sep 2003 18:10:13 -0000       1.15
@@ -63,6 +63,11 @@
         return transition(new DataPending(this), true);
     }
     
+    public State receivedMessage(Node n, SendFinished sf) throws StateException {
+       mq.addElement(sf);
+       return this;
+    }
+    
     public State receivedMessage(Node n, QueryRejected qr) throws StateException {
         if (!fromLastPeer(qr)) {
             throw new BadStateException("QueryRejected from the wrong peer! for 
"+this);

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to