2016-11-26 16:19:38 +01:00
|
|
|
/***************************************************************************
|
|
|
|
* By Théophile Bastian, 2017
|
|
|
|
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
|
|
|
|
* License: WTFPL v2 <http://www.wtfpl.net/>
|
|
|
|
**************************************************************************/
|
|
|
|
|
|
|
|
#include "flooder.h"
|
|
|
|
|
|
|
|
Flooder::Flooder(const Bytes& data, u64 datId, u32 seqno, Protocol* proto,
|
|
|
|
const std::list<Neighbour>& peers) :
|
|
|
|
data(data), datId(datId), seqno(seqno), proto(proto)
|
|
|
|
{
|
|
|
|
for(auto it=peers.begin(); it != peers.end(); ++it) {
|
|
|
|
triesCount[it->id] = 0;
|
|
|
|
toFlood.push(FloodEvt(time(NULL), it->id));
|
|
|
|
}
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2016-11-26 19:17:56 +01:00
|
|
|
Flooder::Flooder(const Bytes& data, u64 datId, u32 seqno, Protocol* proto,
|
|
|
|
u64 peer) :
|
|
|
|
data(data), datId(datId), seqno(seqno), proto(proto)
|
|
|
|
{
|
|
|
|
addPeer(peer);
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
|
|
|
Flooder::~Flooder() {
|
|
|
|
}
|
|
|
|
|
2016-11-26 16:19:38 +01:00
|
|
|
void Flooder::update() {
|
|
|
|
while(!toFlood.empty()) {
|
|
|
|
const FloodEvt& evt = toFlood.top();
|
2016-11-26 19:17:56 +01:00
|
|
|
if(evt.time > time(NULL)) {
|
2016-11-26 16:19:38 +01:00
|
|
|
break;
|
2016-11-26 19:17:56 +01:00
|
|
|
}
|
2016-11-26 16:19:38 +01:00
|
|
|
toFlood.pop();
|
|
|
|
|
|
|
|
if(triesCount[evt.id] > csts::FLOOD_RETRIES) {
|
|
|
|
fprintf(stderr, "[WARNING] Could not flood to %lX: no IHave.\n",
|
|
|
|
evt.id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(triesCount[evt.id] < 0) {
|
|
|
|
continue; // IHave received
|
|
|
|
}
|
|
|
|
|
|
|
|
sendTo(evt.id);
|
|
|
|
triesCount[evt.id]++;
|
|
|
|
toFlood.push(FloodEvt(time(NULL) + csts::TIME_RESEND_FLOOD, evt.id));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Flooder::gotIHave(u64 id, u32 withSeqno) {
|
|
|
|
if(seqno > withSeqno)
|
|
|
|
return;
|
|
|
|
|
|
|
|
triesCount[id] = -1;
|
|
|
|
}
|
|
|
|
|
2016-11-26 19:17:56 +01:00
|
|
|
void Flooder::addPeer(u64 peer) {
|
|
|
|
triesCount[peer] = 0;
|
|
|
|
toFlood.push(FloodEvt(time(NULL), peer));
|
|
|
|
}
|
|
|
|
|
2016-11-26 16:19:38 +01:00
|
|
|
void Flooder::sendTo(u64 id) {
|
|
|
|
proto->sendData(id, data, seqno, datId);
|
|
|
|
}
|
|
|
|
|