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));
|
2016-11-28 15:17:35 +01:00
|
|
|
fprintf(stderr, "[DBG] Flood %lX init towards %lX\n",
|
|
|
|
datId, it->id);
|
2016-11-26 16:19:38 +01:00
|
|
|
}
|
|
|
|
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()) {
|
2016-11-28 15:17:35 +01:00
|
|
|
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();
|
|
|
|
|
2016-11-28 15:17:35 +01:00
|
|
|
fprintf(stderr, "[DBG] Flooding to %lX\n", evt.id);
|
|
|
|
|
2016-11-26 16:19:38 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|