nodechan

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

commit db980457ada15a906d3f8b1fa55f792d6b2eb2f5
parent 517122fbf01d8034673ba68b0a4f0c5775a84396
Author: joshiemoore <jxm5210@g.rit.edu>
Date:   Tue,  6 Aug 2019 15:46:34 -0400

Add initial peer on startup

Diffstat:
Msrc/NodeChan.java | 36+++++++++++++++++++++++++++++++++++-
Msrc/Peer.java | 23+++++++++++++++++++----
2 files changed, 54 insertions(+), 5 deletions(-)

diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -19,7 +19,14 @@ import com.dosse.upnp.UPnP; */ public class NodeChan { /** The port the application will use to connect. **/ - public static int PORT = 13370; + public static final int PORT = 13370; + + /** Max time to keep a peer alive without hearing from it (seconds) **/ + public static final int PEER_TIMEOUT = 300; + + + + /** The IP address of this NodeChan node. **/ private static String node_ip; @@ -27,9 +34,14 @@ public class NodeChan { /** The IP address of the first peer to connect to. **/ private static String first_peer_ip; + /** List of this node's peers **/ + private static ArrayList<Peer> peers; + public static void main(String[] args) { System.out.println("Welcome to NodeChan."); + peers = new ArrayList<Peer>(); + // get the local ip address try { URL whatis = new URL("http://bot.whatismyipaddress.com"); @@ -83,9 +95,18 @@ public class NodeChan { first_peer_ip = input; } + System.out.println(""); + if (first_peer_ip.equals("nopeer")) { System.out.println("No peer available from tracker. Waiting for " + "connections...\n"); + } else { + Peer firstPeer = new Peer(first_peer_ip); + + // verify that the peer has a valid address, then add it to the peer list + if (firstPeer.isResolved()) { + peers.add(firstPeer); + } } } @@ -108,4 +129,17 @@ public class NodeChan { return "ptfail"; } } + + /** + * Check the list of peers and remove all that have timed out. + */ + public static void checkPeers() { + for (int i = 0; i < peers.size(); i++) { + if (System.currentTimeMillis() - peers.get(i).getLastHeard() > + (PEER_TIMEOUT * 1000)) { + peers.remove(i); + i--; + } + } + } } diff --git a/src/Peer.java b/src/Peer.java @@ -1,4 +1,5 @@ import java.net.InetAddress; +import java.net.UnknownHostException; public class Peer { /** This peer's address information **/ @@ -7,16 +8,26 @@ public class Peer { /** The time this peer was last heard **/ private long lastHeard; - public Peer(String ip, long time) { - this.addr = InetAddress.getByName(ip); - this.time = System.currentTimeMillis(); + /** Whether or not we have resolved this peer's address **/ + private boolean resolved; + + public Peer(String ip) { + this.lastHeard = System.currentTimeMillis(); + + try { + this.addr = InetAddress.getByName(ip); + + this.resolved = true; + } catch (UnknownHostException e) { + this.resolved = false; + } } /** * We just heard from this peer, update their time */ public void heard() { - this.time = System.currentTimeMillis(); + this.lastHeard = System.currentTimeMillis(); } /* @@ -29,4 +40,8 @@ public class Peer { public long getLastHeard() { return this.lastHeard; } + + public boolean isResolved() { + return this.resolved; + } }