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 final 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 * Returns the options used by the core.
195 * @return The options of the core
197 public Options getOptions() {
202 * Returns whether the “Sone rescue mode” is currently activated.
204 * @return {@code true} if the “Sone rescue mode” is currently activated,
205 * {@code false} if it is not
207 public boolean isSoneRescueMode() {
208 return options.getBooleanOption("SoneRescueMode").get();
212 * Returns the identity manager used by the core.
214 * @return The identity manager
216 public IdentityManager getIdentityManager() {
217 return identityManager;
221 * Returns the status of the given Sone.
224 * The Sone to get the status for
225 * @return The status of the Sone
227 public SoneStatus getSoneStatus(Sone sone) {
228 synchronized (soneStatuses) {
229 return soneStatuses.get(sone);
234 * Sets the status of the given Sone.
237 * The Sone to set the status of
241 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
242 synchronized (soneStatuses) {
243 soneStatuses.put(sone, soneStatus);
248 * Returns whether the given Sone is currently locked.
252 * @return {@code true} if the Sone is locked, {@code false} if it is not
254 public boolean isLocked(Sone sone) {
255 synchronized (lockedSones) {
256 return lockedSones.contains(sone);
261 * Returns all Sones, remote and local.
265 public Set<Sone> getSones() {
266 Set<Sone> allSones = new HashSet<Sone>();
267 allSones.addAll(getLocalSones());
268 allSones.addAll(getRemoteSones());
273 * Returns the Sone with the given ID, regardless whether it’s local or
277 * The ID of the Sone to get
278 * @return The Sone with the given ID, or {@code null} if there is no such
281 public Sone getSone(String id) {
282 return getSone(id, true);
286 * Returns the Sone with the given ID, regardless whether it’s local or
290 * The ID of the Sone to get
292 * {@code true} to create a new Sone if none exists,
293 * {@code false} to return {@code null} if a Sone with the given
295 * @return The Sone with the given ID, or {@code null} if there is no such
298 public Sone getSone(String id, boolean create) {
299 if (isLocalSone(id)) {
300 return getLocalSone(id);
302 return getRemoteSone(id, create);
306 * Checks whether the core knows a Sone with the given ID.
310 * @return {@code true} if there is a Sone with the given ID, {@code false}
313 public boolean hasSone(String id) {
314 return isLocalSone(id) || isRemoteSone(id);
318 * Returns whether the given Sone is a local Sone.
321 * The Sone to check for its locality
322 * @return {@code true} if the given Sone is local, {@code false} otherwise
324 public boolean isLocalSone(Sone sone) {
325 synchronized (localSones) {
326 return localSones.containsKey(sone.getId());
331 * Returns whether the given ID is the ID of a local Sone.
334 * The Sone ID to check for its locality
335 * @return {@code true} if the given ID is a local Sone, {@code false}
338 public boolean isLocalSone(String id) {
339 synchronized (localSones) {
340 return localSones.containsKey(id);
345 * Returns all local Sones.
347 * @return All local Sones
349 public Set<Sone> getLocalSones() {
350 synchronized (localSones) {
351 return new HashSet<Sone>(localSones.values());
356 * Returns the local Sone with the given ID.
359 * The ID of the Sone to get
360 * @return The Sone with the given ID
362 public Sone getLocalSone(String id) {
363 return getLocalSone(id, true);
367 * Returns the local Sone with the given ID, optionally creating a new Sone.
372 * {@code true} to create a new Sone if none exists,
373 * {@code false} to return null if none exists
374 * @return The Sone with the given ID, or {@code null}
376 public Sone getLocalSone(String id, boolean create) {
377 synchronized (localSones) {
378 Sone sone = localSones.get(id);
379 if ((sone == null) && create) {
381 localSones.put(id, sone);
388 * Returns all remote Sones.
390 * @return All remote Sones
392 public Set<Sone> getRemoteSones() {
393 synchronized (remoteSones) {
394 return new HashSet<Sone>(remoteSones.values());
399 * Returns the remote Sone with the given ID.
402 * The ID of the remote Sone to get
403 * @return The Sone with the given ID
405 public Sone getRemoteSone(String id) {
406 return getRemoteSone(id, true);
410 * Returns the remote Sone with the given ID.
413 * The ID of the remote Sone to get
415 * {@code true} to always create a Sone, {@code false} to return
416 * {@code null} if no Sone with the given ID exists
417 * @return The Sone with the given ID
419 public Sone getRemoteSone(String id, boolean create) {
420 synchronized (remoteSones) {
421 Sone sone = remoteSones.get(id);
422 if ((sone == null) && create) {
424 remoteSones.put(id, sone);
431 * Returns whether the given Sone is a remote Sone.
435 * @return {@code true} if the given Sone is a remote Sone, {@code false}
438 public boolean isRemoteSone(Sone sone) {
439 synchronized (remoteSones) {
440 return remoteSones.containsKey(sone.getId());
445 * Returns whether the Sone with the given ID is a remote Sone.
448 * The ID of the Sone to check
449 * @return {@code true} if the Sone with the given ID is a remote Sone,
450 * {@code false} otherwise
452 public boolean isRemoteSone(String id) {
453 synchronized (remoteSones) {
454 return remoteSones.containsKey(id);
459 * Returns whether the given Sone is a new Sone. After this check, the Sone
460 * is marked as known, i.e. a second call with the same parameters will
461 * always yield {@code false}.
464 * The sone to check for
465 * @return {@code true} if the given Sone is new, false otherwise
467 public boolean isNewSone(Sone sone) {
468 synchronized (newSones) {
469 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
470 knownSones.add(sone.getId());
472 coreListenerManager.fireMarkSoneKnown(sone);
479 * Returns whether the given Sone has been modified.
482 * The Sone to check for modifications
483 * @return {@code true} if a modification has been detected in the Sone,
484 * {@code false} otherwise
486 public boolean isModifiedSone(Sone sone) {
487 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
491 * Returns the post with the given ID.
494 * The ID of the post to get
495 * @return The post, or {@code null} if there is no such post
497 public Post getPost(String postId) {
498 synchronized (posts) {
499 Post post = posts.get(postId);
501 post = new Post(postId);
502 posts.put(postId, post);
509 * Returns whether the given post ID is new. After this method returns it is
510 * marked a known post ID.
514 * @return {@code true} if the post is considered to be new, {@code false}
517 public boolean isNewPost(String postId) {
518 synchronized (newPosts) {
519 boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
520 knownPosts.add(postId);
522 coreListenerManager.fireMarkPostKnown(getPost(postId));
529 * Returns the reply with the given ID.
532 * The ID of the reply to get
533 * @return The reply, or {@code null} if there is no such reply
535 public Reply getReply(String replyId) {
536 synchronized (replies) {
537 Reply reply = replies.get(replyId);
539 reply = new Reply(replyId);
540 replies.put(replyId, reply);
547 * Returns all replies for the given post, order ascending by time.
550 * The post to get all replies for
551 * @return All replies for the given post
553 public List<Reply> getReplies(Post post) {
554 Set<Sone> sones = getSones();
555 List<Reply> replies = new ArrayList<Reply>();
556 for (Sone sone : sones) {
557 for (Reply reply : sone.getReplies()) {
558 if (reply.getPost().equals(post)) {
563 Collections.sort(replies, Reply.TIME_COMPARATOR);
568 * Returns whether the reply with the given ID is new.
571 * The ID of the reply to check
572 * @return {@code true} if the reply is considered to be new, {@code false}
575 public boolean isNewReply(String replyId) {
576 synchronized (newReplies) {
577 boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
578 knownReplies.add(replyId);
580 coreListenerManager.fireMarkReplyKnown(getReply(replyId));
587 * Returns all Sones that have liked the given post.
590 * The post to get the liking Sones for
591 * @return The Sones that like the given post
593 public Set<Sone> getLikes(Post post) {
594 Set<Sone> sones = new HashSet<Sone>();
595 for (Sone sone : getSones()) {
596 if (sone.getLikedPostIds().contains(post.getId())) {
604 * Returns all Sones that have liked the given reply.
607 * The reply to get the liking Sones for
608 * @return The Sones that like the given reply
610 public Set<Sone> getLikes(Reply reply) {
611 Set<Sone> sones = new HashSet<Sone>();
612 for (Sone sone : getSones()) {
613 if (sone.getLikedReplyIds().contains(reply.getId())) {
625 * Locks the given Sone. A locked Sone will not be inserted by
626 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
632 public void lockSone(Sone sone) {
633 synchronized (lockedSones) {
634 lockedSones.add(sone);
639 * Unlocks the given Sone.
641 * @see #lockSone(Sone)
645 public void unlockSone(Sone sone) {
646 synchronized (lockedSones) {
647 lockedSones.remove(sone);
652 * Adds a local Sone from the given ID which has to be the ID of an own
656 * The ID of an own identity to add a Sone for
657 * @return The added (or already existing) Sone
659 public Sone addLocalSone(String id) {
660 synchronized (localSones) {
661 if (localSones.containsKey(id)) {
662 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
663 return localSones.get(id);
665 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
666 if (ownIdentity == null) {
667 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
670 return addLocalSone(ownIdentity);
675 * Adds a local Sone from the given own identity.
678 * The own identity to create a Sone from
679 * @return The added (or already existing) Sone
681 public Sone addLocalSone(OwnIdentity ownIdentity) {
682 if (ownIdentity == null) {
683 logger.log(Level.WARNING, "Given OwnIdentity is null!");
686 synchronized (localSones) {
689 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
690 } catch (MalformedURLException mue1) {
691 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
694 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
695 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
696 /* TODO - load posts ’n stuff */
697 localSones.put(ownIdentity.getId(), sone);
698 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
699 soneInserters.put(sone, soneInserter);
700 setSoneStatus(sone, SoneStatus.idle);
702 if (!isSoneRescueMode()) {
703 soneInserter.start();
705 new Thread(new Runnable() {
708 @SuppressWarnings("synthetic-access")
710 if (!isSoneRescueMode()) {
711 soneDownloader.fetchSone(sone);
714 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
715 long edition = sone.getLatestEdition();
716 while (!stopped && (edition >= 0) && isSoneRescueMode()) {
717 logger.log(Level.FINE, "Downloading edition " + edition + "…");
718 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
721 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
722 soneInserter.start();
725 }, "Sone Downloader").start();
731 * Creates a new Sone for the given own identity.
734 * The own identity to create a Sone for
735 * @return The created Sone
737 public Sone createSone(OwnIdentity ownIdentity) {
738 identityManager.addContext(ownIdentity, "Sone");
739 Sone sone = addLocalSone(ownIdentity);
744 * Adds the Sone of the given identity.
747 * The identity whose Sone to add
748 * @return The added or already existing Sone
750 public Sone addRemoteSone(Identity identity) {
751 if (identity == null) {
752 logger.log(Level.WARNING, "Given Identity is null!");
755 synchronized (remoteSones) {
756 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
757 boolean newSone = sone.getRequestUri() == null;
758 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
759 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
761 synchronized (newSones) {
762 newSone = !knownSones.contains(sone.getId());
764 newSones.add(sone.getId());
768 coreListenerManager.fireNewSoneFound(sone);
771 remoteSones.put(identity.getId(), sone);
772 soneDownloader.addSone(sone);
773 setSoneStatus(sone, SoneStatus.unknown);
774 new Thread(new Runnable() {
777 @SuppressWarnings("synthetic-access")
779 soneDownloader.fetchSone(sone);
782 }, "Sone Downloader").start();
788 * Updates the stores Sone with the given Sone.
793 public void updateSone(Sone sone) {
794 if (hasSone(sone.getId())) {
795 boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode();
796 Sone storedSone = getSone(sone.getId());
797 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
798 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
801 synchronized (posts) {
802 if (!soneRescueMode) {
803 for (Post post : storedSone.getPosts()) {
804 posts.remove(post.getId());
807 synchronized (newPosts) {
808 for (Post post : sone.getPosts()) {
809 if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
810 newPosts.add(post.getId());
811 coreListenerManager.fireNewPostFound(post);
813 posts.put(post.getId(), post);
817 synchronized (replies) {
818 if (!soneRescueMode) {
819 for (Reply reply : storedSone.getReplies()) {
820 replies.remove(reply.getId());
823 synchronized (newReplies) {
824 for (Reply reply : sone.getReplies()) {
825 if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
826 newReplies.add(reply.getId());
827 coreListenerManager.fireNewReplyFound(reply);
829 replies.put(reply.getId(), reply);
833 synchronized (storedSone) {
834 storedSone.setTime(sone.getTime());
835 storedSone.setClient(sone.getClient());
836 storedSone.setProfile(sone.getProfile());
837 if (soneRescueMode) {
838 for (Post post : sone.getPosts()) {
839 storedSone.addPost(post);
841 for (Reply reply : sone.getReplies()) {
842 storedSone.addReply(reply);
844 for (String likedPostId : sone.getLikedPostIds()) {
845 storedSone.addLikedPostId(likedPostId);
847 for (String likedReplyId : sone.getLikedReplyIds()) {
848 storedSone.addLikedReplyId(likedReplyId);
851 storedSone.setPosts(sone.getPosts());
852 storedSone.setReplies(sone.getReplies());
853 storedSone.setLikePostIds(sone.getLikedPostIds());
854 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
856 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
862 * Deletes the given Sone. This will remove the Sone from the
863 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
864 * and remove the context from its identity.
869 public void deleteSone(Sone sone) {
870 if (!(sone.getIdentity() instanceof OwnIdentity)) {
871 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
874 synchronized (localSones) {
875 if (!localSones.containsKey(sone.getId())) {
876 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
879 localSones.remove(sone.getId());
880 soneInserters.remove(sone).stop();
882 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
883 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
885 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
886 } catch (ConfigurationException ce1) {
887 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
892 * Loads and updates the given Sone from the configuration. If any error is
893 * encountered, loading is aborted and the given Sone is not changed.
896 * The Sone to load and update
898 public void loadSone(Sone sone) {
899 if (!isLocalSone(sone)) {
900 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
905 String sonePrefix = "Sone/" + sone.getId();
906 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
907 if (soneTime == null) {
908 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
911 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
914 Profile profile = new Profile();
915 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
916 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
917 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
918 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
919 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
920 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
923 Set<Post> posts = new HashSet<Post>();
925 String postPrefix = sonePrefix + "/Posts/" + posts.size();
926 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
927 if (postId == null) {
930 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
931 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
932 if ((postTime == 0) || (postText == null)) {
933 logger.log(Level.WARNING, "Invalid post found, aborting load!");
936 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
940 Set<Reply> replies = new HashSet<Reply>();
942 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
943 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
944 if (replyId == null) {
947 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
948 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
949 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
950 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
951 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
954 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
957 /* load post likes. */
958 Set<String> likedPostIds = new HashSet<String>();
960 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
961 if (likedPostId == null) {
964 likedPostIds.add(likedPostId);
967 /* load reply likes. */
968 Set<String> likedReplyIds = new HashSet<String>();
970 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
971 if (likedReplyId == null) {
974 likedReplyIds.add(likedReplyId);
978 Set<String> friends = new HashSet<String>();
980 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
981 if (friendId == null) {
984 friends.add(friendId);
987 /* if we’re still here, Sone was loaded successfully. */
988 synchronized (sone) {
989 sone.setTime(soneTime);
990 sone.setProfile(profile);
991 sone.setPosts(posts);
992 sone.setReplies(replies);
993 sone.setLikePostIds(likedPostIds);
994 sone.setLikeReplyIds(likedReplyIds);
995 sone.setFriends(friends);
996 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
998 synchronized (newSones) {
999 for (String friend : friends) {
1000 knownSones.add(friend);
1003 synchronized (newPosts) {
1004 for (Post post : posts) {
1005 knownPosts.add(post.getId());
1008 synchronized (newReplies) {
1009 for (Reply reply : replies) {
1010 knownReplies.add(reply.getId());
1016 * Saves the given Sone. This will persist all local settings for the given
1017 * Sone, such as the friends list and similar, private options.
1022 public void saveSone(Sone sone) {
1023 if (!isLocalSone(sone)) {
1024 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1027 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1028 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1032 logger.log(Level.INFO, "Saving Sone: %s", sone);
1033 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1035 /* save Sone into configuration. */
1036 String sonePrefix = "Sone/" + sone.getId();
1037 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1038 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1041 Profile profile = sone.getProfile();
1042 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1043 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1044 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1045 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1046 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1047 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1050 int postCounter = 0;
1051 for (Post post : sone.getPosts()) {
1052 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1053 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1054 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1055 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1057 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1060 int replyCounter = 0;
1061 for (Reply reply : sone.getReplies()) {
1062 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1063 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1064 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1065 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1066 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1068 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1070 /* save post likes. */
1071 int postLikeCounter = 0;
1072 for (String postId : sone.getLikedPostIds()) {
1073 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1075 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1077 /* save reply likes. */
1078 int replyLikeCounter = 0;
1079 for (String replyId : sone.getLikedReplyIds()) {
1080 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1082 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1085 int friendCounter = 0;
1086 for (String friendId : sone.getFriends()) {
1087 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1089 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1091 logger.log(Level.INFO, "Sone %s saved.", sone);
1092 } catch (ConfigurationException ce1) {
1093 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1098 * Creates a new post.
1101 * The Sone that creates the post
1103 * The text of the post
1105 public void createPost(Sone sone, String text) {
1106 createPost(sone, System.currentTimeMillis(), text);
1110 * Creates a new post.
1113 * The Sone that creates the post
1115 * The time of the post
1117 * The text of the post
1119 public void createPost(Sone sone, long time, String text) {
1120 if (!isLocalSone(sone)) {
1121 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1124 Post post = new Post(sone, time, text);
1125 synchronized (posts) {
1126 posts.put(post.getId(), post);
1128 synchronized (newPosts) {
1129 knownPosts.add(post.getId());
1136 * Deletes the given post.
1139 * The post to delete
1141 public void deletePost(Post post) {
1142 if (!isLocalSone(post.getSone())) {
1143 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1146 post.getSone().removePost(post);
1147 synchronized (posts) {
1148 posts.remove(post.getId());
1150 saveSone(post.getSone());
1154 * Creates a new reply.
1157 * The Sone that creates the reply
1159 * The post that this reply refers to
1161 * The text of the reply
1162 * @return The created reply
1164 public Reply createReply(Sone sone, Post post, String text) {
1165 return createReply(sone, post, System.currentTimeMillis(), text);
1169 * Creates a new reply.
1172 * The Sone that creates the reply
1174 * The post that this reply refers to
1176 * The time of the reply
1178 * The text of the reply
1179 * @return The created reply
1181 public Reply createReply(Sone sone, Post post, long time, String text) {
1182 if (!isLocalSone(sone)) {
1183 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1186 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1187 synchronized (replies) {
1188 replies.put(reply.getId(), reply);
1190 synchronized (newReplies) {
1191 knownReplies.add(reply.getId());
1193 sone.addReply(reply);
1199 * Deletes the given reply.
1202 * The reply to delete
1204 public void deleteReply(Reply reply) {
1205 Sone sone = reply.getSone();
1206 if (!isLocalSone(sone)) {
1207 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1210 synchronized (replies) {
1211 replies.remove(reply.getId());
1213 sone.removeReply(reply);
1220 public void start() {
1221 loadConfiguration();
1227 public void stop() {
1228 synchronized (localSones) {
1229 for (SoneInserter soneInserter : soneInserters.values()) {
1230 soneInserter.stop();
1233 saveConfiguration();
1242 * Loads the configuration.
1244 @SuppressWarnings("unchecked")
1245 private void loadConfiguration() {
1246 /* create options. */
1247 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1250 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1251 SoneInserter.setInsertionDelay(newValue);
1255 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1256 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1257 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1259 /* read options from configuration. */
1260 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1261 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1262 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1263 options.getBooleanOption("ClearOnNextRestart").set(null);
1264 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1265 if (clearConfiguration) {
1266 /* stop loading the configuration. */
1270 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1271 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1273 /* load known Sones. */
1274 int soneCounter = 0;
1276 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1277 if (knownSoneId == null) {
1280 synchronized (newSones) {
1281 knownSones.add(knownSoneId);
1285 /* load known posts. */
1286 int postCounter = 0;
1288 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1289 if (knownPostId == null) {
1292 synchronized (newPosts) {
1293 knownPosts.add(knownPostId);
1297 /* load known replies. */
1298 int replyCounter = 0;
1300 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1301 if (knownReplyId == null) {
1304 synchronized (newReplies) {
1305 knownReplies.add(knownReplyId);
1312 * Saves the current options.
1314 private void saveConfiguration() {
1315 /* store the options first. */
1317 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1318 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1319 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1320 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1322 /* save known Sones. */
1323 int soneCounter = 0;
1324 synchronized (newSones) {
1325 for (String knownSoneId : knownSones) {
1326 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1328 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1331 /* save known posts. */
1332 int postCounter = 0;
1333 synchronized (newPosts) {
1334 for (String knownPostId : knownPosts) {
1335 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1337 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1340 /* save known replies. */
1341 int replyCounter = 0;
1342 synchronized (newReplies) {
1343 for (String knownReplyId : knownReplies) {
1344 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1346 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1349 } catch (ConfigurationException ce1) {
1350 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1355 * Generate a Sone URI from the given URI and latest edition.
1358 * The URI to derive the Sone URI from
1359 * @return The derived URI
1361 private FreenetURI getSoneUri(String uriString) {
1363 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1365 } catch (MalformedURLException mue1) {
1366 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1372 // INTERFACE IdentityListener
1379 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1380 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1381 if (ownIdentity.hasContext("Sone")) {
1382 addLocalSone(ownIdentity);
1390 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1391 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1398 public void identityAdded(Identity identity) {
1399 logger.log(Level.FINEST, "Adding Identity: " + identity);
1400 addRemoteSone(identity);
1407 public void identityUpdated(final Identity identity) {
1408 new Thread(new Runnable() {
1411 @SuppressWarnings("synthetic-access")
1413 Sone sone = getRemoteSone(identity.getId());
1414 soneDownloader.fetchSone(sone);
1423 public void identityRemoved(Identity identity) {