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=81e5e09ff571c26be946d8755119d51c04861c36;hb=8c1a4ad1d3f11ba0a11a077c47df48425360e0ac;hpb=756202d801245428d09fa0f5174fde0a8b001bd0 diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 81e5e09..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,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.core.event.ImageInsertFinishedEvent; import net.pterodactylus.sone.core.event.MarkPostKnownEvent; import net.pterodactylus.sone.core.event.MarkPostReplyKnownEvent; import net.pterodactylus.sone.core.event.MarkSoneKnownEvent; @@ -42,7 +43,10 @@ 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; @@ -59,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; @@ -74,11 +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; @@ -88,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); @@ -102,9 +110,6 @@ 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; @@ -215,37 +220,13 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis 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; this.eventBus = eventBus; } // - // 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 // @@ -378,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); } @@ -797,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)); } } } @@ -812,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)); } } } @@ -842,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); @@ -1133,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)); } } } @@ -1223,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"); @@ -1909,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(); @@ -1943,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()); } @@ -1951,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(); } @@ -2338,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())); @@ -2355,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 @@ -2393,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()) { @@ -2428,7 +2419,7 @@ 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)); } } } @@ -2438,87 +2429,18 @@ public class Core extends AbstractService implements IdentityListener, UpdateLis eventBus.post(new SoneRemovedEvent(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); - } - - /** - * {@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); } /**