/*************************************************************************** * By Théophile Bastian, 2017 * M1 Network course project at ENS Cachan, Juliusz Chroboczek. * License: WTFPL v2 **************************************************************************/ #pragma once #include #include #include #include #include "data.h" #include "nw_constants.h" #include "protocol.h" class Flooder { public: Flooder(const Bytes& data, u64 datId, u32 seqno, Protocol* proto, const std::list& peers); Flooder(const Bytes& data, u64 datId, u32 seqno, Protocol* proto, u64 peer); ~Flooder(); void update(); /** Call often enough (ie ~1s) */ void gotIHave(u64 id, u32 withSeqno); /** Acknoledges the reception of the resource by peer `id`. */ void addPeer(u64 peer); /** Notifies that a new peer must be flooded. */ bool done() const { return toFlood.empty(); } private: //meth void sendTo(u64 id); private: struct FloodEvt { FloodEvt(time_t time, u64 id) : time(time), id(id) {} time_t time; u64 id; bool operator<(const FloodEvt& e) const { return time > e.time; // Max priority queue. } }; std::unordered_map triesCount; std::priority_queue toFlood; Bytes data; u64 datId; u32 seqno; Protocol* proto; };