nodechan

decentralized peer-to-peer anonymous messageboard client
git clone git://squid-tech.com/nodechan.git
Log | Files | Refs | README

commit e601565b9fccac3bdc8f53dbde7df137cf499780
parent 5e20f2257e7ce555fd802bd09dce5fd7c2e1953f
Author: joshiemoore <jxm5210@g.rit.edu>
Date:   Wed,  7 Aug 2019 02:44:45 -0400

Added the ability to reply to threads!

Diffstat:
Msrc/ChanThread.java | 5++---
Msrc/IncomingThread.java | 3+--
Msrc/NodeChan.java | 47++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/src/ChanThread.java b/src/ChanThread.java @@ -53,9 +53,8 @@ public class ChanThread { this.posts.add(post); } else { // sort by time (earliest first) - for (int i = 0; i < posts.size(); i++) { - if (posts.get(i + 1) != null && - posts.get(i).getPostTime() < post.getPostTime() && + for (int i = 0; i < posts.size() - 1; i++) { + if (posts.get(i).getPostTime() < post.getPostTime() && posts.get(i + 1).getPostTime() >= post.getPostTime()) { this.posts.add(i + 1, post); return; diff --git a/src/IncomingThread.java b/src/IncomingThread.java @@ -46,7 +46,6 @@ public class IncomingThread extends Thread { // TODO: post propagation/forwarding // decode the post packet ChanPost post = ChanPost.decodeUDP(recv_data); - System.out.println("Incoming post!"); if (post.getIsRoot()) { // check whether we already have a copy of this OP @@ -61,7 +60,7 @@ public class IncomingThread extends Thread { if (!haveOP) { // create a new local thread with this OP - ChanThread newThread = new ChanThread(""); + ChanThread newThread = new ChanThread(post.getTid()); newThread.addPost(post); threads.add(newThread); } diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -188,7 +188,7 @@ public class NodeChan { threadFound = true; // print out the thread - System.out.println("\n\n" + t.getTid() + " - " + t.getTitle()); + System.out.println("\n\nThread: " + t.getTid() + " - " + t.getTitle()); System.out.println("===================="); for (int i = 0; i < t.getNumPosts(); i++) { @@ -206,6 +206,27 @@ public class NodeChan { if (!threadFound) { System.out.println("Could not find thread by that TID."); } + } else if (input.equals("reply")) { + System.out.print("To which TID? "); + String readTid = scan.nextLine(); + ChanThread replyThread = null; + + for (ChanThread t : threads) { + if (t.getTid().equals(readTid)) { + replyThread = t; + break; + } + } + + if (replyThread == null) { + System.out.println("Could not find thread by that TID."); + continue; + } + + System.out.println("\nYOUR REPLY:"); + String reply = scan.nextLine(); + + createReplyAndSend(replyThread, reply); } else { System.out.println("Command not recognized."); } @@ -275,4 +296,28 @@ public class NodeChan { new OutgoingThread(p.getAddress(), NC_PORT, outbytes).start(); } } + + /** + * Create a new reply to a thread and send it. + */ + public static void createReplyAndSend(ChanThread thread, String reply) { + ChanPost newPost = new ChanPost( + thread.getTid(), + "", + false, + thread.getTitle(), + reply + ); + + // add new post to local thread + thread.addPost(newPost); + + // translate post to bytes + byte[] outbytes = ChanPost.encodeUDP(newPost); + + // send post to all peers + for (Peer p : peers) { + new OutgoingThread(p.getAddress(), NC_PORT, outbytes).start(); + } + } }