import java.io.StringWriter;
import java.nio.charset.Charset;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
-import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.pterodactylus.sone.data.Reply;
import net.pterodactylus.sone.data.Sone;
import net.pterodactylus.sone.freenet.StringBucket;
-import net.pterodactylus.util.filter.Filter;
-import net.pterodactylus.util.filter.Filters;
import net.pterodactylus.util.io.Closer;
import net.pterodactylus.util.logging.Logging;
import net.pterodactylus.util.service.AbstractService;
/** The logger. */
private static final Logger logger = Logging.getLogger(SoneInserter.class);
+ /** The insertion delay (in seconds). */
+ private static volatile int insertionDelay = 60;
+
/** The template factory used to create the templates. */
private static final DefaultTemplateFactory templateFactory = new DefaultTemplateFactory();
}
//
+ // ACCESSORS
+ //
+
+ /**
+ * Changes the insertion delay, i.e. the time the Sone inserter waits after
+ * it has noticed a Sone modification before it starts the insert.
+ *
+ * @param insertionDelay
+ * The insertion delay (in seconds)
+ */
+ public static void setInsertionDelay(int insertionDelay) {
+ SoneInserter.insertionDelay = insertionDelay;
+ }
+
+ //
// SERVICE METHODS
//
@Override
protected void serviceRun() {
long modificationCounter = 0;
- boolean restartNow = true;
+ long lastModificationTime = 0;
while (!shouldStop()) {
- if (!restartNow) {
- logger.log(Level.FINEST, "Waiting 60 seconds before checking Sone “" + sone.getName() + "”.");
- sleep(60 * 1000);
- }
- restartNow = false;
+ /* check every seconds. */
+ sleep(1000);
+
InsertInformation insertInformation = null;
synchronized (sone) {
- modificationCounter = sone.getModificationCounter();
- if (modificationCounter > 0) {
- sone.setTime(System.currentTimeMillis());
+ 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").setDocName("Sone-" + sone.getName()).setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
- sone.updateUris(finalUri);
+ FreenetURI finalUri = freenetInterface.insertDirectory(insertInformation.getInsertUri().setKeyType("USK").setSuggestedEdition(0), insertInformation.generateManifestEntries(), "index.html");
+ sone.setLatestEdition(finalUri.getEdition());
success = true;
logger.log(Level.INFO, "Inserted Sone “%s” at %s.", new Object[] { sone.getName(), finalUri });
} catch (SoneException se1) {
if (sone.getModificationCounter() == modificationCounter) {
logger.log(Level.FINE, "Sone “%s” was not modified further, resetting counter…", new Object[] { sone });
sone.setModificationCounter(0);
- } else {
- logger.log(Level.FINE, "Sone “%s” was modified since the insert started, starting another insert…", new Object[] { sone });
- restartNow = true;
+ core.saveSone(sone);
+ modificationCounter = 0;
+ lastModificationTime = 0;
}
}
}
soneProperties.put("profile", sone.getProfile());
soneProperties.put("posts", new ArrayList<Post>(sone.getPosts()));
soneProperties.put("replies", new HashSet<Reply>(sone.getReplies()));
- soneProperties.put("friends", new HashSet<Sone>(sone.getFriends()));
- soneProperties.put("blockedSoneIds", new HashSet<String>(sone.getBlockedSoneIds()));
+ soneProperties.put("likedPostIds", new HashSet<String>(sone.getLikedPostIds()));
+ soneProperties.put("likeReplyIds", new HashSet<String>(sone.getLikedReplyIds()));
}
//
} finally {
Closer.close(templateInputStreamReader);
}
- @SuppressWarnings("unchecked")
- final Set<String> blockedSoneIds = (Set<String>) soneProperties.get("blockedSoneIds");
- Collection<Sone> knownSones = Filters.filteredCollection(core.getKnownSones(), new Filter<Sone>() {
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean filterObject(Sone object) {
- return !blockedSoneIds.contains(object.getId()) && !object.getId().equals(soneProperties.get("id"));
- }
- });
template.set("currentSone", soneProperties);
- template.set("knownSones", knownSones);
StringWriter writer = new StringWriter();
StringBucket bucket = null;
try {