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 /** Whether we’re currently saving the configuration. */
90 private boolean storingConfiguration = false;
92 /** The identity manager. */
93 private final IdentityManager identityManager;
95 /** Interface to freenet. */
96 private final FreenetInterface freenetInterface;
98 /** The Sone downloader. */
99 private final SoneDownloader soneDownloader;
101 /** Whether the core has been stopped. */
102 private volatile boolean stopped;
104 /** The Sones’ statuses. */
105 /* synchronize access on itself. */
106 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
108 /** Locked local Sones. */
109 /* synchronize on itself. */
110 private final Set<Sone> lockedSones = new HashSet<Sone>();
112 /** Sone inserters. */
113 /* synchronize access on this on localSones. */
114 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
116 /** All local Sones. */
117 /* synchronize access on this on itself. */
118 private Map<String, Sone> localSones = new HashMap<String, Sone>();
120 /** All remote Sones. */
121 /* synchronize access on this on itself. */
122 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
124 /** All new Sones. */
125 private Set<String> newSones = new HashSet<String>();
127 /** All known Sones. */
128 /* synchronize access on {@link #newSones}. */
129 private Set<String> knownSones = new HashSet<String>();
132 private Map<String, Post> posts = new HashMap<String, Post>();
134 /** All new posts. */
135 private Set<String> newPosts = new HashSet<String>();
137 /** All known posts. */
138 /* synchronize access on {@link #newPosts}. */
139 private Set<String> knownPosts = new HashSet<String>();
142 private Map<String, Reply> replies = new HashMap<String, Reply>();
144 /** All new replies. */
145 private Set<String> newReplies = new HashSet<String>();
147 /** All known replies. */
148 private Set<String> knownReplies = new HashSet<String>();
151 * Creates a new core.
153 * @param configuration
154 * The configuration of the core
155 * @param freenetInterface
156 * The freenet interface
157 * @param identityManager
158 * The identity manager
160 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
161 this.configuration = configuration;
162 this.freenetInterface = freenetInterface;
163 this.identityManager = identityManager;
164 this.soneDownloader = new SoneDownloader(this, freenetInterface);
168 // LISTENER MANAGEMENT
172 * Adds a new core listener.
174 * @param coreListener
175 * The listener to add
177 public void addCoreListener(CoreListener coreListener) {
178 coreListenerManager.addListener(coreListener);
182 * Removes a core listener.
184 * @param coreListener
185 * The listener to remove
187 public void removeCoreListener(CoreListener coreListener) {
188 coreListenerManager.removeListener(coreListener);
196 * Sets the configuration to use. This will automatically save the current
197 * configuration to the given configuration.
199 * @param configuration
200 * The new configuration to use
202 public void setConfiguration(Configuration configuration) {
203 this.configuration = configuration;
208 * Returns the options used by the core.
210 * @return The options of the core
212 public Options getOptions() {
217 * Returns whether the “Sone rescue mode” is currently activated.
219 * @return {@code true} if the “Sone rescue mode” is currently activated,
220 * {@code false} if it is not
222 public boolean isSoneRescueMode() {
223 return options.getBooleanOption("SoneRescueMode").get();
227 * Returns the identity manager used by the core.
229 * @return The identity manager
231 public IdentityManager getIdentityManager() {
232 return identityManager;
236 * Returns the status of the given Sone.
239 * The Sone to get the status for
240 * @return The status of the Sone
242 public SoneStatus getSoneStatus(Sone sone) {
243 synchronized (soneStatuses) {
244 return soneStatuses.get(sone);
249 * Sets the status of the given Sone.
252 * The Sone to set the status of
256 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
257 synchronized (soneStatuses) {
258 soneStatuses.put(sone, soneStatus);
263 * Returns whether the given Sone is currently locked.
267 * @return {@code true} if the Sone is locked, {@code false} if it is not
269 public boolean isLocked(Sone sone) {
270 synchronized (lockedSones) {
271 return lockedSones.contains(sone);
276 * Returns all Sones, remote and local.
280 public Set<Sone> getSones() {
281 Set<Sone> allSones = new HashSet<Sone>();
282 allSones.addAll(getLocalSones());
283 allSones.addAll(getRemoteSones());
288 * Returns the Sone with the given ID, regardless whether it’s local or
292 * The ID of the Sone to get
293 * @return The Sone with the given ID, or {@code null} if there is no such
296 public Sone getSone(String id) {
297 return getSone(id, true);
301 * Returns the Sone with the given ID, regardless whether it’s local or
305 * The ID of the Sone to get
307 * {@code true} to create a new Sone if none exists,
308 * {@code false} to return {@code null} if a Sone with the given
310 * @return The Sone with the given ID, or {@code null} if there is no such
313 public Sone getSone(String id, boolean create) {
314 if (isLocalSone(id)) {
315 return getLocalSone(id);
317 return getRemoteSone(id, create);
321 * Checks whether the core knows a Sone with the given ID.
325 * @return {@code true} if there is a Sone with the given ID, {@code false}
328 public boolean hasSone(String id) {
329 return isLocalSone(id) || isRemoteSone(id);
333 * Returns whether the given Sone is a local Sone.
336 * The Sone to check for its locality
337 * @return {@code true} if the given Sone is local, {@code false} otherwise
339 public boolean isLocalSone(Sone sone) {
340 synchronized (localSones) {
341 return localSones.containsKey(sone.getId());
346 * Returns whether the given ID is the ID of a local Sone.
349 * The Sone ID to check for its locality
350 * @return {@code true} if the given ID is a local Sone, {@code false}
353 public boolean isLocalSone(String id) {
354 synchronized (localSones) {
355 return localSones.containsKey(id);
360 * Returns all local Sones.
362 * @return All local Sones
364 public Set<Sone> getLocalSones() {
365 synchronized (localSones) {
366 return new HashSet<Sone>(localSones.values());
371 * Returns the local Sone with the given ID.
374 * The ID of the Sone to get
375 * @return The Sone with the given ID
377 public Sone getLocalSone(String id) {
378 return getLocalSone(id, true);
382 * Returns the local Sone with the given ID, optionally creating a new Sone.
387 * {@code true} to create a new Sone if none exists,
388 * {@code false} to return null if none exists
389 * @return The Sone with the given ID, or {@code null}
391 public Sone getLocalSone(String id, boolean create) {
392 synchronized (localSones) {
393 Sone sone = localSones.get(id);
394 if ((sone == null) && create) {
396 localSones.put(id, sone);
403 * Returns all remote Sones.
405 * @return All remote Sones
407 public Set<Sone> getRemoteSones() {
408 synchronized (remoteSones) {
409 return new HashSet<Sone>(remoteSones.values());
414 * Returns the remote Sone with the given ID.
417 * The ID of the remote Sone to get
418 * @return The Sone with the given ID
420 public Sone getRemoteSone(String id) {
421 return getRemoteSone(id, true);
425 * Returns the remote Sone with the given ID.
428 * The ID of the remote Sone to get
430 * {@code true} to always create a Sone, {@code false} to return
431 * {@code null} if no Sone with the given ID exists
432 * @return The Sone with the given ID
434 public Sone getRemoteSone(String id, boolean create) {
435 synchronized (remoteSones) {
436 Sone sone = remoteSones.get(id);
437 if ((sone == null) && create) {
439 remoteSones.put(id, sone);
446 * Returns whether the given Sone is a remote Sone.
450 * @return {@code true} if the given Sone is a remote Sone, {@code false}
453 public boolean isRemoteSone(Sone sone) {
454 synchronized (remoteSones) {
455 return remoteSones.containsKey(sone.getId());
460 * Returns whether the Sone with the given ID is a remote Sone.
463 * The ID of the Sone to check
464 * @return {@code true} if the Sone with the given ID is a remote Sone,
465 * {@code false} otherwise
467 public boolean isRemoteSone(String id) {
468 synchronized (remoteSones) {
469 return remoteSones.containsKey(id);
474 * Returns whether the given Sone is a new Sone. After this check, the Sone
475 * is marked as known, i.e. a second call with the same parameters will
476 * always yield {@code false}.
479 * The sone to check for
480 * @return {@code true} if the given Sone is new, false otherwise
482 public boolean isNewSone(Sone sone) {
483 synchronized (newSones) {
484 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
485 knownSones.add(sone.getId());
487 coreListenerManager.fireMarkSoneKnown(sone);
494 * Returns whether the given Sone has been modified.
497 * The Sone to check for modifications
498 * @return {@code true} if a modification has been detected in the Sone,
499 * {@code false} otherwise
501 public boolean isModifiedSone(Sone sone) {
502 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
506 * Returns the post with the given ID.
509 * The ID of the post to get
510 * @return The post, or {@code null} if there is no such post
512 public Post getPost(String postId) {
513 return getPost(postId, true);
517 * Returns the post with the given ID, optionally creating a new post.
520 * The ID of the post to get
522 * {@code true} it create a new post if no post with the given ID
523 * exists, {@code false} to return {@code null}
524 * @return The post, or {@code null} if there is no such post
526 public Post getPost(String postId, boolean create) {
527 synchronized (posts) {
528 Post post = posts.get(postId);
529 if ((post == null) && create) {
530 post = new Post(postId);
531 posts.put(postId, post);
538 * Returns whether the given post ID is new. After this method returns it is
539 * marked a known post ID.
543 * @return {@code true} if the post is considered to be new, {@code false}
546 public boolean isNewPost(String postId) {
547 return isNewPost(postId, true);
551 * Returns whether the given post ID is new. If {@code markAsKnown} is
552 * {@code true} then after this method returns the post ID is marked a known
558 * {@code true} to mark the post ID as known, {@code false} to
559 * not to mark it as known
560 * @return {@code true} if the post is considered to be new, {@code false}
563 public boolean isNewPost(String postId, boolean markAsKnown) {
564 synchronized (newPosts) {
565 boolean isNew = !knownPosts.contains(postId) && newPosts.contains(postId);
567 Post post = getPost(postId, false);
577 * Returns the reply with the given ID. If there is no reply with the given
578 * ID yet, a new one is created.
581 * The ID of the reply to get
584 public Reply getReply(String replyId) {
585 return getReply(replyId, true);
589 * Returns the reply with the given ID. If there is no reply with the given
590 * ID yet, a new one is created, unless {@code create} is false in which
591 * case {@code null} is returned.
594 * The ID of the reply to get
596 * {@code true} to always return a {@link Reply}, {@code false}
597 * to return {@code null} if no reply can be found
598 * @return The reply, or {@code null} if there is no such reply
600 public Reply getReply(String replyId, boolean create) {
601 synchronized (replies) {
602 Reply reply = replies.get(replyId);
603 if (create && (reply == null)) {
604 reply = new Reply(replyId);
605 replies.put(replyId, reply);
612 * Returns all replies for the given post, order ascending by time.
615 * The post to get all replies for
616 * @return All replies for the given post
618 public List<Reply> getReplies(Post post) {
619 Set<Sone> sones = getSones();
620 List<Reply> replies = new ArrayList<Reply>();
621 for (Sone sone : sones) {
622 for (Reply reply : sone.getReplies()) {
623 if (reply.getPost().equals(post)) {
628 Collections.sort(replies, Reply.TIME_COMPARATOR);
633 * Returns whether the reply with the given ID is new.
636 * The ID of the reply to check
637 * @return {@code true} if the reply is considered to be new, {@code false}
640 public boolean isNewReply(String replyId) {
641 return isNewReply(replyId, true);
645 * Returns whether the reply with the given ID is new.
648 * The ID of the reply to check
650 * {@code true} to mark the reply as known, {@code false} to not
651 * to mark it as known
652 * @return {@code true} if the reply is considered to be new, {@code false}
655 public boolean isNewReply(String replyId, boolean markAsKnown) {
656 synchronized (newReplies) {
657 boolean isNew = !knownReplies.contains(replyId) && newReplies.contains(replyId);
659 Reply reply = getReply(replyId, false);
661 markReplyKnown(reply);
669 * Returns all Sones that have liked the given post.
672 * The post to get the liking Sones for
673 * @return The Sones that like the given post
675 public Set<Sone> getLikes(Post post) {
676 Set<Sone> sones = new HashSet<Sone>();
677 for (Sone sone : getSones()) {
678 if (sone.getLikedPostIds().contains(post.getId())) {
686 * Returns all Sones that have liked the given reply.
689 * The reply to get the liking Sones for
690 * @return The Sones that like the given reply
692 public Set<Sone> getLikes(Reply reply) {
693 Set<Sone> sones = new HashSet<Sone>();
694 for (Sone sone : getSones()) {
695 if (sone.getLikedReplyIds().contains(reply.getId())) {
707 * Locks the given Sone. A locked Sone will not be inserted by
708 * {@link SoneInserter} until it is {@link #unlockSone(Sone) unlocked}
714 public void lockSone(Sone sone) {
715 synchronized (lockedSones) {
716 if (lockedSones.add(sone)) {
717 coreListenerManager.fireSoneLocked(sone);
723 * Unlocks the given Sone.
725 * @see #lockSone(Sone)
729 public void unlockSone(Sone sone) {
730 synchronized (lockedSones) {
731 if (lockedSones.remove(sone)) {
732 coreListenerManager.fireSoneUnlocked(sone);
738 * Adds a local Sone from the given ID which has to be the ID of an own
742 * The ID of an own identity to add a Sone for
743 * @return The added (or already existing) Sone
745 public Sone addLocalSone(String id) {
746 synchronized (localSones) {
747 if (localSones.containsKey(id)) {
748 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
749 return localSones.get(id);
751 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
752 if (ownIdentity == null) {
753 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
756 return addLocalSone(ownIdentity);
761 * Adds a local Sone from the given own identity.
764 * The own identity to create a Sone from
765 * @return The added (or already existing) Sone
767 public Sone addLocalSone(OwnIdentity ownIdentity) {
768 if (ownIdentity == null) {
769 logger.log(Level.WARNING, "Given OwnIdentity is null!");
772 synchronized (localSones) {
775 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
776 } catch (MalformedURLException mue1) {
777 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
780 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
781 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
782 /* TODO - load posts ’n stuff */
783 localSones.put(ownIdentity.getId(), sone);
784 final SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
785 soneInserters.put(sone, soneInserter);
786 setSoneStatus(sone, SoneStatus.idle);
788 if (!isSoneRescueMode()) {
789 soneInserter.start();
791 new Thread(new Runnable() {
794 @SuppressWarnings("synthetic-access")
796 if (!isSoneRescueMode()) {
797 soneDownloader.fetchSone(sone);
800 logger.log(Level.INFO, "Trying to restore Sone from Freenet…");
801 coreListenerManager.fireRescuingSone(sone);
803 long edition = sone.getLatestEdition();
804 while (!stopped && (edition >= 0) && isSoneRescueMode()) {
805 logger.log(Level.FINE, "Downloading edition " + edition + "…");
806 soneDownloader.fetchSone(sone, sone.getRequestUri().setKeyType("SSK").setDocName("Sone-" + edition));
809 logger.log(Level.INFO, "Finished restoring Sone from Freenet, starting Inserter…");
811 coreListenerManager.fireRescuedSone(sone);
812 soneInserter.start();
815 }, "Sone Downloader").start();
821 * Creates a new Sone for the given own identity.
824 * The own identity to create a Sone for
825 * @return The created Sone
827 public Sone createSone(OwnIdentity ownIdentity) {
828 identityManager.addContext(ownIdentity, "Sone");
829 Sone sone = addLocalSone(ownIdentity);
834 * Adds the Sone of the given identity.
837 * The identity whose Sone to add
838 * @return The added or already existing Sone
840 public Sone addRemoteSone(Identity identity) {
841 if (identity == null) {
842 logger.log(Level.WARNING, "Given Identity is null!");
845 synchronized (remoteSones) {
846 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
847 boolean newSone = sone.getRequestUri() == null;
848 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
849 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
851 synchronized (newSones) {
852 newSone = !knownSones.contains(sone.getId());
854 newSones.add(sone.getId());
858 coreListenerManager.fireNewSoneFound(sone);
861 remoteSones.put(identity.getId(), sone);
862 soneDownloader.addSone(sone);
863 setSoneStatus(sone, SoneStatus.unknown);
864 new Thread(new Runnable() {
867 @SuppressWarnings("synthetic-access")
869 soneDownloader.fetchSone(sone);
872 }, "Sone Downloader").start();
878 * Updates the stores Sone with the given Sone.
883 public void updateSone(Sone sone) {
884 if (hasSone(sone.getId())) {
885 boolean soneRescueMode = isLocalSone(sone) && isSoneRescueMode();
886 Sone storedSone = getSone(sone.getId());
887 if (!soneRescueMode && !(sone.getTime() > storedSone.getTime())) {
888 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
891 synchronized (posts) {
892 if (!soneRescueMode) {
893 for (Post post : storedSone.getPosts()) {
894 posts.remove(post.getId());
895 if (!sone.getPosts().contains(post)) {
896 coreListenerManager.firePostRemoved(post);
900 synchronized (newPosts) {
901 for (Post post : sone.getPosts()) {
902 post.setSone(getSone(post.getSone().getId()));
903 if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
904 newPosts.add(post.getId());
905 coreListenerManager.fireNewPostFound(post);
907 posts.put(post.getId(), post);
911 synchronized (replies) {
912 if (!soneRescueMode) {
913 for (Reply reply : storedSone.getReplies()) {
914 replies.remove(reply.getId());
915 if (!sone.getReplies().contains(reply)) {
916 coreListenerManager.fireReplyRemoved(reply);
920 synchronized (newReplies) {
921 for (Reply reply : sone.getReplies()) {
922 reply.setSone(getSone(reply.getSone().getId()));
923 if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
924 newReplies.add(reply.getId());
925 coreListenerManager.fireNewReplyFound(reply);
927 replies.put(reply.getId(), reply);
931 synchronized (storedSone) {
932 if (!soneRescueMode || (sone.getTime() > storedSone.getTime())) {
933 storedSone.setTime(sone.getTime());
935 storedSone.setClient(sone.getClient());
936 storedSone.setProfile(sone.getProfile());
937 if (soneRescueMode) {
938 for (Post post : sone.getPosts()) {
939 storedSone.addPost(post);
941 for (Reply reply : sone.getReplies()) {
942 storedSone.addReply(reply);
944 for (String likedPostId : sone.getLikedPostIds()) {
945 storedSone.addLikedPostId(likedPostId);
947 for (String likedReplyId : sone.getLikedReplyIds()) {
948 storedSone.addLikedReplyId(likedReplyId);
951 storedSone.setPosts(sone.getPosts());
952 storedSone.setReplies(sone.getReplies());
953 storedSone.setLikePostIds(sone.getLikedPostIds());
954 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
956 storedSone.setLatestEdition(sone.getLatestEdition());
962 * Deletes the given Sone. This will remove the Sone from the
963 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
964 * and remove the context from its identity.
969 public void deleteSone(Sone sone) {
970 if (!(sone.getIdentity() instanceof OwnIdentity)) {
971 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
974 synchronized (localSones) {
975 if (!localSones.containsKey(sone.getId())) {
976 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
979 localSones.remove(sone.getId());
980 soneInserters.remove(sone).stop();
982 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
983 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
985 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
986 } catch (ConfigurationException ce1) {
987 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
992 * Loads and updates the given Sone from the configuration. If any error is
993 * encountered, loading is aborted and the given Sone is not changed.
996 * The Sone to load and update
998 public void loadSone(Sone sone) {
999 if (!isLocalSone(sone)) {
1000 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
1005 String sonePrefix = "Sone/" + sone.getId();
1006 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
1007 if (soneTime == null) {
1008 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
1011 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
1014 Profile profile = new Profile();
1015 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
1016 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
1017 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
1018 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
1019 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
1020 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
1023 Set<Post> posts = new HashSet<Post>();
1025 String postPrefix = sonePrefix + "/Posts/" + posts.size();
1026 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
1027 if (postId == null) {
1030 String postRecipientId = configuration.getStringValue(postPrefix + "/Recipient").getValue(null);
1031 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
1032 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
1033 if ((postTime == 0) || (postText == null)) {
1034 logger.log(Level.WARNING, "Invalid post found, aborting load!");
1037 Post post = getPost(postId).setSone(sone).setTime(postTime).setText(postText);
1038 if ((postRecipientId != null) && (postRecipientId.length() == 43)) {
1039 post.setRecipient(getSone(postRecipientId));
1045 Set<Reply> replies = new HashSet<Reply>();
1047 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
1048 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
1049 if (replyId == null) {
1052 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
1053 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
1054 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
1055 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
1056 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
1059 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
1062 /* load post likes. */
1063 Set<String> likedPostIds = new HashSet<String>();
1065 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
1066 if (likedPostId == null) {
1069 likedPostIds.add(likedPostId);
1072 /* load reply likes. */
1073 Set<String> likedReplyIds = new HashSet<String>();
1075 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
1076 if (likedReplyId == null) {
1079 likedReplyIds.add(likedReplyId);
1083 Set<String> friends = new HashSet<String>();
1085 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
1086 if (friendId == null) {
1089 friends.add(friendId);
1092 /* if we’re still here, Sone was loaded successfully. */
1093 synchronized (sone) {
1094 sone.setTime(soneTime);
1095 sone.setProfile(profile);
1096 sone.setPosts(posts);
1097 sone.setReplies(replies);
1098 sone.setLikePostIds(likedPostIds);
1099 sone.setLikeReplyIds(likedReplyIds);
1100 sone.setFriends(friends);
1101 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
1103 synchronized (newSones) {
1104 for (String friend : friends) {
1105 knownSones.add(friend);
1108 synchronized (newPosts) {
1109 for (Post post : posts) {
1110 knownPosts.add(post.getId());
1113 synchronized (newReplies) {
1114 for (Reply reply : replies) {
1115 knownReplies.add(reply.getId());
1121 * Saves the given Sone. This will persist all local settings for the given
1122 * Sone, such as the friends list and similar, private options.
1127 public synchronized void saveSone(Sone sone) {
1128 if (!isLocalSone(sone)) {
1129 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
1132 if (!(sone.getIdentity() instanceof OwnIdentity)) {
1133 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
1137 logger.log(Level.INFO, "Saving Sone: %s", sone);
1138 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
1140 /* save Sone into configuration. */
1141 String sonePrefix = "Sone/" + sone.getId();
1142 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
1143 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
1146 Profile profile = sone.getProfile();
1147 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
1148 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
1149 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
1150 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
1151 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
1152 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
1155 int postCounter = 0;
1156 for (Post post : sone.getPosts()) {
1157 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
1158 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
1159 configuration.getStringValue(postPrefix + "/Recipient").setValue((post.getRecipient() != null) ? post.getRecipient().getId() : null);
1160 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
1161 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
1163 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
1166 int replyCounter = 0;
1167 for (Reply reply : sone.getReplies()) {
1168 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
1169 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
1170 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
1171 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
1172 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
1174 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
1176 /* save post likes. */
1177 int postLikeCounter = 0;
1178 for (String postId : sone.getLikedPostIds()) {
1179 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
1181 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
1183 /* save reply likes. */
1184 int replyLikeCounter = 0;
1185 for (String replyId : sone.getLikedReplyIds()) {
1186 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1188 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1191 int friendCounter = 0;
1192 for (String friendId : sone.getFriends()) {
1193 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1195 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1197 configuration.save();
1198 logger.log(Level.INFO, "Sone %s saved.", sone);
1199 } catch (ConfigurationException ce1) {
1200 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1205 * Creates a new post.
1208 * The Sone that creates the post
1210 * The text of the post
1211 * @return The created post
1213 public Post createPost(Sone sone, String text) {
1214 return createPost(sone, System.currentTimeMillis(), text);
1218 * Creates a new post.
1221 * The Sone that creates the post
1223 * The time of the post
1225 * The text of the post
1226 * @return The created post
1228 public Post createPost(Sone sone, long time, String text) {
1229 return createPost(sone, null, time, text);
1233 * Creates a new post.
1236 * The Sone that creates the post
1238 * The recipient Sone, or {@code null} if this post does not have
1241 * The text of the post
1242 * @return The created post
1244 public Post createPost(Sone sone, Sone recipient, String text) {
1245 return createPost(sone, recipient, System.currentTimeMillis(), text);
1249 * Creates a new post.
1252 * The Sone that creates the post
1254 * The recipient Sone, or {@code null} if this post does not have
1257 * The time of the post
1259 * The text of the post
1260 * @return The created post
1262 public Post createPost(Sone sone, Sone recipient, long time, String text) {
1263 if (!isLocalSone(sone)) {
1264 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1267 Post post = new Post(sone, time, text);
1268 if (recipient != null) {
1269 post.setRecipient(recipient);
1271 synchronized (posts) {
1272 posts.put(post.getId(), post);
1274 synchronized (newPosts) {
1275 knownPosts.add(post.getId());
1283 * Deletes the given post.
1286 * The post to delete
1288 public void deletePost(Post post) {
1289 if (!isLocalSone(post.getSone())) {
1290 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1293 post.getSone().removePost(post);
1294 synchronized (posts) {
1295 posts.remove(post.getId());
1297 saveSone(post.getSone());
1301 * Marks the given post as known, if it is currently a new post (according
1302 * to {@link #isNewPost(String)}).
1305 * The post to mark as known
1307 public void markPostKnown(Post post) {
1308 synchronized (newPosts) {
1309 if (newPosts.remove(post.getId())) {
1310 knownPosts.add(post.getId());
1311 coreListenerManager.fireMarkPostKnown(post);
1312 saveConfiguration();
1318 * Creates a new reply.
1321 * The Sone that creates the reply
1323 * The post that this reply refers to
1325 * The text of the reply
1326 * @return The created reply
1328 public Reply createReply(Sone sone, Post post, String text) {
1329 return createReply(sone, post, System.currentTimeMillis(), text);
1333 * Creates a new reply.
1336 * The Sone that creates the reply
1338 * The post that this reply refers to
1340 * The time of the reply
1342 * The text of the reply
1343 * @return The created reply
1345 public Reply createReply(Sone sone, Post post, long time, String text) {
1346 if (!isLocalSone(sone)) {
1347 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1350 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1351 synchronized (replies) {
1352 replies.put(reply.getId(), reply);
1354 synchronized (newReplies) {
1355 knownReplies.add(reply.getId());
1357 sone.addReply(reply);
1363 * Deletes the given reply.
1366 * The reply to delete
1368 public void deleteReply(Reply reply) {
1369 Sone sone = reply.getSone();
1370 if (!isLocalSone(sone)) {
1371 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1374 synchronized (replies) {
1375 replies.remove(reply.getId());
1377 sone.removeReply(reply);
1382 * Marks the given reply as known, if it is currently a new reply (according
1383 * to {@link #isNewReply(String)}).
1386 * The reply to mark as known
1388 public void markReplyKnown(Reply reply) {
1389 synchronized (newReplies) {
1390 if (newReplies.remove(reply.getId())) {
1391 knownReplies.add(reply.getId());
1392 coreListenerManager.fireMarkReplyKnown(reply);
1393 saveConfiguration();
1401 public void start() {
1402 loadConfiguration();
1408 public void stop() {
1409 synchronized (localSones) {
1410 for (SoneInserter soneInserter : soneInserters.values()) {
1411 soneInserter.stop();
1414 soneDownloader.stop();
1415 saveConfiguration();
1420 * Saves the current options.
1422 public void saveConfiguration() {
1423 synchronized (configuration) {
1424 if (storingConfiguration) {
1425 logger.log(Level.FINE, "Already storing configuration…");
1428 storingConfiguration = true;
1431 /* store the options first. */
1433 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1434 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1435 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1436 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1438 /* save known Sones. */
1439 int soneCounter = 0;
1440 synchronized (newSones) {
1441 for (String knownSoneId : knownSones) {
1442 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1444 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1447 /* save known posts. */
1448 int postCounter = 0;
1449 synchronized (newPosts) {
1450 for (String knownPostId : knownPosts) {
1451 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1453 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1456 /* save known replies. */
1457 int replyCounter = 0;
1458 synchronized (newReplies) {
1459 for (String knownReplyId : knownReplies) {
1460 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1462 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1466 configuration.save();
1468 } catch (ConfigurationException ce1) {
1469 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1471 synchronized (configuration) {
1472 storingConfiguration = false;
1482 * Loads the configuration.
1484 @SuppressWarnings("unchecked")
1485 private void loadConfiguration() {
1486 /* create options. */
1487 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1490 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1491 SoneInserter.setInsertionDelay(newValue);
1495 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1496 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1497 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1499 /* read options from configuration. */
1500 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1501 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1502 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1503 options.getBooleanOption("ClearOnNextRestart").set(null);
1504 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1505 if (clearConfiguration) {
1506 /* stop loading the configuration. */
1510 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1511 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1513 /* load known Sones. */
1514 int soneCounter = 0;
1516 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1517 if (knownSoneId == null) {
1520 synchronized (newSones) {
1521 knownSones.add(knownSoneId);
1525 /* load known posts. */
1526 int postCounter = 0;
1528 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1529 if (knownPostId == null) {
1532 synchronized (newPosts) {
1533 knownPosts.add(knownPostId);
1537 /* load known replies. */
1538 int replyCounter = 0;
1540 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1541 if (knownReplyId == null) {
1544 synchronized (newReplies) {
1545 knownReplies.add(knownReplyId);
1552 * Generate a Sone URI from the given URI and latest edition.
1555 * The URI to derive the Sone URI from
1556 * @return The derived URI
1558 private FreenetURI getSoneUri(String uriString) {
1560 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1562 } catch (MalformedURLException mue1) {
1563 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1569 // INTERFACE IdentityListener
1576 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1577 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1578 if (ownIdentity.hasContext("Sone")) {
1579 addLocalSone(ownIdentity);
1587 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1588 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1595 public void identityAdded(Identity identity) {
1596 logger.log(Level.FINEST, "Adding Identity: " + identity);
1597 addRemoteSone(identity);
1604 public void identityUpdated(final Identity identity) {
1605 new Thread(new Runnable() {
1608 @SuppressWarnings("synthetic-access")
1610 Sone sone = getRemoteSone(identity.getId());
1611 soneDownloader.fetchSone(sone);
1620 public void identityRemoved(Identity identity) {