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 /** Sone inserters. */
106 /* synchronize access on this on localSones. */
107 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
109 /** All local Sones. */
110 /* synchronize access on this on itself. */
111 private Map<String, Sone> localSones = new HashMap<String, Sone>();
113 /** All remote Sones. */
114 /* synchronize access on this on itself. */
115 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
117 /** All new Sones. */
118 private Set<String> newSones = new HashSet<String>();
120 /** All known Sones. */
121 /* synchronize access on {@link #newSones}. */
122 private Set<String> knownSones = new HashSet<String>();
125 private Map<String, Post> posts = new HashMap<String, Post>();
127 /** All new posts. */
128 private Set<String> newPosts = new HashSet<String>();
130 /** All known posts. */
131 /* synchronize access on {@link #newPosts}. */
132 private Set<String> knownPosts = new HashSet<String>();
135 private Map<String, Reply> replies = new HashMap<String, Reply>();
137 /** All new replies. */
138 private Set<String> newReplies = new HashSet<String>();
140 /** All known replies. */
141 private Set<String> knownReplies = new HashSet<String>();
144 * Creates a new core.
146 * @param configuration
147 * The configuration of the core
148 * @param freenetInterface
149 * The freenet interface
150 * @param identityManager
151 * The identity manager
153 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
154 this.configuration = configuration;
155 this.freenetInterface = freenetInterface;
156 this.identityManager = identityManager;
157 this.soneDownloader = new SoneDownloader(this, freenetInterface);
161 // LISTENER MANAGEMENT
165 * Adds a new core listener.
167 * @param coreListener
168 * The listener to add
170 public void addCoreListener(CoreListener coreListener) {
171 coreListenerManager.addListener(coreListener);
175 * Removes a core listener.
177 * @param coreListener
178 * The listener to remove
180 public void removeCoreListener(CoreListener coreListener) {
181 coreListenerManager.removeListener(coreListener);
189 * Returns the options used by the core.
191 * @return The options of the core
193 public Options getOptions() {
198 * Returns whether the “Sone rescue mode” is currently activated.
200 * @return {@code true} if the “Sone rescue mode” is currently activated,
201 * {@code false} if it is not
203 public boolean isSoneRescueMode() {
204 return options.getBooleanOption("SoneRescueMode").get();
208 * Returns the identity manager used by the core.
210 * @return The identity manager
212 public IdentityManager getIdentityManager() {
213 return identityManager;
217 * Returns the status of the given Sone.
220 * The Sone to get the status for
221 * @return The status of the Sone
223 public SoneStatus getSoneStatus(Sone sone) {
224 synchronized (soneStatuses) {
225 return soneStatuses.get(sone);
230 * Sets the status of the given Sone.
233 * The Sone to set the status of
237 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
238 synchronized (soneStatuses) {
239 soneStatuses.put(sone, soneStatus);
244 * Returns all Sones, remote and local.
248 public Set<Sone> getSones() {
249 Set<Sone> allSones = new HashSet<Sone>();
250 allSones.addAll(getLocalSones());
251 allSones.addAll(getRemoteSones());
256 * Returns the Sone with the given ID, regardless whether it’s local or
260 * The ID of the Sone to get
261 * @return The Sone with the given ID, or {@code null} if there is no such
264 public Sone getSone(String id) {
265 return getSone(id, true);
269 * Returns the Sone with the given ID, regardless whether it’s local or
273 * The ID of the Sone to get
275 * {@code true} to create a new Sone if none exists,
276 * {@code false} to return {@code null} if a Sone with the given
278 * @return The Sone with the given ID, or {@code null} if there is no such
281 public Sone getSone(String id, boolean create) {
282 if (isLocalSone(id)) {
283 return getLocalSone(id);
285 return getRemoteSone(id, create);
289 * Checks whether the core knows a Sone with the given ID.
293 * @return {@code true} if there is a Sone with the given ID, {@code false}
296 public boolean hasSone(String id) {
297 return isLocalSone(id) || isRemoteSone(id);
301 * Returns whether the given Sone is a local Sone.
304 * The Sone to check for its locality
305 * @return {@code true} if the given Sone is local, {@code false} otherwise
307 public boolean isLocalSone(Sone sone) {
308 synchronized (localSones) {
309 return localSones.containsKey(sone.getId());
314 * Returns whether the given ID is the ID of a local Sone.
317 * The Sone ID to check for its locality
318 * @return {@code true} if the given ID is a local Sone, {@code false}
321 public boolean isLocalSone(String id) {
322 synchronized (localSones) {
323 return localSones.containsKey(id);
328 * Returns all local Sones.
330 * @return All local Sones
332 public Set<Sone> getLocalSones() {
333 synchronized (localSones) {
334 return new HashSet<Sone>(localSones.values());
339 * Returns the local Sone with the given ID.
342 * The ID of the Sone to get
343 * @return The Sone with the given ID
345 public Sone getLocalSone(String id) {
346 return getLocalSone(id, true);
350 * Returns the local Sone with the given ID, optionally creating a new Sone.
355 * {@code true} to create a new Sone if none exists,
356 * {@code false} to return null if none exists
357 * @return The Sone with the given ID, or {@code null}
359 public Sone getLocalSone(String id, boolean create) {
360 synchronized (localSones) {
361 Sone sone = localSones.get(id);
362 if ((sone == null) && create) {
364 localSones.put(id, sone);
371 * Returns all remote Sones.
373 * @return All remote Sones
375 public Set<Sone> getRemoteSones() {
376 synchronized (remoteSones) {
377 return new HashSet<Sone>(remoteSones.values());
382 * Returns the remote Sone with the given ID.
385 * The ID of the remote Sone to get
386 * @return The Sone with the given ID
388 public Sone getRemoteSone(String id) {
389 return getRemoteSone(id, true);
393 * Returns the remote Sone with the given ID.
396 * The ID of the remote Sone to get
398 * {@code true} to always create a Sone, {@code false} to return
399 * {@code null} if no Sone with the given ID exists
400 * @return The Sone with the given ID
402 public Sone getRemoteSone(String id, boolean create) {
403 synchronized (remoteSones) {
404 Sone sone = remoteSones.get(id);
405 if ((sone == null) && create) {
407 remoteSones.put(id, sone);
414 * Returns whether the given Sone is a remote Sone.
418 * @return {@code true} if the given Sone is a remote Sone, {@code false}
421 public boolean isRemoteSone(Sone sone) {
422 synchronized (remoteSones) {
423 return remoteSones.containsKey(sone.getId());
428 * Returns whether the Sone with the given ID is a remote Sone.
431 * The ID of the Sone to check
432 * @return {@code true} if the Sone with the given ID is a remote Sone,
433 * {@code false} otherwise
435 public boolean isRemoteSone(String id) {
436 synchronized (remoteSones) {
437 return remoteSones.containsKey(id);
442 * Returns whether the given Sone is a new Sone. After this check, the Sone
443 * is marked as known, i.e. a second call with the same parameters will
444 * always yield {@code false}.
447 * The sone to check for
448 * @return {@code true} if the given Sone is new, false otherwise
450 public boolean isNewSone(Sone sone) {
451 synchronized (newSones) {
452 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
453 knownSones.add(sone.getId());
455 coreListenerManager.fireMarkSoneKnown(sone);
462 * Returns whether the given Sone has been modified.
465 * The Sone to check for modifications
466 * @return {@code true} if a modification has been detected in the Sone,
467 * {@code false} otherwise
469 public boolean isModifiedSone(Sone sone) {
470 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
474 * Returns the post with the given ID.
477 * The ID of the post to get
478 * @return The post, or {@code null} if there is no such post
480 public Post getPost(String postId) {
481 synchronized (posts) {
482 Post post = posts.get(postId);
484 post = new Post(postId);
485 posts.put(postId, post);
492 * Returns whether the given post ID is new. After this method returns it is
493 * marked a known post ID.
497 * @return {@code true} if the post is considered to be new, {@code false}
500 public boolean isNewPost(String postId) {
501 synchronized (newPosts) {
502 boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
503 knownPosts.add(postId);
505 coreListenerManager.fireMarkPostKnown(getPost(postId));
512 * Returns the reply with the given ID.
515 * The ID of the reply to get
516 * @return The reply, or {@code null} if there is no such reply
518 public Reply getReply(String replyId) {
519 synchronized (replies) {
520 Reply reply = replies.get(replyId);
522 reply = new Reply(replyId);
523 replies.put(replyId, reply);
530 * Returns all replies for the given post, order ascending by time.
533 * The post to get all replies for
534 * @return All replies for the given post
536 public List<Reply> getReplies(Post post) {
537 Set<Sone> sones = getSones();
538 List<Reply> replies = new ArrayList<Reply>();
539 for (Sone sone : sones) {
540 for (Reply reply : sone.getReplies()) {
541 if (reply.getPost().equals(post)) {
546 Collections.sort(replies, Reply.TIME_COMPARATOR);
551 * Returns whether the reply with the given ID is new.
554 * The ID of the reply to check
555 * @return {@code true} if the reply is considered to be new, {@code false}
558 public boolean isNewReply(String replyId) {
559 synchronized (newReplies) {
560 boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
561 knownReplies.add(replyId);
563 coreListenerManager.fireMarkReplyKnown(getReply(replyId));
570 * Returns all Sones that have liked the given post.
573 * The post to get the liking Sones for
574 * @return The Sones that like the given post
576 public Set<Sone> getLikes(Post post) {
577 Set<Sone> sones = new HashSet<Sone>();
578 for (Sone sone : getSones()) {
579 if (sone.getLikedPostIds().contains(post.getId())) {
587 * Returns all Sones that have liked the given reply.
590 * The reply to get the liking Sones for
591 * @return The Sones that like the given reply
593 public Set<Sone> getLikes(Reply reply) {
594 Set<Sone> sones = new HashSet<Sone>();
595 for (Sone sone : getSones()) {
596 if (sone.getLikedReplyIds().contains(reply.getId())) {
608 * Adds a local Sone from the given ID which has to be the ID of an own
612 * The ID of an own identity to add a Sone for
613 * @return The added (or already existing) Sone
615 public Sone addLocalSone(String id) {
616 synchronized (localSones) {
617 if (localSones.containsKey(id)) {
618 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
619 return localSones.get(id);
621 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
622 if (ownIdentity == null) {
623 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
626 return addLocalSone(ownIdentity);
631 * Adds a local Sone from the given own identity.
634 * The own identity to create a Sone from
635 * @return The added (or already existing) Sone
637 public Sone addLocalSone(OwnIdentity ownIdentity) {
638 if (ownIdentity == null) {
639 logger.log(Level.WARNING, "Given OwnIdentity is null!");
642 synchronized (localSones) {
645 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
646 } catch (MalformedURLException mue1) {
647 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
650 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
651 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
652 /* TODO - load posts ’n stuff */
653 localSones.put(ownIdentity.getId(), sone);
654 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
655 soneInserters.put(sone, soneInserter);
656 setSoneStatus(sone, SoneStatus.idle);
658 soneInserter.start();
659 new Thread(new Runnable() {
662 @SuppressWarnings("synthetic-access")
664 soneDownloader.fetchSone(sone);
667 }, "Sone Downloader").start();
673 * Creates a new Sone for the given own identity.
676 * The own identity to create a Sone for
677 * @return The created Sone
679 public Sone createSone(OwnIdentity ownIdentity) {
680 identityManager.addContext(ownIdentity, "Sone");
681 Sone sone = addLocalSone(ownIdentity);
686 * Adds the Sone of the given identity.
689 * The identity whose Sone to add
690 * @return The added or already existing Sone
692 public Sone addRemoteSone(Identity identity) {
693 if (identity == null) {
694 logger.log(Level.WARNING, "Given Identity is null!");
697 synchronized (remoteSones) {
698 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
699 boolean newSone = sone.getRequestUri() == null;
700 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
701 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
703 synchronized (newSones) {
704 newSone = !knownSones.contains(sone.getId());
706 newSones.add(sone.getId());
710 coreListenerManager.fireNewSoneFound(sone);
713 remoteSones.put(identity.getId(), sone);
714 soneDownloader.addSone(sone);
715 setSoneStatus(sone, SoneStatus.unknown);
716 new Thread(new Runnable() {
719 @SuppressWarnings("synthetic-access")
721 soneDownloader.fetchSone(sone);
724 }, "Sone Downloader").start();
730 * Updates the stores Sone with the given Sone.
735 public void updateSone(Sone sone) {
736 if (hasSone(sone.getId())) {
737 Sone storedSone = getSone(sone.getId());
738 if (!(sone.getTime() > storedSone.getTime())) {
739 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
742 synchronized (posts) {
743 for (Post post : storedSone.getPosts()) {
744 posts.remove(post.getId());
746 synchronized (newPosts) {
747 for (Post post : sone.getPosts()) {
748 if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
749 newPosts.add(post.getId());
750 coreListenerManager.fireNewPostFound(post);
752 posts.put(post.getId(), post);
756 synchronized (replies) {
757 for (Reply reply : storedSone.getReplies()) {
758 replies.remove(reply.getId());
760 synchronized (newReplies) {
761 for (Reply reply : sone.getReplies()) {
762 if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
763 newReplies.add(reply.getId());
764 coreListenerManager.fireNewReplyFound(reply);
766 replies.put(reply.getId(), reply);
770 synchronized (storedSone) {
771 storedSone.setTime(sone.getTime());
772 storedSone.setClient(sone.getClient());
773 storedSone.setProfile(sone.getProfile());
774 storedSone.setPosts(sone.getPosts());
775 storedSone.setReplies(sone.getReplies());
776 storedSone.setLikePostIds(sone.getLikedPostIds());
777 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
778 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
784 * Deletes the given Sone. This will remove the Sone from the
785 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
786 * and remove the context from its identity.
791 public void deleteSone(Sone sone) {
792 if (!(sone.getIdentity() instanceof OwnIdentity)) {
793 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
796 synchronized (localSones) {
797 if (!localSones.containsKey(sone.getId())) {
798 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
801 localSones.remove(sone.getId());
802 soneInserters.remove(sone).stop();
804 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
805 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
807 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
808 } catch (ConfigurationException ce1) {
809 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
814 * Loads and updates the given Sone from the configuration. If any error is
815 * encountered, loading is aborted and the given Sone is not changed.
818 * The Sone to load and update
820 public void loadSone(Sone sone) {
821 if (!isLocalSone(sone)) {
822 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
827 String sonePrefix = "Sone/" + sone.getId();
828 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
829 if (soneTime == null) {
830 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
833 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
836 Profile profile = new Profile();
837 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
838 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
839 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
840 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
841 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
842 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
845 Set<Post> posts = new HashSet<Post>();
847 String postPrefix = sonePrefix + "/Posts/" + posts.size();
848 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
849 if (postId == null) {
852 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
853 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
854 if ((postTime == 0) || (postText == null)) {
855 logger.log(Level.WARNING, "Invalid post found, aborting load!");
858 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
862 Set<Reply> replies = new HashSet<Reply>();
864 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
865 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
866 if (replyId == null) {
869 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
870 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
871 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
872 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
873 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
876 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
879 /* load post likes. */
880 Set<String> likedPostIds = new HashSet<String>();
882 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
883 if (likedPostId == null) {
886 likedPostIds.add(likedPostId);
889 /* load reply likes. */
890 Set<String> likedReplyIds = new HashSet<String>();
892 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
893 if (likedReplyId == null) {
896 likedReplyIds.add(likedReplyId);
900 Set<String> friends = new HashSet<String>();
902 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
903 if (friendId == null) {
906 friends.add(friendId);
909 /* if we’re still here, Sone was loaded successfully. */
910 synchronized (sone) {
911 sone.setTime(soneTime);
912 sone.setProfile(profile);
913 sone.setPosts(posts);
914 sone.setReplies(replies);
915 sone.setLikePostIds(likedPostIds);
916 sone.setLikeReplyIds(likedReplyIds);
917 sone.setFriends(friends);
918 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
920 synchronized (newSones) {
921 for (String friend : friends) {
922 knownSones.add(friend);
925 synchronized (newPosts) {
926 for (Post post : posts) {
927 knownPosts.add(post.getId());
930 synchronized (newReplies) {
931 for (Reply reply : replies) {
932 knownReplies.add(reply.getId());
938 * Saves the given Sone. This will persist all local settings for the given
939 * Sone, such as the friends list and similar, private options.
944 public void saveSone(Sone sone) {
945 if (!isLocalSone(sone)) {
946 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
949 if (!(sone.getIdentity() instanceof OwnIdentity)) {
950 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
954 logger.log(Level.INFO, "Saving Sone: %s", sone);
955 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
957 /* save Sone into configuration. */
958 String sonePrefix = "Sone/" + sone.getId();
959 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
960 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
963 Profile profile = sone.getProfile();
964 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
965 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
966 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
967 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
968 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
969 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
973 for (Post post : sone.getPosts()) {
974 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
975 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
976 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
977 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
979 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
982 int replyCounter = 0;
983 for (Reply reply : sone.getReplies()) {
984 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
985 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
986 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
987 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
988 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
990 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
992 /* save post likes. */
993 int postLikeCounter = 0;
994 for (String postId : sone.getLikedPostIds()) {
995 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
997 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
999 /* save reply likes. */
1000 int replyLikeCounter = 0;
1001 for (String replyId : sone.getLikedReplyIds()) {
1002 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
1004 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
1007 int friendCounter = 0;
1008 for (String friendId : sone.getFriends()) {
1009 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1011 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1013 logger.log(Level.INFO, "Sone %s saved.", sone);
1014 } catch (ConfigurationException ce1) {
1015 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1020 * Creates a new post.
1023 * The Sone that creates the post
1025 * The text of the post
1027 public void createPost(Sone sone, String text) {
1028 createPost(sone, System.currentTimeMillis(), text);
1032 * Creates a new post.
1035 * The Sone that creates the post
1037 * The time of the post
1039 * The text of the post
1041 public void createPost(Sone sone, long time, String text) {
1042 if (!isLocalSone(sone)) {
1043 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1046 Post post = new Post(sone, time, text);
1047 synchronized (posts) {
1048 posts.put(post.getId(), post);
1050 synchronized (newPosts) {
1051 knownPosts.add(post.getId());
1058 * Deletes the given post.
1061 * The post to delete
1063 public void deletePost(Post post) {
1064 if (!isLocalSone(post.getSone())) {
1065 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1068 post.getSone().removePost(post);
1069 synchronized (posts) {
1070 posts.remove(post.getId());
1072 saveSone(post.getSone());
1076 * Creates a new reply.
1079 * The Sone that creates the reply
1081 * The post that this reply refers to
1083 * The text of the reply
1084 * @return The created reply
1086 public Reply createReply(Sone sone, Post post, String text) {
1087 return createReply(sone, post, System.currentTimeMillis(), text);
1091 * Creates a new reply.
1094 * The Sone that creates the reply
1096 * The post that this reply refers to
1098 * The time of the reply
1100 * The text of the reply
1101 * @return The created reply
1103 public Reply createReply(Sone sone, Post post, long time, String text) {
1104 if (!isLocalSone(sone)) {
1105 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1108 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1109 synchronized (replies) {
1110 replies.put(reply.getId(), reply);
1112 synchronized (newReplies) {
1113 knownReplies.add(reply.getId());
1115 sone.addReply(reply);
1121 * Deletes the given reply.
1124 * The reply to delete
1126 public void deleteReply(Reply reply) {
1127 Sone sone = reply.getSone();
1128 if (!isLocalSone(sone)) {
1129 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1132 synchronized (replies) {
1133 replies.remove(reply.getId());
1135 sone.removeReply(reply);
1142 public void start() {
1143 loadConfiguration();
1149 public void stop() {
1150 synchronized (localSones) {
1151 for (SoneInserter soneInserter : soneInserters.values()) {
1152 soneInserter.stop();
1155 saveConfiguration();
1164 * Loads the configuration.
1166 @SuppressWarnings("unchecked")
1167 private void loadConfiguration() {
1168 /* create options. */
1169 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1172 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1173 SoneInserter.setInsertionDelay(newValue);
1177 options.addBooleanOption("SoneRescueMode", new DefaultOption<Boolean>(false));
1178 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1179 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1181 /* read options from configuration. */
1182 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1183 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1184 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1185 options.getBooleanOption("ClearOnNextRestart").set(null);
1186 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1187 if (clearConfiguration) {
1188 /* stop loading the configuration. */
1192 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1193 options.getBooleanOption("SoneRescueMode").set(configuration.getBooleanValue("Option/SoneRescueMode").getValue(null));
1195 /* load known Sones. */
1196 int soneCounter = 0;
1198 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1199 if (knownSoneId == null) {
1202 synchronized (newSones) {
1203 knownSones.add(knownSoneId);
1207 /* load known posts. */
1208 int postCounter = 0;
1210 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1211 if (knownPostId == null) {
1214 synchronized (newPosts) {
1215 knownPosts.add(knownPostId);
1219 /* load known replies. */
1220 int replyCounter = 0;
1222 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1223 if (knownReplyId == null) {
1226 synchronized (newReplies) {
1227 knownReplies.add(knownReplyId);
1234 * Saves the current options.
1236 private void saveConfiguration() {
1237 /* store the options first. */
1239 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1240 configuration.getBooleanValue("Option/SoneRescueMode").setValue(options.getBooleanOption("SoneRescueMode").getReal());
1241 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1242 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1244 /* save known Sones. */
1245 int soneCounter = 0;
1246 synchronized (newSones) {
1247 for (String knownSoneId : knownSones) {
1248 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1250 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1253 /* save known posts. */
1254 int postCounter = 0;
1255 synchronized (newPosts) {
1256 for (String knownPostId : knownPosts) {
1257 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1259 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1262 /* save known replies. */
1263 int replyCounter = 0;
1264 synchronized (newReplies) {
1265 for (String knownReplyId : knownReplies) {
1266 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1268 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1271 } catch (ConfigurationException ce1) {
1272 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1277 * Generate a Sone URI from the given URI and latest edition.
1280 * The URI to derive the Sone URI from
1281 * @return The derived URI
1283 private FreenetURI getSoneUri(String uriString) {
1285 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1287 } catch (MalformedURLException mue1) {
1288 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1294 // INTERFACE IdentityListener
1301 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1302 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1303 if (ownIdentity.hasContext("Sone")) {
1304 addLocalSone(ownIdentity);
1312 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1313 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1320 public void identityAdded(Identity identity) {
1321 logger.log(Level.FINEST, "Adding Identity: " + identity);
1322 addRemoteSone(identity);
1329 public void identityUpdated(final Identity identity) {
1330 new Thread(new Runnable() {
1333 @SuppressWarnings("synthetic-access")
1335 Sone sone = getRemoteSone(identity.getId());
1336 soneDownloader.fetchSone(sone);
1345 public void identityRemoved(Identity identity) {