- /* check every seconds. */
- sleep(1000);
-
- /* don’t insert locked Sones. */
- if (core.isLocked(sone)) {
- /* trigger redetection when the Sone is unlocked. */
- synchronized (sone) {
- modified = !sone.getFingerprint().equals(lastInsertFingerprint);
- }
- lastFingerprint = "";
- lastModificationTime = 0;
- continue;
- }
-
- InsertInformation insertInformation = null;
- synchronized (sone) {
- String fingerprint = sone.getFingerprint();
- if (!fingerprint.equals(lastFingerprint)) {
- if (fingerprint.equals(lastInsertFingerprint)) {
- modified = false;
- lastModificationTime = 0;
- logger.log(Level.FINE, "Sone %s has been reverted to last insert state.", sone);
- } else {
- lastModificationTime = System.currentTimeMillis();
- modified = true;
- sone.setTime(lastModificationTime);
- logger.log(Level.FINE, "Sone %s has been modified, waiting %d seconds before inserting.", new Object[] { sone.getName(), insertionDelay });
- }
- lastFingerprint = fingerprint;
- }
- if (modified && (lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) {
- lastInsertFingerprint = fingerprint;
- insertInformation = new InsertInformation(sone);
- }
- }
-
- if (insertInformation != null) {
- logger.log(Level.INFO, "Inserting Sone “%s”…", new Object[] { sone.getName() });
-
- boolean success = false;
- try {
- core.setSoneStatus(sone, SoneStatus.inserting);
- FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
- /* at this point we might already be stopped. */
- if (shouldStop()) {
- /* if so, bail out, don’t change anything. */
- break;
+ try {
+ /* check every second. */
+ sleep(delay);
+
+ if (soneModificationDetector.isEligibleForInsert()) {
+ InsertInformation insertInformation = new InsertInformation(sone);
+ logger.log(Level.INFO, String.format("Inserting Sone “%s”…", sone.getName()));
+
+ boolean success = false;
+ try {
+ sone.setStatus(SoneStatus.inserting);
+ long insertTime = currentTimeMillis();
+ eventBus.post(new SoneInsertingEvent(sone));
+ FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri(), insertInformation.generateManifestEntries(), "index.html");
+ eventBus.post(new SoneInsertedEvent(sone, currentTimeMillis() - insertTime));
+ /* at this point we might already be stopped. */
+ if (shouldStop()) {
+ /* if so, bail out, don’t change anything. */
+ break;
+ }
+ sone.setTime(insertTime);
+ sone.setLatestEdition(finalUri.getEdition());
+ core.touchConfiguration();
+ success = true;
+ logger.log(Level.INFO, String.format("Inserted Sone “%s” at %s.", sone.getName(), finalUri));
+ } catch (SoneException se1) {
+ eventBus.post(new SoneInsertAbortedEvent(sone, se1));
+ logger.log(Level.WARNING, String.format("Could not insert Sone “%s”!", sone.getName()), se1);
+ } finally {
+ sone.setStatus(SoneStatus.idle);