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 configuration. */
84 private final Configuration configuration;
86 /** The identity manager. */
87 private final IdentityManager identityManager;
89 /** Interface to freenet. */
90 private final FreenetInterface freenetInterface;
92 /** The Sone downloader. */
93 private final SoneDownloader soneDownloader;
95 /** The Sones’ statuses. */
96 /* synchronize access on itself. */
97 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
99 /** Sone inserters. */
100 /* synchronize access on this on localSones. */
101 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
103 /** All local Sones. */
104 /* synchronize access on this on itself. */
105 private Map<String, Sone> localSones = new HashMap<String, Sone>();
107 /** All remote Sones. */
108 /* synchronize access on this on itself. */
109 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
111 /** All new Sones. */
112 private Set<String> newSones = new HashSet<String>();
114 /** All known Sones. */
115 /* synchronize access on {@link #newSones}. */
116 private Set<String> knownSones = new HashSet<String>();
119 private Map<String, Post> posts = new HashMap<String, Post>();
121 /** All new posts. */
122 private Set<String> newPosts = new HashSet<String>();
124 /** All known posts. */
125 /* synchronize access on {@link #newPosts}. */
126 private Set<String> knownPosts = new HashSet<String>();
129 private Map<String, Reply> replies = new HashMap<String, Reply>();
131 /** All new replies. */
132 private Set<String> newReplies = new HashSet<String>();
134 /** All known replies. */
135 private Set<String> knownReplies = new HashSet<String>();
138 * Creates a new core.
140 * @param configuration
141 * The configuration of the core
142 * @param freenetInterface
143 * The freenet interface
144 * @param identityManager
145 * The identity manager
147 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
148 this.configuration = configuration;
149 this.freenetInterface = freenetInterface;
150 this.identityManager = identityManager;
151 this.soneDownloader = new SoneDownloader(this, freenetInterface);
159 * Returns the options used by the core.
161 * @return The options of the core
163 public Options getOptions() {
168 * Returns the identity manager used by the core.
170 * @return The identity manager
172 public IdentityManager getIdentityManager() {
173 return identityManager;
177 * Returns the status of the given Sone.
180 * The Sone to get the status for
181 * @return The status of the Sone
183 public SoneStatus getSoneStatus(Sone sone) {
184 synchronized (soneStatuses) {
185 return soneStatuses.get(sone);
190 * Sets the status of the given Sone.
193 * The Sone to set the status of
197 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
198 synchronized (soneStatuses) {
199 soneStatuses.put(sone, soneStatus);
204 * Returns all Sones, remote and local.
208 public Set<Sone> getSones() {
209 Set<Sone> allSones = new HashSet<Sone>();
210 allSones.addAll(getLocalSones());
211 allSones.addAll(getRemoteSones());
216 * Returns the Sone with the given ID, regardless whether it’s local or
220 * The ID of the Sone to get
221 * @return The Sone with the given ID, or {@code null} if there is no such
224 public Sone getSone(String id) {
225 return getSone(id, true);
229 * Returns the Sone with the given ID, regardless whether it’s local or
233 * The ID of the Sone to get
235 * {@code true} to create a new Sone if none exists,
236 * {@code false} to return {@code null} if a Sone with the given
238 * @return The Sone with the given ID, or {@code null} if there is no such
241 public Sone getSone(String id, boolean create) {
242 if (isLocalSone(id)) {
243 return getLocalSone(id);
245 return getRemoteSone(id, create);
249 * Checks whether the core knows a Sone with the given ID.
253 * @return {@code true} if there is a Sone with the given ID, {@code false}
256 public boolean hasSone(String id) {
257 return isLocalSone(id) || isRemoteSone(id);
261 * Returns whether the given Sone is a local Sone.
264 * The Sone to check for its locality
265 * @return {@code true} if the given Sone is local, {@code false} otherwise
267 public boolean isLocalSone(Sone sone) {
268 synchronized (localSones) {
269 return localSones.containsKey(sone.getId());
274 * Returns whether the given ID is the ID of a local Sone.
277 * The Sone ID to check for its locality
278 * @return {@code true} if the given ID is a local Sone, {@code false}
281 public boolean isLocalSone(String id) {
282 synchronized (localSones) {
283 return localSones.containsKey(id);
288 * Returns all local Sones.
290 * @return All local Sones
292 public Set<Sone> getLocalSones() {
293 synchronized (localSones) {
294 return new HashSet<Sone>(localSones.values());
299 * Returns the local Sone with the given ID.
302 * The ID of the Sone to get
303 * @return The Sone with the given ID
305 public Sone getLocalSone(String id) {
306 return getLocalSone(id, true);
310 * Returns the local Sone with the given ID, optionally creating a new Sone.
315 * {@code true} to create a new Sone if none exists,
316 * {@code false} to return null if none exists
317 * @return The Sone with the given ID, or {@code null}
319 public Sone getLocalSone(String id, boolean create) {
320 synchronized (localSones) {
321 Sone sone = localSones.get(id);
322 if ((sone == null) && create) {
324 localSones.put(id, sone);
331 * Returns all remote Sones.
333 * @return All remote Sones
335 public Set<Sone> getRemoteSones() {
336 synchronized (remoteSones) {
337 return new HashSet<Sone>(remoteSones.values());
342 * Returns the remote Sone with the given ID.
345 * The ID of the remote Sone to get
346 * @return The Sone with the given ID
348 public Sone getRemoteSone(String id) {
349 return getRemoteSone(id, true);
353 * Returns the remote Sone with the given ID.
356 * The ID of the remote Sone to get
358 * {@code true} to always create a Sone, {@code false} to return
359 * {@code null} if no Sone with the given ID exists
360 * @return The Sone with the given ID
362 public Sone getRemoteSone(String id, boolean create) {
363 synchronized (remoteSones) {
364 Sone sone = remoteSones.get(id);
365 if ((sone == null) && create) {
367 remoteSones.put(id, sone);
374 * Returns whether the given Sone is a remote Sone.
378 * @return {@code true} if the given Sone is a remote Sone, {@code false}
381 public boolean isRemoteSone(Sone sone) {
382 synchronized (remoteSones) {
383 return remoteSones.containsKey(sone.getId());
388 * Returns whether the Sone with the given ID is a remote Sone.
391 * The ID of the Sone to check
392 * @return {@code true} if the Sone with the given ID is a remote Sone,
393 * {@code false} otherwise
395 public boolean isRemoteSone(String id) {
396 synchronized (remoteSones) {
397 return remoteSones.containsKey(id);
402 * Returns whether the given Sone is a new Sone. After this check, the Sone
403 * is marked as known, i.e. a second call with the same parameters will
404 * always yield {@code false}.
407 * The sone to check for
408 * @return {@code true} if the given Sone is new, false otherwise
410 public boolean isNewSone(Sone sone) {
411 synchronized (newSones) {
412 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
413 knownSones.add(sone.getId());
419 * Returns whether the given Sone has been modified.
422 * The Sone to check for modifications
423 * @return {@code true} if a modification has been detected in the Sone,
424 * {@code false} otherwise
426 public boolean isModifiedSone(Sone sone) {
427 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
431 * Returns the post with the given ID.
434 * The ID of the post to get
435 * @return The post, or {@code null} if there is no such post
437 public Post getPost(String postId) {
438 synchronized (posts) {
439 Post post = posts.get(postId);
441 post = new Post(postId);
442 posts.put(postId, post);
449 * Returns whether the given post ID is new. After this method returns it is
450 * marked a known post ID.
454 * @return {@code true} if the post is considered to be new, {@code false}
457 public boolean isNewPost(String postId) {
458 synchronized (newPosts) {
459 boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
460 knownPosts.add(postId);
466 * Returns the reply with the given ID.
469 * The ID of the reply to get
470 * @return The reply, or {@code null} if there is no such reply
472 public Reply getReply(String replyId) {
473 synchronized (replies) {
474 Reply reply = replies.get(replyId);
476 reply = new Reply(replyId);
477 replies.put(replyId, reply);
484 * Returns all replies for the given post, order ascending by time.
487 * The post to get all replies for
488 * @return All replies for the given post
490 public List<Reply> getReplies(Post post) {
491 Set<Sone> sones = getSones();
492 List<Reply> replies = new ArrayList<Reply>();
493 for (Sone sone : sones) {
494 for (Reply reply : sone.getReplies()) {
495 if (reply.getPost().equals(post)) {
500 Collections.sort(replies, Reply.TIME_COMPARATOR);
505 * Returns whether the reply with the given ID is new.
508 * The ID of the reply to check
509 * @return {@code true} if the reply is considered to be new, {@code false}
512 public boolean isNewReply(String replyId) {
513 synchronized (newReplies) {
514 boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
515 knownReplies.add(replyId);
521 * Returns all Sones that have liked the given post.
524 * The post to get the liking Sones for
525 * @return The Sones that like the given post
527 public Set<Sone> getLikes(Post post) {
528 Set<Sone> sones = new HashSet<Sone>();
529 for (Sone sone : getSones()) {
530 if (sone.getLikedPostIds().contains(post.getId())) {
538 * Returns all Sones that have liked the given reply.
541 * The reply to get the liking Sones for
542 * @return The Sones that like the given reply
544 public Set<Sone> getLikes(Reply reply) {
545 Set<Sone> sones = new HashSet<Sone>();
546 for (Sone sone : getSones()) {
547 if (sone.getLikedReplyIds().contains(reply.getId())) {
559 * Adds a local Sone from the given ID which has to be the ID of an own
563 * The ID of an own identity to add a Sone for
564 * @return The added (or already existing) Sone
566 public Sone addLocalSone(String id) {
567 synchronized (localSones) {
568 if (localSones.containsKey(id)) {
569 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
570 return localSones.get(id);
572 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
573 if (ownIdentity == null) {
574 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
577 return addLocalSone(ownIdentity);
582 * Adds a local Sone from the given own identity.
585 * The own identity to create a Sone from
586 * @return The added (or already existing) Sone
588 public Sone addLocalSone(OwnIdentity ownIdentity) {
589 if (ownIdentity == null) {
590 logger.log(Level.WARNING, "Given OwnIdentity is null!");
593 synchronized (localSones) {
596 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
597 } catch (MalformedURLException mue1) {
598 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
601 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
602 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
603 /* TODO - load posts ’n stuff */
604 localSones.put(ownIdentity.getId(), sone);
605 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
606 soneInserters.put(sone, soneInserter);
607 setSoneStatus(sone, SoneStatus.idle);
609 soneInserter.start();
610 new Thread(new Runnable() {
613 @SuppressWarnings("synthetic-access")
615 soneDownloader.fetchSone(sone);
618 }, "Sone Downloader").start();
624 * Creates a new Sone for the given own identity.
627 * The own identity to create a Sone for
628 * @return The created Sone
630 public Sone createSone(OwnIdentity ownIdentity) {
631 identityManager.addContext(ownIdentity, "Sone");
632 Sone sone = addLocalSone(ownIdentity);
637 * Adds the Sone of the given identity.
640 * The identity whose Sone to add
641 * @return The added or already existing Sone
643 public Sone addRemoteSone(Identity identity) {
644 if (identity == null) {
645 logger.log(Level.WARNING, "Given Identity is null!");
648 synchronized (remoteSones) {
649 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
650 boolean newSone = sone.getRequestUri() == null;
651 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
652 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
654 synchronized (newSones) {
655 newSones.add(sone.getId());
658 remoteSones.put(identity.getId(), sone);
659 soneDownloader.addSone(sone);
660 setSoneStatus(sone, SoneStatus.unknown);
661 new Thread(new Runnable() {
664 @SuppressWarnings("synthetic-access")
666 soneDownloader.fetchSone(sone);
669 }, "Sone Downloader").start();
675 * Updates the stores Sone with the given Sone.
680 public void updateSone(Sone sone) {
681 if (hasSone(sone.getId())) {
682 Sone storedSone = getSone(sone.getId());
683 if (!(sone.getTime() > storedSone.getTime())) {
684 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
687 synchronized (posts) {
688 for (Post post : storedSone.getPosts()) {
689 posts.remove(post.getId());
691 synchronized (newPosts) {
692 for (Post post : sone.getPosts()) {
693 if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) {
694 newPosts.add(post.getId());
696 posts.put(post.getId(), post);
700 synchronized (replies) {
701 for (Reply reply : storedSone.getReplies()) {
702 replies.remove(reply.getId());
704 synchronized (newReplies) {
705 for (Reply reply : sone.getReplies()) {
706 if (!storedSone.getReplies().contains(reply) && !knownSones.contains(reply.getId())) {
707 newReplies.add(reply.getId());
709 replies.put(reply.getId(), reply);
713 synchronized (storedSone) {
714 storedSone.setTime(sone.getTime());
715 storedSone.setClient(sone.getClient());
716 storedSone.setProfile(sone.getProfile());
717 storedSone.setPosts(sone.getPosts());
718 storedSone.setReplies(sone.getReplies());
719 storedSone.setLikePostIds(sone.getLikedPostIds());
720 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
721 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
727 * Deletes the given Sone. This will remove the Sone from the
728 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
729 * and remove the context from its identity.
734 public void deleteSone(Sone sone) {
735 if (!(sone.getIdentity() instanceof OwnIdentity)) {
736 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
739 synchronized (localSones) {
740 if (!localSones.containsKey(sone.getId())) {
741 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
744 localSones.remove(sone.getId());
745 soneInserters.remove(sone).stop();
747 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
748 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
750 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
751 } catch (ConfigurationException ce1) {
752 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
757 * Loads and updates the given Sone from the configuration. If any error is
758 * encountered, loading is aborted and the given Sone is not changed.
761 * The Sone to load and update
763 public void loadSone(Sone sone) {
764 if (!isLocalSone(sone)) {
765 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
770 String sonePrefix = "Sone/" + sone.getId();
771 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
772 if (soneTime == null) {
773 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
776 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
779 Profile profile = new Profile();
780 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
781 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
782 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
783 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
784 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
785 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
788 Set<Post> posts = new HashSet<Post>();
790 String postPrefix = sonePrefix + "/Posts/" + posts.size();
791 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
792 if (postId == null) {
795 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
796 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
797 if ((postTime == 0) || (postText == null)) {
798 logger.log(Level.WARNING, "Invalid post found, aborting load!");
801 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
805 Set<Reply> replies = new HashSet<Reply>();
807 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
808 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
809 if (replyId == null) {
812 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
813 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
814 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
815 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
816 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
819 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
822 /* load post likes. */
823 Set<String> likedPostIds = new HashSet<String>();
825 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
826 if (likedPostId == null) {
829 likedPostIds.add(likedPostId);
832 /* load reply likes. */
833 Set<String> likedReplyIds = new HashSet<String>();
835 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
836 if (likedReplyId == null) {
839 likedReplyIds.add(likedReplyId);
843 Set<String> friends = new HashSet<String>();
845 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
846 if (friendId == null) {
849 friends.add(friendId);
852 /* if we’re still here, Sone was loaded successfully. */
853 synchronized (sone) {
854 sone.setTime(soneTime);
855 sone.setProfile(profile);
856 sone.setPosts(posts);
857 sone.setReplies(replies);
858 sone.setLikePostIds(likedPostIds);
859 sone.setLikeReplyIds(likedReplyIds);
860 sone.setFriends(friends);
861 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
863 synchronized (newSones) {
864 for (String friend : friends) {
865 knownSones.add(friend);
868 synchronized (newPosts) {
869 for (Post post : posts) {
870 knownPosts.add(post.getId());
873 synchronized (newReplies) {
874 for (Reply reply : replies) {
875 knownReplies.add(reply.getId());
881 * Saves the given Sone. This will persist all local settings for the given
882 * Sone, such as the friends list and similar, private options.
887 public void saveSone(Sone sone) {
888 if (!isLocalSone(sone)) {
889 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
892 if (!(sone.getIdentity() instanceof OwnIdentity)) {
893 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
897 logger.log(Level.INFO, "Saving Sone: %s", sone);
898 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
900 /* save Sone into configuration. */
901 String sonePrefix = "Sone/" + sone.getId();
902 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
903 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
906 Profile profile = sone.getProfile();
907 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
908 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
909 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
910 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
911 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
912 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
916 for (Post post : sone.getPosts()) {
917 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
918 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
919 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
920 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
922 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
925 int replyCounter = 0;
926 for (Reply reply : sone.getReplies()) {
927 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
928 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
929 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
930 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
931 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
933 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
935 /* save post likes. */
936 int postLikeCounter = 0;
937 for (String postId : sone.getLikedPostIds()) {
938 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
940 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
942 /* save reply likes. */
943 int replyLikeCounter = 0;
944 for (String replyId : sone.getLikedReplyIds()) {
945 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
947 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
950 int friendCounter = 0;
951 for (String friendId : sone.getFriends()) {
952 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
954 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
956 logger.log(Level.INFO, "Sone %s saved.", sone);
957 } catch (ConfigurationException ce1) {
958 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
963 * Creates a new post.
966 * The Sone that creates the post
968 * The text of the post
970 public void createPost(Sone sone, String text) {
971 createPost(sone, System.currentTimeMillis(), text);
975 * Creates a new post.
978 * The Sone that creates the post
980 * The time of the post
982 * The text of the post
984 public void createPost(Sone sone, long time, String text) {
985 if (!isLocalSone(sone)) {
986 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
989 Post post = new Post(sone, time, text);
990 synchronized (posts) {
991 posts.put(post.getId(), post);
993 synchronized (newPosts) {
994 knownPosts.add(post.getId());
1001 * Deletes the given post.
1004 * The post to delete
1006 public void deletePost(Post post) {
1007 if (!isLocalSone(post.getSone())) {
1008 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1011 post.getSone().removePost(post);
1012 synchronized (posts) {
1013 posts.remove(post.getId());
1015 saveSone(post.getSone());
1019 * Creates a new reply.
1022 * The Sone that creates the reply
1024 * The post that this reply refers to
1026 * The text of the reply
1027 * @return The created reply
1029 public Reply createReply(Sone sone, Post post, String text) {
1030 return createReply(sone, post, System.currentTimeMillis(), text);
1034 * Creates a new reply.
1037 * The Sone that creates the reply
1039 * The post that this reply refers to
1041 * The time of the reply
1043 * The text of the reply
1044 * @return The created reply
1046 public Reply createReply(Sone sone, Post post, long time, String text) {
1047 if (!isLocalSone(sone)) {
1048 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1051 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1052 synchronized (replies) {
1053 replies.put(reply.getId(), reply);
1055 synchronized (newReplies) {
1056 knownReplies.add(reply.getId());
1058 sone.addReply(reply);
1064 * Deletes the given reply.
1067 * The reply to delete
1069 public void deleteReply(Reply reply) {
1070 Sone sone = reply.getSone();
1071 if (!isLocalSone(sone)) {
1072 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1075 synchronized (replies) {
1076 replies.remove(reply.getId());
1078 sone.removeReply(reply);
1085 public void start() {
1086 loadConfiguration();
1092 public void stop() {
1093 synchronized (localSones) {
1094 for (SoneInserter soneInserter : soneInserters.values()) {
1095 soneInserter.stop();
1098 saveConfiguration();
1106 * Loads the configuration.
1108 @SuppressWarnings("unchecked")
1109 private void loadConfiguration() {
1110 /* create options. */
1111 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1114 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1115 SoneInserter.setInsertionDelay(newValue);
1119 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1120 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1122 /* read options from configuration. */
1123 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1124 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1125 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1126 options.getBooleanOption("ClearOnNextRestart").set(null);
1127 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1128 if (clearConfiguration) {
1129 /* stop loading the configuration. */
1133 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1135 /* load known Sones. */
1136 int soneCounter = 0;
1138 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1139 if (knownSoneId == null) {
1142 synchronized (newSones) {
1143 knownSones.add(knownSoneId);
1147 /* load known posts. */
1148 int postCounter = 0;
1150 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1151 if (knownPostId == null) {
1154 synchronized (newPosts) {
1155 knownPosts.add(knownPostId);
1159 /* load known replies. */
1160 int replyCounter = 0;
1162 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1163 if (knownReplyId == null) {
1166 synchronized (newReplies) {
1167 knownReplies.add(knownReplyId);
1174 * Saves the current options.
1176 private void saveConfiguration() {
1177 /* store the options first. */
1179 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1180 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1181 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1183 /* save known Sones. */
1184 int soneCounter = 0;
1185 synchronized (newSones) {
1186 for (String knownSoneId : knownSones) {
1187 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1189 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1192 /* save known posts. */
1193 int postCounter = 0;
1194 synchronized (newPosts) {
1195 for (String knownPostId : knownPosts) {
1196 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1198 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1201 /* save known replies. */
1202 int replyCounter = 0;
1203 synchronized (newReplies) {
1204 for (String knownReplyId : knownReplies) {
1205 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1207 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1210 } catch (ConfigurationException ce1) {
1211 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1216 * Generate a Sone URI from the given URI and latest edition.
1219 * The URI to derive the Sone URI from
1220 * @return The derived URI
1222 private FreenetURI getSoneUri(String uriString) {
1224 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1226 } catch (MalformedURLException mue1) {
1227 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1233 // INTERFACE IdentityListener
1240 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1241 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1242 if (ownIdentity.hasContext("Sone")) {
1243 addLocalSone(ownIdentity);
1251 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1252 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1259 public void identityAdded(Identity identity) {
1260 logger.log(Level.FINEST, "Adding Identity: " + identity);
1261 addRemoteSone(identity);
1268 public void identityUpdated(final Identity identity) {
1269 new Thread(new Runnable() {
1272 @SuppressWarnings("synthetic-access")
1274 Sone sone = getRemoteSone(identity.getId());
1275 soneDownloader.fetchSone(sone);
1284 public void identityRemoved(Identity identity) {