nodechan

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

commit 0757770620e21e0ca9f17d6563c9f2a165e36ca5
parent 9762b42627fa0d924082f8d91aa438339f461173
Author: joshiemoore <jxm5210@g.rit.edu>
Date:   Tue,  6 Aug 2019 14:59:45 -0400

ChanPost and ChanThread initialization/getters

Diffstat:
Mbuild.sh | 2+-
Msrc/ChanPost.java | 67+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/ChanThread.java | 68+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Msrc/NodeChan.java | 5+++--
4 files changed, 132 insertions(+), 10 deletions(-)

diff --git a/build.sh b/build.sh @@ -1 +1 @@ -sudo javac -cp lib/WaifUPnP.jar src/NodeChan.java +sudo javac -cp lib/WaifUPnP.jar src/*.java diff --git a/src/ChanPost.java b/src/ChanPost.java @@ -1,11 +1,74 @@ +import java.util.Random; + +/** + * + * This class represents a single post in a NodeChan thread. + * + */ public class ChanPost { + /** The unique tid this post belongs to **/ private String tid; - private boolean isRoot; + + /** The unique id representing this specific post **/ + private String pid; + + /** whether or not this post is the first in its thread **/ + boolean isRoot; + + /** The title of the thread this post belongs to **/ + String title; + + /** The time at which this post was created (in millis) **/ + long postTime; + + /** The text of the post **/ private String text; - public ChanPost(String tid, boolean isRoot, String text) { + public ChanPost(String tid, boolean isRoot, String title, String text) { this.tid = tid; this.isRoot = isRoot; + this.title = title; this.text = text; + + this.postTime = System.currentTimeMillis(); + + // generate random 8-character post ID + byte[] pid_bytes = new byte[8]; + Random rand = new Random(); + + for (int i = 0; i < 8; i++) { + // generate a random ASCII character from 33 to 126 + pid_bytes[i] = (byte)(rand.nextInt(93) + 33); + } + + this.pid = new String(pid_bytes); + } + + + /* + * getters + */ + public String getTid() { + return this.tid; + } + + public String getPid() { + return this.pid; + } + + public boolean getIsRoot() { + return this.isRoot; + } + + public String getTitle() { + return this.title; + } + + public String getText() { + return this.text; + } + + public long getPostTime() { + return this.postTime; } } diff --git a/src/ChanThread.java b/src/ChanThread.java @@ -1,16 +1,74 @@ +import java.util.Random; import java.util.ArrayList; +/** + * + * An entity that represents a single collection of posts or "conversation" on + * NodeChan. + * Despite the unfortunate name, this class has nothing to do with + * multithreading. It's only a collection of posts. + * + * This class is a local collection of the posts in a specific thread that the + * client has received. + * + */ public class ChanThread { + /** Unique thread ID **/ private String tid; + + /** The title of this thread **/ private String title; + + /** Storage for all of the posts that have been made in this thread **/ private ArrayList<ChanPost> posts; - public ChanThread(String tid, ChanPost firstPost, String title) { - this.tid = tid; - this.title = title; + public ChanThread(String tid, ChanPost firstPost) { + // generate random 8-character thread ID + byte[] tid_bytes = new byte[8]; + Random rand = new Random(); + + for (int i = 0; i < 8; i++) { + // generate a random ASCII character from 33 to 126 + tid_bytes[i] = (byte)(rand.nextInt(93) + 33); + } + + this.tid = new String(tid_bytes); - posts = new ArrayList<ChanPost>(); + this.posts = new ArrayList<ChanPost>(); + this.posts.add(firstPost); + + this.title = firstPost.getTitle(); + } + + /** + * Add a post to this thread. + */ + public void addPost(ChanPost post) { + // TODO: sort by post time! + this.posts.add(post); + } + + + /* + * getters + */ + public String tid() { + return this.tid; + } + + public String title() { + return this.title; + } + + public int getNumPosts() { + return this.posts.size(); + } - posts.insert(firstPost); + /** + * We want to avoid giving outside classes direct access to the posts + * ArrayList + */ + public ChanPost getPost(int i) { + return this.posts.get(i); } } diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -1,4 +1,5 @@ import java.util.Scanner; +import java.util.ArrayList; import java.io.BufferedReader; import java.io.InputStreamReader; @@ -49,7 +50,7 @@ public class NodeChan { System.out.println("UPnP port mapping enabled.\n"); } else { // UPnP port mapping failed - System.out.println("UPnP port mapping failed. You will need to " + + System.out.println("UPnP port mapping failed. You may need to " + "manually forward port " + PORT + " to your " + "local IP.\n"); } @@ -60,7 +61,7 @@ public class NodeChan { // client does not have UPnP // the user is either not behind a NAT or they will need to manually // configure port forwarding on their router - System.out.println("UPnP not available. You will need to manually " + + System.out.println("UPnP not available. You may need to manually " + "forward port " + PORT + " to your local IP.\n"); }