/*************************************************************************** * 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) { fprintf(stderr, "[DBG] Created flooder for %lX (%u)\n", datId, seqno); for(auto it=peers.begin(); it != peers.end(); ++it) { triesCount[it->id] = 0; toFlood.push(FloodEvt(time(NULL), it->id)); } update(); } 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) { fprintf(stderr, "[DBG] Got your IHave, %lX!\n", evt.id); 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::sendTo(u64 id) { proto->sendData(id, data, seqno, datId); }