X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=ccf2cd3528957ab696e2c4e624d191b88aa64b72;hb=d09a140deee9781a8e9673618c5f2d91130d896d;hp=055ede4a30c59c5036bdd78730861292ba92a335;hpb=0dc8ce4308f55d8f13e41c7ac312c11a379418b9;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 055ede4..ccf2cd3 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -31,6 +31,7 @@ import java.util.logging.Logger; import net.pterodactylus.sone.core.Options.DefaultOption; import net.pterodactylus.sone.core.Options.Option; import net.pterodactylus.sone.core.Options.OptionWatcher; +import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Reply; @@ -39,6 +40,7 @@ import net.pterodactylus.sone.freenet.wot.Identity; import net.pterodactylus.sone.freenet.wot.IdentityListener; import net.pterodactylus.sone.freenet.wot.IdentityManager; import net.pterodactylus.sone.freenet.wot.OwnIdentity; +import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; import net.pterodactylus.util.logging.Logging; @@ -78,6 +80,9 @@ public class Core implements IdentityListener { /** The options. */ private final Options options = new Options(); + /** The core listener manager. */ + private final CoreListenerManager coreListenerManager = new CoreListenerManager(this); + /** The configuration. */ private final Configuration configuration; @@ -90,6 +95,9 @@ public class Core implements IdentityListener { /** The Sone downloader. */ private final SoneDownloader soneDownloader; + /** Whether the core has been stopped. */ + private volatile boolean stopped; + /** The Sones’ statuses. */ /* synchronize access on itself. */ private final Map soneStatuses = new HashMap(); @@ -107,14 +115,31 @@ public class Core implements IdentityListener { private Map remoteSones = new HashMap(); /** All new Sones. */ - private Set newSones = new HashSet(); + private Set newSones = new HashSet(); + + /** All known Sones. */ + /* synchronize access on {@link #newSones}. */ + private Set knownSones = new HashSet(); /** All posts. */ private Map posts = new HashMap(); + /** All new posts. */ + private Set newPosts = new HashSet(); + + /** All known posts. */ + /* synchronize access on {@link #newPosts}. */ + private Set knownPosts = new HashSet(); + /** All replies. */ private Map replies = new HashMap(); + /** All new replies. */ + private Set newReplies = new HashSet(); + + /** All known replies. */ + private Set knownReplies = new HashSet(); + /** * Creates a new core. * @@ -133,6 +158,30 @@ public class Core implements IdentityListener { } // + // LISTENER MANAGEMENT + // + + /** + * Adds a new core listener. + * + * @param coreListener + * The listener to add + */ + public void addCoreListener(CoreListener coreListener) { + coreListenerManager.addListener(coreListener); + } + + /** + * Removes a core listener. + * + * @param coreListener + * The listener to remove + */ + public void removeCoreListener(CoreListener coreListener) { + coreListenerManager.removeListener(coreListener); + } + + // // ACCESSORS // @@ -146,6 +195,16 @@ public class Core implements IdentityListener { } /** + * Returns whether the “Sone rescue mode” is currently activated. + * + * @return {@code true} if the “Sone rescue mode” is currently activated, + * {@code false} if it is not + */ + public boolean isSoneRescueMode() { + return options.getBooleanOption("SoneRescueMode").get(); + } + + /** * Returns the identity manager used by the core. * * @return The identity manager @@ -203,10 +262,39 @@ public class Core implements IdentityListener { * 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); } /** @@ -255,9 +343,23 @@ public class Core implements IdentityListener { * @return The Sone with the given ID */ public Sone getLocalSone(String id) { + return getLocalSone(id, true); + } + + /** + * Returns the local Sone with the given ID, optionally creating a new Sone. + * + * @param id + * The ID of the Sone + * @param create + * {@code true} to create a new Sone if none exists, + * {@code false} to return null if none exists + * @return The Sone with the given ID, or {@code null} + */ + public Sone getLocalSone(String id, boolean create) { synchronized (localSones) { Sone sone = localSones.get(id); - if (sone == null) { + if ((sone == null) && create) { sone = new Sone(id); localSones.put(id, sone); } @@ -284,9 +386,23 @@ public class Core implements IdentityListener { * @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); } @@ -333,11 +449,28 @@ public class Core implements IdentityListener { */ public boolean isNewSone(Sone sone) { synchronized (newSones) { - return newSones.remove(sone); + boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId()); + knownSones.add(sone.getId()); + if (isNew) { + coreListenerManager.fireMarkSoneKnown(sone); + } + return isNew; } } /** + * Returns whether the given Sone has been modified. + * + * @param sone + * The Sone to check for modifications + * @return {@code true} if a modification has been detected in the Sone, + * {@code false} otherwise + */ + public boolean isModifiedSone(Sone sone) { + return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false; + } + + /** * Returns the post with the given ID. * * @param postId @@ -356,6 +489,26 @@ public class Core implements IdentityListener { } /** + * 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); + if (isNew) { + coreListenerManager.fireMarkPostKnown(getPost(postId)); + } + return isNew; + } + } + + /** * Returns the reply with the given ID. * * @param replyId @@ -395,6 +548,25 @@ public class Core implements IdentityListener { } /** + * Returns whether the reply with the given ID is new. + * + * @param replyId + * The ID of the reply to check + * @return {@code true} if the reply is considered to be new, {@code false} + * otherwise + */ + public boolean isNewReply(String replyId) { + synchronized (newReplies) { + boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId); + knownReplies.add(replyId); + if (isNew) { + coreListenerManager.fireMarkReplyKnown(getReply(replyId)); + } + return isNew; + } + } + + /** * Returns all Sones that have liked the given post. * * @param post @@ -471,18 +643,19 @@ public class Core implements IdentityListener { final Sone sone; try { sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri())); - sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)); } catch (MalformedURLException mue1) { logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1); return null; } + sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)); + sone.setClient(new Client("Sone", SonePlugin.VERSION.toString())); /* TODO - load posts ’n stuff */ localSones.put(ownIdentity.getId(), sone); SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone); soneInserters.put(sone, soneInserter); - soneInserter.start(); setSoneStatus(sone, SoneStatus.idle); loadSone(sone); + soneInserter.start(); new Thread(new Runnable() { @Override @@ -506,10 +679,6 @@ public class Core implements IdentityListener { public Sone createSone(OwnIdentity ownIdentity) { identityManager.addContext(ownIdentity, "Sone"); Sone sone = addLocalSone(ownIdentity); - synchronized (sone) { - /* mark as modified so that it gets inserted immediately. */ - sone.setModificationCounter(sone.getModificationCounter() + 1); - } return sone; } @@ -526,13 +695,19 @@ public class Core implements IdentityListener { return null; } synchronized (remoteSones) { - boolean newSone = !isRemoteSone(identity.getId()); final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity); + 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); + newSone = !knownSones.contains(sone.getId()); + if (newSone) { + newSones.add(sone.getId()); + } + } + if (newSone) { + coreListenerManager.fireNewSoneFound(sone); } } remoteSones.put(identity.getId(), sone); @@ -558,8 +733,8 @@ public class Core implements IdentityListener { * 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; @@ -568,27 +743,39 @@ public class Core implements IdentityListener { 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) && !knownPosts.contains(post.getId())) { + newPosts.add(post.getId()); + coreListenerManager.fireNewPostFound(post); + } + posts.put(post.getId(), post); + } } } synchronized (replies) { for (Reply reply : storedSone.getReplies()) { replies.remove(reply.getId()); } - for (Reply reply : sone.getReplies()) { - replies.put(reply.getId(), reply); + synchronized (newReplies) { + for (Reply reply : sone.getReplies()) { + if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) { + newReplies.add(reply.getId()); + coreListenerManager.fireNewReplyFound(reply); + } + replies.put(reply.getId(), reply); + } } } synchronized (storedSone) { storedSone.setTime(sone.getTime()); + storedSone.setClient(sone.getClient()); storedSone.setProfile(sone.getProfile()); storedSone.setPosts(sone.getPosts()); storedSone.setReplies(sone.getReplies()); storedSone.setLikePostIds(sone.getLikedPostIds()); storedSone.setLikeReplyIds(sone.getLikedReplyIds()); storedSone.setLatestEdition(sone.getRequestUri().getEdition()); - storedSone.setModificationCounter(0); } } } @@ -643,7 +830,7 @@ public class Core implements IdentityListener { logger.log(Level.INFO, "Could not load Sone because no Sone has been saved."); return; } - long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0); + String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue(""); /* load profile. */ Profile profile = new Profile(); @@ -710,18 +897,13 @@ public class Core implements IdentityListener { } /* load friends. */ - Set friends = new HashSet(); + Set friends = new HashSet(); 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. */ @@ -733,7 +915,22 @@ public class Core implements IdentityListener { sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); sone.setFriends(friends); - sone.setModificationCounter(soneModificationCounter); + soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint); + } + synchronized (newSones) { + for (String friend : friends) { + knownSones.add(friend); + } + } + synchronized (newPosts) { + for (Post post : posts) { + knownPosts.add(post.getId()); + } + } + synchronized (newReplies) { + for (Reply reply : replies) { + knownReplies.add(reply.getId()); + } } } @@ -760,7 +957,7 @@ public class Core implements IdentityListener { /* save Sone into configuration. */ String sonePrefix = "Sone/" + sone.getId(); configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime()); - configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter()); + configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint()); /* save profile. */ Profile profile = sone.getProfile(); @@ -808,9 +1005,8 @@ public class Core implements IdentityListener { /* 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); @@ -851,6 +1047,9 @@ public class Core implements IdentityListener { synchronized (posts) { posts.put(post.getId(), post); } + synchronized (newPosts) { + knownPosts.add(post.getId()); + } sone.addPost(post); saveSone(sone); } @@ -882,9 +1081,10 @@ public class Core implements IdentityListener { * The post that this reply refers to * @param text * The text of the reply + * @return The created reply */ - public void createReply(Sone sone, Post post, String text) { - createReply(sone, post, System.currentTimeMillis(), text); + public Reply createReply(Sone sone, Post post, String text) { + return createReply(sone, post, System.currentTimeMillis(), text); } /** @@ -898,18 +1098,23 @@ public class Core implements IdentityListener { * The time of the reply * @param text * The text of the reply + * @return The created reply */ - public void createReply(Sone sone, Post post, long time, String text) { + public Reply createReply(Sone sone, Post post, long time, String text) { if (!isLocalSone(sone)) { logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone); - return; + return null; } Reply reply = new Reply(sone, post, System.currentTimeMillis(), text); synchronized (replies) { replies.put(reply.getId(), reply); } + synchronized (newReplies) { + knownReplies.add(reply.getId()); + } sone.addReply(reply); saveSone(sone); + return reply; } /** @@ -948,6 +1153,7 @@ public class Core implements IdentityListener { } } saveConfiguration(); + stopped = true; } // @@ -968,6 +1174,7 @@ public class Core implements IdentityListener { } })); + options.addBooleanOption("SoneRescueMode", new DefaultOption(false)); options.addBooleanOption("ClearOnNextRestart", new DefaultOption(false)); options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption(false)); @@ -983,6 +1190,43 @@ public class Core implements IdentityListener { } options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null)); + options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").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); + } + } + + /* load known replies. */ + int replyCounter = 0; + while (true) { + String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null); + if (knownReplyId == null) { + break; + } + synchronized (newReplies) { + knownReplies.add(knownReplyId); + } + } } @@ -993,8 +1237,37 @@ public class Core implements IdentityListener { /* store the options first. */ try { configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal()); + configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").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); + } + + /* save known replies. */ + int replyCounter = 0; + synchronized (newReplies) { + for (String knownReplyId : knownReplies) { + configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId); + } + configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null); + } + } catch (ConfigurationException ce1) { logger.log(Level.SEVERE, "Could not store configuration!", ce1); }