+ public Sone addLocalSone(String id) {
+ synchronized (localSones) {
+ if (localSones.containsKey(id)) {
+ logger.log(Level.FINE, "Tried to add known local Sone: %s", id);
+ return localSones.get(id);
+ }
+ OwnIdentity ownIdentity = identityManager.getOwnIdentity(id);
+ if (ownIdentity == null) {
+ logger.log(Level.INFO, "Invalid Sone ID: %s", id);
+ return null;
+ }
+ return addLocalSone(ownIdentity);
+ }
+ }
+
+ /**
+ * Adds a local Sone from the given own identity.
+ *
+ * @param ownIdentity
+ * The own identity to create a Sone from
+ * @return The added (or already existing) Sone
+ */
+ public Sone addLocalSone(OwnIdentity ownIdentity) {
+ if (ownIdentity == null) {
+ logger.log(Level.WARNING, "Given OwnIdentity is null!");
+ return null;
+ }
+ synchronized (localSones) {
+ final Sone sone;
+ try {
+ sone = getLocalSone(ownIdentity.getId()).setIdentity(ownIdentity).setInsertUri(new FreenetURI(ownIdentity.getInsertUri())).setRequestUri(new FreenetURI(ownIdentity.getRequestUri()));
+ sone.setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0));
+ } catch (MalformedURLException mue1) {
+ logger.log(Level.SEVERE, "Could not convert the Identity’s URIs to Freenet URIs: " + ownIdentity.getInsertUri() + ", " + ownIdentity.getRequestUri(), mue1);
+ return null;
+ }
+ /* TODO - load posts ’n stuff */
+ localSones.put(ownIdentity.getId(), sone);
+ SoneInserter soneInserter = new SoneInserter(this, freenetInterface, sone);
+ soneInserters.put(sone, soneInserter);
+ soneInserter.start();
+ setSoneStatus(sone, SoneStatus.idle);
+ loadSone(sone);
+ new Thread(new Runnable() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ soneDownloader.fetchSone(sone);
+ }
+
+ }, "Sone Downloader").start();
+ return sone;
+ }
+ }
+
+ /**
+ * Creates a new Sone for the given own identity.
+ *
+ * @param ownIdentity
+ * The own identity to create a Sone for
+ * @return The created Sone
+ */
+ public Sone createSone(OwnIdentity ownIdentity) {
+ identityManager.addContext(ownIdentity, "Sone");
+ Sone sone = addLocalSone(ownIdentity);
+ synchronized (sone) {
+ /* mark as modified so that it gets inserted immediately. */
+ sone.setModificationCounter(sone.getModificationCounter() + 1);
+ }
+ return sone;
+ }
+
+ /**
+ * Adds the Sone of the given identity.
+ *
+ * @param identity
+ * The identity whose Sone to add
+ * @return The added or already existing Sone
+ */
+ public Sone addRemoteSone(Identity identity) {
+ if (identity == null) {
+ logger.log(Level.WARNING, "Given Identity is null!");
+ return null;
+ }
+ synchronized (remoteSones) {
+ final Sone sone = getRemoteSone(identity.getId()).setIdentity(identity);
+ sone.setRequestUri(getSoneUri(identity.getRequestUri()));
+ sone.setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0));
+ remoteSones.put(identity.getId(), sone);
+ soneDownloader.addSone(sone);
+ setSoneStatus(sone, SoneStatus.unknown);
+ new Thread(new Runnable() {
+
+ @Override
+ @SuppressWarnings("synthetic-access")
+ public void run() {
+ soneDownloader.fetchSone(sone);
+ }
+
+ }, "Sone Downloader").start();
+ return sone;
+ }
+ }
+
+ /**
+ * Updates the stores Sone with the given Sone.
+ *
+ * @param sone
+ * The updated Sone
+ */
+ public void updateSone(Sone sone) {
+ if (isRemoteSone(sone)) {
+ Sone storedSone = getRemoteSone(sone.getId());
+ if (!(sone.getTime() > storedSone.getTime())) {
+ logger.log(Level.FINE, "Downloaded Sone %s is not newer than stored Sone %s.", new Object[] { sone, storedSone });
+ return;
+ }
+ synchronized (posts) {
+ for (Post post : storedSone.getPosts()) {
+ posts.remove(post.getId());
+ }
+ for (Post post : sone.getPosts()) {
+ posts.put(post.getId(), post);
+ }
+ }
+ synchronized (replies) {
+ for (Reply reply : storedSone.getReplies()) {
+ replies.remove(reply.getId());
+ }
+ for (Reply reply : sone.getReplies()) {
+ replies.put(reply.getId(), reply);
+ }
+ }
+ synchronized (storedSone) {
+ storedSone.setTime(sone.getTime());
+ storedSone.setProfile(sone.getProfile());
+ storedSone.setPosts(sone.getPosts());
+ storedSone.setReplies(sone.getReplies());
+ storedSone.setLikePostIds(sone.getLikedPostIds());
+ storedSone.setLikeReplyIds(sone.getLikedReplyIds());
+ storedSone.setLatestEdition(sone.getRequestUri().getEdition());
+ storedSone.setModificationCounter(0);
+ }
+ }
+ }
+
+ /**
+ * Deletes the given Sone. This will remove the Sone from the
+ * {@link #getLocalSone(String) local Sones}, stops its {@link SoneInserter}
+ * and remove the context from its identity.
+ *
+ * @param sone
+ * The Sone to delete
+ */
+ public void deleteSone(Sone sone) {
+ if (!(sone.getIdentity() instanceof OwnIdentity)) {
+ logger.log(Level.WARNING, "Tried to delete Sone of non-own identity: %s", sone);
+ return;
+ }
+ synchronized (localSones) {
+ if (!localSones.containsKey(sone.getId())) {
+ logger.log(Level.WARNING, "Tried to delete non-local Sone: %s", sone);
+ return;
+ }
+ localSones.remove(sone.getId());
+ soneInserters.remove(sone).stop();
+ }
+ identityManager.removeContext((OwnIdentity) sone.getIdentity(), "Sone");
+ identityManager.removeProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition");
+ try {
+ configuration.getLongValue("Sone/" + sone.getId() + "/Time").setValue(null);
+ } catch (ConfigurationException ce1) {
+ logger.log(Level.WARNING, "Could not remove Sone from configuration!", ce1);
+ }
+ }
+
+ /**
+ * Loads and updates the given Sone from the configuration. If any error is
+ * encountered, loading is aborted and the given Sone is not changed.
+ *
+ * @param sone
+ * The Sone to load and update
+ */
+ public void loadSone(Sone sone) {
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to load non-local Sone: %s", sone);
+ return;
+ }
+
+ /* load Sone. */
+ String sonePrefix = "Sone/" + sone.getId();
+ Long soneTime = configuration.getLongValue(sonePrefix + "/Time").getValue(null);
+ if (soneTime == null) {
+ logger.log(Level.INFO, "Could not load Sone because no Sone has been saved.");
+ return;
+ }
+ long soneModificationCounter = configuration.getLongValue(sonePrefix + "/ModificationCounter").getValue((long) 0);
+
+ /* load profile. */
+ Profile profile = new Profile();
+ profile.setFirstName(configuration.getStringValue(sonePrefix + "/Profile/FirstName").getValue(null));
+ profile.setMiddleName(configuration.getStringValue(sonePrefix + "/Profile/MiddleName").getValue(null));
+ profile.setLastName(configuration.getStringValue(sonePrefix + "/Profile/LastName").getValue(null));
+ profile.setBirthDay(configuration.getIntValue(sonePrefix + "/Profile/BirthDay").getValue(null));
+ profile.setBirthMonth(configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").getValue(null));
+ profile.setBirthYear(configuration.getIntValue(sonePrefix + "/Profile/BirthYear").getValue(null));
+
+ /* load posts. */
+ Set<Post> posts = new HashSet<Post>();
+ while (true) {
+ String postPrefix = sonePrefix + "/Posts/" + posts.size();
+ String postId = configuration.getStringValue(postPrefix + "/ID").getValue(null);
+ if (postId == null) {
+ break;
+ }
+ long postTime = configuration.getLongValue(postPrefix + "/Time").getValue((long) 0);
+ String postText = configuration.getStringValue(postPrefix + "/Text").getValue(null);
+ if ((postTime == 0) || (postText == null)) {
+ logger.log(Level.WARNING, "Invalid post found, aborting load!");
+ return;
+ }
+ posts.add(getPost(postId).setSone(sone).setTime(postTime).setText(postText));
+ }
+
+ /* load replies. */
+ Set<Reply> replies = new HashSet<Reply>();
+ while (true) {
+ String replyPrefix = sonePrefix + "/Replies/" + replies.size();
+ String replyId = configuration.getStringValue(replyPrefix + "/ID").getValue(null);
+ if (replyId == null) {
+ break;
+ }
+ String postId = configuration.getStringValue(replyPrefix + "/Post/ID").getValue(null);
+ long replyTime = configuration.getLongValue(replyPrefix + "/Time").getValue((long) 0);
+ String replyText = configuration.getStringValue(replyPrefix + "/Text").getValue(null);
+ if ((postId == null) || (replyTime == 0) || (replyText == null)) {
+ logger.log(Level.WARNING, "Invalid reply found, aborting load!");
+ return;
+ }
+ replies.add(getReply(replyId).setSone(sone).setPost(getPost(postId)).setTime(replyTime).setText(replyText));
+ }
+
+ /* load post likes. */
+ Set<String> likedPostIds = new HashSet<String>();
+ while (true) {
+ String likedPostId = configuration.getStringValue(sonePrefix + "/Likes/Post/" + likedPostIds.size() + "/ID").getValue(null);
+ if (likedPostId == null) {
+ break;
+ }
+ likedPostIds.add(likedPostId);
+ }
+
+ /* load reply likes. */
+ Set<String> likedReplyIds = new HashSet<String>();
+ while (true) {
+ String likedReplyId = configuration.getStringValue(sonePrefix + "/Likes/Reply/" + likedReplyIds.size() + "/ID").getValue(null);
+ if (likedReplyId == null) {
+ break;
+ }
+ likedReplyIds.add(likedReplyId);
+ }
+
+ /* load friends. */
+ Set<Sone> friends = new HashSet<Sone>();
+ while (true) {
+ String friendId = configuration.getStringValue(sonePrefix + "/Friends/" + friends.size() + "/ID").getValue(null);
+ if (friendId == null) {
+ break;
+ }
+ Boolean friendLocal = configuration.getBooleanValue(sonePrefix + "/Friends/" + friends.size() + "/Local").getValue(null);
+ if (friendLocal == null) {
+ logger.log(Level.WARNING, "Invalid friend found, aborting load!");
+ return;
+ }
+ friends.add(friendLocal ? getLocalSone(friendId) : getRemoteSone(friendId));
+ }
+
+ /* if we’re still here, Sone was loaded successfully. */
+ synchronized (sone) {
+ sone.setTime(soneTime);
+ sone.setProfile(profile);
+ sone.setPosts(posts);
+ sone.setReplies(replies);
+ sone.setLikePostIds(likedPostIds);
+ sone.setLikeReplyIds(likedReplyIds);
+ sone.setFriends(friends);
+ sone.setModificationCounter(soneModificationCounter);
+ }
+ }
+
+ /**
+ * Saves the given Sone. This will persist all local settings for the given
+ * Sone, such as the friends list and similar, private options.
+ *
+ * @param sone
+ * The Sone to save
+ */
+ public void saveSone(Sone sone) {
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to save non-local Sone: %s", sone);
+ return;
+ }
+ if (!(sone.getIdentity() instanceof OwnIdentity)) {
+ logger.log(Level.WARNING, "Local Sone without OwnIdentity found, refusing to save: %s", sone);
+ return;
+ }
+
+ logger.log(Level.INFO, "Saving Sone: %s", sone);
+ identityManager.setProperty((OwnIdentity) sone.getIdentity(), "Sone.LatestEdition", String.valueOf(sone.getLatestEdition()));
+ try {
+ /* save Sone into configuration. */
+ String sonePrefix = "Sone/" + sone.getId();
+ configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
+ configuration.getLongValue(sonePrefix + "/ModificationCounter").setValue(sone.getModificationCounter());
+
+ /* save profile. */
+ Profile profile = sone.getProfile();
+ configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
+ configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
+ configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
+ configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
+ configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
+ configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
+
+ /* save posts. */
+ int postCounter = 0;
+ for (Post post : sone.getPosts()) {
+ String postPrefix = sonePrefix + "/Posts/" + postCounter++;
+ configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
+ configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
+ configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
+ }
+ configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
+
+ /* save replies. */
+ int replyCounter = 0;
+ for (Reply reply : sone.getReplies()) {
+ String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
+ configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
+ configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPost().getId());
+ configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
+ configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
+ }
+ configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
+
+ /* save post likes. */
+ int postLikeCounter = 0;
+ for (String postId : sone.getLikedPostIds()) {
+ configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
+ }
+ configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
+
+ /* save reply likes. */
+ int replyLikeCounter = 0;
+ for (String replyId : sone.getLikedReplyIds()) {
+ configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
+ }
+ configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
+
+ /* save friends. */
+ int friendCounter = 0;
+ for (Sone friend : sone.getFriends()) {
+ configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(friend.getId());
+ configuration.getBooleanValue(sonePrefix + "/Friends/" + friendCounter++ + "/Local").setValue(friend.getInsertUri() != null);
+ }
+ configuration.getStringValue(sonePrefix + "/Friends/" + friendCounter + "/ID").setValue(null);
+
+ logger.log(Level.INFO, "Sone %s saved.", sone);
+ } catch (ConfigurationException ce1) {
+ logger.log(Level.WARNING, "Could not save Sone: " + sone, ce1);
+ }
+ }
+
+ /**
+ * Creates a new post.
+ *
+ * @param sone
+ * The Sone that creates the post
+ * @param text
+ * The text of the post
+ */
+ public void createPost(Sone sone, String text) {
+ createPost(sone, System.currentTimeMillis(), text);
+ }
+
+ /**
+ * Creates a new post.
+ *
+ * @param sone
+ * The Sone that creates the post
+ * @param time
+ * The time of the post
+ * @param text
+ * The text of the post
+ */
+ public void createPost(Sone sone, long time, String text) {
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to create post for non-local Sone: %s", sone);
+ return;
+ }
+ Post post = new Post(sone, time, text);
+ synchronized (posts) {
+ posts.put(post.getId(), post);
+ }
+ sone.addPost(post);
+ saveSone(sone);
+ }
+
+ /**
+ * Deletes the given post.
+ *
+ * @param post
+ * The post to delete
+ */
+ public void deletePost(Post post) {
+ if (!isLocalSone(post.getSone())) {
+ logger.log(Level.WARNING, "Tried to delete post of non-local Sone: %s", post.getSone());
+ return;
+ }
+ post.getSone().removePost(post);
+ synchronized (posts) {
+ posts.remove(post.getId());
+ }
+ saveSone(post.getSone());
+ }
+
+ /**
+ * Creates a new reply.
+ *
+ * @param sone
+ * The Sone that creates the reply
+ * @param post
+ * The post that this reply refers to
+ * @param text
+ * The text of the reply
+ */
+ public void createReply(Sone sone, Post post, String text) {
+ createReply(sone, post, System.currentTimeMillis(), text);
+ }
+
+ /**
+ * Creates a new reply.
+ *
+ * @param sone
+ * The Sone that creates the reply
+ * @param post
+ * The post that this reply refers to
+ * @param time
+ * The time of the reply
+ * @param text
+ * The text of the reply
+ */
+ public void createReply(Sone sone, Post post, long time, String text) {
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to create reply for non-local Sone: %s", sone);
+ return;
+ }
+ Reply reply = new Reply(sone, post, System.currentTimeMillis(), text);
+ synchronized (replies) {
+ replies.put(reply.getId(), reply);
+ }
+ sone.addReply(reply);
+ saveSone(sone);
+ }
+
+ /**
+ * Deletes the given reply.
+ *
+ * @param reply
+ * The reply to delete
+ */
+ public void deleteReply(Reply reply) {
+ Sone sone = reply.getSone();
+ if (!isLocalSone(sone)) {
+ logger.log(Level.FINE, "Tried to delete non-local reply: %s", reply);
+ return;
+ }
+ synchronized (replies) {
+ replies.remove(reply.getId());
+ }
+ sone.removeReply(reply);
+ saveSone(sone);
+ }
+
+ /**
+ * Starts the core.
+ */
+ public void start() {