Download presentation
Presentation is loading. Please wait.
1
In peer-to-peer networks such as gnutella, 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. Lets assume that a host will continue to search for hosts until it has N neighbors. In this project, peer-to-peer neighborhoods are made and maintained. Each host maintains list of neighbors and sends hello packets to these neighbors every 10 seconds. 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. If a node does not have enough neighbors, then it selects an address (e.g., IP and port) at random and tries to become its neighbor.
2
Objectives Find N neighbors Maintain N neighbors
A node is a neighbor if two-way communication is possible and verified Two-communication == bidirectional link Maintain N neighbors If two-way communication is no longer verified, the node is no longer a neighbor
3
Detecting Bidirectional Links
Node A has a bidirectional with node B if Node A can hear node B Node B can hear node A To determine if a link between node A and B is bidirectional Node A sends a message to node B Node B sends a message to node A saying that it can hear node A Now node A believes the link is bidirectional Node A sends a message to node B saying that it can hear node B Now node B believes the link is bidirectional In general, to determine is links are bidirectional Send hello messages where the message includes a list of all nodes that have been heard Upon receiving a hello message, If you are listed as one of the nodes that has been recently heard, then the link is bidirectional Add the sender of the hello to the list of nodes that you can hear
4
I am A I have heard: no one A B Have heard list Have heard list
5
I am A I have heard: no one A B Have heard list Have heard list A
6
I am B I have heard: A A B Have heard list Have heard list A
7
A B A I am B I have heard: A Have heard list Have heard list
B and B hears me A
8
A B A I am A I have heard: B Have heard list Have heard list
B and B hears me A
9
A B I am A I have heard: B Have heard list Have heard list
B and B hears me A and A hears me
10
Neighbor States nothing one-way (receivable) bidirectional
Received a hello and this node is not listed in the hello as a recently heard node No hello received for a long time Received a hello with this node listed as recently heard one-way (receivable) Received a hello with this node listed as recently heard Received a hello and this node is not listed as recently heard No hello received for a long time bidirectional
11
Neighbor States nothing semiActive active
Received a hello and this node is not listed as recently heard No hello received for a long time Received a hello with this node listed as recently heard semiActive Received a hello with this node listed as recently heard Received a hello and this node is not listed as recently heard active No hello received for a long time
12
Neighbor List Activity Diagram
activeNeighbors – list of nodes in active state semiActiveNeighbors – list of nodes in semi-active state waiting Hello arrived from node B Node B is semiActiveNeighbors Node B is activeNeighbors Node B is neither list This node is listed in the hello as a recently heard node Move B from semiActiveNeighbors to ActiveNeighbors Move B from ActiveNeighbors to semiActiveNeighbors This node is NOT listed in the hello as a recently heard node Put B into semiActiveNeighbors Put B into activeNeighbors
13
Lists C++ Standard template library list
Add the following to the top of cpp file #include <list> using namespace std; E.g., list of ints making a list of ints list<int> myList; Add an element to myList myList.push_back(1); myList.push_back(2); Iterate through the list and print each element for( list<int>::iterator it=myList.begin(); it!=myList.end(); ++it) printf(“entry=%d\n”,*it); Remove an element from the list int elementToRemove = 2; { if (*it == elementToRemove) myList.erase(it); break; // not breaking would result in a crash } Alternatively myList.remove(1); // removes all elements == 1. But this requires == operator, which exists for int, but might not for other types
14
for( list<int>::iterator it=myList. begin(); it. =myList
for( list<int>::iterator it=myList.begin(); it!=myList.end(); ++it) printf(“entry=%d\n”,*it); Boost foreach.hpp foreach( int x, mylist) printf(“entry=%d\n”,x);
15
Structures struct HostID { char ip[16]; unsigned int port;
unsigned int lastTimeHelloRec; }; struct PktStruct { int type; char senderIP[16]; unsigned int senderPort; int numberOfRecentlyHeardNeighbors; struct RecentNeighborEntry recentNeighbors[100]; struct RecentNeighborEntry {
16
List struct HostID { char ip[16]; unsigned int port;
unsigned int lastTimeHelloRec; }; bool checkIfInList(HostID &hid, list<HostID> &L) { list<struct HostID>::iterator it; for (it=listToCheck.begin(); it!=listToCheck.end(); ++it) if (it->ip == hid.ip && it->port == hid.port) return true; return false; }
17
List struct HostID { char ip[16]; unsigned int port;
unsigned int lastTimeHelloRec; }; bool checkIfInList(HostID &hid, list<HostID> &L) { std::list<struct HostID>::iterator it; for (it=listToCheck.begin(); it!=listToCheck.end(); ++it) if (strcmp(it->ip,hid.ip)==0 && it->port == hid.port) return true; return false; }
18
Neighbor List Activity Diagram
activeNeighbors – list of nodes in active state semiActiveNeighbors – list of nodes in semi-active state waiting Hello arrived from node B This node is listed in the hello as a recently heard node Node B is neither list Put B into activeNeighbors Node B is semiActiveNeighbors This node is NOT listed in the hello as a recently heard node Node B is activeNeighbors Put B into semiActiveNeighbors This node is NOT listed in the hello as a recently heard node This node is listed in the hello as a recently heard node Move B from semiActiveNeighbors to ActiveNeighbors Move B from ActiveNeighbors to semiActiveNeighbors
19
Neighbor List Activity Diagram
activeNeighbors – list of nodes in active state semiActiveNeighbors – list of nodes in semi-active state waiting Hello arrived from node B This node is listed in the hello as a recently heard node else Put B into activeNeighbors checkIfInList(sender, semiActiveNeighbors) ==true checkIfInList(sender, activeNeighbors) ==true This node is NOT listed in the hello as a recently heard node Put B into semiActiveNeighbors This node is NOT listed in the hello as a recently heard node This node is listed in the hello as a recently heard node Move B from semiActiveNeighbors to ActiveNeighbors Move B from ActiveNeighbors to semiActiveNeighbors
20
Neighbor List Activity Diagram
activeNeighbors – list of nodes in active state semiActiveNeighbors – list of nodes in semi-active state int checkForNewPacket(SOCKET UDPSock, char *pkt, int TimeOut) else Hello arrived Extract sender from hello This node is listed in the hello as a recently heard node else Put B into activeNeighbors checkIfInList(sender, semiActiveNeighbors) ==true checkIfInList(sender, activeNeighbors) ==true This node is NOT listed in the hello as a recently heard node Put B into semiActiveNeighbors This node is NOT listed in the hello as a recently heard node This node is listed in the hello as a recently heard node Move B from semiActiveNeighbors to ActiveNeighbors Move B from ActiveNeighbors to semiActiveNeighbors
21
Receiving a Packet PktStruct pkt;
struct PktStruct { int type; char senderIP[16]; unsigned int senderPort; int numberOfRecentlyHeardNeighbors; struct RecentNeighborEntry recentNeighbors[100]; }; Receiving a Packet PktStruct pkt; int ret = checkForNewPacket(UDPSock, (char *)&pkt, 2); // time out of 2 seconds The pkt object is a string or chunk of bytes &pkt is the pointer to pkt (char *)&pkt treats this pointer as a string of bytes
22
Extract Sender from Hello Message
PktStruct pkt; int ret = checkForNewPacket(UDPSock, (char *)&pkt, 2); HostID sender; sender.ip = pkt.senderIP; sender.port = pkt.senderPort; struct HostID { char ip[16]; unsigned int port; unsigned int lastTimeHelloRec; }; struct PktStruct { int type; char senderIP[16]; unsigned int senderPort; int numberOfRecentlyHeardNeighbors; struct RecentNeighborEntry recentNeighbors[100];
23
Extract Sender from Hello Message
PktStruct pkt; int ret = checkForNewPacket(UDPSock, (char *)&pkt, 2); HostID sender; strcpy(sender.ip , pkt.senderIP); sender.port = pkt.senderPort; struct HostID { char ip[16]; unsigned int port; unsigned int lastTimeHelloRec; }; struct PktStruct { int type; char senderIP[16]; unsigned int senderPort; int numberOfRecentlyHeardNeighbors; struct RecentNeighborEntry recentNeighbors[100];
24
Neighbor List Activity Diagram
activeNeighbors – list of nodes in active state semiActiveNeighbors – list of nodes in semi-active state int checkForNewPacket(SOCKET UDPSock, char *pkt, int TimeOut) else Hello arrived Extract sender from hello This node is listed in the hello as a recently heard node else Put B into activeNeighbors checkIfInList(sender, semiActiveNeighbors) ==true checkIfInList(sender, activeNeighbors) ==true This node is NOT listed in the hello as a recently heard node Put B into semiActiveNeighbors This node is NOT listed in the hello as a recently heard node This node is listed in the hello as a recently heard node Move B from semiActiveNeighbors to ActiveNeighbors Move B from ActiveNeighbors to semiActiveNeighbors
25
thisHost int main(int argc, char* argv[]) { HostID thisHost;
fillThisHostIP(thisHost); // provided in helper.cpp thisHost.port = atoi(argv[1]); …
26
Neighbor List Activity Diagram
activeNeighbors – list of nodes in active state semiActiveNeighbors – list of nodes in semi-active state int checkForNewPacket(SOCKET UDPSock, char *pkt, int TimeOut) else Hello arrived Extract sender from hello This node is listed in recently heard nodes else Put B into activeNeighbors checkIfInList(sender, semiActiveNeighbors) ==true checkIfInList(sender, activeNeighbors) ==true This node is NOT listed in recently heard nodes Put B into semiActiveNeighbors checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost)==true checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost) ==false Move B from semiActiveNeighbors to ActiveNeighbors Move B from ActiveNeighbors to semiActiveNeighbors
27
checkIfThisHostIsInRecentlyHeardNeighbors
struct HostID { char ip[16]; unsigned int port; unsigned int lastTimeHelloRec; }; struct PktStruct { int type; char senderIP[16]; unsigned int senderPort; int numberOfRecentlyHeardNeighbors; struct RecentNeighborEntry recentNeighbors[100]; struct RecentNeighborEntry { for (int i=0; i<pkt.numberOfRecentlyHeardNeighbors; i++) { if (pkt.recentNeighbors[i] == thisHost) return true; } return false;
28
Neighbor List Activity Diagram
activeNeighbors – list of nodes in active state semiActiveNeighbors – list of nodes in semi-active state int checkForNewPacket(SOCKET UDPSock, char *pkt, int TimeOut) else Hello arrived Extract sender from hello checkIfThisHostIsInRecently HeardNeighbors(pkt, thisHost)==true else Put sender into activeNeighbors checkIfInList(sender, semiActiveNeighbors) ==true checkIfInList(sender, activeNeighbors) ==true checkIfThisHostIsInRecently HeardNeighbors(pkt, thisHost)==false Put sender into semiActiveNeighbors checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost)==true checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost) ==false Move sender from semiActiveNeighbors to ActiveNeighbors Move sender from ActiveNeighbors to semiActiveNeighbors
29
Neighbor List Activity Diagram
activeNeighbors – list of nodes in active state semiActiveNeighbors – list of nodes in semi-active state int checkForNewPacket(SOCKET UDPSock, char *pkt, int TimeOut) else Hello arrived Extract sender from hello checkIfThisHostIsInRecently HeardNeighbors(pkt, thisHost)==true else Put sender into activeNeighbors checkIfInList(sender, semiActiveNeighbors) ==true checkIfInList(sender, activeNeighbors) ==true checkIfThisHostIsInRecently HeardNeighbors(pkt, thisHost)==false Put sender into semiActiveNeighbors checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost)==true checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost) ==false Move sender from semiActiveNeighbors to ActiveNeighbors Move sender from ActiveNeighbors to semiActiveNeighbors
30
Lists C++ Standard template library list Making a list of ints
#include <list> using namespace std; Making a list of ints list<int> myList; Add an element to myList myList.push_back(1); myList.push_back(2); Iterate through the list for( list<int>::iterator it=myList.begin(); it!=myList.end(); ++it) printf(“entry=%d\n”,*it); Remove an element from the list int elementToRemove = 2; for( list<int> iterator::it=myList.begin(); it!=myList.end(); ++it) { if (*it == elementToRemove) myList.erase(it); break; // not breaking would result in a crash } Alternatively myList.remove(1); // removes all elements == 1. But this requires == operator, which exists for int, but might not for other types
31
Objectives Find N neighbors Maintain N neighbors
A node is a neighbor if two-way communication is possible and verified Two-communication == bidirectional link Maintain N neighbors If two-way communication is no longer verified, the node is no longer a neighbor
32
To do While (1) Receive hello Search for more neighbors?
Process neighbor states (sort of like what we did) Search for more neighbors? Check and if so, search Update neighbor set If some neighbors have timed out, remove them Send hello messages to neighbors Send every 10 sec
33
To do While (1) Receive hello Search for more neighbors?
Process neighbor states (sort of like what we did) Search for more neighbors? Check and if so, search Update neighbor set If some neighbors have timed out, remove them Send hello messages to neighbors Send every 10 sec
34
Search for more neighbors
int main(int argc, char* argv[]) { bool searchingForNeighborFlag = false; readAllHostsList(argv[2], allHosts); // provided while (1) if (activeNeighbors.size() + semiActiveNeighbors.size() < DESIRED_NUMBER_OF_NEIGHBORS && searchingForNeighborFlag ==false) searchingForNeighborFlag = true; tempNeighbor = selectNeighborAtRandom(activeNeighbors, semiActiveNeighbors, allHost, thisHost); // provided } ….
35
To do While (1) Receive hello Search for more neighbors?
Process neighbor states (sort of like what we did) Search for more neighbors? Check and if so, search Update neighbor set If some neighbors have timed out, remove them Send hello messages to neighbors Send every 10 sec
36
Process Hello Put sender into activeNeighbors
int checkForNewPacket(SOCKET UDPSock, char *pkt, int TimeOut) checkIfThisHostIsInRecently HeardNeighbors(pkt, thisHost)==true Put sender into activeNeighbors else checkIfThisHostIsInRecently HeardNeighbors(pkt, thisHost)==false Hello arrived Extract sender from hello Put sender into semiActiveNeighbors else checkIfInList(sender, semiActiveNeighbors) ==true checkIfInList(sender, activeNeighbors) ==true sender==tempNeighbor searchingForNeighborFlag = false checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost)==true checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost) ==false checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost)==true checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost) ==false Move sender from semiActiveNeighbors to ActiveNeighbors Move sender from ActiveNeighbors to semiActiveNeighbors Put sender into activeNeighbors Put sender into semiActiveNeighbors
37
To do While (1) Receive hello Search for more neighbors?
Process neighbor states (sort of like what we did) Search for more neighbors? Check and if so, search Update neighbor set If some neighbors have timed out, remove them Send hello messages to neighbors Send every 10 sec
38
Process Hello updateLastReceivedTime(sender, activeNeighbors)
int checkForNewPacket(SOCKET UDPSock, char *pkt, int TimeOut) checkIfThisHostIsInRecently HeardNeighbors(pkt, thisHost)==true Put sender into activeNeighbors else checkIfThisHostIsInRecently HeardNeighbors(pkt, thisHost)==false Hello arrived else Extract sender from hello Put sender into semiActiveNeighbors updateLastReceivedTime(sender, semiActiveNeighbors) checkIfInList(sender, semiActiveNeighbors) ==true checkIfInList(sender, activeNeighbors) ==true sender==tempNeighbor searchingForNeighborFlag = false checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost)==true checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost) ==false checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost)==true checkIfThisHostIsInRecently HeardNeighbors(pkt,thisHost) ==false Move sender from semiActiveNeighbors to ActiveNeighbors Move sender from ActiveNeighbors to semiActiveNeighbors Put sender into activeNeighbors Put sender into semiActiveNeighbors updateLastReceivedTime(sender, activeNeighbors) updateLastReceivedTime(sender, semiActiveNeighbors) updateLastReceivedTime(sender, activeNeighbors) updateLastReceivedTime(sender, semiActiveNeighbors)
39
Lists C++ Standard template library list #include <list>
struct HostID { char ip[16]; unsigned int port; unsigned int lastTimeHelloRec; }; struct PktStruct { int type; char senderIP[16]; unsigned int senderPort; int numberOfRecentlyHeardNeighbors; struct RecentNeighborEntry recentNeighbors[100]; struct RecentNeighborEntry { C++ Standard template library list #include <list> using namespace std; Making a list of ints list<int> myList; Add an element to myList myList.push_back(1); myList.push_back(2); Iterate through the list for( list<int>::iterator it=myList.begin(); it!=myList.end(); ++it) printf(“entry=%d\n”,*it); Remove an element from the list int elementToRemove = 2; { if (*it == elementToRemove) myList.erase(it); break; // not breaking would result in a crash } Alternatively myList.remove(1); // removes all elements == 1. But this requires == operator, which exists for int, but might not for other types
40
To do While (1) Receive hello Search for more neighbors?
Process neighbor states (sort of like what we did) Search for more neighbors? Check and if so, search Update neighbor set If some neighbors have timed out, remove them Send hello messages to neighbors Send every 10 sec
41
Send hello messages to neighbors every 10 sec
#include <time.h> #include <sys/types.h> #include <sys/timeb.h> int main(int argc, char* argv[]) { …. struct _timeb lastTimeHellosWereSent; _ftime_s( &lastTimeHellosWereSent ); struct _timeb currentTime; while (1) _ftime_s( ¤tTime ); if (currentTime.time > lastTimeHellosWereSent.time + 10) lastTimeHellosWereSent = currentTime; sendHellos(…) }
42
Send hello messages to neighbors every 10 sec
#include <time.h> #include <sys/types.h> #include <sys/timeb.h> int main(int argc, char* argv[]) { …. struct _timeb lastTimeHellosWereSent; lastTimeHellosWereSent.time = 0; _ftime_s( &lastTimeHellosWereSent ); struct _timeb currentTime; while (1) _ftime_s( ¤tTime ); if (currentTime.time > lastTimeHellosWereSent.time + 10) lastTimeHellosWereSent = currentTime; sendHellos(…) }
43
sendHellos(…) sendHelloToANeighbor
struct HostID { char ip[16]; unsigned int port; unsigned int lastTimeHelloRec; }; struct PktStruct { int type; char senderIP[16]; unsigned int senderPort; int numberOfRecentlyHeardNeighbors; struct RecentNeighborEntry recentNeighbors[100]; struct RecentNeighborEntry { sendHelloToANeighbor Hello must include all heard neighbors activeNeighbors semiActiveNeighbors Not tempNeighbor void sendHelloToNeighbor( SOCKET UDPSock, HostID destination, list<HostID> &activeNeighbors, list<HostID> &semiActiveNeighbors, HostID &thisHost); Hello must be sent to AND tempNeighbor for(it= activeNeighbors.begin(); it!=activeNeighbors.end() ++it) { sendHelloToNeighbor(UDPSock, *it, activeNeighbors, semiActiveNeighbors, thisHost); } ….
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.