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<Sone> newSones = new HashSet<Sone>();
112 /** All known Sones. */
113 /* synchronize access on {@link #newSones}. */
114 private Set<Sone> knownSones = new HashSet<Sone>();
117 private Map<String, Post> posts = new HashMap<String, Post>();
120 private Map<String, Reply> replies = new HashMap<String, Reply>();
123 * Creates a new core.
125 * @param configuration
126 * The configuration of the core
127 * @param freenetInterface
128 * The freenet interface
129 * @param identityManager
130 * The identity manager
132 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
133 this.configuration = configuration;
134 this.freenetInterface = freenetInterface;
135 this.identityManager = identityManager;
136 this.soneDownloader = new SoneDownloader(this, freenetInterface);
144 * Returns the options used by the core.
146 * @return The options of the core
148 public Options getOptions() {
153 * Returns the identity manager used by the core.
155 * @return The identity manager
157 public IdentityManager getIdentityManager() {
158 return identityManager;
162 * Returns the status of the given Sone.
165 * The Sone to get the status for
166 * @return The status of the Sone
168 public SoneStatus getSoneStatus(Sone sone) {
169 synchronized (soneStatuses) {
170 return soneStatuses.get(sone);
175 * Sets the status of the given Sone.
178 * The Sone to set the status of
182 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
183 synchronized (soneStatuses) {
184 soneStatuses.put(sone, soneStatus);
189 * Returns all Sones, remote and local.
193 public Set<Sone> getSones() {
194 Set<Sone> allSones = new HashSet<Sone>();
195 allSones.addAll(getLocalSones());
196 allSones.addAll(getRemoteSones());
201 * Returns the Sone with the given ID, regardless whether it’s local or
205 * The ID of the Sone to get
206 * @return The Sone with the given ID, or {@code null} if there is no such
209 public Sone getSone(String id) {
210 return getSone(id, true);
214 * Returns the Sone with the given ID, regardless whether it’s local or
218 * The ID of the Sone to get
220 * {@code true} to create a new Sone if none exists,
221 * {@code false} to return {@code null} if a Sone with the given
223 * @return The Sone with the given ID, or {@code null} if there is no such
226 public Sone getSone(String id, boolean create) {
227 if (isLocalSone(id)) {
228 return getLocalSone(id);
230 return getRemoteSone(id, create);
234 * Checks whether the core knows a Sone with the given ID.
238 * @return {@code true} if there is a Sone with the given ID, {@code false}
241 public boolean hasSone(String id) {
242 return isLocalSone(id) || isRemoteSone(id);
246 * Returns whether the given Sone is a local Sone.
249 * The Sone to check for its locality
250 * @return {@code true} if the given Sone is local, {@code false} otherwise
252 public boolean isLocalSone(Sone sone) {
253 synchronized (localSones) {
254 return localSones.containsKey(sone.getId());
259 * Returns whether the given ID is the ID of a local Sone.
262 * The Sone ID to check for its locality
263 * @return {@code true} if the given ID is a local Sone, {@code false}
266 public boolean isLocalSone(String id) {
267 synchronized (localSones) {
268 return localSones.containsKey(id);
273 * Returns all local Sones.
275 * @return All local Sones
277 public Set<Sone> getLocalSones() {
278 synchronized (localSones) {
279 return new HashSet<Sone>(localSones.values());
284 * Returns the local Sone with the given ID.
287 * The ID of the Sone to get
288 * @return The Sone with the given ID
290 public Sone getLocalSone(String id) {
291 synchronized (localSones) {
292 Sone sone = localSones.get(id);
295 localSones.put(id, sone);
302 * Returns all remote Sones.
304 * @return All remote Sones
306 public Set<Sone> getRemoteSones() {
307 synchronized (remoteSones) {
308 return new HashSet<Sone>(remoteSones.values());
313 * Returns the remote Sone with the given ID.
316 * The ID of the remote Sone to get
317 * @return The Sone with the given ID
319 public Sone getRemoteSone(String id) {
320 return getRemoteSone(id, true);
324 * Returns the remote Sone with the given ID.
327 * The ID of the remote Sone to get
329 * {@code true} to always create a Sone, {@code false} to return
330 * {@code null} if no Sone with the given ID exists
331 * @return The Sone with the given ID
333 public Sone getRemoteSone(String id, boolean create) {
334 synchronized (remoteSones) {
335 Sone sone = remoteSones.get(id);
336 if ((sone == null) && create) {
338 remoteSones.put(id, sone);
345 * Returns whether the given Sone is a remote Sone.
349 * @return {@code true} if the given Sone is a remote Sone, {@code false}
352 public boolean isRemoteSone(Sone sone) {
353 synchronized (remoteSones) {
354 return remoteSones.containsKey(sone.getId());
359 * Returns whether the Sone with the given ID is a remote Sone.
362 * The ID of the Sone to check
363 * @return {@code true} if the Sone with the given ID is a remote Sone,
364 * {@code false} otherwise
366 public boolean isRemoteSone(String id) {
367 synchronized (remoteSones) {
368 return remoteSones.containsKey(id);
373 * Returns whether the given Sone is a new Sone. After this check, the Sone
374 * is marked as known, i.e. a second call with the same parameters will
375 * always yield {@code false}.
378 * The sone to check for
379 * @return {@code true} if the given Sone is new, false otherwise
381 public boolean isNewSone(Sone sone) {
382 synchronized (newSones) {
383 boolean isNew = !knownSones.contains(sone) && newSones.remove(sone);
384 knownSones.add(sone);
390 * Returns the post with the given ID.
393 * The ID of the post to get
394 * @return The post, or {@code null} if there is no such post
396 public Post getPost(String postId) {
397 synchronized (posts) {
398 Post post = posts.get(postId);
400 post = new Post(postId);
401 posts.put(postId, post);
408 * Returns the reply with the given ID.
411 * The ID of the reply to get
412 * @return The reply, or {@code null} if there is no such reply
414 public Reply getReply(String replyId) {
415 synchronized (replies) {
416 Reply reply = replies.get(replyId);
418 reply = new Reply(replyId);
419 replies.put(replyId, reply);
426 * Returns all replies for the given post, order ascending by time.
429 * The post to get all replies for
430 * @return All replies for the given post
432 public List<Reply> getReplies(Post post) {
433 Set<Sone> sones = getSones();
434 List<Reply> replies = new ArrayList<Reply>();
435 for (Sone sone : sones) {
436 for (Reply reply : sone.getReplies()) {
437 if (reply.getPost().equals(post)) {
442 Collections.sort(replies, Reply.TIME_COMPARATOR);
447 * Returns all Sones that have liked the given post.
450 * The post to get the liking Sones for
451 * @return The Sones that like the given post
453 public Set<Sone> getLikes(Post post) {
454 Set<Sone> sones = new HashSet<Sone>();
455 for (Sone sone : getSones()) {
456 if (sone.getLikedPostIds().contains(post.getId())) {
464 * Returns all Sones that have liked the given reply.
467 * The reply to get the liking Sones for
468 * @return The Sones that like the given reply
470 public Set<Sone> getLikes(Reply reply) {
471 Set<Sone> sones = new HashSet<Sone>();
472 for (Sone sone : getSones()) {
473 if (sone.getLikedReplyIds().contains(reply.getId())) {
485 * Adds a local Sone from the given ID which has to be the ID of an own
489 * The ID of an own identity to add a Sone for
490 * @return The added (or already existing) Sone
492 public Sone addLocalSone(String id) {
493 synchronized (localSones) {
494 if (localSones.containsKey(id)) {
495 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
496 return localSones.get(id);
498 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
499 if (ownIdentity == null) {
500 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
503 return addLocalSone(ownIdentity);
508 * Adds a local Sone from the given own identity.
511 * The own identity to create a Sone from
512 * @return The added (or already existing) Sone
514 public Sone addLocalSone(OwnIdentity ownIdentity) {
515 if (ownIdentity == null) {
516 logger.log(Level.WARNING, "Given OwnIdentity is null!");
519 synchronized (localSones) {
522 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
523 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
524 } catch (MalformedURLException mue1) {
525 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
528 /* TODO - load posts ’n stuff */
529 localSones.put(ownIdentity.getId(), sone);
530 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
531 soneInserters.put(sone, soneInserter);
532 soneInserter.start();
533 setSoneStatus(sone, SoneStatus.idle);
535 new Thread(new Runnable() {
538 @SuppressWarnings("synthetic-access")
540 soneDownloader.fetchSone(sone);
543 }, "Sone Downloader").start();
549 * Creates a new Sone for the given own identity.
552 * The own identity to create a Sone for
553 * @return The created Sone
555 public Sone createSone(OwnIdentity ownIdentity) {
556 identityManager.addContext(ownIdentity, "Sone");
557 Sone sone = addLocalSone(ownIdentity);
558 synchronized (sone) {
559 /* mark as modified so that it gets inserted immediately. */
560 sone.setModificationCounter(sone.getModificationCounter() + 1);
566 * Adds the Sone of the given identity.
569 * The identity whose Sone to add
570 * @return The added or already existing Sone
572 public Sone addRemoteSone(Identity identity) {
573 if (identity == null) {
574 logger.log(Level.WARNING, "Given Identity is null!");
577 synchronized (remoteSones) {
578 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
579 boolean newSone = sone.getRequestUri() == null;
580 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
581 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
583 synchronized (newSones) {
587 remoteSones.put(identity.getId(), sone);
588 soneDownloader.addSone(sone);
589 setSoneStatus(sone, SoneStatus.unknown);
590 new Thread(new Runnable() {
593 @SuppressWarnings("synthetic-access")
595 soneDownloader.fetchSone(sone);
598 }, "Sone Downloader").start();
604 * Updates the stores Sone with the given Sone.
609 public void updateSone(Sone sone) {
610 if (isRemoteSone(sone)) {
611 Sone storedSone = getRemoteSone(sone.getId());
612 if (!(sone.getTime() > storedSone.getTime())) {
613 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
616 synchronized (posts) {
617 for (Post post : storedSone.getPosts()) {
618 posts.remove(post.getId());
620 for (Post post : sone.getPosts()) {
621 posts.put(post.getId(), post);
624 synchronized (replies) {
625 for (Reply reply : storedSone.getReplies()) {
626 replies.remove(reply.getId());
628 for (Reply reply : sone.getReplies()) {
629 replies.put(reply.getId(), reply);
632 synchronized (storedSone) {
633 storedSone.setTime(sone.getTime());
634 storedSone.setProfile(sone.getProfile());
635 storedSone.setPosts(sone.getPosts());
636 storedSone.setReplies(sone.getReplies());
637 storedSone.setLikePostIds(sone.getLikedPostIds());
638 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
639 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
640 storedSone.setModificationCounter(0);
646 * Deletes the given Sone. This will remove the Sone from the
647 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
648 * and remove the context from its identity.
653 public void deleteSone(Sone sone) {
654 if (!(sone.getIdentity() instanceof OwnIdentity)) {
655 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
658 synchronized (localSones) {
659 if (!localSones.containsKey(sone.getId())) {
660 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
663 localSones.remove(sone.getId());
664 soneInserters.remove(sone).stop();
666 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
667 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
669 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
670 } catch (ConfigurationException ce1) {
671 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
676 * Loads and updates the given Sone from the configuration. If any error is
677 * encountered, loading is aborted and the given Sone is not changed.
680 * The Sone to load and update
682 public void loadSone(Sone sone) {
683 if (!isLocalSone(sone)) {
684 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
689 String sonePrefix = "Sone/" + sone.getId();
690 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
691 if (soneTime == null) {
692 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
695 long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
698 Profile profile = new Profile();
699 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
700 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
701 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
702 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
703 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
704 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
707 Set<Post> posts = new HashSet<Post>();
709 String postPrefix = sonePrefix + "/Posts/" + posts.size();
710 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
711 if (postId == null) {
714 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
715 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
716 if ((postTime == 0) || (postText == null)) {
717 logger.log(Level.WARNING, "Invalid post found, aborting load!");
720 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
724 Set<Reply> replies = new HashSet<Reply>();
726 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
727 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
728 if (replyId == null) {
731 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
732 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
733 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
734 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
735 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
738 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
741 /* load post likes. */
742 Set<String> likedPostIds = new HashSet<String>();
744 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
745 if (likedPostId == null) {
748 likedPostIds.add(likedPostId);
751 /* load reply likes. */
752 Set<String> likedReplyIds = new HashSet<String>();
754 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
755 if (likedReplyId == null) {
758 likedReplyIds.add(likedReplyId);
762 Set<Sone> friends = new HashSet<Sone>();
764 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
765 if (friendId == null) {
768 Boolean friendLocal = configuration.getBooleanValue(sonePrefix + "/Friends/" + friends.size() + "/Local").getValue(null);
769 if (friendLocal == null) {
770 logger.log(Level.WARNING, "Invalid friend found, aborting load!");
773 friends.add(friendLocal ? getLocalSone(friendId) : getRemoteSone(friendId));
776 /* if we’re still here, Sone was loaded successfully. */
777 synchronized (sone) {
778 sone.setTime(soneTime);
779 sone.setProfile(profile);
780 sone.setPosts(posts);
781 sone.setReplies(replies);
782 sone.setLikePostIds(likedPostIds);
783 sone.setLikeReplyIds(likedReplyIds);
784 sone.setFriends(friends);
785 sone.setModificationCounter(soneModificationCounter);
787 synchronized (newSones) {
788 for (Sone friend : friends) {
789 knownSones.add(friend);
795 * Saves the given Sone. This will persist all local settings for the given
796 * Sone, such as the friends list and similar, private options.
801 public void saveSone(Sone sone) {
802 if (!isLocalSone(sone)) {
803 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
806 if (!(sone.getIdentity() instanceof OwnIdentity)) {
807 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
811 logger.log(Level.INFO, "Saving Sone: %s", sone);
812 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
814 /* save Sone into configuration. */
815 String sonePrefix = "Sone/" + sone.getId();
816 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
817 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
820 Profile profile = sone.getProfile();
821 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
822 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
823 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
824 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
825 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
826 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
830 for (Post post : sone.getPosts()) {
831 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
832 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
833 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
834 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
836 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
839 int replyCounter = 0;
840 for (Reply reply : sone.getReplies()) {
841 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
842 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
843 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
844 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
845 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
847 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
849 /* save post likes. */
850 int postLikeCounter = 0;
851 for (String postId : sone.getLikedPostIds()) {
852 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
854 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
856 /* save reply likes. */
857 int replyLikeCounter = 0;
858 for (String replyId : sone.getLikedReplyIds()) {
859 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
861 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
864 int friendCounter = 0;
865 for (Sone friend : sone.getFriends()) {
866 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend.getId());
867 configuration.getBooleanValue(sonePrefix + "/Friends/" + friendCounter++ + "/Local").setValue(friend.getInsertUri() != null);
869 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
871 logger.log(Level.INFO, "Sone %s saved.", sone);
872 } catch (ConfigurationException ce1) {
873 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
878 * Creates a new post.
881 * The Sone that creates the post
883 * The text of the post
885 public void createPost(Sone sone, String text) {
886 createPost(sone, System.currentTimeMillis(), text);
890 * Creates a new post.
893 * The Sone that creates the post
895 * The time of the post
897 * The text of the post
899 public void createPost(Sone sone, long time, String text) {
900 if (!isLocalSone(sone)) {
901 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
904 Post post = new Post(sone, time, text);
905 synchronized (posts) {
906 posts.put(post.getId(), post);
913 * Deletes the given post.
918 public void deletePost(Post post) {
919 if (!isLocalSone(post.getSone())) {
920 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
923 post.getSone().removePost(post);
924 synchronized (posts) {
925 posts.remove(post.getId());
927 saveSone(post.getSone());
931 * Creates a new reply.
934 * The Sone that creates the reply
936 * The post that this reply refers to
938 * The text of the reply
940 public void createReply(Sone sone, Post post, String text) {
941 createReply(sone, post, System.currentTimeMillis(), text);
945 * Creates a new reply.
948 * The Sone that creates the reply
950 * The post that this reply refers to
952 * The time of the reply
954 * The text of the reply
956 public void createReply(Sone sone, Post post, long time, String text) {
957 if (!isLocalSone(sone)) {
958 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
961 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
962 synchronized (replies) {
963 replies.put(reply.getId(), reply);
965 sone.addReply(reply);
970 * Deletes the given reply.
973 * The reply to delete
975 public void deleteReply(Reply reply) {
976 Sone sone = reply.getSone();
977 if (!isLocalSone(sone)) {
978 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
981 synchronized (replies) {
982 replies.remove(reply.getId());
984 sone.removeReply(reply);
991 public void start() {
999 synchronized (localSones) {
1000 for (SoneInserter soneInserter : soneInserters.values()) {
1001 soneInserter.stop();
1004 saveConfiguration();
1012 * Loads the configuration.
1014 @SuppressWarnings("unchecked")
1015 private void loadConfiguration() {
1016 /* create options. */
1017 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1020 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1021 SoneInserter.setInsertionDelay(newValue);
1025 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1026 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1028 /* read options from configuration. */
1029 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1030 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1031 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1032 options.getBooleanOption("ClearOnNextRestart").set(null);
1033 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1034 if (clearConfiguration) {
1035 /* stop loading the configuration. */
1039 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1041 /* load known Sones. */
1042 int soneCounter = 0;
1044 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1045 if (knownSoneId == null) {
1048 synchronized (newSones) {
1049 knownSones.add(getRemoteSone(knownSoneId));
1055 * Saves the current options.
1057 private void saveConfiguration() {
1058 /* store the options first. */
1060 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1061 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1062 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1064 /* save known Sones. */
1065 int soneCounter = 0;
1066 synchronized (newSones) {
1067 for (Sone knownSone : knownSones) {
1068 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSone.getId());
1070 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1073 } catch (ConfigurationException ce1) {
1074 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1079 * Generate a Sone URI from the given URI and latest edition.
1082 * The URI to derive the Sone URI from
1083 * @return The derived URI
1085 private FreenetURI getSoneUri(String uriString) {
1087 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1089 } catch (MalformedURLException mue1) {
1090 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1096 // INTERFACE IdentityListener
1103 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1104 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1105 if (ownIdentity.hasContext("Sone")) {
1106 addLocalSone(ownIdentity);
1114 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1115 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1122 public void identityAdded(Identity identity) {
1123 logger.log(Level.FINEST, "Adding Identity: " + identity);
1124 addRemoteSone(identity);
1131 public void identityUpdated(final Identity identity) {
1132 new Thread(new Runnable() {
1135 @SuppressWarnings("synthetic-access")
1137 Sone sone = getRemoteSone(identity.getId());
1138 soneDownloader.fetchSone(sone);
1147 public void identityRemoved(Identity identity) {