M1-nw-project/neighbours.cpp

274 lines
6.4 KiB
C++
Raw Normal View History

/***************************************************************************
* By Théophile Bastian, 2017
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
* License: WTFPL v2 <http://www.wtfpl.net/>
**************************************************************************/
#include "neighbours.h"
#include "nw_constants.h"
#include <cstdlib>
using namespace std;
2016-11-26 16:20:20 +01:00
Neighbours::Neighbours(Protocol* proto, DataStore* dataStore) :
proto(proto), dataStore(dataStore), lastPeerPeek(0), lastSentNR(0)
{}
2016-11-26 19:17:56 +01:00
Neighbours::~Neighbours() {
for(auto fl : dataFlooder) {
delete fl.second;
}
}
void Neighbours::fullCheck() {
for(auto nei : neiType) {
switch(nei.second) {
case NEI_SYM:
if(time(NULL) - lastRecv[nei.first] > csts::TIMEOUT_SYM_RECV
|| (time(NULL) - lastIHU[nei.first]
> csts::TIMEOUT_SYM_IHU))
{
changeNeiType(nei.first, NEI_POTENTIAL);
}
break;
case NEI_UNIDIR:
if(time(NULL) - lastRecv[nei.first] > csts::TIMEOUT_UNIDIR)
changeNeiType(nei.first, NEI_POTENTIAL);
break;
default:
break;
}
}
}
void Neighbours::fullUpdate() {
fullCheck();
for(auto nei : unidirNei)
2016-11-25 15:47:35 +01:00
updateSendPackets(nei);
for(auto nei : symNei)
2016-11-25 15:47:35 +01:00
updateSendPackets(nei);
if(potentialNei.size() > 0
&& symNei.size() < csts::SYM_COUNT_BEFORE_PEEK
2016-11-26 16:20:20 +01:00
&& time(NULL) - lastPeerPeek >= csts::TIME_PEER_PEEK)
{
auto it = randPeer(&potentialNei);
proto->sendEmpty(it->id);
2016-11-25 15:47:35 +01:00
lastPckSent[it->id] = time(NULL);
lastPeerPeek = time(NULL);
}
2016-11-26 16:20:20 +01:00
if(symNei.size() > 0 && potentialNei.size() < 5
&& time(NULL) - lastSentNR >= csts::TIME_SEND_NR)
{
auto it = randPeer(&symNei);
proto->sendNReq(it->id);
lastPckSent[it->id] = time(NULL);
lastSentNR = time(NULL);
}
for(auto flooder=dataFlooder.begin(); flooder != dataFlooder.end(); )
{
2016-11-26 19:17:56 +01:00
if(flooder->second->done()) {
delete flooder->second;
2016-11-26 16:20:20 +01:00
flooder = dataFlooder.erase(flooder);
2016-11-26 19:17:56 +01:00
}
2016-11-26 16:20:20 +01:00
else {
2016-11-26 19:17:56 +01:00
flooder->second->update();
2016-11-26 16:20:20 +01:00
++flooder;
}
}
for(u64 datId : dataStore->toFlood()) {
dataFlooder.insert({datId,
2016-11-26 19:17:56 +01:00
new Flooder(
2016-11-26 16:20:20 +01:00
(*dataStore)[datId], datId, dataStore->getSeqno(datId),
proto, symNei)});
dataStore->setFlooded(datId);
}
}
void Neighbours::addPotentialNei(const Neighbour& nei) {
2016-11-26 16:20:20 +01:00
if(neiType.find(nei.id) != neiType.end())
return; // We already know him
fprintf(stderr, "[INFO] Adding potential neighbour %lX\n", nei.id);
potentialNei.push_back(nei);
lastRecv.insert({nei.id, 0});
lastIHU.insert({nei.id, 0});
neiType.insert({nei.id, NEI_POTENTIAL});
proto->addIdAddr(nei.addr, nei.id);
}
2016-11-26 16:20:20 +01:00
void Neighbours::receivedFrom(u64 id, const SockAddr& addr) {
if(neiType.find(id) == neiType.end())
addPotentialNei(Neighbour(id, addr));
NeiType typ = neiType[id];
2016-11-25 15:47:35 +01:00
lastRecv[id] = time(NULL);
if(typ == NEI_POTENTIAL)
changeNeiType(id, NEI_UNIDIR);
}
2016-11-26 16:20:20 +01:00
void Neighbours::hadIHU(u64 id, const SockAddr& addr) {
if(neiType.find(id) == neiType.end()) {
fprintf(stderr, "[WARNING] %lX heard us — yet we did not send "
"anything?\n", id);
addPotentialNei(Neighbour(id, addr));
}
NeiType typ = neiType[id];
2016-11-25 15:47:35 +01:00
lastRecv[id] = time(NULL);
lastIHU[id] = time(NULL);
if(typ == NEI_POTENTIAL || typ == NEI_UNIDIR)
changeNeiType(id, NEI_SYM);
}
2016-11-26 16:20:20 +01:00
void Neighbours::getNeighbours(vector<Neighbour>& out, u64 except, int count) {
vector<list<Neighbour>::iterator > permutation;
for(auto it = symNei.begin(); it != symNei.end(); ++it)
permutation.push_back(it);
for(size_t i=0; i < permutation.size(); i++) {
int swapWith = rand() % permutation.size();
auto tmp = permutation[i];
permutation[i] = permutation[swapWith];
permutation[swapWith] = tmp;
}
for(int cur=0, pos=0; cur < count && (size_t)pos < permutation.size();
pos++) {
if(permutation[pos]->id != except) {
out.push_back(*permutation[pos]);
cur++;
}
}
}
2016-11-25 15:47:35 +01:00
list<Neighbour>* Neighbours::listOfType(NeiType typ) {
switch(typ) {
case NEI_POTENTIAL:
2016-11-25 15:47:35 +01:00
return &potentialNei;
case NEI_UNIDIR:
2016-11-25 15:47:35 +01:00
return &unidirNei;
case NEI_SYM:
2016-11-25 15:47:35 +01:00
return &symNei;
default:
break;
}
throw WrongNeiType();
}
2016-11-26 16:20:20 +01:00
void Neighbours::gotIHave(u64 from, u64 datId, u32 seqno) {
if(dataFlooder.find(datId) != dataFlooder.end()) {
2016-11-26 19:17:56 +01:00
dataFlooder.at(datId)->gotIHave(from, seqno);
2016-11-26 16:20:20 +01:00
}
}
void Neighbours::dump() {
for(auto nei : potentialNei)
dumpNei(nei, NEI_POTENTIAL);
for(auto nei : unidirNei)
dumpNei(nei, NEI_UNIDIR);
for(auto nei : symNei)
dumpNei(nei, NEI_SYM);
}
void Neighbours::dumpNei(const Neighbour& nei, NeiType type) {
printf(">> NEIGHBOUR ");
switch(type) {
case NEI_POTENTIAL:
printf("potential ");
break;
case NEI_UNIDIR:
printf("unidir ");
break;
case NEI_SYM:
printf("sym ");
break;
default:
break;
}
printf("%lX lastPck=%ld lastIHU=%ld stateMatch=%d\n",
nei.id, time(NULL) - lastRecv[nei.id],
time(NULL) - lastIHU[nei.id],
type == neiType[nei.id]);
}
void Neighbours::changeNeiType(u64 id, NeiType nType) {
NeiType cType = neiType[id];
if(cType == nType)
return;
2016-11-25 15:47:35 +01:00
list<Neighbour> *fromList = listOfType(cType),
*toList = listOfType(nType);
neiType[id] = nType;
bool wasSpliced=false;
2016-11-25 15:47:35 +01:00
for(auto it=fromList->begin(); it != fromList->end(); ++it) {
if(it->id == id) {
2016-11-25 15:47:35 +01:00
toList->push_back(*it); // splice() doesn't work?!
fromList->erase(it);
wasSpliced=true;
break;
}
}
if(!wasSpliced) {
2016-11-25 15:47:35 +01:00
fprintf(stderr, "[ERROR] Node %lX wasn't found (type change)\n", id);
2016-11-26 19:17:56 +01:00
return;
}
if(nType == NEI_SYM) {
floodTo(id);
}
2016-11-25 15:47:35 +01:00
}
void Neighbours::updateSendPackets(const Neighbour& nei) {
if(!sendIHU(nei.id))
sendEmpty(nei.id);
}
2016-11-25 15:47:35 +01:00
bool Neighbours::sendEmpty(u64 id) {
if(time(NULL) - lastPckSent[id] >= csts::TIME_RESEND_EMPTY) {
lastPckSent[id] = time(NULL);
proto->sendEmpty(id);
return true;
}
2016-11-25 15:47:35 +01:00
return false;
}
bool Neighbours::sendIHU(u64 id) {
if(time(NULL) - lastIHUSent[id] >= csts::TIME_RESEND_IHU) {
lastIHUSent[id] = time(NULL);
lastPckSent[id] = time(NULL);
proto->sendIHU(id);
return true;
}
2016-11-25 15:47:35 +01:00
return false;
}
2016-11-26 16:20:20 +01:00
list<Neighbour>::iterator Neighbours::randPeer(list<Neighbour>* list) {
int nPeerId = rand() % list->size();
auto it = list->begin();
for(int at=0; at < nPeerId; ++at, ++it);
return it;
}
2016-11-26 19:17:56 +01:00
void Neighbours::floodTo(u64 peer) {
vector<u64> datIds;
dataStore->ids(datIds);
for(u64 datId : datIds) {
auto curFlooder = dataFlooder.find(datId);
if(curFlooder != dataFlooder.end() && !curFlooder->second->done()) {
curFlooder->second->addPeer(peer);
}
else {
dataFlooder.insert({datId, new Flooder(
(*dataStore)[datId], datId,
dataStore->getSeqno(datId), proto, peer)});
}
}
}