Making Neighborhoods
In peer-to-peer networks, each host must search out other hosts. When a host
finds another host, these hosts become neighbors. Often a host will continue
to search for peers until a sufficient number of hosts have been found. Let’s
assume that a host will continue to search for hosts until it has N neighbors.
In order to determine if a host is still “alive,” each host must periodically
send a hello packet to all of its neighbors. If one of a host’s neighbors “disappears”
(i.e., the host has not received a hello packet from the neighbor in a long
time) and number of neighbors of this host is smaller than N, then a
new neighbor search is performed.
In this project, peer-to-peer neighborhoods are made and maintained. Each host
maintains list of neighbors and sends hello packets to these neighbors.
If a host is on the neighbor list, and no hello packet is received from the
host for 40 seconds, then this host is removed from the neighbor list. Hosts
can be in two lists: active neighbor list and semi-active neighbor
list. Also temporary neighbor may hold a host ID. The lists are maintained
as follows.
- Each host sends a hello packet to the hosts listed in its active, semi-active,
and temporary neighbors lists every 10 seconds.
- Contained in the hello packet is a list of the IDs of the recently-heard-neighbors.
Recently-heard-neighbors are the active and semi-active neighbors of the sending
host. A host's ID is its IP address and port. Note that the temporary neighbor
(if there is one) is not included in the list of recently heard neighbors.
- If a host receives a hello packet from a neighbor with its ID listed in
the list of recently-heard-eighbors, then this neighbor is listed as an active
neighbor.
- If a host receives a hello packet from a neighbor and the host is not listed
in the list of recently-heard-eighbors, then this neighbor is put in the list
of semi-active neighbors (if it is not already there).
- If a host does not get a hello packet from an active or semi-active neighbor
with the host's ID listed in the recently heard neighbors for 40 seconds,
then it assumes that it is not connected to the neighbor and removes the neighbor
from the active neighbor and semi-active neighbor lists.
- If the number of active neighbors is less than N and the SearchingForNeighborFlag
is not set, the host randomly searches for a neighbor. Searching for a neighbor
is as follows.
- Set SearchingForNeighborFlag = 1.
- Select a host at random from the list of possible neighbors and putting
this neighbors ID in the list of temporary neighbors.
- Set NeighborSearchTimeFlag to the current time.
- When the time is 40 seconds past NeighborSearchTimeFlag, then the
temporary list of neighbors is cleared and SearchingForNeighborFlag
is set to 0.
- Notes: if a neighbor was successfully found, it would be put into the
active neighbor list. If, after a host is searched for, there are still
less than N host in the active neighbors list, a new host is selected
from the list of possible neighbors.
- Every two minutes save the active neighbors list and the time to disk.
Hello Packet specification, version 1 (if you change the order of these or
change the type (e.g.. int to double), then you will not be compatible with
other people's program)
struct PktStruct {
int Type;
char SenderIP[16];
int SenderPort;
int NumberOfRecentlyHeardNeighbors;
struct RecentNeighborEntry RecentNeighbors[100];
};
where
struct RecentNeighborEntry {
char IP[16];
int Port;
};
What to turn in
- Run the program with N equal to 1, 2, and 4.
- Turn in code, and output lists of active neighbors after the network has
stabilized.
- Collect active neighbor lists from other all other host. With these outputs
determine
- For which N is the graph connected.
- For each N, what is the average degree of the hosts (make a plot).
- For each N, what is the diameter of the largest connected component
(make a plot).
- Draw the graph.
- Turn in a discussion of what the program does (the objective of the program)
and how it works. Include a block diagram / flow diagram. Be sure to name
to the functions that perform the tasks referred to in the discussion.
- Turn in output and demonstrate to the TA the ability of find the correct
number of hosts and find new hosts when some of the active neighbors fail
(i.e., when those programs are ended).
- Extra credit (one point of the course grade for the first part only and
two points for both parts)
- Add the feature that keeps the node from accepting too many neighbors
and redo the graph drawing for N=2, 4, and 7.
- Examine how long it takes the for the network to stabilize when a new
node enters of leaves the network.
Hints:
A .h file for the data types.
A file with a few helper functions
The .h file for the helper function
Initialization
CheckForANewPacket
Function ideas:
void SendHello(
struct HostID Dest,
struct HostID ThisHost,
std::list<struct HostID> &ActiveNeighbors,
std::list<struct HostID> &SemiActiveNeighbors,
SOCKET Sock);
void SendHellosIfNeeded(
std::list<struct HostID> &ActiveNeighbors,
std::list<struct HostID> &SemiActiveNeighbors,
struct HostID ThisHost,
SOCKET Sock);
void CheckIfNeighborsHaveSentHello(std::list<struct HostID> &Neighbors);
int CheckForNewPacket(
SOCKET Sock,
char *pkt,
int TimeOut);
struct HostID GetSendersID(struct PktStruct *pkt);
void SetNeighborRecTime(struct HostID HID, std::list<struct HostID> &Neighbors);
void AddNodeToList(struct HostID HID, std::list<struct HostID> &List);
int CheckIfThisHostIsInRecentlyHeardNeighbors(struct PktStruct *pkt, struct HostID ThisHost);
void MoveHostFromSemiActiveToActive(
struct HostID SenderID,
std::list<struct HostID> &ActiveNeighbors,
std::list<struct HostID> &SemiActiveNeighbors);