/* synchronize access on this on itself. */
private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
+ /** All new Sones. */
+ private Set<String> newSones = new HashSet<String>();
+
+ /** All known Sones. */
+ /* synchronize access on {@link #newSones}. */
+ private Set<String> knownSones = new HashSet<String>();
+
+ /** All new posts. */
+ private Set<String> newPosts = new HashSet<String>();
+
+ /** All known posts. */
+ /* synchronize access on {@link #newPosts}. */
+ private Set<String> knownPosts = new HashSet<String>();
+
/** All posts. */
private Map<String, Post> posts = new HashMap<String, Post>();
* Sone
*/
public Sone getSone(String id) {
+ return getSone(id, true);
+ }
+
+ /**
+ * Returns the Sone with the given ID, regardless whether it’s local or
+ * remote.
+ *
+ * @param id
+ * The ID of the Sone to get
+ * @param create
+ * {@code true} to create a new Sone if none exists,
+ * {@code false} to return {@code null} if a Sone with the given
+ * ID does not exist
+ * @return The Sone with the given ID, or {@code null} if there is no such
+ * Sone
+ */
+ public Sone getSone(String id, boolean create) {
if (isLocalSone(id)) {
return getLocalSone(id);
}
- return getRemoteSone(id);
+ return getRemoteSone(id, create);
+ }
+
+ /**
+ * Checks whether the core knows a Sone with the given ID.
+ *
+ * @param id
+ * The ID of the Sone
+ * @return {@code true} if there is a Sone with the given ID, {@code false}
+ * otherwise
+ */
+ public boolean hasSone(String id) {
+ return isLocalSone(id) || isRemoteSone(id);
}
/**
* @return The Sone with the given ID
*/
public Sone getRemoteSone(String id) {
+ return getRemoteSone(id, true);
+ }
+
+ /**
+ * Returns the remote Sone with the given ID.
+ *
+ * @param id
+ * The ID of the remote Sone to get
+ * @param create
+ * {@code true} to always create a Sone, {@code false} to return
+ * {@code null} if no Sone with the given ID exists
+ * @return The Sone with the given ID
+ */
+ public Sone getRemoteSone(String id, boolean create) {
synchronized (remoteSones) {
Sone sone = remoteSones.get(id);
- if (sone == null) {
+ if ((sone == null) && create) {
sone = new Sone(id);
remoteSones.put(id, sone);
}
}
/**
+ * Returns whether the Sone with the given ID is a remote Sone.
+ *
+ * @param id
+ * The ID of the Sone to check
+ * @return {@code true} if the Sone with the given ID is a remote Sone,
+ * {@code false} otherwise
+ */
+ public boolean isRemoteSone(String id) {
+ synchronized (remoteSones) {
+ return remoteSones.containsKey(id);
+ }
+ }
+
+ /**
+ * Returns whether the given Sone is a new Sone. After this check, the Sone
+ * is marked as known, i.e. a second call with the same parameters will
+ * always yield {@code false}.
+ *
+ * @param sone
+ * The sone to check for
+ * @return {@code true} if the given Sone is new, false otherwise
+ */
+ public boolean isNewSone(Sone sone) {
+ synchronized (newSones) {
+ boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
+ knownSones.add(sone.getId());
+ return isNew;
+ }
+ }
+
+ /**
* Returns the post with the given ID.
*
* @param postId
}
/**
+ * Returns whether the given post ID is new. After this method returns it is
+ * marked a known post ID.
+ *
+ * @param postId
+ * The post ID
+ * @return {@code true} if the post is considered to be new, {@code false}
+ * otherwise
+ */
+ public boolean isNewPost(String postId) {
+ synchronized (newPosts) {
+ boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
+ knownPosts.add(postId);
+ return isNew;
+ }
+ }
+
+ /**
* Returns the reply with the given ID.
*
* @param replyId
public Set<Sone> getLikes(Reply reply) {
Set<Sone> sones = new HashSet<Sone>();
for (Sone sone : getSones()) {
- if (sone.getLikedPostIds().contains(reply.getId())) {
+ if (sone.getLikedReplyIds().contains(reply.getId())) {
sones.add(sone);
}
}
soneInserter.start();
setSoneStatus(sone, SoneStatus.idle);
loadSone(sone);
+ new Thread(new Runnable() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ soneDownloader.fetchSone(sone);
+ }
+
+ }, "Sone Downloader").start();
return sone;
}
}
}
synchronized (remoteSones) {
final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
- sone.setRequestUri(getSoneUri(identity.getRequestUri(), identity.getProperty("Sone.LatestEdition")));
+ boolean newSone = sone.getRequestUri() == null;
+ sone.setRequestUri(getSoneUri(identity.getRequestUri()));
+ sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ if (newSone) {
+ synchronized (newSones) {
+ newSones.add(sone.getId());
+ }
+ }
remoteSones.put(identity.getId(), sone);
soneDownloader.addSone(sone);
+ setSoneStatus(sone, SoneStatus.unknown);
new Thread(new Runnable() {
@Override
}
}, "Sone Downloader").start();
- setSoneStatus(sone, SoneStatus.idle);
return sone;
}
}
* The updated Sone
*/
public void updateSone(Sone sone) {
- if (isRemoteSone(sone)) {
- Sone storedSone = getRemoteSone(sone.getId());
+ if (hasSone(sone.getId())) {
+ Sone storedSone = getSone(sone.getId());
if (!(sone.getTime() > storedSone.getTime())) {
logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
return;
for (Post post : storedSone.getPosts()) {
posts.remove(post.getId());
}
- for (Post post : sone.getPosts()) {
- posts.put(post.getId(), post);
+ synchronized (newPosts) {
+ for (Post post : sone.getPosts()) {
+ if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) {
+ newPosts.add(post.getId());
+ }
+ posts.put(post.getId(), post);
+ }
}
}
synchronized (replies) {
storedSone.setLikePostIds(sone.getLikedPostIds());
storedSone.setLikeReplyIds(sone.getLikedReplyIds());
storedSone.setLatestEdition(sone.getRequestUri().getEdition());
+ storedSone.setModificationCounter(0);
}
- saveSone(storedSone);
}
}
return;
}
localSones.remove(sone.getId());
- soneInserters.remove(sone.getId()).stop();
+ soneInserters.remove(sone).stop();
}
identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
+ identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
+ try {
+ configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
+ } catch (ConfigurationException ce1) {
+ logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
+ }
}
/**
/* load Sone. */
String sonePrefix = "Sone/" + sone.getId();
- long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue((long) 0);
+ Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
+ if (soneTime == null) {
+ logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
+ return;
+ }
long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
/* load profile. */
}
/* load friends. */
- Set<Sone> friends = new HashSet<Sone>();
+ Set<String> friends = new HashSet<String>();
while (true) {
String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
if (friendId == null) {
break;
}
- Boolean friendLocal = configuration.getBooleanValue(sonePrefix + "/Friends/" + friends.size() + "/Local").getValue(null);
- if (friendLocal == null) {
- logger.log(Level.WARNING, "Invalid friend found, aborting load!");
- return;
- }
- friends.add(friendLocal ? getLocalSone(friendId) : getRemoteSone(friendId));
+ friends.add(friendId);
}
/* if we’re still here, Sone was loaded successfully. */
sone.setFriends(friends);
sone.setModificationCounter(soneModificationCounter);
}
+ synchronized (newSones) {
+ for (String friend : friends) {
+ knownSones.add(friend);
+ }
+ }
+ synchronized (newPosts) {
+ for (Post post : posts) {
+ knownPosts.add(post.getId());
+ }
+ }
}
/**
/* save friends. */
int friendCounter = 0;
- for (Sone friend : sone.getFriends()) {
- configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend.getId());
- configuration.getBooleanValue(sonePrefix + "/Friends/" + friendCounter++ + "/Local").setValue(friend.getInsertUri() != null);
+ for (String friendId : sone.getFriends()) {
+ configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
}
configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
+ /* load known Sones. */
+ int soneCounter = 0;
+ while (true) {
+ String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
+ if (knownSoneId == null) {
+ break;
+ }
+ synchronized (newSones) {
+ knownSones.add(knownSoneId);
+ }
+ }
+
+ /* load known posts. */
+ int postCounter = 0;
+ while (true) {
+ String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
+ if (knownPostId == null) {
+ break;
+ }
+ synchronized (newPosts) {
+ knownPosts.add(knownPostId);
+ }
+ }
+
}
/**
configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
+
+ /* save known Sones. */
+ int soneCounter = 0;
+ synchronized (newSones) {
+ for (String knownSoneId : knownSones) {
+ configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
+ }
+ configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
+ }
+
+ /* save known posts. */
+ int postCounter = 0;
+ synchronized (newPosts) {
+ for (String knownPostId : knownPosts) {
+ configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
+ }
+ configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
+ }
+
} catch (ConfigurationException ce1) {
logger.log(Level.SEVERE, "Could not store configuration!", ce1);
}
*
* @param uriString
* The URI to derive the Sone URI from
- * @param latestEditionString
- * The latest edition as a {@link String}, or {@code null}
* @return The derived URI
*/
- private FreenetURI getSoneUri(String uriString, String latestEditionString) {
+ private FreenetURI getSoneUri(String uriString) {
try {
- FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]).setSuggestedEdition(Numbers.safeParseLong(latestEditionString, (long) 0));
+ FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
return uri;
} catch (MalformedURLException mue1) {
logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
*/
@Override
public void ownIdentityRemoved(OwnIdentity ownIdentity) {
- /* TODO */
+ logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
}
/**
* {@inheritDoc}
*/
@Override
- public void identityUpdated(Identity identity) {
- /* TODO */
+ public void identityUpdated(final Identity identity) {
+ new Thread(new Runnable() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ Sone sone = getRemoteSone(identity.getId());
+ soneDownloader.fetchSone(sone);
+ }
+ }).start();
}
/**