Author: toad
Date: 2008-04-11 23:30:11 +0000 (Fri, 11 Apr 2008)
New Revision: 19213

Added:
   trunk/freenet/src/freenet/node/fcp/RequestCompletionCallback.java
Modified:
   trunk/freenet/src/freenet/clients/http/QueueToadlet.java
   trunk/freenet/src/freenet/l10n/freenet.l10n.en.properties
   trunk/freenet/src/freenet/node/fcp/ClientGet.java
   trunk/freenet/src/freenet/node/fcp/ClientPutBase.java
   trunk/freenet/src/freenet/node/fcp/FCPClient.java
   trunk/freenet/src/freenet/node/fcp/FCPServer.java
Log:
Notify client when an upload or download completes.
At the moment these don't persist across restarts.

Modified: trunk/freenet/src/freenet/clients/http/QueueToadlet.java
===================================================================
--- trunk/freenet/src/freenet/clients/http/QueueToadlet.java    2008-04-11 
22:00:00 UTC (rev 19212)
+++ trunk/freenet/src/freenet/clients/http/QueueToadlet.java    2008-04-11 
23:30:11 UTC (rev 19213)
@@ -30,6 +30,10 @@
 import freenet.node.fcp.IdentifierCollisionException;
 import freenet.node.fcp.MessageInvalidException;
 import freenet.node.fcp.NotAllowedException;
+import freenet.node.fcp.RequestCompletionCallback;
+import freenet.node.useralerts.SimpleHTMLUserAlert;
+import freenet.node.useralerts.SimpleUserAlert;
+import freenet.node.useralerts.UserAlert;
 import freenet.support.HTMLNode;
 import freenet.support.Logger;
 import freenet.support.MultiValueTable;
@@ -40,7 +44,7 @@
 import freenet.support.io.BucketTools;
 import freenet.support.io.FileBucket;

-public class QueueToadlet extends Toadlet {
+public class QueueToadlet extends Toadlet implements RequestCompletionCallback 
{

        private static final int LIST_IDENTIFIER = 1;
        private static final int LIST_SIZE = 2;
@@ -70,6 +74,7 @@
                this.core = core;
                this.fcp = fcp;
                if(fcp == null) throw new NullPointerException();
+               fcp.setCompletionCallback(this);
        }

        public void handlePost(URI uri, HTTPRequest request, ToadletContext 
ctx) throws ToadletContextClosedException, IOException, RedirectException {
@@ -1051,4 +1056,48 @@
                return "GET, POST";
        }

+       public void notifyFailure(ClientRequest req) {
+               // FIXME do something???
+       }
+
+       public void notifySuccess(ClientRequest req) {
+               // FIXME persist across restarts.
+               if(req instanceof ClientGet) {
+                       FreenetURI uri = ((ClientGet)req).getURI();
+                       long size = ((ClientGet)req).getDataSize();
+                       String name = uri.getPreferredFilename();
+                       String title = l10n("downloadSucceededTitle", 
"filename", name);
+                       HTMLNode text = new HTMLNode("div");
+                       L10n.addL10nSubstitution(text, 
"QueueToadlet.downloadSucceeded",
+                                       new String[] { "link", "/link", 
"origlink", "/origlink", "filename", "size" },
+                                       new String[] { "<a 
href=\"/queue/"+uri.toACIIString()+"\">", "</a>", "<a 
href=\"/"+uri.toACIIString()+"\">", "</a>", name, SizeUtil.formatSize(size) } );
+                       core.alerts.register(new SimpleHTMLUserAlert(true, 
title, text, UserAlert.MINOR));
+               } else if(req instanceof ClientPut) {
+                       FreenetURI uri = ((ClientPut)req).getFinalURI();
+                       long size = ((ClientPut)req).getDataSize();
+                       String name = uri.getPreferredFilename();
+                       String title = l10n("uploadSucceededTitle", "filename", 
name);
+                       HTMLNode text = new HTMLNode("div");
+                       L10n.addL10nSubstitution(text, 
"QueueToadlet.uploadSucceeded",
+                                       new String[] { "link", "/link", 
"filename", "size" },
+                                       new String[] { "<a 
href=\"/"+uri.toACIIString()+"\">", "</a>", name, SizeUtil.formatSize(size) } );
+                       core.alerts.register(new SimpleHTMLUserAlert(true, 
title, text, UserAlert.MINOR));
+               } else if(req instanceof ClientPutDir) {
+                       FreenetURI uri = ((ClientPutDir)req).getFinalURI();
+                       long size = ((ClientPutDir)req).getTotalDataSize();
+                       int files = ((ClientPutDir)req).getNumberOfFiles();
+                       String name = uri.getPreferredFilename();
+                       String title = 
l10n("QueueToadlet.siteUploadSucceededTitle", "filename", name);
+                       HTMLNode text = new HTMLNode("div");
+                       L10n.addL10nSubstitution(text, 
"QueueToadlet.siteUploadSucceeded",
+                                       new String[] { "link", "/link", 
"origlink", "/origlink", "filename", "size", "files" },
+                                       new String[] { "<a 
href=\"/"+uri.toACIIString()+"\">", "</a>", name, SizeUtil.formatSize(size), 
Integer.toString(files) } );
+                       core.alerts.register(new SimpleHTMLUserAlert(true, 
title, text, UserAlert.MINOR));
+               }
+       }
+       
+       String l10n(String key, String pattern, String value) {
+               return L10n.getString("QueueToadlet."+key, pattern, value);
+       }
+
 }

