/***************************************************************************
* By Théophile Bastian, 2017
* M1 Network course project at ENS Cachan, Juliusz Chroboczek.
* License: WTFPL v2
**************************************************************************/
#include "flooder.h"
Flooder::Flooder(const Bytes& data, u64 datId, u32 seqno, Protocol* proto,
const std::list& 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();
}
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() {
}
void Flooder::update() {
while(!toFlood.empty()) {
const FloodEvt& evt = toFlood.top();
if(evt.time > time(NULL)) {
break;
}
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;
}
void Flooder::addPeer(u64 peer) {
triesCount[peer] = 0;
toFlood.push(FloodEvt(time(NULL), peer));
}
void Flooder::sendTo(u64 id) {
proto->sendData(id, data, seqno, datId);
}