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 /** The Sones’ statuses. */
99 /* synchronize access on itself. */
100 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
102 /** Sone inserters. */
103 /* synchronize access on this on localSones. */
104 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
106 /** All local Sones. */
107 /* synchronize access on this on itself. */
108 private Map<String, Sone> localSones = new HashMap<String, Sone>();
110 /** All remote Sones. */
111 /* synchronize access on this on itself. */
112 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
114 /** All new Sones. */
115 private Set<String> newSones = new HashSet<String>();
117 /** All known Sones. */
118 /* synchronize access on {@link #newSones}. */
119 private Set<String> knownSones = new HashSet<String>();
122 private Map<String, Post> posts = new HashMap<String, Post>();
124 /** All new posts. */
125 private Set<String> newPosts = new HashSet<String>();
127 /** All known posts. */
128 /* synchronize access on {@link #newPosts}. */
129 private Set<String> knownPosts = new HashSet<String>();
132 private Map<String, Reply> replies = new HashMap<String, Reply>();
134 /** All new replies. */
135 private Set<String> newReplies = new HashSet<String>();
137 /** All known replies. */
138 private Set<String> knownReplies = new HashSet<String>();
141 * Creates a new core.
143 * @param configuration
144 * The configuration of the core
145 * @param freenetInterface
146 * The freenet interface
147 * @param identityManager
148 * The identity manager
150 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
151 this.configuration = configuration;
152 this.freenetInterface = freenetInterface;
153 this.identityManager = identityManager;
154 this.soneDownloader = new SoneDownloader(this, freenetInterface);
158 // LISTENER MANAGEMENT
162 * Adds a new core listener.
164 * @param coreListener
165 * The listener to add
167 public void addCoreListener(CoreListener coreListener) {
168 coreListenerManager.addListener(coreListener);
172 * Removes a core listener.
174 * @param coreListener
175 * The listener to remove
177 public void removeCoreListener(CoreListener coreListener) {
178 coreListenerManager.removeListener(coreListener);
186 * Returns the options used by the core.
188 * @return The options of the core
190 public Options getOptions() {
195 * Returns the identity manager used by the core.
197 * @return The identity manager
199 public IdentityManager getIdentityManager() {
200 return identityManager;
204 * Returns the status of the given Sone.
207 * The Sone to get the status for
208 * @return The status of the Sone
210 public SoneStatus getSoneStatus(Sone sone) {
211 synchronized (soneStatuses) {
212 return soneStatuses.get(sone);
217 * Sets the status of the given Sone.
220 * The Sone to set the status of
224 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
225 synchronized (soneStatuses) {
226 soneStatuses.put(sone, soneStatus);
231 * Returns all Sones, remote and local.
235 public Set<Sone> getSones() {
236 Set<Sone> allSones = new HashSet<Sone>();
237 allSones.addAll(getLocalSones());
238 allSones.addAll(getRemoteSones());
243 * Returns the Sone with the given ID, regardless whether it’s local or
247 * The ID of the Sone to get
248 * @return The Sone with the given ID, or {@code null} if there is no such
251 public Sone getSone(String id) {
252 return getSone(id, true);
256 * Returns the Sone with the given ID, regardless whether it’s local or
260 * The ID of the Sone to get
262 * {@code true} to create a new Sone if none exists,
263 * {@code false} to return {@code null} if a Sone with the given
265 * @return The Sone with the given ID, or {@code null} if there is no such
268 public Sone getSone(String id, boolean create) {
269 if (isLocalSone(id)) {
270 return getLocalSone(id);
272 return getRemoteSone(id, create);
276 * Checks whether the core knows a Sone with the given ID.
280 * @return {@code true} if there is a Sone with the given ID, {@code false}
283 public boolean hasSone(String id) {
284 return isLocalSone(id) || isRemoteSone(id);
288 * Returns whether the given Sone is a local Sone.
291 * The Sone to check for its locality
292 * @return {@code true} if the given Sone is local, {@code false} otherwise
294 public boolean isLocalSone(Sone sone) {
295 synchronized (localSones) {
296 return localSones.containsKey(sone.getId());
301 * Returns whether the given ID is the ID of a local Sone.
304 * The Sone ID to check for its locality
305 * @return {@code true} if the given ID is a local Sone, {@code false}
308 public boolean isLocalSone(String id) {
309 synchronized (localSones) {
310 return localSones.containsKey(id);
315 * Returns all local Sones.
317 * @return All local Sones
319 public Set<Sone> getLocalSones() {
320 synchronized (localSones) {
321 return new HashSet<Sone>(localSones.values());
326 * Returns the local Sone with the given ID.
329 * The ID of the Sone to get
330 * @return The Sone with the given ID
332 public Sone getLocalSone(String id) {
333 return getLocalSone(id, true);
337 * Returns the local Sone with the given ID, optionally creating a new Sone.
342 * {@code true} to create a new Sone if none exists,
343 * {@code false} to return null if none exists
344 * @return The Sone with the given ID, or {@code null}
346 public Sone getLocalSone(String id, boolean create) {
347 synchronized (localSones) {
348 Sone sone = localSones.get(id);
349 if ((sone == null) && create) {
351 localSones.put(id, sone);
358 * Returns all remote Sones.
360 * @return All remote Sones
362 public Set<Sone> getRemoteSones() {
363 synchronized (remoteSones) {
364 return new HashSet<Sone>(remoteSones.values());
369 * Returns the remote Sone with the given ID.
372 * The ID of the remote Sone to get
373 * @return The Sone with the given ID
375 public Sone getRemoteSone(String id) {
376 return getRemoteSone(id, true);
380 * Returns the remote Sone with the given ID.
383 * The ID of the remote Sone to get
385 * {@code true} to always create a Sone, {@code false} to return
386 * {@code null} if no Sone with the given ID exists
387 * @return The Sone with the given ID
389 public Sone getRemoteSone(String id, boolean create) {
390 synchronized (remoteSones) {
391 Sone sone = remoteSones.get(id);
392 if ((sone == null) && create) {
394 remoteSones.put(id, sone);
401 * Returns whether the given Sone is a remote Sone.
405 * @return {@code true} if the given Sone is a remote Sone, {@code false}
408 public boolean isRemoteSone(Sone sone) {
409 synchronized (remoteSones) {
410 return remoteSones.containsKey(sone.getId());
415 * Returns whether the Sone with the given ID is a remote Sone.
418 * The ID of the Sone to check
419 * @return {@code true} if the Sone with the given ID is a remote Sone,
420 * {@code false} otherwise
422 public boolean isRemoteSone(String id) {
423 synchronized (remoteSones) {
424 return remoteSones.containsKey(id);
429 * Returns whether the given Sone is a new Sone. After this check, the Sone
430 * is marked as known, i.e. a second call with the same parameters will
431 * always yield {@code false}.
434 * The sone to check for
435 * @return {@code true} if the given Sone is new, false otherwise
437 public boolean isNewSone(Sone sone) {
438 synchronized (newSones) {
439 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
440 knownSones.add(sone.getId());
442 coreListenerManager.fireMarkSoneKnown(sone);
449 * Returns whether the given Sone has been modified.
452 * The Sone to check for modifications
453 * @return {@code true} if a modification has been detected in the Sone,
454 * {@code false} otherwise
456 public boolean isModifiedSone(Sone sone) {
457 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
461 * Returns the post with the given ID.
464 * The ID of the post to get
465 * @return The post, or {@code null} if there is no such post
467 public Post getPost(String postId) {
468 synchronized (posts) {
469 Post post = posts.get(postId);
471 post = new Post(postId);
472 posts.put(postId, post);
479 * Returns whether the given post ID is new. After this method returns it is
480 * marked a known post ID.
484 * @return {@code true} if the post is considered to be new, {@code false}
487 public boolean isNewPost(String postId) {
488 synchronized (newPosts) {
489 boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
490 knownPosts.add(postId);
496 * Returns the reply with the given ID.
499 * The ID of the reply to get
500 * @return The reply, or {@code null} if there is no such reply
502 public Reply getReply(String replyId) {
503 synchronized (replies) {
504 Reply reply = replies.get(replyId);
506 reply = new Reply(replyId);
507 replies.put(replyId, reply);
514 * Returns all replies for the given post, order ascending by time.
517 * The post to get all replies for
518 * @return All replies for the given post
520 public List<Reply> getReplies(Post post) {
521 Set<Sone> sones = getSones();
522 List<Reply> replies = new ArrayList<Reply>();
523 for (Sone sone : sones) {
524 for (Reply reply : sone.getReplies()) {
525 if (reply.getPost().equals(post)) {
530 Collections.sort(replies, Reply.TIME_COMPARATOR);
535 * Returns whether the reply with the given ID is new.
538 * The ID of the reply to check
539 * @return {@code true} if the reply is considered to be new, {@code false}
542 public boolean isNewReply(String replyId) {
543 synchronized (newReplies) {
544 boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
545 knownReplies.add(replyId);
551 * Returns all Sones that have liked the given post.
554 * The post to get the liking Sones for
555 * @return The Sones that like the given post
557 public Set<Sone> getLikes(Post post) {
558 Set<Sone> sones = new HashSet<Sone>();
559 for (Sone sone : getSones()) {
560 if (sone.getLikedPostIds().contains(post.getId())) {
568 * Returns all Sones that have liked the given reply.
571 * The reply to get the liking Sones for
572 * @return The Sones that like the given reply
574 public Set<Sone> getLikes(Reply reply) {
575 Set<Sone> sones = new HashSet<Sone>();
576 for (Sone sone : getSones()) {
577 if (sone.getLikedReplyIds().contains(reply.getId())) {
589 * Adds a local Sone from the given ID which has to be the ID of an own
593 * The ID of an own identity to add a Sone for
594 * @return The added (or already existing) Sone
596 public Sone addLocalSone(String id) {
597 synchronized (localSones) {
598 if (localSones.containsKey(id)) {
599 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
600 return localSones.get(id);
602 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
603 if (ownIdentity == null) {
604 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
607 return addLocalSone(ownIdentity);
612 * Adds a local Sone from the given own identity.
615 * The own identity to create a Sone from
616 * @return The added (or already existing) Sone
618 public Sone addLocalSone(OwnIdentity ownIdentity) {
619 if (ownIdentity == null) {
620 logger.log(Level.WARNING, "Given OwnIdentity is null!");
623 synchronized (localSones) {
626 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
627 } catch (MalformedURLException mue1) {
628 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
631 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
632 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
633 /* TODO - load posts ’n stuff */
634 localSones.put(ownIdentity.getId(), sone);
635 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
636 soneInserters.put(sone, soneInserter);
637 setSoneStatus(sone, SoneStatus.idle);
639 soneInserter.start();
640 new Thread(new Runnable() {
643 @SuppressWarnings("synthetic-access")
645 soneDownloader.fetchSone(sone);
648 }, "Sone Downloader").start();
654 * Creates a new Sone for the given own identity.
657 * The own identity to create a Sone for
658 * @return The created Sone
660 public Sone createSone(OwnIdentity ownIdentity) {
661 identityManager.addContext(ownIdentity, "Sone");
662 Sone sone = addLocalSone(ownIdentity);
667 * Adds the Sone of the given identity.
670 * The identity whose Sone to add
671 * @return The added or already existing Sone
673 public Sone addRemoteSone(Identity identity) {
674 if (identity == null) {
675 logger.log(Level.WARNING, "Given Identity is null!");
678 synchronized (remoteSones) {
679 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
680 boolean newSone = sone.getRequestUri() == null;
681 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
682 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
684 synchronized (newSones) {
685 newSone = !knownSones.contains(sone.getId());
687 newSones.add(sone.getId());
691 coreListenerManager.fireNewSoneFound(sone);
694 remoteSones.put(identity.getId(), sone);
695 soneDownloader.addSone(sone);
696 setSoneStatus(sone, SoneStatus.unknown);
697 new Thread(new Runnable() {
700 @SuppressWarnings("synthetic-access")
702 soneDownloader.fetchSone(sone);
705 }, "Sone Downloader").start();
711 * Updates the stores Sone with the given Sone.
716 public void updateSone(Sone sone) {
717 if (hasSone(sone.getId())) {
718 Sone storedSone = getSone(sone.getId());
719 if (!(sone.getTime() > storedSone.getTime())) {
720 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
723 synchronized (posts) {
724 for (Post post : storedSone.getPosts()) {
725 posts.remove(post.getId());
727 synchronized (newPosts) {
728 for (Post post : sone.getPosts()) {
729 if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) {
730 newPosts.add(post.getId());
732 posts.put(post.getId(), post);
736 synchronized (replies) {
737 for (Reply reply : storedSone.getReplies()) {
738 replies.remove(reply.getId());
740 synchronized (newReplies) {
741 for (Reply reply : sone.getReplies()) {
742 if (!storedSone.getReplies().contains(reply) && !knownSones.contains(reply.getId())) {
743 newReplies.add(reply.getId());
745 replies.put(reply.getId(), reply);
749 synchronized (storedSone) {
750 storedSone.setTime(sone.getTime());
751 storedSone.setClient(sone.getClient());
752 storedSone.setProfile(sone.getProfile());
753 storedSone.setPosts(sone.getPosts());
754 storedSone.setReplies(sone.getReplies());
755 storedSone.setLikePostIds(sone.getLikedPostIds());
756 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
757 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
763 * Deletes the given Sone. This will remove the Sone from the
764 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
765 * and remove the context from its identity.
770 public void deleteSone(Sone sone) {
771 if (!(sone.getIdentity() instanceof OwnIdentity)) {
772 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
775 synchronized (localSones) {
776 if (!localSones.containsKey(sone.getId())) {
777 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
780 localSones.remove(sone.getId());
781 soneInserters.remove(sone).stop();
783 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
784 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
786 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
787 } catch (ConfigurationException ce1) {
788 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
793 * Loads and updates the given Sone from the configuration. If any error is
794 * encountered, loading is aborted and the given Sone is not changed.
797 * The Sone to load and update
799 public void loadSone(Sone sone) {
800 if (!isLocalSone(sone)) {
801 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
806 String sonePrefix = "Sone/" + sone.getId();
807 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
808 if (soneTime == null) {
809 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
812 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
815 Profile profile = new Profile();
816 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
817 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
818 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
819 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
820 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
821 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
824 Set<Post> posts = new HashSet<Post>();
826 String postPrefix = sonePrefix + "/Posts/" + posts.size();
827 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
828 if (postId == null) {
831 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
832 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
833 if ((postTime == 0) || (postText == null)) {
834 logger.log(Level.WARNING, "Invalid post found, aborting load!");
837 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
841 Set<Reply> replies = new HashSet<Reply>();
843 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
844 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
845 if (replyId == null) {
848 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
849 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
850 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
851 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
852 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
855 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
858 /* load post likes. */
859 Set<String> likedPostIds = new HashSet<String>();
861 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
862 if (likedPostId == null) {
865 likedPostIds.add(likedPostId);
868 /* load reply likes. */
869 Set<String> likedReplyIds = new HashSet<String>();
871 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
872 if (likedReplyId == null) {
875 likedReplyIds.add(likedReplyId);
879 Set<String> friends = new HashSet<String>();
881 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
882 if (friendId == null) {
885 friends.add(friendId);
888 /* if we’re still here, Sone was loaded successfully. */
889 synchronized (sone) {
890 sone.setTime(soneTime);
891 sone.setProfile(profile);
892 sone.setPosts(posts);
893 sone.setReplies(replies);
894 sone.setLikePostIds(likedPostIds);
895 sone.setLikeReplyIds(likedReplyIds);
896 sone.setFriends(friends);
897 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
899 synchronized (newSones) {
900 for (String friend : friends) {
901 knownSones.add(friend);
904 synchronized (newPosts) {
905 for (Post post : posts) {
906 knownPosts.add(post.getId());
909 synchronized (newReplies) {
910 for (Reply reply : replies) {
911 knownReplies.add(reply.getId());
917 * Saves the given Sone. This will persist all local settings for the given
918 * Sone, such as the friends list and similar, private options.
923 public void saveSone(Sone sone) {
924 if (!isLocalSone(sone)) {
925 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
928 if (!(sone.getIdentity() instanceof OwnIdentity)) {
929 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
933 logger.log(Level.INFO, "Saving Sone: %s", sone);
934 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
936 /* save Sone into configuration. */
937 String sonePrefix = "Sone/" + sone.getId();
938 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
939 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
942 Profile profile = sone.getProfile();
943 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
944 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
945 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
946 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
947 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
948 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
952 for (Post post : sone.getPosts()) {
953 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
954 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
955 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
956 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
958 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
961 int replyCounter = 0;
962 for (Reply reply : sone.getReplies()) {
963 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
964 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
965 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
966 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
967 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
969 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
971 /* save post likes. */
972 int postLikeCounter = 0;
973 for (String postId : sone.getLikedPostIds()) {
974 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
976 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
978 /* save reply likes. */
979 int replyLikeCounter = 0;
980 for (String replyId : sone.getLikedReplyIds()) {
981 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
983 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
986 int friendCounter = 0;
987 for (String friendId : sone.getFriends()) {
988 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
990 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
992 logger.log(Level.INFO, "Sone %s saved.", sone);
993 } catch (ConfigurationException ce1) {
994 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
999 * Creates a new post.
1002 * The Sone that creates the post
1004 * The text of the post
1006 public void createPost(Sone sone, String text) {
1007 createPost(sone, System.currentTimeMillis(), text);
1011 * Creates a new post.
1014 * The Sone that creates the post
1016 * The time of the post
1018 * The text of the post
1020 public void createPost(Sone sone, long time, String text) {
1021 if (!isLocalSone(sone)) {
1022 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1025 Post post = new Post(sone, time, text);
1026 synchronized (posts) {
1027 posts.put(post.getId(), post);
1029 synchronized (newPosts) {
1030 knownPosts.add(post.getId());
1037 * Deletes the given post.
1040 * The post to delete
1042 public void deletePost(Post post) {
1043 if (!isLocalSone(post.getSone())) {
1044 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1047 post.getSone().removePost(post);
1048 synchronized (posts) {
1049 posts.remove(post.getId());
1051 saveSone(post.getSone());
1055 * Creates a new reply.
1058 * The Sone that creates the reply
1060 * The post that this reply refers to
1062 * The text of the reply
1063 * @return The created reply
1065 public Reply createReply(Sone sone, Post post, String text) {
1066 return createReply(sone, post, System.currentTimeMillis(), text);
1070 * Creates a new reply.
1073 * The Sone that creates the reply
1075 * The post that this reply refers to
1077 * The time of the reply
1079 * The text of the reply
1080 * @return The created reply
1082 public Reply createReply(Sone sone, Post post, long time, String text) {
1083 if (!isLocalSone(sone)) {
1084 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1087 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1088 synchronized (replies) {
1089 replies.put(reply.getId(), reply);
1091 synchronized (newReplies) {
1092 knownReplies.add(reply.getId());
1094 sone.addReply(reply);
1100 * Deletes the given reply.
1103 * The reply to delete
1105 public void deleteReply(Reply reply) {
1106 Sone sone = reply.getSone();
1107 if (!isLocalSone(sone)) {
1108 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1111 synchronized (replies) {
1112 replies.remove(reply.getId());
1114 sone.removeReply(reply);
1121 public void start() {
1122 loadConfiguration();
1128 public void stop() {
1129 synchronized (localSones) {
1130 for (SoneInserter soneInserter : soneInserters.values()) {
1131 soneInserter.stop();
1134 saveConfiguration();
1142 * Loads the configuration.
1144 @SuppressWarnings("unchecked")
1145 private void loadConfiguration() {
1146 /* create options. */
1147 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1150 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1151 SoneInserter.setInsertionDelay(newValue);
1155 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1156 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1158 /* read options from configuration. */
1159 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1160 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1161 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1162 options.getBooleanOption("ClearOnNextRestart").set(null);
1163 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1164 if (clearConfiguration) {
1165 /* stop loading the configuration. */
1169 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1171 /* load known Sones. */
1172 int soneCounter = 0;
1174 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1175 if (knownSoneId == null) {
1178 synchronized (newSones) {
1179 knownSones.add(knownSoneId);
1183 /* load known posts. */
1184 int postCounter = 0;
1186 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1187 if (knownPostId == null) {
1190 synchronized (newPosts) {
1191 knownPosts.add(knownPostId);
1195 /* load known replies. */
1196 int replyCounter = 0;
1198 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1199 if (knownReplyId == null) {
1202 synchronized (newReplies) {
1203 knownReplies.add(knownReplyId);
1210 * Saves the current options.
1212 private void saveConfiguration() {
1213 /* store the options first. */
1215 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1216 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1217 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1219 /* save known Sones. */
1220 int soneCounter = 0;
1221 synchronized (newSones) {
1222 for (String knownSoneId : knownSones) {
1223 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1225 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1228 /* save known posts. */
1229 int postCounter = 0;
1230 synchronized (newPosts) {
1231 for (String knownPostId : knownPosts) {
1232 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1234 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1237 /* save known replies. */
1238 int replyCounter = 0;
1239 synchronized (newReplies) {
1240 for (String knownReplyId : knownReplies) {
1241 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1243 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1246 } catch (ConfigurationException ce1) {
1247 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1252 * Generate a Sone URI from the given URI and latest edition.
1255 * The URI to derive the Sone URI from
1256 * @return The derived URI
1258 private FreenetURI getSoneUri(String uriString) {
1260 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1262 } catch (MalformedURLException mue1) {
1263 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1269 // INTERFACE IdentityListener
1276 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1277 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1278 if (ownIdentity.hasContext("Sone")) {
1279 addLocalSone(ownIdentity);
1287 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1288 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1295 public void identityAdded(Identity identity) {
1296 logger.log(Level.FINEST, "Adding Identity: " + identity);
1297 addRemoteSone(identity);
1304 public void identityUpdated(final Identity identity) {
1305 new Thread(new Runnable() {
1308 @SuppressWarnings("synthetic-access")
1310 Sone sone = getRemoteSone(identity.getId());
1311 soneDownloader.fetchSone(sone);
1320 public void identityRemoved(Identity identity) {