nodechan

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

commit 498c7faac16c35d7dc0c00af2d549d6020c98ed1
parent a8adf2326944c04ff9ce1866eeed9260d6f5767f
Author: joshiemoore <jxm5210@g.rit.edu>
Date:   Fri,  9 Aug 2019 00:38:06 -0400

Add hello-packet functionality

Diffstat:
Mformat.txt | 7+++++++
Msrc/IncomingThread.java | 3+++
Msrc/NodeChan.java | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 61 insertions(+), 0 deletions(-)

diff --git a/format.txt b/format.txt @@ -24,3 +24,10 @@ BYTE 16-23 - post ID BYTE 24-73 - title of the thread the post belongs to BYTE 74-329 - post text + + + +Layout of a hello-packet ('H'): +Hello-packets contain no data beyond the header bytes. These packets are only +used for a client to add itself to one of its peers' peer list, or for the +client to keep itself alive in its peers' peer list. diff --git a/src/IncomingThread.java b/src/IncomingThread.java @@ -147,6 +147,9 @@ public class IncomingThread extends Thread { post.received(); break; + case 'H': + // do nothing, the hello-packet is just for adding new peers + break; } } } diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -43,6 +43,10 @@ public class NodeChan { (no tracker or peers outside of LAN) **/ public static boolean local = false; + /** If true, do not send hello-packets to new peers. + This will cause new peers to not see this client until a message travels + from this client to theirs. **/ + public static boolean nohello = false; @@ -286,6 +290,17 @@ public class NodeChan { } else { System.out.println("Could not get peer from tracker."); } + } else if (input.equals("hello")) { + // keep ourselves alive in the peer lists of our peers + if (nohello) { + System.out.println("Cannot send hello-packets when the -nohello " + + "flag is set."); + continue; + } else { + for (Peer p : peers) { + sendHelloPacket(p); + } + } } else { System.out.println("Command not recognized."); } @@ -409,6 +424,42 @@ public class NodeChan { // add the retrieved peer to our peer list peers.add(retrieved); + + // send a hello-packet to the new peer + sendHelloPacket(retrieved); + return true; } + + /** + * Send a hello-packet in order to add ourselves to a peer's own peer list, + * or to keep ourselves alive in their peer list + */ + public static void sendHelloPacket(Peer p) { + // do not send hello-packets if the -hohello option is specified + if (nohello) return; + + byte[] hello = new byte[8]; + + // header bytes + hello[0] = 'N'; + hello[1] = 'C'; + + // post type + hello[2] = 'H'; + + // flags + // TODO: request threads from peer + hello[3] = 0; + + // this node's IP + byte[] node_addr_bytes = node_ip.getAddress(); + + for (int i = 0; i < 4; i++) { + hello[i + 4] = node_addr_bytes[i]; + } + + // send the hello-packet to the peer + new OutgoingThread(p.getAddress(), NC_PORT, hello); + } }