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.Post;
35 import net.pterodactylus.sone.data.Profile;
36 import net.pterodactylus.sone.data.Reply;
37 import net.pterodactylus.sone.data.Sone;
38 import net.pterodactylus.sone.freenet.wot.Identity;
39 import net.pterodactylus.sone.freenet.wot.IdentityListener;
40 import net.pterodactylus.sone.freenet.wot.IdentityManager;
41 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
42 import net.pterodactylus.util.config.Configuration;
43 import net.pterodactylus.util.config.ConfigurationException;
44 import net.pterodactylus.util.logging.Logging;
45 import net.pterodactylus.util.number.Numbers;
46 import freenet.keys.FreenetURI;
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class Core implements IdentityListener {
56 * Enumeration for the possible states of a {@link Sone}.
58 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60 public enum SoneStatus {
62 /** The Sone is unknown, i.e. not yet downloaded. */
65 /** The Sone is idle, i.e. not being downloaded or inserted. */
68 /** The Sone is currently being inserted. */
71 /** The Sone is currently being downloaded. */
76 private static final Logger logger = Logging.getLogger(Core.class);
79 private final Options options = new Options();
81 /** The configuration. */
82 private final Configuration configuration;
84 /** The identity manager. */
85 private final IdentityManager identityManager;
87 /** Interface to freenet. */
88 private final FreenetInterface freenetInterface;
90 /** The Sone downloader. */
91 private final SoneDownloader soneDownloader;
93 /** The Sones’ statuses. */
94 /* synchronize access on itself. */
95 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
97 /** Sone inserters. */
98 /* synchronize access on this on localSones. */
99 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
101 /** All local Sones. */
102 /* synchronize access on this on itself. */
103 private Map<String, Sone> localSones = new HashMap<String, Sone>();
105 /** All remote Sones. */
106 /* synchronize access on this on itself. */
107 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
109 /** All new Sones. */
110 private Set<String> newSones = new HashSet<String>();
112 /** All known Sones. */
113 /* synchronize access on {@link #newSones}. */
114 private Set<String> knownSones = new HashSet<String>();
117 private Map<String, Post> posts = new HashMap<String, Post>();
119 /** All new posts. */
120 private Set<String> newPosts = new HashSet<String>();
122 /** All known posts. */
123 /* synchronize access on {@link #newPosts}. */
124 private Set<String> knownPosts = new HashSet<String>();
127 private Map<String, Reply> replies = new HashMap<String, Reply>();
130 * Creates a new core.
132 * @param configuration
133 * The configuration of the core
134 * @param freenetInterface
135 * The freenet interface
136 * @param identityManager
137 * The identity manager
139 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
140 this.configuration = configuration;
141 this.freenetInterface = freenetInterface;
142 this.identityManager = identityManager;
143 this.soneDownloader = new SoneDownloader(this, freenetInterface);
151 * Returns the options used by the core.
153 * @return The options of the core
155 public Options getOptions() {
160 * Returns the identity manager used by the core.
162 * @return The identity manager
164 public IdentityManager getIdentityManager() {
165 return identityManager;
169 * Returns the status of the given Sone.
172 * The Sone to get the status for
173 * @return The status of the Sone
175 public SoneStatus getSoneStatus(Sone sone) {
176 synchronized (soneStatuses) {
177 return soneStatuses.get(sone);
182 * Sets the status of the given Sone.
185 * The Sone to set the status of
189 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
190 synchronized (soneStatuses) {
191 soneStatuses.put(sone, soneStatus);
196 * Returns all Sones, remote and local.
200 public Set<Sone> getSones() {
201 Set<Sone> allSones = new HashSet<Sone>();
202 allSones.addAll(getLocalSones());
203 allSones.addAll(getRemoteSones());
208 * Returns the Sone with the given ID, regardless whether it’s local or
212 * The ID of the Sone to get
213 * @return The Sone with the given ID, or {@code null} if there is no such
216 public Sone getSone(String id) {
217 return getSone(id, true);
221 * Returns the Sone with the given ID, regardless whether it’s local or
225 * The ID of the Sone to get
227 * {@code true} to create a new Sone if none exists,
228 * {@code false} to return {@code null} if a Sone with the given
230 * @return The Sone with the given ID, or {@code null} if there is no such
233 public Sone getSone(String id, boolean create) {
234 if (isLocalSone(id)) {
235 return getLocalSone(id);
237 return getRemoteSone(id, create);
241 * Checks whether the core knows a Sone with the given ID.
245 * @return {@code true} if there is a Sone with the given ID, {@code false}
248 public boolean hasSone(String id) {
249 return isLocalSone(id) || isRemoteSone(id);
253 * Returns whether the given Sone is a local Sone.
256 * The Sone to check for its locality
257 * @return {@code true} if the given Sone is local, {@code false} otherwise
259 public boolean isLocalSone(Sone sone) {
260 synchronized (localSones) {
261 return localSones.containsKey(sone.getId());
266 * Returns whether the given ID is the ID of a local Sone.
269 * The Sone ID to check for its locality
270 * @return {@code true} if the given ID is a local Sone, {@code false}
273 public boolean isLocalSone(String id) {
274 synchronized (localSones) {
275 return localSones.containsKey(id);
280 * Returns all local Sones.
282 * @return All local Sones
284 public Set<Sone> getLocalSones() {
285 synchronized (localSones) {
286 return new HashSet<Sone>(localSones.values());
291 * Returns the local Sone with the given ID.
294 * The ID of the Sone to get
295 * @return The Sone with the given ID
297 public Sone getLocalSone(String id) {
298 synchronized (localSones) {
299 Sone sone = localSones.get(id);
302 localSones.put(id, sone);
309 * Returns all remote Sones.
311 * @return All remote Sones
313 public Set<Sone> getRemoteSones() {
314 synchronized (remoteSones) {
315 return new HashSet<Sone>(remoteSones.values());
320 * Returns the remote Sone with the given ID.
323 * The ID of the remote Sone to get
324 * @return The Sone with the given ID
326 public Sone getRemoteSone(String id) {
327 return getRemoteSone(id, true);
331 * Returns the remote Sone with the given ID.
334 * The ID of the remote Sone to get
336 * {@code true} to always create a Sone, {@code false} to return
337 * {@code null} if no Sone with the given ID exists
338 * @return The Sone with the given ID
340 public Sone getRemoteSone(String id, boolean create) {
341 synchronized (remoteSones) {
342 Sone sone = remoteSones.get(id);
343 if ((sone == null) && create) {
345 remoteSones.put(id, sone);
352 * Returns whether the given Sone is a remote Sone.
356 * @return {@code true} if the given Sone is a remote Sone, {@code false}
359 public boolean isRemoteSone(Sone sone) {
360 synchronized (remoteSones) {
361 return remoteSones.containsKey(sone.getId());
366 * Returns whether the Sone with the given ID is a remote Sone.
369 * The ID of the Sone to check
370 * @return {@code true} if the Sone with the given ID is a remote Sone,
371 * {@code false} otherwise
373 public boolean isRemoteSone(String id) {
374 synchronized (remoteSones) {
375 return remoteSones.containsKey(id);
380 * Returns whether the given Sone is a new Sone. After this check, the Sone
381 * is marked as known, i.e. a second call with the same parameters will
382 * always yield {@code false}.
385 * The sone to check for
386 * @return {@code true} if the given Sone is new, false otherwise
388 public boolean isNewSone(Sone sone) {
389 synchronized (newSones) {
390 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
391 knownSones.add(sone.getId());
397 * Returns the post with the given ID.
400 * The ID of the post to get
401 * @return The post, or {@code null} if there is no such post
403 public Post getPost(String postId) {
404 synchronized (posts) {
405 Post post = posts.get(postId);
407 post = new Post(postId);
408 posts.put(postId, post);
415 * Returns whether the given post ID is new. After this method returns it is
416 * marked a known post ID.
420 * @return {@code true} if the post is considered to be new, {@code false}
423 public boolean isNewPost(String postId) {
424 synchronized (newPosts) {
425 boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
426 knownPosts.add(postId);
432 * Returns the reply with the given ID.
435 * The ID of the reply to get
436 * @return The reply, or {@code null} if there is no such reply
438 public Reply getReply(String replyId) {
439 synchronized (replies) {
440 Reply reply = replies.get(replyId);
442 reply = new Reply(replyId);
443 replies.put(replyId, reply);
450 * Returns all replies for the given post, order ascending by time.
453 * The post to get all replies for
454 * @return All replies for the given post
456 public List<Reply> getReplies(Post post) {
457 Set<Sone> sones = getSones();
458 List<Reply> replies = new ArrayList<Reply>();
459 for (Sone sone : sones) {
460 for (Reply reply : sone.getReplies()) {
461 if (reply.getPost().equals(post)) {
466 Collections.sort(replies, Reply.TIME_COMPARATOR);
471 * Returns all Sones that have liked the given post.
474 * The post to get the liking Sones for
475 * @return The Sones that like the given post
477 public Set<Sone> getLikes(Post post) {
478 Set<Sone> sones = new HashSet<Sone>();
479 for (Sone sone : getSones()) {
480 if (sone.getLikedPostIds().contains(post.getId())) {
488 * Returns all Sones that have liked the given reply.
491 * The reply to get the liking Sones for
492 * @return The Sones that like the given reply
494 public Set<Sone> getLikes(Reply reply) {
495 Set<Sone> sones = new HashSet<Sone>();
496 for (Sone sone : getSones()) {
497 if (sone.getLikedReplyIds().contains(reply.getId())) {
509 * Adds a local Sone from the given ID which has to be the ID of an own
513 * The ID of an own identity to add a Sone for
514 * @return The added (or already existing) Sone
516 public Sone addLocalSone(String id) {
517 synchronized (localSones) {
518 if (localSones.containsKey(id)) {
519 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
520 return localSones.get(id);
522 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
523 if (ownIdentity == null) {
524 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
527 return addLocalSone(ownIdentity);
532 * Adds a local Sone from the given own identity.
535 * The own identity to create a Sone from
536 * @return The added (or already existing) Sone
538 public Sone addLocalSone(OwnIdentity ownIdentity) {
539 if (ownIdentity == null) {
540 logger.log(Level.WARNING, "Given OwnIdentity is null!");
543 synchronized (localSones) {
546 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
547 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
548 } catch (MalformedURLException mue1) {
549 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
552 /* TODO - load posts ’n stuff */
553 localSones.put(ownIdentity.getId(), sone);
554 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
555 soneInserters.put(sone, soneInserter);
556 soneInserter.start();
557 setSoneStatus(sone, SoneStatus.idle);
559 new Thread(new Runnable() {
562 @SuppressWarnings("synthetic-access")
564 soneDownloader.fetchSone(sone);
567 }, "Sone Downloader").start();
573 * Creates a new Sone for the given own identity.
576 * The own identity to create a Sone for
577 * @return The created Sone
579 public Sone createSone(OwnIdentity ownIdentity) {
580 identityManager.addContext(ownIdentity, "Sone");
581 Sone sone = addLocalSone(ownIdentity);
582 synchronized (sone) {
583 /* mark as modified so that it gets inserted immediately. */
584 sone.setModificationCounter(sone.getModificationCounter() + 1);
590 * Adds the Sone of the given identity.
593 * The identity whose Sone to add
594 * @return The added or already existing Sone
596 public Sone addRemoteSone(Identity identity) {
597 if (identity == null) {
598 logger.log(Level.WARNING, "Given Identity is null!");
601 synchronized (remoteSones) {
602 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
603 boolean newSone = sone.getRequestUri() == null;
604 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
605 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
607 synchronized (newSones) {
608 newSones.add(sone.getId());
611 remoteSones.put(identity.getId(), sone);
612 soneDownloader.addSone(sone);
613 setSoneStatus(sone, SoneStatus.unknown);
614 new Thread(new Runnable() {
617 @SuppressWarnings("synthetic-access")
619 soneDownloader.fetchSone(sone);
622 }, "Sone Downloader").start();
628 * Updates the stores Sone with the given Sone.
633 public void updateSone(Sone sone) {
634 if (hasSone(sone.getId())) {
635 Sone storedSone = getSone(sone.getId());
636 if (!(sone.getTime() > storedSone.getTime())) {
637 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
640 synchronized (posts) {
641 for (Post post : storedSone.getPosts()) {
642 posts.remove(post.getId());
644 synchronized (newPosts) {
645 for (Post post : sone.getPosts()) {
646 if (!storedSone.getPosts().contains(post) && !knownSones.contains(post.getId())) {
647 newPosts.add(post.getId());
649 posts.put(post.getId(), post);
653 synchronized (replies) {
654 for (Reply reply : storedSone.getReplies()) {
655 replies.remove(reply.getId());
657 for (Reply reply : sone.getReplies()) {
658 replies.put(reply.getId(), reply);
661 synchronized (storedSone) {
662 storedSone.setTime(sone.getTime());
663 storedSone.setProfile(sone.getProfile());
664 storedSone.setPosts(sone.getPosts());
665 storedSone.setReplies(sone.getReplies());
666 storedSone.setLikePostIds(sone.getLikedPostIds());
667 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
668 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
669 storedSone.setModificationCounter(0);
675 * Deletes the given Sone. This will remove the Sone from the
676 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
677 * and remove the context from its identity.
682 public void deleteSone(Sone sone) {
683 if (!(sone.getIdentity() instanceof OwnIdentity)) {
684 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
687 synchronized (localSones) {
688 if (!localSones.containsKey(sone.getId())) {
689 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
692 localSones.remove(sone.getId());
693 soneInserters.remove(sone).stop();
695 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
696 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
698 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
699 } catch (ConfigurationException ce1) {
700 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
705 * Loads and updates the given Sone from the configuration. If any error is
706 * encountered, loading is aborted and the given Sone is not changed.
709 * The Sone to load and update
711 public void loadSone(Sone sone) {
712 if (!isLocalSone(sone)) {
713 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
718 String sonePrefix = "Sone/" + sone.getId();
719 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
720 if (soneTime == null) {
721 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
724 long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
727 Profile profile = new Profile();
728 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
729 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
730 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
731 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
732 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
733 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
736 Set<Post> posts = new HashSet<Post>();
738 String postPrefix = sonePrefix + "/Posts/" + posts.size();
739 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
740 if (postId == null) {
743 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
744 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
745 if ((postTime == 0) || (postText == null)) {
746 logger.log(Level.WARNING, "Invalid post found, aborting load!");
749 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
753 Set<Reply> replies = new HashSet<Reply>();
755 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
756 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
757 if (replyId == null) {
760 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
761 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
762 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
763 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
764 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
767 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
770 /* load post likes. */
771 Set<String> likedPostIds = new HashSet<String>();
773 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
774 if (likedPostId == null) {
777 likedPostIds.add(likedPostId);
780 /* load reply likes. */
781 Set<String> likedReplyIds = new HashSet<String>();
783 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
784 if (likedReplyId == null) {
787 likedReplyIds.add(likedReplyId);
791 Set<String> friends = new HashSet<String>();
793 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
794 if (friendId == null) {
797 friends.add(friendId);
800 /* if we’re still here, Sone was loaded successfully. */
801 synchronized (sone) {
802 sone.setTime(soneTime);
803 sone.setProfile(profile);
804 sone.setPosts(posts);
805 sone.setReplies(replies);
806 sone.setLikePostIds(likedPostIds);
807 sone.setLikeReplyIds(likedReplyIds);
808 sone.setFriends(friends);
809 sone.setModificationCounter(soneModificationCounter);
811 synchronized (newSones) {
812 for (String friend : friends) {
813 knownSones.add(friend);
816 synchronized (newPosts) {
817 for (Post post : posts) {
818 knownPosts.add(post.getId());
824 * Saves the given Sone. This will persist all local settings for the given
825 * Sone, such as the friends list and similar, private options.
830 public void saveSone(Sone sone) {
831 if (!isLocalSone(sone)) {
832 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
835 if (!(sone.getIdentity() instanceof OwnIdentity)) {
836 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
840 logger.log(Level.INFO, "Saving Sone: %s", sone);
841 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
843 /* save Sone into configuration. */
844 String sonePrefix = "Sone/" + sone.getId();
845 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
846 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
849 Profile profile = sone.getProfile();
850 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
851 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
852 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
853 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
854 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
855 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
859 for (Post post : sone.getPosts()) {
860 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
861 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
862 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
863 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
865 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
868 int replyCounter = 0;
869 for (Reply reply : sone.getReplies()) {
870 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
871 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
872 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
873 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
874 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
876 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
878 /* save post likes. */
879 int postLikeCounter = 0;
880 for (String postId : sone.getLikedPostIds()) {
881 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
883 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
885 /* save reply likes. */
886 int replyLikeCounter = 0;
887 for (String replyId : sone.getLikedReplyIds()) {
888 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
890 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
893 int friendCounter = 0;
894 for (String friendId : sone.getFriends()) {
895 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
897 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
899 logger.log(Level.INFO, "Sone %s saved.", sone);
900 } catch (ConfigurationException ce1) {
901 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
906 * Creates a new post.
909 * The Sone that creates the post
911 * The text of the post
913 public void createPost(Sone sone, String text) {
914 createPost(sone, System.currentTimeMillis(), text);
918 * Creates a new post.
921 * The Sone that creates the post
923 * The time of the post
925 * The text of the post
927 public void createPost(Sone sone, long time, String text) {
928 if (!isLocalSone(sone)) {
929 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
932 Post post = new Post(sone, time, text);
933 synchronized (posts) {
934 posts.put(post.getId(), post);
936 synchronized (newPosts) {
937 knownPosts.add(post.getId());
944 * Deletes the given post.
949 public void deletePost(Post post) {
950 if (!isLocalSone(post.getSone())) {
951 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
954 post.getSone().removePost(post);
955 synchronized (posts) {
956 posts.remove(post.getId());
958 saveSone(post.getSone());
962 * Creates a new reply.
965 * The Sone that creates the reply
967 * The post that this reply refers to
969 * The text of the reply
971 public void createReply(Sone sone, Post post, String text) {
972 createReply(sone, post, System.currentTimeMillis(), text);
976 * Creates a new reply.
979 * The Sone that creates the reply
981 * The post that this reply refers to
983 * The time of the reply
985 * The text of the reply
987 public void createReply(Sone sone, Post post, long time, String text) {
988 if (!isLocalSone(sone)) {
989 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
992 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
993 synchronized (replies) {
994 replies.put(reply.getId(), reply);
996 sone.addReply(reply);
1001 * Deletes the given reply.
1004 * The reply to delete
1006 public void deleteReply(Reply reply) {
1007 Sone sone = reply.getSone();
1008 if (!isLocalSone(sone)) {
1009 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1012 synchronized (replies) {
1013 replies.remove(reply.getId());
1015 sone.removeReply(reply);
1022 public void start() {
1023 loadConfiguration();
1029 public void stop() {
1030 synchronized (localSones) {
1031 for (SoneInserter soneInserter : soneInserters.values()) {
1032 soneInserter.stop();
1035 saveConfiguration();
1043 * Loads the configuration.
1045 @SuppressWarnings("unchecked")
1046 private void loadConfiguration() {
1047 /* create options. */
1048 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1051 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1052 SoneInserter.setInsertionDelay(newValue);
1056 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1057 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1059 /* read options from configuration. */
1060 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1061 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1062 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1063 options.getBooleanOption("ClearOnNextRestart").set(null);
1064 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1065 if (clearConfiguration) {
1066 /* stop loading the configuration. */
1070 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1072 /* load known Sones. */
1073 int soneCounter = 0;
1075 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1076 if (knownSoneId == null) {
1079 synchronized (newSones) {
1080 knownSones.add(knownSoneId);
1084 /* load known posts. */
1085 int postCounter = 0;
1087 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1088 if (knownPostId == null) {
1091 synchronized (newPosts) {
1092 knownPosts.add(knownPostId);
1099 * Saves the current options.
1101 private void saveConfiguration() {
1102 /* store the options first. */
1104 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1105 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1106 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1108 /* save known Sones. */
1109 int soneCounter = 0;
1110 synchronized (newSones) {
1111 for (String knownSoneId : knownSones) {
1112 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1114 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1117 /* save known posts. */
1118 int postCounter = 0;
1119 synchronized (newPosts) {
1120 for (String knownPostId : knownPosts) {
1121 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1123 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1126 } catch (ConfigurationException ce1) {
1127 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1132 * Generate a Sone URI from the given URI and latest edition.
1135 * The URI to derive the Sone URI from
1136 * @return The derived URI
1138 private FreenetURI getSoneUri(String uriString) {
1140 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1142 } catch (MalformedURLException mue1) {
1143 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1149 // INTERFACE IdentityListener
1156 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1157 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1158 if (ownIdentity.hasContext("Sone")) {
1159 addLocalSone(ownIdentity);
1167 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1168 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1175 public void identityAdded(Identity identity) {
1176 logger.log(Level.FINEST, "Adding Identity: " + identity);
1177 addRemoteSone(identity);
1184 public void identityUpdated(final Identity identity) {
1185 new Thread(new Runnable() {
1188 @SuppressWarnings("synthetic-access")
1190 Sone sone = getRemoteSone(identity.getId());
1191 soneDownloader.fetchSone(sone);
1200 public void identityRemoved(Identity identity) {