X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=82fb4c5b004328db135fe3942551f23284b75fed;hp=2a2eb8f8a59c7701584ada61193f68a8913cb99c;hb=8c1a4ad1d3f11ba0a11a077c47df48425360e0ac;hpb=299b20708098b51ae3dd48dae5d7f7e4934285b8 diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 2a2eb8f..82fb4c5 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -35,6 +35,18 @@ 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.core.event.ImageInsertFinishedEvent; +import net.pterodactylus.sone.core.event.MarkPostKnownEvent; +import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent; +import net.pterodactylus.sone.core.event.MarkSoneKnownEvent; +import net.pterodactylus.sone.core.event.NewPostFoundEvent; +import net.pterodactylus.sone.core.event.NewPostReplyFoundEvent; +import net.pterodactylus.sone.core.event.NewSoneFoundEvent; +import net.pterodactylus.sone.core.event.PostRemovedEvent; +import net.pterodactylus.sone.core.event.PostReplyRemovedEvent; +import net.pterodactylus.sone.core.event.SoneLockedEvent; +import net.pterodactylus.sone.core.event.SoneRemovedEvent; +import net.pterodactylus.sone.core.event.SoneUnlockedEvent; import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Client; import net.pterodactylus.sone.data.Image; @@ -51,9 +63,13 @@ import net.pterodactylus.sone.data.impl.PostImpl; import net.pterodactylus.sone.fcp.FcpInterface; import net.pterodactylus.sone.fcp.FcpInterface.FullAccessRequired; 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.freenet.wot.event.IdentityAddedEvent; +import net.pterodactylus.sone.freenet.wot.event.IdentityRemovedEvent; +import net.pterodactylus.sone.freenet.wot.event.IdentityUpdatedEvent; +import net.pterodactylus.sone.freenet.wot.event.OwnIdentityAddedEvent; +import net.pterodactylus.sone.freenet.wot.event.OwnIdentityRemovedEvent; import net.pterodactylus.sone.main.SonePlugin; import net.pterodactylus.util.config.Configuration; import net.pterodactylus.util.config.ConfigurationException; @@ -66,10 +82,11 @@ import net.pterodactylus.util.validation.EqualityValidator; import net.pterodactylus.util.validation.IntegerRangeValidator; import net.pterodactylus.util.validation.OrValidator; import net.pterodactylus.util.validation.Validation; -import net.pterodactylus.util.version.Version; import com.google.common.base.Predicate; import com.google.common.collect.Collections2; +import com.google.common.eventbus.EventBus; +import com.google.common.eventbus.Subscribe; import com.google.inject.Inject; import freenet.keys.FreenetURI; @@ -79,7 +96,7 @@ import freenet.keys.FreenetURI; * * @author David ‘Bombe’ Roden */ -public class Core extends AbstractService implements IdentityListener, UpdateListener, SoneProvider, PostProvider, SoneInsertListener, ImageInsertListener { +public class Core extends AbstractService implements SoneProvider, PostProvider { /** The logger. */ private static final Logger logger = Logging.getLogger(Core.class); @@ -93,8 +110,8 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /** The preferences. */ private final Preferences preferences = new Preferences(options); - /** The core listener manager. */ - private final CoreListenerManager coreListenerManager = new CoreListenerManager(this); + /** The event bus. */ + private final EventBus eventBus; /** The configuration. */ private Configuration configuration; @@ -193,41 +210,20 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis * The identity manager * @param webOfTrustUpdater * The WebOfTrust updater + * @param eventBus + * The event bus */ @Inject - public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater) { + public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager, WebOfTrustUpdater webOfTrustUpdater, EventBus eventBus) { super("Sone Core"); this.configuration = configuration; this.freenetInterface = freenetInterface; this.identityManager = identityManager; this.soneDownloader = new SoneDownloader(this, freenetInterface); - this.imageInserter = new ImageInserter(this, freenetInterface); - this.updateChecker = new UpdateChecker(freenetInterface); + this.imageInserter = new ImageInserter(freenetInterface); + this.updateChecker = new UpdateChecker(eventBus, freenetInterface); this.webOfTrustUpdater = webOfTrustUpdater; - } - - // - // 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); + this.eventBus = eventBus; } // @@ -363,7 +359,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis @Override public Sone getSone(String id, boolean create) { synchronized (sones) { - if (!sones.containsKey(id)) { + if (!sones.containsKey(id) && create) { Sone sone = new Sone(id, false); sones.put(id, sone); } @@ -782,7 +778,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis public void lockSone(Sone sone) { synchronized (lockedSones) { if (lockedSones.add(sone)) { - coreListenerManager.fireSoneLocked(sone); + eventBus.post(new SoneLockedEvent(sone)); } } } @@ -797,7 +793,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis public void unlockSone(Sone sone) { synchronized (lockedSones) { if (lockedSones.remove(sone)) { - coreListenerManager.fireSoneUnlocked(sone); + eventBus.post(new SoneUnlockedEvent(sone)); } } } @@ -827,8 +823,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis sone.setKnown(true); /* TODO - load posts ’n stuff */ sones.put(ownIdentity.getId(), sone); - final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone); - soneInserter.addSoneInsertListener(this); + final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone); soneInserters.put(sone, soneInserter); sone.setStatus(SoneStatus.idle); loadSone(sone); @@ -885,7 +880,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } sone.setKnown(!newSone); if (newSone) { - coreListenerManager.fireNewSoneFound(sone); + eventBus.post(new NewSoneFoundEvent(sone)); for (Sone localSone : getLocalSones()) { if (localSone.getOptions().getBooleanOption("AutoFollow").get()) { followSone(localSone, sone); @@ -1093,7 +1088,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis for (Post post : storedSone.getPosts()) { posts.remove(post.getId()); if (!sone.getPosts().contains(post)) { - coreListenerManager.firePostRemoved(post); + eventBus.post(new PostRemovedEvent(post)); } } } @@ -1106,7 +1101,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis knownPosts.add(post.getId()); post.setKnown(true); } else if (!knownPosts.contains(post.getId())) { - coreListenerManager.fireNewPostFound(post); + eventBus.post(new NewPostFoundEvent(post)); } } posts.put(post.getId(), post); @@ -1118,7 +1113,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis for (PostReply reply : storedSone.getReplies()) { replies.remove(reply.getId()); if (!sone.getReplies().contains(reply)) { - coreListenerManager.fireReplyRemoved(reply); + eventBus.post(new PostReplyRemovedEvent(reply)); } } } @@ -1131,7 +1126,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis knownReplies.add(reply.getId()); reply.setKnown(true); } else if (!knownReplies.contains(reply.getId())) { - coreListenerManager.fireNewReplyFound(reply); + eventBus.post(new NewPostReplyFoundEvent(reply)); } } replies.put(reply.getId(), reply); @@ -1208,7 +1203,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } sones.remove(sone.getId()); SoneInserter soneInserter = soneInserters.remove(sone); - soneInserter.removeSoneInsertListener(this); soneInserter.stop(); } webOfTrustUpdater.removeContext((OwnIdentity) sone.getIdentity(), "Sone"); @@ -1222,7 +1216,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis /** * Marks the given Sone as known. If the Sone was not {@link Post#isKnown() - * known} before, a {@link CoreListener#markSoneKnown(Sone)} event is fired. + * known} before, a {@link MarkSoneKnownEvent} is fired. * * @param sone * The Sone to mark as known @@ -1233,7 +1227,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (knownSones) { knownSones.add(sone.getId()); } - coreListenerManager.fireMarkSoneKnown(sone); + eventBus.post(new MarkSoneKnownEvent(sone)); touchConfiguration(); } } @@ -1535,7 +1529,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (posts) { posts.put(post.getId(), post); } - coreListenerManager.fireNewPostFound(post); + eventBus.post(new NewPostFoundEvent(post)); sone.addPost(post); touchConfiguration(); localElementTicker.registerEvent(System.currentTimeMillis() + 10 * 1000, new Runnable() { @@ -1566,7 +1560,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (posts) { posts.remove(post.getId()); } - coreListenerManager.firePostRemoved(post); + eventBus.post(new PostRemovedEvent(post)); markPostKnown(post); touchConfiguration(); } @@ -1581,7 +1575,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis public void markPostKnown(Post post) { post.setKnown(true); synchronized (knownPosts) { - coreListenerManager.fireMarkPostKnown(post); + eventBus.post(new MarkPostKnownEvent(post)); if (knownPosts.add(post.getId())) { touchConfiguration(); } @@ -1674,7 +1668,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis replies.put(reply.getId(), reply); } synchronized (knownReplies) { - coreListenerManager.fireNewReplyFound(reply); + eventBus.post(new NewPostReplyFoundEvent(reply)); } sone.addReply(reply); touchConfiguration(); @@ -1724,7 +1718,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis public void markReplyKnown(PostReply reply) { reply.setKnown(true); synchronized (knownReplies) { - coreListenerManager.fireMarkReplyKnown(reply); + eventBus.post(new MarkPostReplyKnownEvent(reply)); if (knownReplies.add(reply.getId())) { touchConfiguration(); } @@ -1894,9 +1888,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis @Override public void serviceStart() { loadConfiguration(); - updateChecker.addUpdateListener(this); updateChecker.start(); - identityManager.addIdentityListener(this); identityManager.start(); webOfTrustUpdater.init(); webOfTrustUpdater.start(); @@ -1928,7 +1920,6 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis public void serviceStop() { synchronized (sones) { for (Entry soneInserter : soneInserters.entrySet()) { - soneInserter.getValue().removeSoneInsertListener(this); soneInserter.getValue().stop(); saveSone(soneInserter.getKey()); } @@ -1936,9 +1927,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis saveConfiguration(); webOfTrustUpdater.stop(); updateChecker.stop(); - updateChecker.removeUpdateListener(this); soneDownloader.stop(); - identityManager.removeIdentityListener(this); identityManager.stop(); } @@ -2323,15 +2312,15 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } } - // - // INTERFACE IdentityListener - // - /** - * {@inheritDoc} + * Notifies the core that a new {@link OwnIdentity} was added. + * + * @param ownIdentityAddedEvent + * The event */ - @Override - public void ownIdentityAdded(OwnIdentity ownIdentity) { + @Subscribe + public void ownIdentityAdded(OwnIdentityAddedEvent ownIdentityAddedEvent) { + OwnIdentity ownIdentity = ownIdentityAddedEvent.ownIdentity(); logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity)); if (ownIdentity.hasContext("Sone")) { trustedIdentities.put(ownIdentity, Collections.synchronizedSet(new HashSet())); @@ -2340,29 +2329,41 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /** - * {@inheritDoc} + * Notifies the core that an {@link OwnIdentity} was removed. + * + * @param ownIdentityRemovedEvent + * The event */ - @Override - public void ownIdentityRemoved(OwnIdentity ownIdentity) { + @Subscribe + public void ownIdentityRemoved(OwnIdentityRemovedEvent ownIdentityRemovedEvent) { + OwnIdentity ownIdentity = ownIdentityRemovedEvent.ownIdentity(); logger.log(Level.FINEST, String.format("Removing OwnIdentity: %s", ownIdentity)); trustedIdentities.remove(ownIdentity); } /** - * {@inheritDoc} + * Notifies the core that a new {@link Identity} was added. + * + * @param identityAddedEvent + * The event */ - @Override - public void identityAdded(OwnIdentity ownIdentity, Identity identity) { + @Subscribe + public void identityAdded(IdentityAddedEvent identityAddedEvent) { + Identity identity = identityAddedEvent.identity(); logger.log(Level.FINEST, String.format("Adding Identity: %s", identity)); - trustedIdentities.get(ownIdentity).add(identity); + trustedIdentities.get(identityAddedEvent.ownIdentity()).add(identity); addRemoteSone(identity); } /** - * {@inheritDoc} + * Notifies the core that an {@link Identity} was updated. + * + * @param identityUpdatedEvent + * The event */ - @Override - public void identityUpdated(OwnIdentity ownIdentity, final Identity identity) { + @Subscribe + public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) { + final Identity identity = identityUpdatedEvent.identity(); soneDownloaders.execute(new Runnable() { @Override @@ -2378,10 +2379,15 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis } /** - * {@inheritDoc} + * Notifies the core that an {@link Identity} was removed. + * + * @param identityRemovedEvent + * The event */ - @Override - public void identityRemoved(OwnIdentity ownIdentity, Identity identity) { + @Subscribe + public void identityRemoved(IdentityRemovedEvent identityRemovedEvent) { + OwnIdentity ownIdentity = identityRemovedEvent.ownIdentity(); + Identity identity = identityRemovedEvent.identity(); trustedIdentities.get(ownIdentity).remove(identity); boolean foundIdentity = false; for (Entry> trustedIdentity : trustedIdentities.entrySet()) { @@ -2405,7 +2411,7 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (knownPosts) { for (Post post : sone.getPosts()) { posts.remove(post.getId()); - coreListenerManager.firePostRemoved(post); + eventBus.post(new PostRemovedEvent(post)); } } } @@ -2413,97 +2419,28 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis synchronized (knownReplies) { for (PostReply reply : sone.getReplies()) { replies.remove(reply.getId()); - coreListenerManager.fireReplyRemoved(reply); + eventBus.post(new PostReplyRemovedEvent(reply)); } } } synchronized (sones) { sones.remove(identity.getId()); } - coreListenerManager.fireSoneRemoved(sone); - } - - // - // INTERFACE UpdateListener - // - - /** - * {@inheritDoc} - */ - @Override - public void updateFound(Version version, long releaseTime, long latestEdition) { - coreListenerManager.fireUpdateFound(version, releaseTime, latestEdition); - } - - // - // INTERFACE ImageInsertListener - // - - /** - * {@inheritDoc} - */ - @Override - public void insertStarted(Sone sone) { - coreListenerManager.fireSoneInserting(sone); - } - - /** - * {@inheritDoc} - */ - @Override - public void insertFinished(Sone sone, long insertDuration) { - coreListenerManager.fireSoneInserted(sone, insertDuration); + eventBus.post(new SoneRemovedEvent(sone)); } /** - * {@inheritDoc} - */ - @Override - public void insertAborted(Sone sone, Throwable cause) { - coreListenerManager.fireSoneInsertAborted(sone, cause); - } - - // - // SONEINSERTLISTENER METHODS - // - - /** - * {@inheritDoc} - */ - @Override - public void imageInsertStarted(Image image) { - logger.log(Level.WARNING, String.format("Image insert started for %s...", image)); - coreListenerManager.fireImageInsertStarted(image); - } - - /** - * {@inheritDoc} - */ - @Override - public void imageInsertAborted(Image image) { - logger.log(Level.WARNING, String.format("Image insert aborted for %s.", image)); - coreListenerManager.fireImageInsertAborted(image); - } - - /** - * {@inheritDoc} + * Deletes the temporary image. + * + * @param imageInsertFinishedEvent + * The event */ - @Override - public void imageInsertFinished(Image image, FreenetURI key) { - logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", image, key)); - image.setKey(key.toString()); - deleteTemporaryImage(image.getId()); + @Subscribe + public void imageInsertFinished(ImageInsertFinishedEvent imageInsertFinishedEvent) { + logger.log(Level.WARNING, String.format("Image insert finished for %s: %s", imageInsertFinishedEvent.image(), imageInsertFinishedEvent.resultingUri())); + imageInsertFinishedEvent.image().setKey(imageInsertFinishedEvent.resultingUri().toString()); + deleteTemporaryImage(imageInsertFinishedEvent.image().getId()); touchConfiguration(); - coreListenerManager.fireImageInsertFinished(image); - } - - /** - * {@inheritDoc} - */ - @Override - public void imageInsertFailed(Image image, Throwable cause) { - logger.log(Level.WARNING, String.format("Image insert failed for %s." + image), cause); - coreListenerManager.fireImageInsertFailed(image, cause); } /**