I've never really done network programming so this is all new to me. What I'm attempting to make is a Peer to Peer "chat" client. The idea is that anyone can connect to someone whose IP they know. All chat messages are to be distributed to everyone.
What I'm getting stuck on is the peer discovery. What I've done so far is:
- App has an IP of a known person (user provided)
- Connects to known IP
- Known IP provides it's list of known Peers; we store it in
peer list
- Discovery loop:
- Make a local copy of
peer list
- For each Peer in
local peer list
- Request Peer list (i.e. step 2 + 3)
- If error, remove this peer from original
peer list
- Otherwise, add new peers to original
peer list
- If error, remove this peer from original
- Request Peer list (i.e. step 2 + 3)
- Queue another Discovery Loop in 1min
- Make a local copy of
The listening for connections in step 3 and the discovery loop are asynchronous.
The reason a copy is made in the first place is due to some other design decisions for down the road.
The problem is that if I introduce a client with a bad peer on it's peer list
, i.e. you can't connect to it, what happens is that this Peer is passed via step 3 when serving the known peer list
and then other clients have it in theirs. They propagate this bad peer and later on, during the Discover Loop, remove that peer. But then, they will do another Discover Loop and will receive that Peer from someone else again.
The end result is that bad peer never leaves the network.
One option I can think of is a vague idea of having Peer "health" that each client keeps track of separately so that they can ignore peers provided on someone elses list. However, this would, I think, need to be somewhat "timed" so that when someone disconnects they aren't shut out of the network forever.
The other idea I had was for a broadcast message to all known peers when a bad peer is encountered. However, I didn't like this idea because it leaves it open for someone to make a different client that's job is to poison the network by insisting peers are bad. This would then require me to have peers check that a remove request is genuine by also attempting to connect to his peer immediately.
I'm looking for opinions on the two options above.