nodechan

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

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

Fleshed out incoming packet handling

Diffstat:
Msrc/ChanThread.java | 19+++++++++++++++----
Msrc/IncomingThread.java | 54++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/NodeChan.java | 48++++++++++++++++++++++++++++++++++++++++++++++--
3 files changed, 113 insertions(+), 8 deletions(-)

diff --git a/src/ChanThread.java b/src/ChanThread.java @@ -44,14 +44,25 @@ public class ChanThread { /** * Add a post to this thread. + * Sorted by post time - earliest first. */ public void addPost(ChanPost post) { - // TODO: sort by post time! - this.posts.add(post); - - if (posts.size() == 1 && post.getIsRoot()) { + if (posts.size() == 0 && post.getIsRoot()) { // this is the first post in the thread this.title = post.getTitle(); + 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() && + posts.get(i + 1).getPostTime() >= post.getPostTime()) { + this.posts.add(i + 1, post); + return; + } + } + + this.posts.add(post); } } diff --git a/src/IncomingThread.java b/src/IncomingThread.java @@ -43,11 +43,61 @@ public class IncomingThread extends Thread { switch(recv_data[2]) { case 'P': + // TODO: post propagation/forwarding // decode the post packet ChanPost post = ChanPost.decodeUDP(recv_data); + System.out.println("Incoming post!"); - // TODO: actually handle posts - System.out.println(post.getText()); + if (post.getIsRoot()) { + // check whether we already have a copy of this OP + boolean haveOP = false; + + for (ChanThread t : threads) { + if (t.getTid().equals(post.getTid())) { + haveOP = true; + break; + } + } + + if (!haveOP) { + // create a new local thread with this OP + ChanThread newThread = new ChanThread(""); + newThread.addPost(post); + threads.add(newThread); + } + } else { + // check whether we have this thread + // if not, ignore this post, since it would be pointless to start + // in the middle of the conversation + ChanThread existThread = null; + + for (ChanThread t : threads) { + if (t.getTid().equals(post.getTid())) { + existThread = t; + break; + } + } + + if (existThread != null) { + // check whether we already have this post + boolean havePost = false; + + for (int i = 0; i < existThread.getNumPosts(); i++) { + if (existThread.getPost(i).getPid().equals(post.getPid())) { + havePost = true; + break; + } + } + + if (!havePost) { + // we have the thread, but don't have this post yet, so add it + existThread.addPost(post); + } + } else { + // ignore the post... for now + } + } + break; } } diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -134,8 +134,6 @@ public class NodeChan { first_peer_ip = input; } - System.out.println(""); - if (first_peer_ip.equals("nopeer") || first_peer_ip.equals("ptfail")) { System.out.println("No peer available from tracker.\n"); } else { @@ -164,6 +162,52 @@ public class NodeChan { text = scan.nextLine(); createThreadAndSend(title, text); + } else if (input.equals("numpeers")) { + System.out.println(peers.size()); + } else if (input.equals("exit")) { + System.exit(0); + } else if (input.equals("threadlist")) { + if (threads.size() == 0) { + System.out.println("No active threads."); + continue; + } + + System.out.println("TID Subject"); + for (int i = 0; i < threads.size(); i++) { + ChanThread l = threads.get(i); + + System.out.println(l.getTid() + " - " + l.getTitle() + "\n"); + } + } else if (input.equals("readthread")) { + System.out.print("Which TID? "); + String readTid = scan.nextLine(); + boolean threadFound = false; + + for (ChanThread t : threads) { + if (t.getTid().equals(readTid)) { + threadFound = true; + + // print out the thread + System.out.println("\n\n" + t.getTid() + " - " + t.getTitle()); + System.out.println("===================="); + + for (int i = 0; i < t.getNumPosts(); i++) { + ChanPost postRead = t.getPost(i); + + if (postRead != null) { + System.out.println(postRead.getPid() + "\n"); + System.out.println(postRead.getText()); + System.out.println("===================="); + } + } + } + } + + if (!threadFound) { + System.out.println("Could not find thread by that TID."); + } + } else { + System.out.println("Command not recognized."); } } } else {