Modified: trunk/freenet/src/freenet/l10n/freenet.l10n.en.properties
===================================================================
--- trunk/freenet/src/freenet/l10n/freenet.l10n.en.properties   2008-04-11 
22:00:00 UTC (rev 19212)
+++ trunk/freenet/src/freenet/l10n/freenet.l10n.en.properties   2008-04-11 
23:30:11 UTC (rev 19213)
@@ -842,6 +842,8 @@
 QueueToadlet.completedU=Completed: Uploads (${size})
 QueueToadlet.completedUDirectory=Completed: Directory Uploads (${size})
 QueueToadlet.download=Download
+QueueToadlet.downloadSucceededTitle=Download succeeded: ${filename}
+QueueToadlet.downloadSucceeded=The file ${origlink}${filename}${/origlink} has 
been downloaded successfully. ${link}Click here${/link} to open the file 
(${size}).
 QueueToadlet.errorAccessDenied=Error: Access Denied!
 QueueToadlet.errorAccessDeniedFile=The current configuration of the node 
prohibits you from uploading the file "${file}".
 QueueToadlet.errorDToDisk=Cannot download to disk
@@ -911,11 +913,15 @@
 QueueToadlet.remove=Remove
 QueueToadlet.requestNavigation=Request Navigation
 QueueToadlet.restart=Restart
+QueueToadlet.siteUploadSucceededTitle=Freesite insert succeeded: ${filename}
+QueueToadlet.siteUploadSucceeded=Your freesite ${filename} (${files} files, 
${size} total size) has been successfully uploaded to Freenet. ${link}Click 
here${/link} to open the site homepage.
 QueueToadlet.size=Size
 QueueToadlet.starting=STARTING
 QueueToadlet.title=Global queue of ${nodeName}
 QueueToadlet.totalSize=Total Size
 QueueToadlet.unknown=Unknown
+QueueToadlet.uploadSucceededTitle=Insert succeeded: ${filename}
+QueueToadlet.uploadSucceeded=Your file ${filename} (size ${size}) has been 
successfully uploaded to Freenet. ${link}Click here${/link} to open the file.
 QueueToadlet.warningUnsafeContent=Potentially Unsafe Content
 QueueToadlet.warningUnsafeContentExplanation=The file you want to download is 
currently not filtered by Freenet's content filter! That means that your 
anonymity can be compromised by opening the file!
 QueueToadlet.wipD=In Progress: Downloads (${size})

Modified: trunk/freenet/src/freenet/node/fcp/ClientGet.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/ClientGet.java   2008-04-11 22:00:00 UTC 
(rev 19212)
+++ trunk/freenet/src/freenet/node/fcp/ClientGet.java   2008-04-11 23:30:11 UTC 
(rev 19213)
@@ -415,6 +415,7 @@
                if(!dontFree)
                        data.free();
                finish();
+               client.notifySuccess(this);
        }

        private void trySendDataFoundOrGetFailed(FCPConnectionOutputHandler 
handler) {
@@ -496,6 +497,7 @@
                        Logger.minor(this, "Caught "+e, e);
                trySendDataFoundOrGetFailed(null);
                finish();
+               client.notifyFailure(this);
                if(persistenceType != PERSIST_CONNECTION)
                        client.server.forceStorePersistentRequests();
        }

Modified: trunk/freenet/src/freenet/node/fcp/ClientPutBase.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/ClientPutBase.java       2008-04-11 
22:00:00 UTC (rev 19212)
+++ trunk/freenet/src/freenet/node/fcp/ClientPutBase.java       2008-04-11 
23:30:11 UTC (rev 19213)
@@ -141,6 +141,7 @@
                freeData();
                finish();
                trySendFinalMessage(null);
