nodechan

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

commit ef80b0e9a0958a8f4b5145369268355c6454f56c
parent dc98a3c5347676b81b1741dd360d9a3ae3c36379
Author: joshiemoore <jxm5210@g.rit.edu>
Date:   Fri,  9 Aug 2019 01:28:13 -0400

Add peer-timeout check to remove dead peers

Diffstat:
Msrc/NodeChan.java | 20+++++++++++++++++++-
1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/src/NodeChan.java b/src/NodeChan.java @@ -26,7 +26,8 @@ public class NodeChan { /** The port the application will use to connect. **/ public static final int NC_PORT = 13370; - /** Max time to keep a peer alive without hearing from it (seconds) **/ + /** Max time to keep a peer alive without hearing from it (seconds) + Set to 0 to keep peers alive indefinitely **/ public static final int PEER_TIMEOUT = 300; /** Max number of times each client will propagate a single post **/ @@ -177,6 +178,8 @@ public class NodeChan { if (nogui) { // command-line mode while(true) { + checkPeerTimeouts(); + System.out.print("> "); input = scan.nextLine(); @@ -482,4 +485,19 @@ public class NodeChan { // send the hello-packet to the peer new OutgoingThread(p.getAddress(), NC_PORT, hello).start(); } + + /** + * Check for peers that are over the timeout limit and remove them. + */ + public static void checkPeerTimeouts() { + if (PEER_TIMEOUT == 0) return; + + for (int i = 0; i < peers.size(); i++) { + if (System.currentTimeMillis() - peers.get(i).getLastHeard() > PEER_TIMEOUT * 1000) { + peers.remove(i); + + if (peers.size() > 0) i--; + } + } + } }