Emit upload-finished event when the first message from the node arrives.
[jSite.git] / src / de / todesbaum / jsite / application / ProjectInserter.java
index a881196..46cdbd4 100644 (file)
@@ -49,11 +49,14 @@ import de.todesbaum.util.io.StreamCopier;
 
 /**
  * Manages project inserts.
- * 
+ *
  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
  */
 public class ProjectInserter implements FileScannerListener, Runnable {
 
+       /** Random number for FCP instances. */
+       private static final int random = (int) (Math.random() * Integer.MAX_VALUE);
+
        /** Counter for FCP connection identifier. */
        private static int counter = 0;
 
@@ -77,7 +80,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Adds a listener to the list of registered listeners.
-        * 
+        *
         * @param insertListener
         *            The listener to add
         */
@@ -87,7 +90,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Removes a listener from the list of registered listeners.
-        * 
+        *
         * @param insertListener
         *            The listener to remove
         */
@@ -97,7 +100,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Notifies all listeners that the project insert has started.
-        * 
+        *
         * @see InsertListener#projectInsertStarted(Project)
         */
        protected void fireProjectInsertStarted() {
@@ -108,7 +111,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Notifies all listeners that the insert has generated a URI.
-        * 
+        *
         * @see InsertListener#projectURIGenerated(Project, String)
         * @param uri
         *            The generated URI
@@ -121,7 +124,18 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Notifies all listeners that the insert has made some progress.
-        * 
+        *
+        * @see InsertListener#projectUploadFinished(Project)
+        */
+       protected void fireProjectUploadFinished() {
+               for (InsertListener insertListener : insertListeners) {
+                       insertListener.projectUploadFinished(project);
+               }
+       }
+
+       /**
+        * Notifies all listeners that the insert has made some progress.
+        *
         * @see InsertListener#projectInsertProgress(Project, int, int, int, int,
         *      boolean)
         * @param succeeded
@@ -144,7 +158,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Notifies all listeners the project insert has finished.
-        * 
+        *
         * @see InsertListener#projectInsertFinished(Project, boolean, Throwable)
         * @param success
         *            <code>true</code> if the project was inserted successfully,
@@ -160,7 +174,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Sets the debug mode.
-        * 
+        *
         * @param debug
         *            <code>true</code> to activate debug mode, <code>false</code>
         *            to deactivate
@@ -171,7 +185,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Sets the project to insert.
-        * 
+        *
         * @param project
         *            The project to insert
         */
@@ -181,7 +195,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Sets the freenet interface to use.
-        * 
+        *
         * @param freenetInterface
         *            The freenet interface to use
         */
@@ -201,7 +215,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /**
         * Creates an input stream that delivers the given file, replacing edition
         * tokens in the file’s content, if necessary.
-        * 
+        *
         * @param filename
         *            The name of the file
         * @param fileOption
@@ -245,7 +259,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Creates an input stream for a container.
-        * 
+        *
         * @param containerFiles
         *            All container definitions
         * @param containerName
@@ -294,7 +308,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
        /**
         * Creates a file entry suitable for handing in to
         * {@link ClientPutComplexDir#addFileEntry(FileEntry)}.
-        * 
+        *
         * @param filename
         *            The name of the file to insert
         * @param edition
@@ -333,7 +347,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
        /**
         * Creates container definitions.
-        * 
+        *
         * @param files
         *            The list of all files
         * @param containers
@@ -366,7 +380,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                List<String> files = fileScanner.getFiles();
 
                /* create connection to node */
-               Connection connection = freenetInterface.getConnection("project-insert-" + counter++);
+               Connection connection = freenetInterface.getConnection("project-insert-" + random + counter++);
                boolean connected = false;
                Throwable cause = null;
                try {
@@ -391,7 +405,9 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                int edition = project.getEdition();
                String dirURI = "USK@" + project.getInsertURI() + "/" + project.getPath() + "/" + edition + "/";
                ClientPutComplexDir putDir = new ClientPutComplexDir("dir-" + counter++, dirURI);
-               putDir.setDefaultName(project.getIndexFile());
+               if ((project.getIndexFile() != null) && (project.getIndexFile().length() > 0)) {
+                       putDir.setDefaultName(project.getIndexFile());
+               }
                putDir.setVerbosity(Verbosity.ALL);
                putDir.setMaxRetries(-1);
                for (String filename : files) {
@@ -411,6 +427,7 @@ public class ProjectInserter implements FileScannerListener, Runnable {
 
                /* parse progress and success messages */
                String finalURI = null;
+               boolean firstMessage = true;
                boolean success = false;
                boolean finished = false;
                boolean disconnected = false;
@@ -420,7 +437,12 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                        if (debug) {
                                System.out.println(message);
                        }
+                       if (firstMessage) {
+                               fireProjectUploadFinished();
+                               firstMessage = false;
+                       }
                        if (!finished) {
+                               @SuppressWarnings("null")
                                String messageName = message.getName();
                                if ("URIGenerated".equals(messageName)) {
                                        finalURI = message.get("URI");
@@ -442,9 +464,11 @@ public class ProjectInserter implements FileScannerListener, Runnable {
                /* post-insert work */
                fireProjectInsertFinished(success, disconnected ? new IOException("Connection terminated") : null);
                if (success) {
+                       @SuppressWarnings("null")
                        String editionPart = finalURI.substring(finalURI.lastIndexOf('/') + 1);
                        int newEdition = Integer.parseInt(editionPart);
                        project.setEdition(newEdition);
+                       project.setLastInsertionTime(System.currentTimeMillis());
                }
        }