Emit upload-finished event when the first message from the node arrives.
[jSite.git] / src / de / todesbaum / jsite / application / ProjectInserter.java
index 88cb20e..46cdbd4 100644 (file)
@@ -54,6 +54,9 @@ import de.todesbaum.util.io.StreamCopier;
  */
 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;
 
@@ -122,6 +125,17 @@ 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
@@ -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());
                }
        }