2 * Sone - Core.java - Copyright © 2010 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.core;
20 import java.net.MalformedURLException;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.List;
28 import java.util.logging.Level;
29 import java.util.logging.Logger;
31 import net.pterodactylus.sone.core.Options.DefaultOption;
32 import net.pterodactylus.sone.core.Options.Option;
33 import net.pterodactylus.sone.core.Options.OptionWatcher;
34 import net.pterodactylus.sone.data.Post;
35 import net.pterodactylus.sone.data.Profile;
36 import net.pterodactylus.sone.data.Reply;
37 import net.pterodactylus.sone.data.Sone;
38 import net.pterodactylus.sone.freenet.wot.Identity;
39 import net.pterodactylus.sone.freenet.wot.IdentityListener;
40 import net.pterodactylus.sone.freenet.wot.IdentityManager;
41 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
42 import net.pterodactylus.util.config.Configuration;
43 import net.pterodactylus.util.config.ConfigurationException;
44 import net.pterodactylus.util.logging.Logging;
45 import net.pterodactylus.util.number.Numbers;
46 import freenet.keys.FreenetURI;
51 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
53 public class Core implements IdentityListener {
56 * Enumeration for the possible states of a {@link Sone}.
58 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
60 public enum SoneStatus {
62 /** The Sone is unknown, i.e. not yet downloaded. */
65 /** The Sone is idle, i.e. not being downloaded or inserted. */
68 /** The Sone is currently being inserted. */
71 /** The Sone is currently being downloaded. */
76 private static final Logger logger = Logging.getLogger(Core.class);
79 private final Options options = new Options();
81 /** The configuration. */
82 private final Configuration configuration;
84 /** The identity manager. */
85 private final IdentityManager identityManager;
87 /** Interface to freenet. */
88 private final FreenetInterface freenetInterface;
90 /** The Sone downloader. */
91 private final SoneDownloader soneDownloader;
93 /** The Sones’ statuses. */
94 /* synchronize access on itself. */
95 private final Map<Sone, SoneStatus> soneStatuses = new HashMap<Sone, SoneStatus>();
97 /** Sone inserters. */
98 /* synchronize access on this on localSones. */
99 private final Map<Sone, SoneInserter> soneInserters = new HashMap<Sone, SoneInserter>();
101 /** All local Sones. */
102 /* synchronize access on this on itself. */
103 private Map<String, Sone> localSones = new HashMap<String, Sone>();
105 /** All remote Sones. */
106 /* synchronize access on this on itself. */
107 private Map<String, Sone> remoteSones = new HashMap<String, Sone>();
109 /** All new Sones. */
110 private Set<String> newSones = new HashSet<String>();
112 /** All known Sones. */
113 /* synchronize access on {@link #newSones}. */
114 private Set<String> knownSones = new HashSet<String>();
117 private Map<String, Post> posts = new HashMap<String, Post>();
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.getId()) && newSones.remove(sone.getId());
384 knownSones.add(sone.getId());
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) {
584 newSones.add(sone.getId());
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<String> friends = new HashSet<String>();
764 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
765 if (friendId == null) {
768 friends.add(friendId);
771 /* if we’re still here, Sone was loaded successfully. */
772 synchronized (sone) {
773 sone.setTime(soneTime);
774 sone.setProfile(profile);
775 sone.setPosts(posts);
776 sone.setReplies(replies);
777 sone.setLikePostIds(likedPostIds);
778 sone.setLikeReplyIds(likedReplyIds);
779 sone.setFriends(friends);
780 sone.setModificationCounter(soneModificationCounter);
782 synchronized (newSones) {
783 for (String friend : friends) {
784 knownSones.add(friend);
790 * Saves the given Sone. This will persist all local settings for the given
791 * Sone, such as the friends list and similar, private options.
796 public void saveSone(Sone sone) {
797 if (!isLocalSone(sone)) {
798 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
801 if (!(sone.getIdentity() instanceof OwnIdentity)) {
802 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
806 logger.log(Level.INFO, "Saving Sone: %s", sone);
807 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
809 /* save Sone into configuration. */
810 String sonePrefix = "Sone/" + sone.getId();
811 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
812 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
815 Profile profile = sone.getProfile();
816 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
817 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
818 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
819 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
820 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
821 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
825 for (Post post : sone.getPosts()) {
826 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
827 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
828 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
829 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
831 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
834 int replyCounter = 0;
835 for (Reply reply : sone.getReplies()) {
836 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
837 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
838 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
839 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
840 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
842 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
844 /* save post likes. */
845 int postLikeCounter = 0;
846 for (String postId : sone.getLikedPostIds()) {
847 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
849 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
851 /* save reply likes. */
852 int replyLikeCounter = 0;
853 for (String replyId : sone.getLikedReplyIds()) {
854 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
856 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
859 int friendCounter = 0;
860 for (String friend : sone.getFriends()) {
861 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend);
863 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
865 logger.log(Level.INFO, "Sone %s saved.", sone);
866 } catch (ConfigurationException ce1) {
867 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
872 * Creates a new post.
875 * The Sone that creates the post
877 * The text of the post
879 public void createPost(Sone sone, String text) {
880 createPost(sone, System.currentTimeMillis(), text);
884 * Creates a new post.
887 * The Sone that creates the post
889 * The time of the post
891 * The text of the post
893 public void createPost(Sone sone, long time, String text) {
894 if (!isLocalSone(sone)) {
895 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
898 Post post = new Post(sone, time, text);
899 synchronized (posts) {
900 posts.put(post.getId(), post);
907 * Deletes the given post.
912 public void deletePost(Post post) {
913 if (!isLocalSone(post.getSone())) {
914 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
917 post.getSone().removePost(post);
918 synchronized (posts) {
919 posts.remove(post.getId());
921 saveSone(post.getSone());
925 * Creates a new reply.
928 * The Sone that creates the reply
930 * The post that this reply refers to
932 * The text of the reply
934 public void createReply(Sone sone, Post post, String text) {
935 createReply(sone, post, System.currentTimeMillis(), text);
939 * Creates a new reply.
942 * The Sone that creates the reply
944 * The post that this reply refers to
946 * The time of the reply
948 * The text of the reply
950 public void createReply(Sone sone, Post post, long time, String text) {
951 if (!isLocalSone(sone)) {
952 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
955 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
956 synchronized (replies) {
957 replies.put(reply.getId(), reply);
959 sone.addReply(reply);
964 * Deletes the given reply.
967 * The reply to delete
969 public void deleteReply(Reply reply) {
970 Sone sone = reply.getSone();
971 if (!isLocalSone(sone)) {
972 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
975 synchronized (replies) {
976 replies.remove(reply.getId());
978 sone.removeReply(reply);
985 public void start() {
993 synchronized (localSones) {
994 for (SoneInserter soneInserter : soneInserters.values()) {
1006 * Loads the configuration.
1008 @SuppressWarnings("unchecked")
1009 private void loadConfiguration() {
1010 /* create options. */
1011 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
1014 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
1015 SoneInserter.setInsertionDelay(newValue);
1019 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
1020 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
1022 /* read options from configuration. */
1023 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
1024 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
1025 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
1026 options.getBooleanOption("ClearOnNextRestart").set(null);
1027 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
1028 if (clearConfiguration) {
1029 /* stop loading the configuration. */
1033 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
1035 /* load known Sones. */
1036 int soneCounter = 0;
1038 String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
1039 if (knownSoneId == null) {
1042 synchronized (newSones) {
1043 knownSones.add(knownSoneId);
1049 * Saves the current options.
1051 private void saveConfiguration() {
1052 /* store the options first. */
1054 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
1055 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
1056 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
1058 /* save known Sones. */
1059 int soneCounter = 0;
1060 synchronized (newSones) {
1061 for (String knownSoneId : knownSones) {
1062 configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
1064 configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
1067 } catch (ConfigurationException ce1) {
1068 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
1073 * Generate a Sone URI from the given URI and latest edition.
1076 * The URI to derive the Sone URI from
1077 * @return The derived URI
1079 private FreenetURI getSoneUri(String uriString) {
1081 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]);
1083 } catch (MalformedURLException mue1) {
1084 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
1090 // INTERFACE IdentityListener
1097 public void ownIdentityAdded(OwnIdentity ownIdentity) {
1098 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
1099 if (ownIdentity.hasContext("Sone")) {
1100 addLocalSone(ownIdentity);
1108 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
1109 logger.log(Level.FINEST, "Removing OwnIdentity: " + ownIdentity);
1116 public void identityAdded(Identity identity) {
1117 logger.log(Level.FINEST, "Adding Identity: " + identity);
1118 addRemoteSone(identity);
1125 public void identityUpdated(final Identity identity) {
1126 new Thread(new Runnable() {
1129 @SuppressWarnings("synthetic-access")
1131 Sone sone = getRemoteSone(identity.getId());
1132 soneDownloader.fetchSone(sone);
1141 public void identityRemoved(Identity identity) {