2 * Sone - Core.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.core.Options.DefaultOption;
32 import net.pterodactylus.sone.core.Options.Option;
33 import net.pterodactylus.sone.core.Options.OptionWatcher;
34 import net.pterodactylus.sone.data.Client;
35 import net.pterodactylus.sone.data.Post;
36 import net.pterodactylus.sone.data.Profile;
37 import net.pterodactylus.sone.data.Reply;
38 import net.pterodactylus.sone.data.Sone;
39 import net.pterodactylus.sone.freenet.wot.Identity;
40 import net.pterodactylus.sone.freenet.wot.IdentityListener;
41 import net.pterodactylus.sone.freenet.wot.IdentityManager;
42 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
43 import net.pterodactylus.sone.main.SonePlugin;
44 import net.pterodactylus.util.config.Configuration;
45 import net.pterodactylus.util.config.ConfigurationException;
46 import net.pterodactylus.util.logging.Logging;
47 import net.pterodactylus.util.number.Numbers;
48 import freenet.keys.FreenetURI;
53 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
55 public class Core implements IdentityListener {
58 * Enumeration for the possible states of a {@link Sone}.
60 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
62 public enum SoneStatus {
64 /** The Sone is unknown, i.e. not yet downloaded. */
67 /** The Sone is idle, i.e. not being downloaded or inserted. */
70 /** The Sone is currently being inserted. */
73 /** The Sone is currently being downloaded. */
78 private static final Logger logger = Logging.getLogger(Core.class);
81 private final Options options = new Options();
83 /** The core listener manager. */
84 private final CoreListenerManager coreListenerManager = new CoreListenerManager(this);
86 /** The configuration. */
87 private Configuration configuration;
89 /** The identity manager. */
90 private final IdentityManager identityManager;
92 /** Interface to freenet. */
93 private final FreenetInterface freenetInterface;
95 /** The Sone downloader. */
96 private final SoneDownloader soneDownloader;
98 /** Whether the core has been stopped. */
99 private volatile boolean stopped;
101 /** The Sones’ statuses. */
102 /* synchronize access on itself. */
103 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
105 /** Locked local Sones. */
106 /* synchronize on itself. */
107 private final Set<Sone> lockedSones = new HashSet<Sone>();
109 /** Sone inserters. */
110 /* synchronize access on this on localSones. */
111 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
113 /** All local Sones. */
114 /* synchronize access on this on itself. */
115 private Map<String, Sone> localSones = new HashMap<String, Sone>();
117 /** All remote Sones. */
118 /* synchronize access on this on itself. */
119 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
121 /** All new Sones. */
122 private Set<String> newSones = new HashSet<String>();
124 /** All known Sones. */
125 /* synchronize access on {@link #newSones}. */
126 private Set<String> knownSones = new HashSet<String>();
129 private Map<String, Post> posts = new HashMap<String, Post>();
131 /** All new posts. */
132 private Set<String> newPosts = new HashSet<String>();
134 /** All known posts. */
135 /* synchronize access on {@link #newPosts}. */
136 private Set<String> knownPosts = new HashSet<String>();
139 private Map<String, Reply> replies = new HashMap<String, Reply>();
141 /** All new replies. */
142 private Set<String> newReplies = new HashSet<String>();
144 /** All known replies. */
145 private Set<String> knownReplies = new HashSet<String>();
148 * Creates a new core.
150 * @param configuration
151 * The configuration of the core
152 * @param freenetInterface
153 * The freenet interface
154 * @param identityManager
155 * The identity manager
157 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
158 this.configuration = configuration;
159 this.freenetInterface = freenetInterface;
160 this.identityManager = identityManager;
161 this.soneDownloader = new SoneDownloader(this, freenetInterface);
165 // LISTENER MANAGEMENT
169 * Adds a new core listener.
171 * @param coreListener
172 * The listener to add
174 public void addCoreListener(CoreListener coreListener) {
175 coreListenerManager.addListener(coreListener);
179 * Removes a core listener.
181 * @param coreListener
182 * The listener to remove
184 public void removeCoreListener(CoreListener coreListener) {
185 coreListenerManager.removeListener(coreListener);
193 * Sets the configuration to use. This will automatically save the current
194 * configuration to the given configuration.
196 * @param configuration
197 * The new configuration to use
199 public void setConfiguration(Configuration configuration) {
200 this.configuration = configuration;
205 * Returns the options used by the core.
207 * @return The options of the core
209 public Options getOptions() {
214 * Returns whether the “Sone rescue mode” is currently activated.
216 * @return {@code true} if the “Sone rescue mode” is currently activated,
217 * {@code false} if it is not
219 public boolean isSoneRescueMode() {
220 return options.getBooleanOption("SoneRescueMode").get();
224 * Returns the identity manager used by the core.
226 * @return The identity manager
228 public IdentityManager getIdentityManager() {
229 return identityManager;
233 * Returns the status of the given Sone.
236 * The Sone to get the status for
237 * @return The status of the Sone
239 public SoneStatus getSoneStatus(Sone sone) {
240 synchronized (soneStatuses) {
241 return soneStatuses.get(sone);
246 * Sets the status of the given Sone.
249 * The Sone to set the status of
253 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
254 synchronized (soneStatuses) {
255 soneStatuses.put(sone, soneStatus);
260 * Returns whether the given Sone is currently locked.
264 * @return {@code true} if the Sone is locked, {@code false} if it is not
266 public boolean isLocked(Sone sone) {
267 synchronized (lockedSones) {
268 return lockedSones.contains(sone);
273 * Returns all Sones, remote and local.
277 public Set<Sone> getSones() {
278 Set<Sone> allSones = new HashSet<Sone>();
279 allSones.addAll(getLocalSones());
280 allSones.addAll(getRemoteSones());
285 * Returns the Sone with the given ID, regardless whether it’s local or
289 * The ID of the Sone to get
290 * @return The Sone with the given ID, or {@code null} if there is no such
293 public Sone getSone(String id) {
294 return getSone(id, true);
298 * Returns the Sone with the given ID, regardless whether it’s local or
302 * The ID of the Sone to get
304 * {@code true} to create a new Sone if none exists,
305 * {@code false} to return {@code null} if a Sone with the given
307 * @return The Sone with the given ID, or {@code null} if there is no such
310 public Sone getSone(String id, boolean create) {
311 if (isLocalSone(id)) {
312 return getLocalSone(id);
314 return getRemoteSone(id, create);
318 * Checks whether the core knows a Sone with the given ID.
322 * @return {@code true} if there is a Sone with the given ID, {@code false}
325 public boolean hasSone(String id) {
326 return isLocalSone(id) || isRemoteSone(id);
330 * Returns whether the given Sone is a local Sone.
333 * The Sone to check for its locality
334 * @return {@code true} if the given Sone is local, {@code false} otherwise
336 public boolean isLocalSone(Sone sone) {
337 synchronized (localSones) {
338 return localSones.containsKey(sone.getId());
343 * Returns whether the given ID is the ID of a local Sone.
346 * The Sone ID to check for its locality
347 * @return {@code true} if the given ID is a local Sone, {@code false}
350 public boolean isLocalSone(String id) {
351 synchronized (localSones) {
352 return localSones.containsKey(id);
357 * Returns all local Sones.
359 * @return All local Sones
361 public Set<Sone> getLocalSones() {
362 synchronized (localSones) {
363 return new HashSet<Sone>(localSones.values());
368 * Returns the local Sone with the given ID.
371 * The ID of the Sone to get
372 * @return The Sone with the given ID
374 public Sone getLocalSone(String id) {
375 return getLocalSone(id, true);
379 * Returns the local Sone with the given ID, optionally creating a new Sone.
384 * {@code true} to create a new Sone if none exists,
385 * {@code false} to return null if none exists
386 * @return The Sone with the given ID, or {@code null}
388 public Sone getLocalSone(String id, boolean create) {
389 synchronized (localSones) {
390 Sone sone = localSones.get(id);
391 if ((sone == null) && create) {
393 localSones.put(id, sone);
400 * Returns all remote Sones.
402 * @return All remote Sones
404 public Set<Sone> getRemoteSones() {
405 synchronized (remoteSones) {
406 return new HashSet<Sone>(remoteSones.values());
411 * Returns the remote Sone with the given ID.
414 * The ID of the remote Sone to get
415 * @return The Sone with the given ID
417 public Sone getRemoteSone(String id) {
418 return getRemoteSone(id, true);
422 * Returns the remote Sone with the given ID.
425 * The ID of the remote Sone to get
427 * {@code true} to always create a Sone, {@code false} to return
428 * {@code null} if no Sone with the given ID exists
429 * @return The Sone with the given ID
431 public Sone getRemoteSone(String id, boolean create) {
432 synchronized (remoteSones) {
433 Sone sone = remoteSones.get(id);
434 if ((sone == null) && create) {
436 remoteSones.put(id, sone);
443 * Returns whether the given Sone is a remote Sone.
447 * @return {@code true} if the given Sone is a remote Sone, {@code false}
450 public boolean isRemoteSone(Sone sone) {
451 synchronized (remoteSones) {
452 return remoteSones.containsKey(sone.getId());
457 * Returns whether the Sone with the given ID is a remote Sone.
460 * The ID of the Sone to check
461 * @return {@code true} if the Sone with the given ID is a remote Sone,
462 * {@code false} otherwise
464 public boolean isRemoteSone(String id) {
465 synchronized (remoteSones) {
466 return remoteSones.containsKey(id);
471 * Returns whether the given Sone is a new Sone. After this check, the Sone
472 * is marked as known, i.e. a second call with the same parameters will
473 * always yield {@code false}.
476 * The sone to check for
477 * @return {@code true} if the given Sone is new, false otherwise
479 public boolean isNewSone(Sone sone) {
480 synchronized (newSones) {
481 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
482 knownSones.add(sone.getId());
484 coreListenerManager.fireMarkSoneKnown(sone);
491 * Returns whether the given Sone has been modified.
494 * The Sone to check for modifications
495 * @return {@code true} if a modification has been detected in the Sone,
496 * {@code false} otherwise
498 public boolean isModifiedSone(Sone sone) {
499 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
503 * Returns the post with the given ID.
506 * The ID of the post to get
507 * @return The post, or {@code null} if there is no such post
509 public Post getPost(String postId) {
510 return getPost(postId, true);
514 * Returns the post with the given ID, optionally creating a new post.
517 * The ID of the post to get
519 * {@code true} it create a new post if no post with the given ID
520 * exists, {@code false} to return {@code null}
521 * @return The post, or {@code null} if there is no such post
523 public Post getPost(String postId, boolean create) {
524 synchronized (posts) {
525 Post post = posts.get(postId);
526 if ((post == null) && create) {
527 post = new Post(postId);
528 posts.put(postId, post);
535 * Returns whether the given post ID is new. After this method returns it is
536 * marked a known post ID.
540 * @return {@code true} if the post is considered to be new, {@code false}
543 public boolean isNewPost(String postId) {
544 return isNewPost(postId, true);
548 * Returns whether the given post ID is new. If {@code markAsKnown} is
549 * {@code true} then after this method returns the post ID is marked a known
555 * {@code true} to mark the post ID as known, {@code false} to
556 * not to mark it as known
557 * @return {@code true} if the post is considered to be new, {@code false}
560 public boolean isNewPost(String postId, boolean markAsKnown) {
561 synchronized (newPosts) {
562 boolean isNew = !knownPosts.contains(postId) && newPosts.contains(postId);
564 Post post = getPost(postId, false);
574 * Returns the reply with the given ID. If there is no reply with the given
575 * ID yet, a new one is created.
578 * The ID of the reply to get
581 public Reply getReply(String replyId) {
582 return getReply(replyId, true);
586 * Returns the reply with the given ID. If there is no reply with the given
587 * ID yet, a new one is created, unless {@code create} is false in which
588 * case {@code null} is returned.
591 * The ID of the reply to get
593 * {@code true} to always return a {@link Reply}, {@code false}
594 * to return {@code null} if no reply can be found
595 * @return The reply, or {@code null} if there is no such reply
597 public Reply getReply(String replyId, boolean create) {
598 synchronized (replies) {
599 Reply reply = replies.get(replyId);
600 if (create && (reply == null)) {
601 reply = new Reply(replyId);
602 replies.put(replyId, reply);
609 * Returns all replies for the given post, order ascending by time.
612 * The post to get all replies for
613 * @return All replies for the given post
615 public List<Reply> getReplies(Post post) {
616 Set<Sone> sones = getSones();
617 List<Reply> replies = new ArrayList<Reply>();
618 for (Sone sone : sones) {
619 for (Reply reply : sone.getReplies()) {
620 if (reply.getPost().equals(post)) {
625 Collections.sort(replies, Reply.TIME_COMPARATOR);
630 * Returns whether the reply with the given ID is new.
633 * The ID of the reply to check
634 * @return {@code true} if the reply is considered to be new, {@code false}
637 public boolean isNewReply(String replyId) {
638 return isNewReply(replyId, true);
642 * Returns whether the reply with the given ID is new.
645 * The ID of the reply to check
647 * {@code true} to mark the reply as known, {@code false} to not
648 * to mark it as known
649 * @return {@code true} if the reply is considered to be new, {@code false}
652 public boolean isNewReply(String replyId, boolean markAsKnown) {
653 synchronized (newReplies) {
654 boolean isNew = !knownReplies.contains(replyId) && newReplies.contains(replyId);
656 Reply reply = getReply(replyId, false);
658 markReplyKnown(reply);
666 * Returns all Sones that have liked the given post.
669 * The post to get the liking Sones for
670 * @return The Sones that like the given post
672 public Set<Sone> getLikes(Post post) {
673 Set<Sone> sones = new HashSet<Sone>();
674 for (Sone sone : getSones()) {
675 if (sone.getLikedPostIds().contains(post.getId())) {
683 * Returns all Sones that have liked the given reply.
686 * The reply to get the liking Sones for
687 * @return The Sones that like the given reply
689 public Set<Sone> getLikes(Reply reply) {
690 Set<Sone> sones = new HashSet<Sone>();
691 for (Sone sone : getSones()) {
692 if (sone.getLikedReplyIds().contains(reply.getId())) {
704 * Locks the given Sone. A locked Sone will not be inserted by
705 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
711 public void lockSone(Sone sone) {
712 synchronized (lockedSones) {
713 if (lockedSones.add(sone)) {
714 coreListenerManager.fireSoneLocked(sone);
720 * Unlocks the given Sone.
722 * @see #lockSone(Sone)
726 public void unlockSone(Sone sone) {
727 synchronized (lockedSones) {
728 if (lockedSones.remove(sone)) {
729 coreListenerManager.fireSoneUnlocked(sone);
735 * Adds a local Sone from the given ID which has to be the ID of an own
739 * The ID of an own identity to add a Sone for
740 * @return The added (or already existing) Sone
742 public Sone addLocalSone(String id) {
743 synchronized (localSones) {
744 if (localSones.containsKey(id)) {
745 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
746 return localSones.get(id);
748 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
749 if (ownIdentity == null) {
750 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
753 return addLocalSone(ownIdentity);
758 * Adds a local Sone from the given own identity.
761 * The own identity to create a Sone from
762 * @return The added (or already existing) Sone
764 public Sone addLocalSone(OwnIdentity ownIdentity) {
765 if (ownIdentity == null) {
766 logger.log(Level.WARNING, "Given OwnIdentity is null!");
769 synchronized (localSones) {
772 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
773 } catch (MalformedURLException mue1) {
774 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
777 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
778 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
779 /* TODO - load posts ’n stuff */
780 localSones.put(ownIdentity.getId(), sone);
781 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
782 soneInserters.put(sone, soneInserter);
783 setSoneStatus(sone, SoneStatus.idle);
785 if (!isSoneRescueMode()) {
786 soneInserter.start();
788 new Thread(new Runnable() {
791 @SuppressWarnings("synthetic-access")
793 if (!isSoneRescueMode()) {
794 soneDownloader.fetchSone(sone);
797 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
798 coreListenerManager.fireRescuingSone(sone);
800 long edition = sone.getLatestEdition();
801 while (!stopped && (edition >= 0) && isSoneRescueMode()) {
802 logger.log(Level.FINE, "Downloading edition " + edition + "…");
803 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
806 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
808 coreListenerManager.fireRescuedSone(sone);
809 soneInserter.start();
812 }, "Sone Downloader").start();
818 * Creates a new Sone for the given own identity.
821 * The own identity to create a Sone for
822 * @return The created Sone
824 public Sone createSone(OwnIdentity ownIdentity) {
825 identityManager.addContext(ownIdentity, "Sone");
826 Sone sone = addLocalSone(ownIdentity);
831 * Adds the Sone of the given identity.
834 * The identity whose Sone to add
835 * @return The added or already existing Sone
837 public Sone addRemoteSone(Identity identity) {
838 if (identity == null) {
839 logger.log(Level.WARNING, "Given Identity is null!");
842 synchronized (remoteSones) {
843 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
844 boolean newSone = sone.getRequestUri() == null;
845 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
846 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
848 synchronized (newSones) {
849 newSone = !knownSones.contains(sone.getId());
851 newSones.add(sone.getId());
855 coreListenerManager.fireNewSoneFound(sone);
858 remoteSones.put(identity.getId(), sone);
859 soneDownloader.addSone(sone);
860 setSoneStatus(sone, SoneStatus.unknown);
861 new Thread(new Runnable() {
864 @SuppressWarnings("synthetic-access")
866 soneDownloader.fetchSone(sone);
869 }, "Sone Downloader").start();
875 * Updates the stores Sone with the given Sone.
880 public void updateSone(Sone sone) {
881 if (hasSone(sone.getId())) {
882 boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode();
883 Sone storedSone = getSone(sone.getId());
884 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
885 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
888 synchronized (posts) {
889 if (!soneRescueMode) {
890 for (Post post : storedSone.getPosts()) {
891 posts.remove(post.getId());
892 if (!sone.getPosts().contains(post)) {
893 coreListenerManager.firePostRemoved(post);
897 synchronized (newPosts) {
898 for (Post post : sone.getPosts()) {
899 post.setSone(getSone(post.getSone().getId()));
900 if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
901 newPosts.add(post.getId());
902 coreListenerManager.fireNewPostFound(post);
904 posts.put(post.getId(), post);
908 synchronized (replies) {
909 if (!soneRescueMode) {
910 for (Reply reply : storedSone.getReplies()) {
911 replies.remove(reply.getId());
912 if (!sone.getReplies().contains(reply)) {
913 coreListenerManager.fireReplyRemoved(reply);
917 synchronized (newReplies) {
918 for (Reply reply : sone.getReplies()) {
919 reply.setSone(getSone(reply.getSone().getId()));
920 if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
921 newReplies.add(reply.getId());
922 coreListenerManager.fireNewReplyFound(reply);
924 replies.put(reply.getId(), reply);
928 synchronized (storedSone) {
929 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
930 storedSone.setTime(sone.getTime());
932 storedSone.setClient(sone.getClient());
933 storedSone.setProfile(sone.getProfile());
934 if (soneRescueMode) {
935 for (Post post : sone.getPosts()) {
936 storedSone.addPost(post);
938 for (Reply reply : sone.getReplies()) {
939 storedSone.addReply(reply);
941 for (String likedPostId : sone.getLikedPostIds()) {
942 storedSone.addLikedPostId(likedPostId);
944 for (String likedReplyId : sone.getLikedReplyIds()) {
945 storedSone.addLikedReplyId(likedReplyId);
948 storedSone.setPosts(sone.getPosts());
949 storedSone.setReplies(sone.getReplies());
950 storedSone.setLikePostIds(sone.getLikedPostIds());
951 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
953 storedSone.setLatestEdition(sone.getLatestEdition());
959 * Deletes the given Sone. This will remove the Sone from the
960 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
961 * and remove the context from its identity.
966 public void deleteSone(Sone sone) {
967 if (!(sone.getIdentity() instanceof OwnIdentity)) {
968 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
971 synchronized (localSones) {
972 if (!localSones.containsKey(sone.getId())) {
973 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
976 localSones.remove(sone.getId());
977 soneInserters.remove(sone).stop();
979 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
980 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
982 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
983 } catch (ConfigurationException ce1) {
984 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
989 * Loads and updates the given Sone from the configuration. If any error is
990 * encountered, loading is aborted and the given Sone is not changed.
993 * The Sone to load and update
995 public void loadSone(Sone sone) {
996 if (!isLocalSone(sone)) {
997 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1002 String sonePrefix = "Sone/" + sone.getId();
1003 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1004 if (soneTime == null) {
1005 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1008 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1011 Profile profile = new Profile();
1012 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1013 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1014 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1015 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1016 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1017 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1020 Set<Post> posts = new HashSet<Post>();
1022 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1023 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1024 if (postId == null) {
1027 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1028 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1029 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1030 if ((postTime == 0) || (postText == null)) {
1031 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1034 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1035 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1036 post.setRecipient(getSone(postRecipientId));
1042 Set<Reply> replies = new HashSet<Reply>();
1044 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1045 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1046 if (replyId == null) {
1049 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1050 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1051 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1052 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1053 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1056 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1059 /* load post likes. */
1060 Set<String> likedPostIds = new HashSet<String>();
1062 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1063 if (likedPostId == null) {
1066 likedPostIds.add(likedPostId);
1069 /* load reply likes. */
1070 Set<String> likedReplyIds = new HashSet<String>();
1072 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1073 if (likedReplyId == null) {
1076 likedReplyIds.add(likedReplyId);
1080 Set<String> friends = new HashSet<String>();
1082 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1083 if (friendId == null) {
1086 friends.add(friendId);
1089 /* if we’re still here, Sone was loaded successfully. */
1090 synchronized (sone) {
1091 sone.setTime(soneTime);
1092 sone.setProfile(profile);
1093 sone.setPosts(posts);
1094 sone.setReplies(replies);
1095 sone.setLikePostIds(likedPostIds);
1096 sone.setLikeReplyIds(likedReplyIds);
1097 sone.setFriends(friends);
1098 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1100 synchronized (newSones) {
1101 for (String friend : friends) {
1102 knownSones.add(friend);
1105 synchronized (newPosts) {
1106 for (Post post : posts) {
1107 knownPosts.add(post.getId());
1110 synchronized (newReplies) {
1111 for (Reply reply : replies) {
1112 knownReplies.add(reply.getId());
1118 * Saves the given Sone. This will persist all local settings for the given
1119 * Sone, such as the friends list and similar, private options.
1124 public synchronized void saveSone(Sone sone) {
1125 if (!isLocalSone(sone)) {
1126 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1129 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1130 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1134 logger.log(Level.INFO, "Saving Sone: %s", sone);
1135 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1137 /* save Sone into configuration. */
1138 String sonePrefix = "Sone/" + sone.getId();
1139 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1140 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1143 Profile profile = sone.getProfile();
1144 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1145 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1146 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1147 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1148 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1149 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1152 int postCounter = 0;
1153 for (Post post : sone.getPosts()) {
1154 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1155 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1156 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1157 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1158 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1160 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1163 int replyCounter = 0;
1164 for (Reply reply : sone.getReplies()) {
1165 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1166 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1167 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1168 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1169 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1171 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1173 /* save post likes. */
1174 int postLikeCounter = 0;
1175 for (String postId : sone.getLikedPostIds()) {
1176 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1178 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1180 /* save reply likes. */
1181 int replyLikeCounter = 0;
1182 for (String replyId : sone.getLikedReplyIds()) {
1183 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1185 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1188 int friendCounter = 0;
1189 for (String friendId : sone.getFriends()) {
1190 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1192 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1194 configuration.save();
1195 logger.log(Level.INFO, "Sone %s saved.", sone);
1196 } catch (ConfigurationException ce1) {
1197 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1202 * Creates a new post.
1205 * The Sone that creates the post
1207 * The text of the post
1208 * @return The created post
1210 public Post createPost(Sone sone, String text) {
1211 return createPost(sone, System.currentTimeMillis(), text);
1215 * Creates a new post.
1218 * The Sone that creates the post
1220 * The time of the post
1222 * The text of the post
1223 * @return The created post
1225 public Post createPost(Sone sone, long time, String text) {
1226 return createPost(sone, null, time, text);
1230 * Creates a new post.
1233 * The Sone that creates the post
1235 * The recipient Sone, or {@code null} if this post does not have
1238 * The text of the post
1239 * @return The created post
1241 public Post createPost(Sone sone, Sone recipient, String text) {
1242 return createPost(sone, recipient, System.currentTimeMillis(), text);
1246 * Creates a new post.
1249 * The Sone that creates the post
1251 * The recipient Sone, or {@code null} if this post does not have
1254 * The time of the post
1256 * The text of the post
1257 * @return The created post
1259 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1260 if (!isLocalSone(sone)) {
1261 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1264 Post post = new Post(sone, time, text);
1265 if (recipient != null) {
1266 post.setRecipient(recipient);
1268 synchronized (posts) {
1269 posts.put(post.getId(), post);
1271 synchronized (newPosts) {
1272 knownPosts.add(post.getId());
1280 * Deletes the given post.
1283 * The post to delete
1285 public void deletePost(Post post) {
1286 if (!isLocalSone(post.getSone())) {
1287 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1290 post.getSone().removePost(post);
1291 synchronized (posts) {
1292 posts.remove(post.getId());
1294 saveSone(post.getSone());
1298 * Marks the given post as known, if it is currently a new post (according
1299 * to {@link #isNewPost(String)}).
1302 * The post to mark as known
1304 public void markPostKnown(Post post) {
1305 synchronized (newPosts) {
1306 if (newPosts.remove(post.getId())) {
1307 knownPosts.add(post.getId());
1308 coreListenerManager.fireMarkPostKnown(post);
1314 * Creates a new reply.
1317 * The Sone that creates the reply
1319 * The post that this reply refers to
1321 * The text of the reply
1322 * @return The created reply
1324 public Reply createReply(Sone sone, Post post, String text) {
1325 return createReply(sone, post, System.currentTimeMillis(), text);
1329 * Creates a new reply.
1332 * The Sone that creates the reply
1334 * The post that this reply refers to
1336 * The time of the reply
1338 * The text of the reply
1339 * @return The created reply
1341 public Reply createReply(Sone sone, Post post, long time, String text) {
1342 if (!isLocalSone(sone)) {
1343 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1346 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1347 synchronized (replies) {
1348 replies.put(reply.getId(), reply);
1350 synchronized (newReplies) {
1351 knownReplies.add(reply.getId());
1353 sone.addReply(reply);
1359 * Deletes the given reply.
1362 * The reply to delete
1364 public void deleteReply(Reply reply) {
1365 Sone sone = reply.getSone();
1366 if (!isLocalSone(sone)) {
1367 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1370 synchronized (replies) {
1371 replies.remove(reply.getId());
1373 sone.removeReply(reply);
1378 * Marks the given reply as known, if it is currently a new reply (according
1379 * to {@link #isNewReply(String)}).
1382 * The reply to mark as known
1384 public void markReplyKnown(Reply reply) {
1385 synchronized (newReplies) {
1386 if (newReplies.remove(reply.getId())) {
1387 knownReplies.add(reply.getId());
1388 coreListenerManager.fireMarkReplyKnown(reply);
1396 public void start() {
1397 loadConfiguration();
1403 public void stop() {
1404 synchronized (localSones) {
1405 for (SoneInserter soneInserter : soneInserters.values()) {
1406 soneInserter.stop();
1409 saveConfiguration();
1414 * Saves the current options.
1416 public synchronized void saveConfiguration() {
1417 /* store the options first. */
1419 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1420 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1421 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1422 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1424 /* save known Sones. */
1425 int soneCounter = 0;
1426 synchronized (newSones) {
1427 for (String knownSoneId : knownSones) {
1428 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1430 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1433 /* save known posts. */
1434 int postCounter = 0;
1435 synchronized (newPosts) {
1436 for (String knownPostId : knownPosts) {
1437 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1439 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1442 /* save known replies. */
1443 int replyCounter = 0;
1444 synchronized (newReplies) {
1445 for (String knownReplyId : knownReplies) {
1446 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1448 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1452 configuration.save();
1454 } catch (ConfigurationException ce1) {
1455 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1464 * Loads the configuration.
1466 @SuppressWarnings("unchecked")
1467 private void loadConfiguration() {
1468 /* create options. */
1469 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1472 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1473 SoneInserter.setInsertionDelay(newValue);
1477 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1478 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1479 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1481 /* read options from configuration. */
1482 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1483 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1484 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1485 options.getBooleanOption("ClearOnNextRestart").set(null);
1486 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1487 if (clearConfiguration) {
1488 /* stop loading the configuration. */
1492 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1493 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1495 /* load known Sones. */
1496 int soneCounter = 0;
1498 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1499 if (knownSoneId == null) {
1502 synchronized (newSones) {
1503 knownSones.add(knownSoneId);
1507 /* load known posts. */
1508 int postCounter = 0;
1510 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1511 if (knownPostId == null) {
1514 synchronized (newPosts) {
1515 knownPosts.add(knownPostId);
1519 /* load known replies. */
1520 int replyCounter = 0;
1522 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1523 if (knownReplyId == null) {
1526 synchronized (newReplies) {
1527 knownReplies.add(knownReplyId);
1534 * Generate a Sone URI from the given URI and latest edition.
1537 * The URI to derive the Sone URI from
1538 * @return The derived URI
1540 private FreenetURI getSoneUri(String uriString) {
1542 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1544 } catch (MalformedURLException mue1) {
1545 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1551 // INTERFACE IdentityListener
1558 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1559 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1560 if (ownIdentity.hasContext("Sone")) {
1561 addLocalSone(ownIdentity);
1569 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1570 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1577 public void identityAdded(Identity identity) {
1578 logger.log(Level.FINEST, "Adding Identity: " + identity);
1579 addRemoteSone(identity);
1586 public void identityUpdated(final Identity identity) {
1587 new Thread(new Runnable() {
1590 @SuppressWarnings("synthetic-access")
1592 Sone sone = getRemoteSone(identity.getId());
1593 soneDownloader.fetchSone(sone);
1602 public void identityRemoved(Identity identity) {