X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=806ac21a10dfc7238cda765d29c7b75516a58220;hb=e58ffeff69ff27c1174114e882cc9eab56b3bf8e;hp=e34e94bf589460d4b46aab7453e9eef70598b38b;hpb=fc5904c02569a36c5ee3707a75cd1fff7d4801a6;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 e34e94b..806ac21 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,10 +95,17 @@ 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(); + /** Locked local Sones. */ + /* synchronize on itself. */ + private final Set lockedSones = new HashSet(); + /** Sone inserters. */ /* synchronize access on this on localSones. */ private final Map soneInserters = new HashMap(); @@ -126,6 +138,12 @@ public class Core implements IdentityListener { /** 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. * @@ -144,6 +162,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 // @@ -157,6 +199,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 @@ -193,6 +245,19 @@ public class Core implements IdentityListener { } /** + * Returns whether the given Sone is currently locked. + * + * @param sone + * The sone to check + * @return {@code true} if the Sone is locked, {@code false} if it is not + */ + public boolean isLocked(Sone sone) { + synchronized (lockedSones) { + return lockedSones.contains(sone); + } + } + + /** * Returns all Sones, remote and local. * * @return All Sones @@ -295,9 +360,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); } @@ -389,11 +468,26 @@ public class Core implements IdentityListener { synchronized (newSones) { 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 @@ -424,6 +518,9 @@ public class Core implements IdentityListener { synchronized (newPosts) { boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId); knownPosts.add(postId); + if (isNew) { + coreListenerManager.fireMarkPostKnown(getPost(postId)); + } return isNew; } } @@ -468,6 +565,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 @@ -506,6 +622,33 @@ public class Core implements IdentityListener { // /** + * Locks the given Sone. A locked Sone will not be inserted by + * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked} + * again. + * + * @param sone + * The sone to lock + */ + public void lockSone(Sone sone) { + synchronized (lockedSones) { + lockedSones.add(sone); + } + } + + /** + * Unlocks the given Sone. + * + * @see #lockSone(Sone) + * @param sone + * The sone to unlock + */ + public void unlockSone(Sone sone) { + synchronized (lockedSones) { + lockedSones.remove(sone); + } + } + + /** * Adds a local Sone from the given ID which has to be the ID of an own * identity. * @@ -544,24 +687,42 @@ 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); + final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone); soneInserters.put(sone, soneInserter); - soneInserter.start(); setSoneStatus(sone, SoneStatus.idle); loadSone(sone); + if (!isSoneRescueMode()) { + soneInserter.start(); + } new Thread(new Runnable() { @Override @SuppressWarnings("synthetic-access") public void run() { - soneDownloader.fetchSone(sone); + if (!isSoneRescueMode()) { + soneDownloader.fetchSone(sone); + return; + } + logger.log(Level.INFO, "Trying to restore Sone from Freenet…"); + coreListenerManager.fireRescuingSone(sone); + lockSone(sone); + long edition = sone.getLatestEdition(); + while (!stopped && (edition >= 0) && isSoneRescueMode()) { + logger.log(Level.FINE, "Downloading edition " + edition + "…"); + soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition)); + --edition; + } + logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…"); + coreListenerManager.fireRescuedSone(sone); + soneInserter.start(); } }, "Sone Downloader").start(); @@ -579,10 +740,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; } @@ -605,7 +762,13 @@ public class Core implements IdentityListener { sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)); if (newSone) { synchronized (newSones) { - newSones.add(sone.getId()); + newSone = !knownSones.contains(sone.getId()); + if (newSone) { + newSones.add(sone.getId()); + } + } + if (newSone) { + coreListenerManager.fireNewSoneFound(sone); } } remoteSones.put(identity.getId(), sone); @@ -632,41 +795,69 @@ public class Core implements IdentityListener { */ public void updateSone(Sone sone) { if (hasSone(sone.getId())) { + boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode(); Sone storedSone = getSone(sone.getId()); - if (!(sone.getTime() > storedSone.getTime())) { + if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) { logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone }); return; } synchronized (posts) { - for (Post post : storedSone.getPosts()) { - posts.remove(post.getId()); + if (!soneRescueMode) { + for (Post post : storedSone.getPosts()) { + posts.remove(post.getId()); + } } synchronized (newPosts) { for (Post post : sone.getPosts()) { - if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) { + if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) { + post.setSone(getSone(post.getSone().getId())); newPosts.add(post.getId()); + coreListenerManager.fireNewPostFound(post); } posts.put(post.getId(), post); } } } synchronized (replies) { - for (Reply reply : storedSone.getReplies()) { - replies.remove(reply.getId()); + if (!soneRescueMode) { + 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()); + if (soneRescueMode) { + for (Post post : sone.getPosts()) { + storedSone.addPost(post); + } + for (Reply reply : sone.getReplies()) { + storedSone.addReply(reply); + } + for (String likedPostId : sone.getLikedPostIds()) { + storedSone.addLikedPostId(likedPostId); + } + for (String likedReplyId : sone.getLikedReplyIds()) { + storedSone.addLikedReplyId(likedReplyId); + } + } else { + storedSone.setPosts(sone.getPosts()); + storedSone.setReplies(sone.getReplies()); + storedSone.setLikePostIds(sone.getLikedPostIds()); + storedSone.setLikeReplyIds(sone.getLikedReplyIds()); + } storedSone.setLatestEdition(sone.getRequestUri().getEdition()); - storedSone.setModificationCounter(0); } } } @@ -721,7 +912,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(); @@ -806,7 +997,7 @@ 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) { @@ -818,6 +1009,11 @@ public class Core implements IdentityListener { knownPosts.add(post.getId()); } } + synchronized (newReplies) { + for (Reply reply : replies) { + knownReplies.add(reply.getId()); + } + } } /** @@ -843,7 +1039,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(); @@ -933,6 +1129,9 @@ public class Core implements IdentityListener { synchronized (posts) { posts.put(post.getId(), post); } + synchronized (newPosts) { + knownPosts.add(post.getId()); + } sone.addPost(post); saveSone(sone); } @@ -964,9 +1163,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); } /** @@ -980,18 +1180,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; } /** @@ -1030,6 +1235,7 @@ public class Core implements IdentityListener { } } saveConfiguration(); + stopped = true; } // @@ -1050,6 +1256,7 @@ public class Core implements IdentityListener { } })); + options.addBooleanOption("SoneRescueMode", new DefaultOption(false)); options.addBooleanOption("ClearOnNextRestart", new DefaultOption(false)); options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption(false)); @@ -1065,6 +1272,7 @@ 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; @@ -1090,6 +1298,18 @@ public class Core implements IdentityListener { } } + /* 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); + } + } + } /** @@ -1099,6 +1319,7 @@ 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()); @@ -1120,6 +1341,15 @@ public class Core implements IdentityListener { 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); }