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 the identity manager used by the core.
200 * @return The identity manager
202 public IdentityManager getIdentityManager() {
203 return identityManager;
207 * Returns the status of the given Sone.
210 * The Sone to get the status for
211 * @return The status of the Sone
213 public SoneStatus getSoneStatus(Sone sone) {
214 synchronized (soneStatuses) {
215 return soneStatuses.get(sone);
220 * Sets the status of the given Sone.
223 * The Sone to set the status of
227 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
228 synchronized (soneStatuses) {
229 soneStatuses.put(sone, soneStatus);
234 * Returns all Sones, remote and local.
238 public Set<Sone> getSones() {
239 Set<Sone> allSones = new HashSet<Sone>();
240 allSones.addAll(getLocalSones());
241 allSones.addAll(getRemoteSones());
246 * Returns the Sone with the given ID, regardless whether it’s local or
250 * The ID of the Sone to get
251 * @return The Sone with the given ID, or {@code null} if there is no such
254 public Sone getSone(String id) {
255 return getSone(id, true);
259 * Returns the Sone with the given ID, regardless whether it’s local or
263 * The ID of the Sone to get
265 * {@code true} to create a new Sone if none exists,
266 * {@code false} to return {@code null} if a Sone with the given
268 * @return The Sone with the given ID, or {@code null} if there is no such
271 public Sone getSone(String id, boolean create) {
272 if (isLocalSone(id)) {
273 return getLocalSone(id);
275 return getRemoteSone(id, create);
279 * Checks whether the core knows a Sone with the given ID.
283 * @return {@code true} if there is a Sone with the given ID, {@code false}
286 public boolean hasSone(String id) {
287 return isLocalSone(id) || isRemoteSone(id);
291 * Returns whether the given Sone is a local Sone.
294 * The Sone to check for its locality
295 * @return {@code true} if the given Sone is local, {@code false} otherwise
297 public boolean isLocalSone(Sone sone) {
298 synchronized (localSones) {
299 return localSones.containsKey(sone.getId());
304 * Returns whether the given ID is the ID of a local Sone.
307 * The Sone ID to check for its locality
308 * @return {@code true} if the given ID is a local Sone, {@code false}
311 public boolean isLocalSone(String id) {
312 synchronized (localSones) {
313 return localSones.containsKey(id);
318 * Returns all local Sones.
320 * @return All local Sones
322 public Set<Sone> getLocalSones() {
323 synchronized (localSones) {
324 return new HashSet<Sone>(localSones.values());
329 * Returns the local Sone with the given ID.
332 * The ID of the Sone to get
333 * @return The Sone with the given ID
335 public Sone getLocalSone(String id) {
336 return getLocalSone(id, true);
340 * Returns the local Sone with the given ID, optionally creating a new Sone.
345 * {@code true} to create a new Sone if none exists,
346 * {@code false} to return null if none exists
347 * @return The Sone with the given ID, or {@code null}
349 public Sone getLocalSone(String id, boolean create) {
350 synchronized (localSones) {
351 Sone sone = localSones.get(id);
352 if ((sone == null) && create) {
354 localSones.put(id, sone);
361 * Returns all remote Sones.
363 * @return All remote Sones
365 public Set<Sone> getRemoteSones() {
366 synchronized (remoteSones) {
367 return new HashSet<Sone>(remoteSones.values());
372 * Returns the remote Sone with the given ID.
375 * The ID of the remote Sone to get
376 * @return The Sone with the given ID
378 public Sone getRemoteSone(String id) {
379 return getRemoteSone(id, true);
383 * Returns the remote Sone with the given ID.
386 * The ID of the remote Sone to get
388 * {@code true} to always create a Sone, {@code false} to return
389 * {@code null} if no Sone with the given ID exists
390 * @return The Sone with the given ID
392 public Sone getRemoteSone(String id, boolean create) {
393 synchronized (remoteSones) {
394 Sone sone = remoteSones.get(id);
395 if ((sone == null) && create) {
397 remoteSones.put(id, sone);
404 * Returns whether the given Sone is a remote Sone.
408 * @return {@code true} if the given Sone is a remote Sone, {@code false}
411 public boolean isRemoteSone(Sone sone) {
412 synchronized (remoteSones) {
413 return remoteSones.containsKey(sone.getId());
418 * Returns whether the Sone with the given ID is a remote Sone.
421 * The ID of the Sone to check
422 * @return {@code true} if the Sone with the given ID is a remote Sone,
423 * {@code false} otherwise
425 public boolean isRemoteSone(String id) {
426 synchronized (remoteSones) {
427 return remoteSones.containsKey(id);
432 * Returns whether the given Sone is a new Sone. After this check, the Sone
433 * is marked as known, i.e. a second call with the same parameters will
434 * always yield {@code false}.
437 * The sone to check for
438 * @return {@code true} if the given Sone is new, false otherwise
440 public boolean isNewSone(Sone sone) {
441 synchronized (newSones) {
442 boolean isNew = !knownSones.contains(sone.getId()) && newSones.remove(sone.getId());
443 knownSones.add(sone.getId());
445 coreListenerManager.fireMarkSoneKnown(sone);
452 * Returns whether the given Sone has been modified.
455 * The Sone to check for modifications
456 * @return {@code true} if a modification has been detected in the Sone,
457 * {@code false} otherwise
459 public boolean isModifiedSone(Sone sone) {
460 return (soneInserters.containsKey(sone)) ? soneInserters.get(sone).isModified() : false;
464 * Returns the post with the given ID.
467 * The ID of the post to get
468 * @return The post, or {@code null} if there is no such post
470 public Post getPost(String postId) {
471 synchronized (posts) {
472 Post post = posts.get(postId);
474 post = new Post(postId);
475 posts.put(postId, post);
482 * Returns whether the given post ID is new. After this method returns it is
483 * marked a known post ID.
487 * @return {@code true} if the post is considered to be new, {@code false}
490 public boolean isNewPost(String postId) {
491 synchronized (newPosts) {
492 boolean isNew = !knownPosts.contains(postId) && newPosts.remove(postId);
493 knownPosts.add(postId);
495 coreListenerManager.fireMarkPostKnown(getPost(postId));
502 * Returns the reply with the given ID.
505 * The ID of the reply to get
506 * @return The reply, or {@code null} if there is no such reply
508 public Reply getReply(String replyId) {
509 synchronized (replies) {
510 Reply reply = replies.get(replyId);
512 reply = new Reply(replyId);
513 replies.put(replyId, reply);
520 * Returns all replies for the given post, order ascending by time.
523 * The post to get all replies for
524 * @return All replies for the given post
526 public List<Reply> getReplies(Post post) {
527 Set<Sone> sones = getSones();
528 List<Reply> replies = new ArrayList<Reply>();
529 for (Sone sone : sones) {
530 for (Reply reply : sone.getReplies()) {
531 if (reply.getPost().equals(post)) {
536 Collections.sort(replies, Reply.TIME_COMPARATOR);
541 * Returns whether the reply with the given ID is new.
544 * The ID of the reply to check
545 * @return {@code true} if the reply is considered to be new, {@code false}
548 public boolean isNewReply(String replyId) {
549 synchronized (newReplies) {
550 boolean isNew = !knownReplies.contains(replyId) && newReplies.remove(replyId);
551 knownReplies.add(replyId);
553 coreListenerManager.fireMarkReplyKnown(getReply(replyId));
560 * Returns all Sones that have liked the given post.
563 * The post to get the liking Sones for
564 * @return The Sones that like the given post
566 public Set<Sone> getLikes(Post post) {
567 Set<Sone> sones = new HashSet<Sone>();
568 for (Sone sone : getSones()) {
569 if (sone.getLikedPostIds().contains(post.getId())) {
577 * Returns all Sones that have liked the given reply.
580 * The reply to get the liking Sones for
581 * @return The Sones that like the given reply
583 public Set<Sone> getLikes(Reply reply) {
584 Set<Sone> sones = new HashSet<Sone>();
585 for (Sone sone : getSones()) {
586 if (sone.getLikedReplyIds().contains(reply.getId())) {
598 * Adds a local Sone from the given ID which has to be the ID of an own
602 * The ID of an own identity to add a Sone for
603 * @return The added (or already existing) Sone
605 public Sone addLocalSone(String id) {
606 synchronized (localSones) {
607 if (localSones.containsKey(id)) {
608 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
609 return localSones.get(id);
611 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
612 if (ownIdentity == null) {
613 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
616 return addLocalSone(ownIdentity);
621 * Adds a local Sone from the given own identity.
624 * The own identity to create a Sone from
625 * @return The added (or already existing) Sone
627 public Sone addLocalSone(OwnIdentity ownIdentity) {
628 if (ownIdentity == null) {
629 logger.log(Level.WARNING, "Given OwnIdentity is null!");
632 synchronized (localSones) {
635 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
636 } catch (MalformedURLException mue1) {
637 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
640 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
641 sone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
642 /* TODO - load posts ’n stuff */
643 localSones.put(ownIdentity.getId(), sone);
644 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
645 soneInserters.put(sone, soneInserter);
646 setSoneStatus(sone, SoneStatus.idle);
648 soneInserter.start();
649 new Thread(new Runnable() {
652 @SuppressWarnings("synthetic-access")
654 soneDownloader.fetchSone(sone);
657 }, "Sone Downloader").start();
663 * Creates a new Sone for the given own identity.
666 * The own identity to create a Sone for
667 * @return The created Sone
669 public Sone createSone(OwnIdentity ownIdentity) {
670 identityManager.addContext(ownIdentity, "Sone");
671 Sone sone = addLocalSone(ownIdentity);
676 * Adds the Sone of the given identity.
679 * The identity whose Sone to add
680 * @return The added or already existing Sone
682 public Sone addRemoteSone(Identity identity) {
683 if (identity == null) {
684 logger.log(Level.WARNING, "Given Identity is null!");
687 synchronized (remoteSones) {
688 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
689 boolean newSone = sone.getRequestUri() == null;
690 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
691 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
693 synchronized (newSones) {
694 newSone = !knownSones.contains(sone.getId());
696 newSones.add(sone.getId());
700 coreListenerManager.fireNewSoneFound(sone);
703 remoteSones.put(identity.getId(), sone);
704 soneDownloader.addSone(sone);
705 setSoneStatus(sone, SoneStatus.unknown);
706 new Thread(new Runnable() {
709 @SuppressWarnings("synthetic-access")
711 soneDownloader.fetchSone(sone);
714 }, "Sone Downloader").start();
720 * Updates the stores Sone with the given Sone.
725 public void updateSone(Sone sone) {
726 if (hasSone(sone.getId())) {
727 Sone storedSone = getSone(sone.getId());
728 if (!(sone.getTime() > storedSone.getTime())) {
729 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
732 synchronized (posts) {
733 for (Post post : storedSone.getPosts()) {
734 posts.remove(post.getId());
736 synchronized (newPosts) {
737 for (Post post : sone.getPosts()) {
738 if (!storedSone.getPosts().contains(post) && !knownPosts.contains(post.getId())) {
739 newPosts.add(post.getId());
740 coreListenerManager.fireNewPostFound(post);
742 posts.put(post.getId(), post);
746 synchronized (replies) {
747 for (Reply reply : storedSone.getReplies()) {
748 replies.remove(reply.getId());
750 synchronized (newReplies) {
751 for (Reply reply : sone.getReplies()) {
752 if (!storedSone.getReplies().contains(reply) && !knownReplies.contains(reply.getId())) {
753 newReplies.add(reply.getId());
754 coreListenerManager.fireNewReplyFound(reply);
756 replies.put(reply.getId(), reply);
760 synchronized (storedSone) {
761 storedSone.setTime(sone.getTime());
762 storedSone.setClient(sone.getClient());
763 storedSone.setProfile(sone.getProfile());
764 storedSone.setPosts(sone.getPosts());
765 storedSone.setReplies(sone.getReplies());
766 storedSone.setLikePostIds(sone.getLikedPostIds());
767 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
768 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
774 * Deletes the given Sone. This will remove the Sone from the
775 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
776 * and remove the context from its identity.
781 public void deleteSone(Sone sone) {
782 if (!(sone.getIdentity() instanceof OwnIdentity)) {
783 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
786 synchronized (localSones) {
787 if (!localSones.containsKey(sone.getId())) {
788 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
791 localSones.remove(sone.getId());
792 soneInserters.remove(sone).stop();
794 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
795 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
797 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
798 } catch (ConfigurationException ce1) {
799 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
804 * Loads and updates the given Sone from the configuration. If any error is
805 * encountered, loading is aborted and the given Sone is not changed.
808 * The Sone to load and update
810 public void loadSone(Sone sone) {
811 if (!isLocalSone(sone)) {
812 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
817 String sonePrefix = "Sone/" + sone.getId();
818 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
819 if (soneTime == null) {
820 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
823 String lastInsertFingerprint = configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").getValue("");
826 Profile profile = new Profile();
827 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
828 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
829 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
830 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
831 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
832 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
835 Set<Post> posts = new HashSet<Post>();
837 String postPrefix = sonePrefix + "/Posts/" + posts.size();
838 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
839 if (postId == null) {
842 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
843 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
844 if ((postTime == 0) || (postText == null)) {
845 logger.log(Level.WARNING, "Invalid post found, aborting load!");
848 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
852 Set<Reply> replies = new HashSet<Reply>();
854 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
855 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
856 if (replyId == null) {
859 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
860 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
861 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
862 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
863 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
866 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
869 /* load post likes. */
870 Set<String> likedPostIds = new HashSet<String>();
872 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
873 if (likedPostId == null) {
876 likedPostIds.add(likedPostId);
879 /* load reply likes. */
880 Set<String> likedReplyIds = new HashSet<String>();
882 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
883 if (likedReplyId == null) {
886 likedReplyIds.add(likedReplyId);
890 Set<String> friends = new HashSet<String>();
892 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
893 if (friendId == null) {
896 friends.add(friendId);
899 /* if we’re still here, Sone was loaded successfully. */
900 synchronized (sone) {
901 sone.setTime(soneTime);
902 sone.setProfile(profile);
903 sone.setPosts(posts);
904 sone.setReplies(replies);
905 sone.setLikePostIds(likedPostIds);
906 sone.setLikeReplyIds(likedReplyIds);
907 sone.setFriends(friends);
908 soneInserters.get(sone).setLastInsertFingerprint(lastInsertFingerprint);
910 synchronized (newSones) {
911 for (String friend : friends) {
912 knownSones.add(friend);
915 synchronized (newPosts) {
916 for (Post post : posts) {
917 knownPosts.add(post.getId());
920 synchronized (newReplies) {
921 for (Reply reply : replies) {
922 knownReplies.add(reply.getId());
928 * Saves the given Sone. This will persist all local settings for the given
929 * Sone, such as the friends list and similar, private options.
934 public void saveSone(Sone sone) {
935 if (!isLocalSone(sone)) {
936 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
939 if (!(sone.getIdentity() instanceof OwnIdentity)) {
940 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
944 logger.log(Level.INFO, "Saving Sone: %s", sone);
945 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
947 /* save Sone into configuration. */
948 String sonePrefix = "Sone/" + sone.getId();
949 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
950 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(soneInserters.get(sone).getLastInsertFingerprint());
953 Profile profile = sone.getProfile();
954 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
955 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
956 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
957 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
958 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
959 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
963 for (Post post : sone.getPosts()) {
964 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
965 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
966 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
967 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
969 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
972 int replyCounter = 0;
973 for (Reply reply : sone.getReplies()) {
974 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
975 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
976 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
977 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
978 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
980 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
982 /* save post likes. */
983 int postLikeCounter = 0;
984 for (String postId : sone.getLikedPostIds()) {
985 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
987 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
989 /* save reply likes. */
990 int replyLikeCounter = 0;
991 for (String replyId : sone.getLikedReplyIds()) {
992 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
994 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
997 int friendCounter = 0;
998 for (String friendId : sone.getFriends()) {
999 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter++ + "/ID").setValue(friendId);
1001 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
1003 logger.log(Level.INFO, "Sone %s saved.", sone);
1004 } catch (ConfigurationException ce1) {
1005 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
1010 * Creates a new post.
1013 * The Sone that creates the post
1015 * The text of the post
1017 public void createPost(Sone sone, String text) {
1018 createPost(sone, System.currentTimeMillis(), text);
1022 * Creates a new post.
1025 * The Sone that creates the post
1027 * The time of the post
1029 * The text of the post
1031 public void createPost(Sone sone, long time, String text) {
1032 if (!isLocalSone(sone)) {
1033 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
1036 Post post = new Post(sone, time, text);
1037 synchronized (posts) {
1038 posts.put(post.getId(), post);
1040 synchronized (newPosts) {
1041 knownPosts.add(post.getId());
1048 * Deletes the given post.
1051 * The post to delete
1053 public void deletePost(Post post) {
1054 if (!isLocalSone(post.getSone())) {
1055 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
1058 post.getSone().removePost(post);
1059 synchronized (posts) {
1060 posts.remove(post.getId());
1062 saveSone(post.getSone());
1066 * Creates a new reply.
1069 * The Sone that creates the reply
1071 * The post that this reply refers to
1073 * The text of the reply
1074 * @return The created reply
1076 public Reply createReply(Sone sone, Post post, String text) {
1077 return createReply(sone, post, System.currentTimeMillis(), text);
1081 * Creates a new reply.
1084 * The Sone that creates the reply
1086 * The post that this reply refers to
1088 * The time of the reply
1090 * The text of the reply
1091 * @return The created reply
1093 public Reply createReply(Sone sone, Post post, long time, String text) {
1094 if (!isLocalSone(sone)) {
1095 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
1098 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
1099 synchronized (replies) {
1100 replies.put(reply.getId(), reply);
1102 synchronized (newReplies) {
1103 knownReplies.add(reply.getId());
1105 sone.addReply(reply);
1111 * Deletes the given reply.
1114 * The reply to delete
1116 public void deleteReply(Reply reply) {
1117 Sone sone = reply.getSone();
1118 if (!isLocalSone(sone)) {
1119 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
1122 synchronized (replies) {
1123 replies.remove(reply.getId());
1125 sone.removeReply(reply);
1132 public void start() {
1133 loadConfiguration();
1139 public void stop() {
1140 synchronized (localSones) {
1141 for (SoneInserter soneInserter : soneInserters.values()) {
1142 soneInserter.stop();
1145 saveConfiguration();
1154 * Loads the configuration.
1156 @SuppressWarnings("unchecked")
1157 private void loadConfiguration() {
1158 /* create options. */
1159 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1162 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1163 SoneInserter.setInsertionDelay(newValue);
1167 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1168 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1170 /* read options from configuration. */
1171 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1172 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1173 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1174 options.getBooleanOption("ClearOnNextRestart").set(null);
1175 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1176 if (clearConfiguration) {
1177 /* stop loading the configuration. */
1181 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1183 /* load known Sones. */
1184 int soneCounter = 0;
1186 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1187 if (knownSoneId == null) {
1190 synchronized (newSones) {
1191 knownSones.add(knownSoneId);
1195 /* load known posts. */
1196 int postCounter = 0;
1198 String knownPostId = configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").getValue(null);
1199 if (knownPostId == null) {
1202 synchronized (newPosts) {
1203 knownPosts.add(knownPostId);
1207 /* load known replies. */
1208 int replyCounter = 0;
1210 String knownReplyId = configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").getValue(null);
1211 if (knownReplyId == null) {
1214 synchronized (newReplies) {
1215 knownReplies.add(knownReplyId);
1222 * Saves the current options.
1224 private void saveConfiguration() {
1225 /* store the options first. */
1227 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1228 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1229 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1231 /* save known Sones. */
1232 int soneCounter = 0;
1233 synchronized (newSones) {
1234 for (String knownSoneId : knownSones) {
1235 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1237 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1240 /* save known posts. */
1241 int postCounter = 0;
1242 synchronized (newPosts) {
1243 for (String knownPostId : knownPosts) {
1244 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(knownPostId);
1246 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1249 /* save known replies. */
1250 int replyCounter = 0;
1251 synchronized (newReplies) {
1252 for (String knownReplyId : knownReplies) {
1253 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(knownReplyId);
1255 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1258 } catch (ConfigurationException ce1) {
1259 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1264 * Generate a Sone URI from the given URI and latest edition.
1267 * The URI to derive the Sone URI from
1268 * @return The derived URI
1270 private FreenetURI getSoneUri(String uriString) {
1272 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1274 } catch (MalformedURLException mue1) {
1275 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1281 // INTERFACE IdentityListener
1288 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1289 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1290 if (ownIdentity.hasContext("Sone")) {
1291 addLocalSone(ownIdentity);
1299 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1300 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1307 public void identityAdded(Identity identity) {
1308 logger.log(Level.FINEST, "Adding Identity: " + identity);
1309 addRemoteSone(identity);
1316 public void identityUpdated(final Identity identity) {
1317 new Thread(new Runnable() {
1320 @SuppressWarnings("synthetic-access")
1322 Sone sone = getRemoteSone(identity.getId());
1323 soneDownloader.fetchSone(sone);
1332 public void identityRemoved(Identity identity) {