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.

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

 

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);