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 if (isLocalSone(id)) {
211 return getLocalSone(id);
213 return getRemoteSone(id);
217 * Returns whether the given Sone is a local Sone.
220 * The Sone to check for its locality
221 * @return {@code true} if the given Sone is local, {@code false} otherwise
223 public boolean isLocalSone(Sone sone) {
224 synchronized (localSones) {
225 return localSones.containsKey(sone.getId());
230 * Returns whether the given ID is the ID of a local Sone.
233 * The Sone ID to check for its locality
234 * @return {@code true} if the given ID is a local Sone, {@code false}
237 public boolean isLocalSone(String id) {
238 synchronized (localSones) {
239 return localSones.containsKey(id);
244 * Returns all local Sones.
246 * @return All local Sones
248 public Set<Sone> getLocalSones() {
249 synchronized (localSones) {
250 return new HashSet<Sone>(localSones.values());
255 * Returns the local Sone with the given ID.
258 * The ID of the Sone to get
259 * @return The Sone with the given ID
261 public Sone getLocalSone(String id) {
262 synchronized (localSones) {
263 Sone sone = localSones.get(id);
266 localSones.put(id, sone);
273 * Returns all remote Sones.
275 * @return All remote Sones
277 public Set<Sone> getRemoteSones() {
278 synchronized (remoteSones) {
279 return new HashSet<Sone>(remoteSones.values());
284 * Returns the remote Sone with the given ID.
287 * The ID of the remote Sone to get
288 * @return The Sone with the given ID
290 public Sone getRemoteSone(String id) {
291 synchronized (remoteSones) {
292 Sone sone = remoteSones.get(id);
295 remoteSones.put(id, sone);
302 * Returns whether the given Sone is a remote Sone.
306 * @return {@code true} if the given Sone is a remote Sone, {@code false}
309 public boolean isRemoteSone(Sone sone) {
310 synchronized (remoteSones) {
311 return remoteSones.containsKey(sone.getId());
316 * Returns whether the Sone with the given ID is a remote Sone.
319 * The ID of the Sone to check
320 * @return {@code true} if the Sone with the given ID is a remote Sone,
321 * {@code false} otherwise
323 public boolean isRemoteSone(String id) {
324 synchronized (remoteSones) {
325 return remoteSones.containsKey(id);
330 * Returns whether the given Sone is a new Sone. After this check, the Sone
331 * is marked as known, i.e. a second call with the same parameters will
332 * always yield {@code false}.
335 * The sone to check for
336 * @return {@code true} if the given Sone is new, false otherwise
338 public boolean isNewSone(Sone sone) {
339 synchronized (newSones) {
340 boolean isNew = !knownSones.contains(sone) && newSones.remove(sone);
341 knownSones.add(sone);
347 * Returns the post with the given ID.
350 * The ID of the post to get
351 * @return The post, or {@code null} if there is no such post
353 public Post getPost(String postId) {
354 synchronized (posts) {
355 Post post = posts.get(postId);
357 post = new Post(postId);
358 posts.put(postId, post);
365 * Returns the reply with the given ID.
368 * The ID of the reply to get
369 * @return The reply, or {@code null} if there is no such reply
371 public Reply getReply(String replyId) {
372 synchronized (replies) {
373 Reply reply = replies.get(replyId);
375 reply = new Reply(replyId);
376 replies.put(replyId, reply);
383 * Returns all replies for the given post, order ascending by time.
386 * The post to get all replies for
387 * @return All replies for the given post
389 public List<Reply> getReplies(Post post) {
390 Set<Sone> sones = getSones();
391 List<Reply> replies = new ArrayList<Reply>();
392 for (Sone sone : sones) {
393 for (Reply reply : sone.getReplies()) {
394 if (reply.getPost().equals(post)) {
399 Collections.sort(replies, Reply.TIME_COMPARATOR);
404 * Returns all Sones that have liked the given post.
407 * The post to get the liking Sones for
408 * @return The Sones that like the given post
410 public Set<Sone> getLikes(Post post) {
411 Set<Sone> sones = new HashSet<Sone>();
412 for (Sone sone : getSones()) {
413 if (sone.getLikedPostIds().contains(post.getId())) {
421 * Returns all Sones that have liked the given reply.
424 * The reply to get the liking Sones for
425 * @return The Sones that like the given reply
427 public Set<Sone> getLikes(Reply reply) {
428 Set<Sone> sones = new HashSet<Sone>();
429 for (Sone sone : getSones()) {
430 if (sone.getLikedReplyIds().contains(reply.getId())) {
442 * Adds a local Sone from the given ID which has to be the ID of an own
446 * The ID of an own identity to add a Sone for
447 * @return The added (or already existing) Sone
449 public Sone addLocalSone(String id) {
450 synchronized (localSones) {
451 if (localSones.containsKey(id)) {
452 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
453 return localSones.get(id);
455 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
456 if (ownIdentity == null) {
457 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
460 return addLocalSone(ownIdentity);
465 * Adds a local Sone from the given own identity.
468 * The own identity to create a Sone from
469 * @return The added (or already existing) Sone
471 public Sone addLocalSone(OwnIdentity ownIdentity) {
472 if (ownIdentity == null) {
473 logger.log(Level.WARNING, "Given OwnIdentity is null!");
476 synchronized (localSones) {
479 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
480 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
481 } catch (MalformedURLException mue1) {
482 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
485 /* TODO - load posts ’n stuff */
486 localSones.put(ownIdentity.getId(), sone);
487 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
488 soneInserters.put(sone, soneInserter);
489 soneInserter.start();
490 setSoneStatus(sone, SoneStatus.idle);
492 new Thread(new Runnable() {
495 @SuppressWarnings("synthetic-access")
497 soneDownloader.fetchSone(sone);
500 }, "Sone Downloader").start();
506 * Creates a new Sone for the given own identity.
509 * The own identity to create a Sone for
510 * @return The created Sone
512 public Sone createSone(OwnIdentity ownIdentity) {
513 identityManager.addContext(ownIdentity, "Sone");
514 Sone sone = addLocalSone(ownIdentity);
515 synchronized (sone) {
516 /* mark as modified so that it gets inserted immediately. */
517 sone.setModificationCounter(sone.getModificationCounter() + 1);
523 * Adds the Sone of the given identity.
526 * The identity whose Sone to add
527 * @return The added or already existing Sone
529 public Sone addRemoteSone(Identity identity) {
530 if (identity == null) {
531 logger.log(Level.WARNING, "Given Identity is null!");
534 synchronized (remoteSones) {
535 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
536 boolean newSone = sone.getRequestUri() == null;
537 sone.setRequestUri(getSoneUri(identity.getRequestUri()));
538 sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
540 synchronized (newSones) {
544 remoteSones.put(identity.getId(), sone);
545 soneDownloader.addSone(sone);
546 setSoneStatus(sone, SoneStatus.unknown);
547 new Thread(new Runnable() {
550 @SuppressWarnings("synthetic-access")
552 soneDownloader.fetchSone(sone);
555 }, "Sone Downloader").start();
561 * Updates the stores Sone with the given Sone.
566 public void updateSone(Sone sone) {
567 if (isRemoteSone(sone)) {
568 Sone storedSone = getRemoteSone(sone.getId());
569 if (!(sone.getTime() > storedSone.getTime())) {
570 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
573 synchronized (posts) {
574 for (Post post : storedSone.getPosts()) {
575 posts.remove(post.getId());
577 for (Post post : sone.getPosts()) {
578 posts.put(post.getId(), post);
581 synchronized (replies) {
582 for (Reply reply : storedSone.getReplies()) {
583 replies.remove(reply.getId());
585 for (Reply reply : sone.getReplies()) {
586 replies.put(reply.getId(), reply);
589 synchronized (storedSone) {
590 storedSone.setTime(sone.getTime());
591 storedSone.setProfile(sone.getProfile());
592 storedSone.setPosts(sone.getPosts());
593 storedSone.setReplies(sone.getReplies());
594 storedSone.setLikePostIds(sone.getLikedPostIds());
595 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
596 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
597 storedSone.setModificationCounter(0);
603 * Deletes the given Sone. This will remove the Sone from the
604 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
605 * and remove the context from its identity.
610 public void deleteSone(Sone sone) {
611 if (!(sone.getIdentity() instanceof OwnIdentity)) {
612 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
615 synchronized (localSones) {
616 if (!localSones.containsKey(sone.getId())) {
617 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
620 localSones.remove(sone.getId());
621 soneInserters.remove(sone).stop();
623 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
624 identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
626 configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
627 } catch (ConfigurationException ce1) {
628 logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
633 * Loads and updates the given Sone from the configuration. If any error is
634 * encountered, loading is aborted and the given Sone is not changed.
637 * The Sone to load and update
639 public void loadSone(Sone sone) {
640 if (!isLocalSone(sone)) {
641 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
646 String sonePrefix = "Sone/" + sone.getId();
647 Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
648 if (soneTime == null) {
649 logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
652 long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
655 Profile profile = new Profile();
656 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
657 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
658 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
659 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
660 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
661 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
664 Set<Post> posts = new HashSet<Post>();
666 String postPrefix = sonePrefix + "/Posts/" + posts.size();
667 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
668 if (postId == null) {
671 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
672 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
673 if ((postTime == 0) || (postText == null)) {
674 logger.log(Level.WARNING, "Invalid post found, aborting load!");
677 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
681 Set<Reply> replies = new HashSet<Reply>();
683 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
684 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
685 if (replyId == null) {
688 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
689 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
690 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
691 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
692 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
695 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
698 /* load post likes. */
699 Set<String> likedPostIds = new HashSet<String>();
701 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
702 if (likedPostId == null) {
705 likedPostIds.add(likedPostId);
708 /* load reply likes. */
709 Set<String> likedReplyIds = new HashSet<String>();
711 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
712 if (likedReplyId == null) {
715 likedReplyIds.add(likedReplyId);
719 Set<Sone> friends = new HashSet<Sone>();
721 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
722 if (friendId == null) {
725 Boolean friendLocal = configuration.getBooleanValue(sonePrefix + "/Friends/" + friends.size() + "/Local").getValue(null);
726 if (friendLocal == null) {
727 logger.log(Level.WARNING, "Invalid friend found, aborting load!");
730 friends.add(friendLocal ? getLocalSone(friendId) : getRemoteSone(friendId));
733 /* if we’re still here, Sone was loaded successfully. */
734 synchronized (sone) {
735 sone.setTime(soneTime);
736 sone.setProfile(profile);
737 sone.setPosts(posts);
738 sone.setReplies(replies);
739 sone.setLikePostIds(likedPostIds);
740 sone.setLikeReplyIds(likedReplyIds);
741 sone.setFriends(friends);
742 sone.setModificationCounter(soneModificationCounter);
744 synchronized (newSones) {
745 for (Sone friend : friends) {
746 knownSones.add(friend);
752 * Saves the given Sone. This will persist all local settings for the given
753 * Sone, such as the friends list and similar, private options.
758 public void saveSone(Sone sone) {
759 if (!isLocalSone(sone)) {
760 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
763 if (!(sone.getIdentity() instanceof OwnIdentity)) {
764 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
768 logger.log(Level.INFO, "Saving Sone: %s", sone);
769 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
771 /* save Sone into configuration. */
772 String sonePrefix = "Sone/" + sone.getId();
773 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
774 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
777 Profile profile = sone.getProfile();
778 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
779 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
780 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
781 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
782 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
783 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
787 for (Post post : sone.getPosts()) {
788 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
789 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
790 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
791 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
793 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
796 int replyCounter = 0;
797 for (Reply reply : sone.getReplies()) {
798 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
799 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
800 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
801 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
802 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
804 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
806 /* save post likes. */
807 int postLikeCounter = 0;
808 for (String postId : sone.getLikedPostIds()) {
809 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
811 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
813 /* save reply likes. */
814 int replyLikeCounter = 0;
815 for (String replyId : sone.getLikedReplyIds()) {
816 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
818 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
821 int friendCounter = 0;
822 for (Sone friend : sone.getFriends()) {
823 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend.getId());
824 configuration.getBooleanValue(sonePrefix + "/Friends/" + friendCounter++ + "/Local").setValue(friend.getInsertUri() != null);
826 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
828 logger.log(Level.INFO, "Sone %s saved.", sone);
829 } catch (ConfigurationException ce1) {
830 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
835 * Creates a new post.
838 * The Sone that creates the post
840 * The text of the post
842 public void createPost(Sone sone, String text) {
843 createPost(sone, System.currentTimeMillis(), text);
847 * Creates a new post.
850 * The Sone that creates the post
852 * The time of the post
854 * The text of the post
856 public void createPost(Sone sone, long time, String text) {
857 if (!isLocalSone(sone)) {
858 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
861 Post post = new Post(sone, time, text);
862 synchronized (posts) {
863 posts.put(post.getId(), post);
870 * Deletes the given post.
875 public void deletePost(Post post) {
876 if (!isLocalSone(post.getSone())) {
877 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
880 post.getSone().removePost(post);
881 synchronized (posts) {
882 posts.remove(post.getId());
884 saveSone(post.getSone());
888 * Creates a new reply.
891 * The Sone that creates the reply
893 * The post that this reply refers to
895 * The text of the reply
897 public void createReply(Sone sone, Post post, String text) {
898 createReply(sone, post, System.currentTimeMillis(), text);
902 * Creates a new reply.
905 * The Sone that creates the reply
907 * The post that this reply refers to
909 * The time of the reply
911 * The text of the reply
913 public void createReply(Sone sone, Post post, long time, String text) {
914 if (!isLocalSone(sone)) {
915 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
918 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
919 synchronized (replies) {
920 replies.put(reply.getId(), reply);
922 sone.addReply(reply);
927 * Deletes the given reply.
930 * The reply to delete
932 public void deleteReply(Reply reply) {
933 Sone sone = reply.getSone();
934 if (!isLocalSone(sone)) {
935 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
938 synchronized (replies) {
939 replies.remove(reply.getId());
941 sone.removeReply(reply);
948 public void start() {
956 synchronized (localSones) {
957 for (SoneInserter soneInserter : soneInserters.values()) {
969 * Loads the configuration.
971 @SuppressWarnings("unchecked")
972 private void loadConfiguration() {
973 /* create options. */
974 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
977 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
978 SoneInserter.setInsertionDelay(newValue);
982 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
983 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
985 /* read options from configuration. */
986 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
987 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
988 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
989 options.getBooleanOption("ClearOnNextRestart").set(null);
990 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
991 if (clearConfiguration) {
992 /* stop loading the configuration. */
996 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
998 /* load known Sones. */
1001 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1002 if (knownSoneId == null) {
1005 synchronized (newSones) {
1006 knownSones.add(getRemoteSone(knownSoneId));
1012 * Saves the current options.
1014 private void saveConfiguration() {
1015 /* store the options first. */
1017 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1018 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1019 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1021 /* save known Sones. */
1022 int soneCounter = 0;
1023 synchronized (newSones) {
1024 for (Sone knownSone : knownSones) {
1025 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSone.getId());
1027 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1030 } catch (ConfigurationException ce1) {
1031 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1036 * Generate a Sone URI from the given URI and latest edition.
1039 * The URI to derive the Sone URI from
1040 * @return The derived URI
1042 private FreenetURI getSoneUri(String uriString) {
1044 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1046 } catch (MalformedURLException mue1) {
1047 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1053 // INTERFACE IdentityListener
1060 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1061 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1062 if (ownIdentity.hasContext("Sone")) {
1063 addLocalSone(ownIdentity);
1071 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1072 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1079 public void identityAdded(Identity identity) {
1080 logger.log(Level.FINEST, "Adding Identity: " + identity);
1081 addRemoteSone(identity);
1088 public void identityUpdated(final Identity identity) {
1089 new Thread(new Runnable() {
1092 @SuppressWarnings("synthetic-access")
1094 Sone sone = getRemoteSone(identity.getId());
1095 soneDownloader.fetchSone(sone);
1104 public void identityRemoved(Identity identity) {