- /* check every seconds. */
- sleep(1000);
-
- InsertInformation insertInformation = null;
- synchronized (sone) {
- if (sone.getModificationCounter() > modificationCounter) {
- modificationCounter = sone.getModificationCounter();
- lastModificationTime = System.currentTimeMillis();
- sone.setTime(lastModificationTime);
- logger.log(Level.FINE, "Sone %s has been modified, waiting %d seconds before inserting.", new Object[] { sone.getName(), insertionDelay });
- }
- if ((lastModificationTime > 0) && ((System.currentTimeMillis() - lastModificationTime) > (insertionDelay * 1000))) {
- 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");
- sone.updateUris(finalUri.getEdition());
- /* TODO - better encapsulation? */
- core.getIdentityManager().setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(finalUri.getEdition()));
- success = true;
- logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri });
- } catch (SoneException se1) {
- logger.log(Level.WARNING, "Could not insert Sone “" + sone.getName() + "”!", se1);
- } finally {
- core.setSoneStatus(sone, SoneStatus.idle);
- }
+ 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);
+ }