+               client.notifySuccess(this);
                if(persistenceType != PERSIST_CONNECTION)
                        client.server.forceStorePersistentRequests();
        }
@@ -154,6 +155,7 @@
                freeData();
                finish();
                trySendFinalMessage(null);
+               client.notifyFailure(this);
                if(persistenceType != PERSIST_CONNECTION)
                        client.server.forceStorePersistentRequests();
        }

Modified: trunk/freenet/src/freenet/node/fcp/FCPClient.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPClient.java   2008-04-11 22:00:00 UTC 
(rev 19212)
+++ trunk/freenet/src/freenet/node/fcp/FCPClient.java   2008-04-11 23:30:11 UTC 
(rev 19213)
@@ -18,7 +18,7 @@
  */
 public class FCPClient {

-       public FCPClient(String name2, FCPServer server, FCPConnectionHandler 
handler, boolean isGlobalQueue) {
+       public FCPClient(String name2, FCPServer server, FCPConnectionHandler 
handler, boolean isGlobalQueue, RequestCompletionCallback cb) {
                this.name = name2;
                if(name == null) throw new NullPointerException();
                this.currentConnection = handler;
@@ -35,6 +35,7 @@
                watchGlobalVerbosityMask = Integer.MAX_VALUE;
                toStart = new LinkedList();
                lowLevelClient = this;
+               completionCallback = cb;
        }

        /** The client's Name sent in the ClientHello message */
@@ -66,6 +67,7 @@
        private final LinkedList toStart;
        /** Low-level client object, for freenet.client.async. Normally == 
this. */
        final Object lowLevelClient;
+       private RequestCompletionCallback completionCallback;

        public synchronized FCPConnectionHandler getConnection() {
                return currentConnection;
@@ -254,4 +256,27 @@
        public String toString() {
                return super.toString()+ ':' +name;
        }
+
+       /**
+        * Callback called when a request succeeds.
+        */
+       public void notifySuccess(ClientRequest req) {
+               if(completionCallback != null)
+                       completionCallback.notifySuccess(req);
+       }
+
+       /**
+        * Callback called when a request fails
+        * @param get
+        */
+       public void notifyFailure(ClientRequest req) {
+               if(completionCallback != null)
+                       completionCallback.notifyFailure(req);
+       }
+       
+       public synchronized RequestCompletionCallback 
setRequestCompletionCallback(RequestCompletionCallback cb) {
+               RequestCompletionCallback old = completionCallback;
+               completionCallback = cb;
+               return old;
+       }
 }

Modified: trunk/freenet/src/freenet/node/fcp/FCPServer.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/FCPServer.java   2008-04-11 22:00:00 UTC 
(rev 19212)
+++ trunk/freenet/src/freenet/node/fcp/FCPServer.java   2008-04-11 23:30:11 UTC 
(rev 19213)
@@ -119,7 +119,7 @@
                defaultFetchContext = client.getFetchContext();
                defaultInsertContext = client.getInsertContext(false);

-               globalClient = new FCPClient("Global Queue", this, null, true);
+               globalClient = new FCPClient("Global Queue", this, null, true, 
null);

                logMINOR = Logger.shouldLog(Logger.MINOR, this);

@@ -517,7 +517,7 @@
                        oldClient = (FCPClient) clientsByName.get(name);
                        if(oldClient == null) {
                                // Create new client
-                               FCPClient client = new FCPClient(name, this, 
handler, false);
+                               FCPClient client = new FCPClient(name, this, 
handler, false, null);
                                clientsByName.put(name, client);
                                return client;
                        } else {
@@ -899,4 +899,9 @@
                return hasFinishedStart;
        }

+       public void setCompletionCallback(RequestCompletionCallback cb) {
+               if(globalClient.setRequestCompletionCallback(cb) != null)
+                       Logger.error(this, "Replacing request completion 
callback "+cb, new Exception("error"));
+       }
+       
 }

Added: trunk/freenet/src/freenet/node/fcp/RequestCompletionCallback.java
===================================================================
--- trunk/freenet/src/freenet/node/fcp/RequestCompletionCallback.java           
                (rev 0)
+++ trunk/freenet/src/freenet/node/fcp/RequestCompletionCallback.java   
2008-04-11 23:30:11 UTC (rev 19213)
@@ -0,0 +1,15 @@
+package freenet.node.fcp;
+
+public interface RequestCompletionCallback {
+
+       /**
+        * Callback called when a request succeeds.
+        */
+       public void notifySuccess(ClientRequest req);
+       
+       /**
+        * Callback called when a request fails
+        */
+       public void notifyFailure(ClientRequest req);
+       
+}


Reply via email to