nodechan

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

commit a85758c901238787574bdcb059bc91481c452c44
parent 5ddbea6dd580ebedcf7d1adaf9b4be55f497169e
Author: joshiemoore <jxm5210@g.rit.edu>
Date:   Tue,  6 Aug 2019 23:49:02 -0400

Add IncomingThread, basic handling of incoming packets

Diffstat:
Asrc/IncomingThread.java | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Msrc/NodeChan.java | 10++++++++++
Msrc/OutgoingThread.java | 2+-
3 files changed, 66 insertions(+), 1 deletion(-)

diff --git a/src/IncomingThread.java b/src/IncomingThread.java @@ -0,0 +1,55 @@ +import java.net.DatagramSocket; +import java.net.DatagramPacket; + +import java.util.ArrayList; + +import java.io.IOException; + +/** + * This class handles all incoming NodeChan packet traffic, and routes the + * incoming data as necessary. + */ +public class IncomingThread extends Thread { + /** The socket we're receiving UDP through **/ + DatagramSocket sock; + + /** The local ChanThread storage **/ + ArrayList<ChanThread> threads; + + /** The local list of peers **/ + ArrayList<Peer> peers; + + public IncomingThread(DatagramSocket sock, ArrayList<ChanThread> threads, ArrayList<Peer> peers) { + this.sock = sock; + this.threads = threads; + this.peers = peers; + } + + public void run() { + // handle incoming packets indefinitely + while (true) { + byte[] recv_data = new byte[326]; + + DatagramPacket receivePacket = new DatagramPacket(recv_data, recv_data.length); + + try { + sock.receive(receivePacket); + } catch (IOException e) { + continue; + } + + // check header + if (recv_data[0] != 'N' || recv_data[1] != 'C') continue; + + switch(recv_data[2]) { + case 'P': + // decode the post packet + ChanPost post = ChanPost.decodeUDP(recv_data); + + // TODO: actually handle posts + System.out.println(post.getText()); + break; + } + } + } +} diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -38,9 +38,15 @@ public class NodeChan { /** UDP socket to send packets to peers with **/ private static DatagramSocket nc_socket; + /** Incoming packet-handling thread **/ + private static IncomingThread nc_incoming; + /** List of this node's peers **/ private static ArrayList<Peer> peers; + /** Local list of ChanThreads this user has received **/ + private static ArrayList<ChanThread> threads; + public static void main(String[] args) { System.out.println("Welcome to NodeChan."); @@ -93,6 +99,10 @@ public class NodeChan { return; } + // initialize incoming packet-handling thread + nc_incoming = new IncomingThread(nc_socket, threads, peers); + nc_incoming.start(); + System.out.println("Enter peer IP to connect directly,\nleave blank to" + " connect via the peer tracker: "); diff --git a/src/OutgoingThread.java b/src/OutgoingThread.java @@ -33,7 +33,7 @@ public class OutgoingThread extends Thread { try { sock.send(outPacket); } catch (IOException e) { - // do nothing, we don't care too heavily about every individual packet + System.err.println(e.getLocalizedMessage()); } } }