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>();
110 private Map<String, Post> posts = new HashMap<String, Post>();
113 private Map<String, Reply> replies = new HashMap<String, Reply>();
116 * Creates a new core.
118 * @param configuration
119 * The configuration of the core
120 * @param freenetInterface
121 * The freenet interface
122 * @param identityManager
123 * The identity manager
125 public Core(Configuration configuration, FreenetInterface freenetInterface, IdentityManager identityManager) {
126 this.configuration = configuration;
127 this.freenetInterface = freenetInterface;
128 this.identityManager = identityManager;
129 this.soneDownloader = new SoneDownloader(this, freenetInterface);
137 * Returns the options used by the core.
139 * @return The options of the core
141 public Options getOptions() {
146 * Returns the identity manager used by the core.
148 * @return The identity manager
150 public IdentityManager getIdentityManager() {
151 return identityManager;
155 * Returns the status of the given Sone.
158 * The Sone to get the status for
159 * @return The status of the Sone
161 public SoneStatus getSoneStatus(Sone sone) {
162 synchronized (soneStatuses) {
163 return soneStatuses.get(sone);
168 * Sets the status of the given Sone.
171 * The Sone to set the status of
175 public void setSoneStatus(Sone sone, SoneStatus soneStatus) {
176 synchronized (soneStatuses) {
177 soneStatuses.put(sone, soneStatus);
182 * Returns all Sones, remote and local.
186 public Set<Sone> getSones() {
187 Set<Sone> allSones = new HashSet<Sone>();
188 allSones.addAll(getLocalSones());
189 allSones.addAll(getRemoteSones());
194 * Returns the Sone with the given ID, regardless whether it’s local or
198 * The ID of the Sone to get
199 * @return The Sone with the given ID, or {@code null} if there is no such
202 public Sone getSone(String id) {
203 if (isLocalSone(id)) {
204 return getLocalSone(id);
206 return getRemoteSone(id);
210 * Returns whether the given Sone is a local Sone.
213 * The Sone to check for its locality
214 * @return {@code true} if the given Sone is local, {@code false} otherwise
216 public boolean isLocalSone(Sone sone) {
217 synchronized (localSones) {
218 return localSones.containsKey(sone.getId());
223 * Returns whether the given ID is the ID of a local Sone.
226 * The Sone ID to check for its locality
227 * @return {@code true} if the given ID is a local Sone, {@code false}
230 public boolean isLocalSone(String id) {
231 synchronized (localSones) {
232 return localSones.containsKey(id);
237 * Returns all local Sones.
239 * @return All local Sones
241 public Set<Sone> getLocalSones() {
242 synchronized (localSones) {
243 return new HashSet<Sone>(localSones.values());
248 * Returns the local Sone with the given ID.
251 * The ID of the Sone to get
252 * @return The Sone with the given ID
254 public Sone getLocalSone(String id) {
255 synchronized (localSones) {
256 Sone sone = localSones.get(id);
259 localSones.put(id, sone);
266 * Returns all remote Sones.
268 * @return All remote Sones
270 public Set<Sone> getRemoteSones() {
271 synchronized (remoteSones) {
272 return new HashSet<Sone>(remoteSones.values());
277 * Returns the remote Sone with the given ID.
280 * The ID of the remote Sone to get
281 * @return The Sone with the given ID
283 public Sone getRemoteSone(String id) {
284 synchronized (remoteSones) {
285 Sone sone = remoteSones.get(id);
288 remoteSones.put(id, sone);
295 * Returns whether the given Sone is a remote Sone.
299 * @return {@code true} if the given Sone is a remote Sone, {@code false}
302 public boolean isRemoteSone(Sone sone) {
303 synchronized (remoteSones) {
304 return remoteSones.containsKey(sone.getId());
309 * Returns the post with the given ID.
312 * The ID of the post to get
313 * @return The post, or {@code null} if there is no such post
315 public Post getPost(String postId) {
316 synchronized (posts) {
317 Post post = posts.get(postId);
319 post = new Post(postId);
320 posts.put(postId, post);
327 * Returns the reply with the given ID.
330 * The ID of the reply to get
331 * @return The reply, or {@code null} if there is no such reply
333 public Reply getReply(String replyId) {
334 synchronized (replies) {
335 Reply reply = replies.get(replyId);
337 reply = new Reply(replyId);
338 replies.put(replyId, reply);
345 * Returns all replies for the given post, order ascending by time.
348 * The post to get all replies for
349 * @return All replies for the given post
351 public List<Reply> getReplies(Post post) {
352 Set<Sone> sones = getSones();
353 List<Reply> replies = new ArrayList<Reply>();
354 for (Sone sone : sones) {
355 for (Reply reply : sone.getReplies()) {
356 if (reply.getPost().equals(post)) {
361 Collections.sort(replies, Reply.TIME_COMPARATOR);
366 * Returns all Sones that have liked the given post.
369 * The post to get the liking Sones for
370 * @return The Sones that like the given post
372 public Set<Sone> getLikes(Post post) {
373 Set<Sone> sones = new HashSet<Sone>();
374 for (Sone sone : getSones()) {
375 if (sone.getLikedPostIds().contains(post.getId())) {
383 * Returns all Sones that have liked the given reply.
386 * The reply to get the liking Sones for
387 * @return The Sones that like the given reply
389 public Set<Sone> getLikes(Reply reply) {
390 Set<Sone> sones = new HashSet<Sone>();
391 for (Sone sone : getSones()) {
392 if (sone.getLikedPostIds().contains(reply.getId())) {
404 * Adds a local Sone from the given ID which has to be the ID of an own
408 * The ID of an own identity to add a Sone for
409 * @return The added (or already existing) Sone
411 public Sone addLocalSone(String id) {
412 synchronized (localSones) {
413 if (localSones.containsKey(id)) {
414 logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
415 return localSones.get(id);
417 OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
418 if (ownIdentity == null) {
419 logger.log(Level.INFO, "Invalid Sone ID: %s", id);
422 return addLocalSone(ownIdentity);
427 * Adds a local Sone from the given own identity.
430 * The own identity to create a Sone from
431 * @return The added (or already existing) Sone
433 public Sone addLocalSone(OwnIdentity ownIdentity) {
434 if (ownIdentity == null) {
435 logger.log(Level.WARNING, "Given OwnIdentity is null!");
438 synchronized (localSones) {
439 if (localSones.containsKey(ownIdentity.getId())) {
440 logger.log(Level.FINE, "Tried to add known local Sone: %s", ownIdentity);
441 return localSones.get(ownIdentity.getId());
445 sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
446 sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
447 } catch (MalformedURLException mue1) {
448 logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
451 /* TODO - load posts ’n stuff */
452 localSones.put(ownIdentity.getId(), sone);
453 SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
454 soneInserters.put(sone, soneInserter);
455 soneInserter.start();
456 setSoneStatus(sone, SoneStatus.idle);
463 * Creates a new Sone for the given own identity.
466 * The own identity to create a Sone for
467 * @return The created Sone
469 public Sone createSone(OwnIdentity ownIdentity) {
470 identityManager.addContext(ownIdentity, "Sone");
471 Sone sone = addLocalSone(ownIdentity);
472 synchronized (sone) {
473 /* mark as modified so that it gets inserted immediately. */
474 sone.setModificationCounter(sone.getModificationCounter() + 1);
480 * Adds the Sone of the given identity.
483 * The identity whose Sone to add
484 * @return The added or already existing Sone
486 public Sone addRemoteSone(Identity identity) {
487 if (identity == null) {
488 logger.log(Level.WARNING, "Given Identity is null!");
491 synchronized (remoteSones) {
492 final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
493 if (remoteSones.containsKey(identity.getId())) {
494 logger.log(Level.FINE, "Identity already exists: %s", identity);
495 return remoteSones.get(identity.getId());
497 sone.setRequestUri(getSoneUri(identity.getRequestUri(), identity.getProperty("Sone.LatestEdition")));
498 remoteSones.put(identity.getId(), sone);
499 soneDownloader.addSone(sone);
500 new Thread(new Runnable() {
503 @SuppressWarnings("synthetic-access")
505 soneDownloader.fetchSone(sone);
508 }, "Sone Downloader").start();
509 setSoneStatus(sone, SoneStatus.idle);
515 * Updates the stores Sone with the given Sone.
520 public void updateSone(Sone sone) {
521 if (isRemoteSone(sone)) {
522 Sone storedSone = getRemoteSone(sone.getId());
523 if (!(sone.getTime() > storedSone.getTime())) {
524 logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
527 synchronized (posts) {
528 for (Post post : storedSone.getPosts()) {
529 posts.remove(post.getId());
531 for (Post post : sone.getPosts()) {
532 posts.put(post.getId(), post);
535 synchronized (replies) {
536 for (Reply reply : storedSone.getReplies()) {
537 replies.remove(reply.getId());
539 for (Reply reply : sone.getReplies()) {
540 replies.put(reply.getId(), reply);
543 synchronized (storedSone) {
544 storedSone.setTime(sone.getTime());
545 storedSone.setProfile(sone.getProfile());
546 storedSone.setPosts(sone.getPosts());
547 storedSone.setReplies(sone.getReplies());
548 storedSone.setLikePostIds(sone.getLikedPostIds());
549 storedSone.setLikeReplyIds(sone.getLikedReplyIds());
550 storedSone.setLatestEdition(sone.getRequestUri().getEdition());
552 saveSone(storedSone);
557 * Deletes the given Sone. This will remove the Sone from the
558 * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
559 * and remove the context from its identity.
564 public void deleteSone(Sone sone) {
565 if (!(sone.getIdentity() instanceof OwnIdentity)) {
566 logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
569 synchronized (localSones) {
570 if (!localSones.containsKey(sone.getId())) {
571 logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
574 localSones.remove(sone.getId());
575 soneInserters.remove(sone.getId()).stop();
577 identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
581 * Loads and updates the given Sone from the configuration. If any error is
582 * encountered, loading is aborted and the given Sone is not changed.
585 * The Sone to load and update
587 public void loadSone(Sone sone) {
588 if (!isLocalSone(sone)) {
589 logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
594 String sonePrefix = "Sone/" + sone.getId();
595 long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue((long) 0);
596 long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
599 Profile profile = new Profile();
600 profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
601 profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
602 profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
603 profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
604 profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
605 profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
608 Set<Post> posts = new HashSet<Post>();
610 String postPrefix = sonePrefix + "/Posts/" + posts.size();
611 String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
612 if (postId == null) {
615 long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
616 String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
617 if ((postTime == 0) || (postText == null)) {
618 logger.log(Level.WARNING, "Invalid post found, aborting load!");
621 posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
625 Set<Reply> replies = new HashSet<Reply>();
627 String replyPrefix = sonePrefix + "/Replies/" + replies.size();
628 String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
629 if (replyId == null) {
632 String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
633 long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
634 String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
635 if ((postId == null) || (replyTime == 0) || (replyText == null)) {
636 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
639 replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
642 /* load post likes. */
643 Set<String> likedPostIds = new HashSet<String>();
645 String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
646 if (likedPostId == null) {
649 likedPostIds.add(likedPostId);
652 /* load reply likes. */
653 Set<String> likedReplyIds = new HashSet<String>();
655 String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
656 if (likedReplyId == null) {
659 likedReplyIds.add(likedReplyId);
663 Set<Sone> friends = new HashSet<Sone>();
665 String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
666 if (friendId == null) {
669 Boolean friendLocal = configuration.getBooleanValue(sonePrefix + "/Friends/" + friends.size() + "/Local").getValue(null);
670 if (friendLocal == null) {
671 logger.log(Level.WARNING, "Invalid friend found, aborting load!");
674 friends.add(friendLocal ? getLocalSone(friendId) : getRemoteSone(friendId));
677 /* if we’re still here, Sone was loaded successfully. */
678 synchronized (sone) {
679 sone.setTime(soneTime);
680 sone.setProfile(profile);
681 sone.setPosts(posts);
682 sone.setReplies(replies);
683 sone.setLikePostIds(likedPostIds);
684 sone.setLikeReplyIds(likedReplyIds);
685 sone.setFriends(friends);
686 sone.setModificationCounter(soneModificationCounter);
691 * Saves the given Sone. This will persist all local settings for the given
692 * Sone, such as the friends list and similar, private options.
697 public void saveSone(Sone sone) {
698 if (!isLocalSone(sone)) {
699 logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
702 if (!(sone.getIdentity() instanceof OwnIdentity)) {
703 logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
707 logger.log(Level.INFO, "Saving Sone: %s", sone);
708 identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
710 /* save Sone into configuration. */
711 String sonePrefix = "Sone/" + sone.getId();
712 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
713 configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
716 Profile profile = sone.getProfile();
717 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
718 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
719 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
720 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
721 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
722 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
726 for (Post post : sone.getPosts()) {
727 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
728 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
729 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
730 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
732 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
735 int replyCounter = 0;
736 for (Reply reply : sone.getReplies()) {
737 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
738 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
739 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
740 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
741 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
743 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
745 /* save post likes. */
746 int postLikeCounter = 0;
747 for (String postId : sone.getLikedPostIds()) {
748 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
750 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
752 /* save reply likes. */
753 int replyLikeCounter = 0;
754 for (String replyId : sone.getLikedReplyIds()) {
755 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
757 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
760 int friendCounter = 0;
761 for (Sone friend : sone.getFriends()) {
762 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend.getId());
763 configuration.getBooleanValue(sonePrefix + "/Friends/" + friendCounter++ + "/Local").setValue(friend.getInsertUri() != null);
765 configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
767 logger.log(Level.INFO, "Sone %s saved.", sone);
768 } catch (ConfigurationException ce1) {
769 logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
774 * Creates a new post.
777 * The Sone that creates the post
779 * The text of the post
781 public void createPost(Sone sone, String text) {
782 createPost(sone, System.currentTimeMillis(), text);
786 * Creates a new post.
789 * The Sone that creates the post
791 * The time of the post
793 * The text of the post
795 public void createPost(Sone sone, long time, String text) {
796 if (!isLocalSone(sone)) {
797 logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
800 Post post = new Post(sone, time, text);
801 synchronized (posts) {
802 posts.put(post.getId(), post);
809 * Deletes the given post.
814 public void deletePost(Post post) {
815 if (!isLocalSone(post.getSone())) {
816 logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
819 post.getSone().removePost(post);
820 synchronized (posts) {
821 posts.remove(post.getId());
823 saveSone(post.getSone());
827 * Creates a new reply.
830 * The Sone that creates the reply
832 * The post that this reply refers to
834 * The text of the reply
836 public void createReply(Sone sone, Post post, String text) {
837 createReply(sone, post, System.currentTimeMillis(), text);
841 * Creates a new reply.
844 * The Sone that creates the reply
846 * The post that this reply refers to
848 * The time of the reply
850 * The text of the reply
852 public void createReply(Sone sone, Post post, long time, String text) {
853 if (!isLocalSone(sone)) {
854 logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
857 Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
858 synchronized (replies) {
859 replies.put(reply.getId(), reply);
861 sone.addReply(reply);
866 * Deletes the given reply.
869 * The reply to delete
871 public void deleteReply(Reply reply) {
872 Sone sone = reply.getSone();
873 if (!isLocalSone(sone)) {
874 logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
877 synchronized (replies) {
878 replies.remove(reply.getId());
880 sone.removeReply(reply);
887 public void start() {
895 synchronized (localSones) {
896 for (SoneInserter soneInserter : soneInserters.values()) {
908 * Loads the configuration.
910 @SuppressWarnings("unchecked")
911 private void loadConfiguration() {
912 /* create options. */
913 options.addIntegerOption("InsertionDelay", new DefaultOption<Integer>(60, new OptionWatcher<Integer>() {
916 public void optionChanged(Option<Integer> option, Integer oldValue, Integer newValue) {
917 SoneInserter.setInsertionDelay(newValue);
921 options.addBooleanOption("ClearOnNextRestart", new DefaultOption<Boolean>(false));
922 options.addBooleanOption("ReallyClearOnNextRestart", new DefaultOption<Boolean>(false));
924 /* read options from configuration. */
925 options.getBooleanOption("ClearOnNextRestart").set(configuration.getBooleanValue("Option/ClearOnNextRestart").getValue(null));
926 options.getBooleanOption("ReallyClearOnNextRestart").set(configuration.getBooleanValue("Option/ReallyClearOnNextRestart").getValue(null));
927 boolean clearConfiguration = options.getBooleanOption("ClearOnNextRestart").get() && options.getBooleanOption("ReallyClearOnNextRestart").get();
928 options.getBooleanOption("ClearOnNextRestart").set(null);
929 options.getBooleanOption("ReallyClearOnNextRestart").set(null);
930 if (clearConfiguration) {
931 /* stop loading the configuration. */
935 options.getIntegerOption("InsertionDelay").set(configuration.getIntValue("Option/InsertionDelay").getValue(null));
940 * Saves the current options.
942 private void saveConfiguration() {
943 /* store the options first. */
945 configuration.getIntValue("Option/InsertionDelay").setValue(options.getIntegerOption("InsertionDelay").getReal());
946 configuration.getBooleanValue("Option/ClearOnNextRestart").setValue(options.getBooleanOption("ClearOnNextRestart").getReal());
947 configuration.getBooleanValue("Option/ReallyClearOnNextRestart").setValue(options.getBooleanOption("ReallyClearOnNextRestart").getReal());
948 } catch (ConfigurationException ce1) {
949 logger.log(Level.SEVERE, "Could not store configuration!", ce1);
954 * Generate a Sone URI from the given URI and latest edition.
957 * The URI to derive the Sone URI from
958 * @param latestEditionString
959 * The latest edition as a {@link String}, or {@code null}
960 * @return The derived URI
962 private FreenetURI getSoneUri(String uriString, String latestEditionString) {
964 FreenetURI uri = new FreenetURI(uriString).setDocName("Sone").setMetaString(new String[0]).setSuggestedEdition(Numbers.safeParseLong(latestEditionString, (long) 0));
966 } catch (MalformedURLException mue1) {
967 logger.log(Level.WARNING, "Could not create Sone URI from URI: " + uriString, mue1);
973 // INTERFACE IdentityListener
980 public void ownIdentityAdded(OwnIdentity ownIdentity) {
981 logger.log(Level.FINEST, "Adding OwnIdentity: " + ownIdentity);
982 if (ownIdentity.hasContext("Sone")) {
983 addLocalSone(ownIdentity);
991 public void ownIdentityRemoved(OwnIdentity ownIdentity) {
999 public void identityAdded(Identity identity) {
1000 logger.log(Level.FINEST, "Adding Identity: " + identity);
1001 addRemoteSone(identity);
1008 public void identityUpdated(Identity identity) {
1016 public void identityRemoved(Identity identity) {