nodechan

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

commit ce82a825aed9aa230b1f5255f926ae0398601762
parent ac2345b701552e31e78aec6a2c83c7d5bcb111f9
Author: joshiemoore <jxm5210@g.rit.edu>
Date:   Wed,  7 Aug 2019 00:52:44 -0400

Basic command line interface, code for sending new thread to peers

Diffstat:
Msrc/ChanThread.java | 35+++++++++++++++++++++--------------
Msrc/NodeChan.java | 59++++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Msrc/OutgoingThread.java | 10++++++++--
3 files changed, 85 insertions(+), 19 deletions(-)

diff --git a/src/ChanThread.java b/src/ChanThread.java @@ -22,22 +22,24 @@ public class ChanThread { /** Storage for all of the posts that have been made in this thread **/ private ArrayList<ChanPost> posts; - public ChanThread(String tid, ChanPost firstPost) { - // generate random 8-character thread ID - byte[] tid_bytes = new byte[8]; - Random rand = new Random(); + public ChanThread(String tid) { + if (tid.equals("")) { + // 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 48 to 122 - tid_bytes[i] = (byte)(rand.nextInt(74) + 48); - } + for (int i = 0; i < 8; i++) { + // generate a random ASCII character from 48 to 122 + tid_bytes[i] = (byte)(rand.nextInt(74) + 48); + } - this.tid = new String(tid_bytes); + this.tid = new String(tid_bytes); + } else { + this.tid = tid; + } this.posts = new ArrayList<ChanPost>(); - this.posts.add(firstPost); - - this.title = firstPost.getTitle(); + this.title = ""; } /** @@ -46,17 +48,22 @@ public class ChanThread { public void addPost(ChanPost post) { // TODO: sort by post time! this.posts.add(post); + + if (posts.size() == 1 && post.getIsRoot()) { + // this is the first post in the thread + this.title = post.getTitle(); + } } /* * getters */ - public String tid() { + public String getTid() { return this.tid; } - public String title() { + public String getTitle() { return this.title; } diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -33,7 +33,7 @@ public class NodeChan { // command-line options /** If true, run in console mode **/ - private static boolean nogui = false; + public static boolean nogui = false; @@ -66,6 +66,7 @@ public class NodeChan { System.out.println("Welcome to NodeChan."); peers = new ArrayList<Peer>(); + threads = new ArrayList<ChanThread>(); // get the local ip address try { @@ -111,6 +112,7 @@ public class NodeChan { nc_socket = new DatagramSocket(NC_PORT); } catch (SocketException e) { System.err.println("Failed to establish UDP socket, quitting."); + System.err.println(e.getLocalizedMessage()); return; } @@ -135,8 +137,7 @@ public class NodeChan { System.out.println(""); if (first_peer_ip.equals("nopeer") || first_peer_ip.equals("ptfail")) { - System.out.println("No peer available from tracker. Waiting for " + - "connections...\n"); + System.out.println("No peer available from tracker.\n"); } else { Peer firstPeer = new Peer(first_peer_ip); @@ -145,6 +146,30 @@ public class NodeChan { peers.add(firstPeer); } } + + if (nogui) { + // command-line mode + while(true) { + System.out.print("> "); + input = scan.nextLine(); + + if (input.equals("newthread")) { + // create a new thread and send it to peers + String title; + String text; + + System.out.println("\nTHREAD TITLE:"); + title = scan.nextLine(); + System.out.println("\nTHREAD TEXT:"); + text = scan.nextLine(); + + createThreadAndSend(title, text); + } + } + } else { + // TODO: implement gui... + System.out.println("GUI not implemented yet. Run with option -nogui."); + } } /** @@ -178,4 +203,32 @@ public class NodeChan { } } } + + /** + * Create a new thread based on title and text, and send it. + */ + public static void createThreadAndSend(String title, String text) { + ChanThread newThread = new ChanThread(""); + + ChanPost newPost = new ChanPost( + newThread.getTid(), + "", + true, + title, + text + ); + + newThread.addPost(newPost); + + // add thread to local thread storage + threads.add(newThread); + + // 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(); + } + } } diff --git a/src/OutgoingThread.java b/src/OutgoingThread.java @@ -1,5 +1,6 @@ import java.net.DatagramSocket; import java.net.DatagramPacket; +import java.net.SocketException; import java.net.InetAddress; import java.io.IOException; @@ -20,8 +21,13 @@ public class OutgoingThread extends Thread { /** The data packet to send **/ private byte[] outbytes; - public OutgoingThread(DatagramSocket sock, InetAddress recip, int port, byte[] outbytes) { - this.sock = sock; + public OutgoingThread(InetAddress recip, int port, byte[] outbytes) { + try { + this.sock = new DatagramSocket(); + } catch (SocketException e) { + + } + this.recip = recip; this.port = port; this.outbytes = outbytes;