M1-nw-project/flooder.h

56 lines
1.3 KiB
C
Raw Normal View History

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/>
**************************************************************************/
#pragma once
#include <ctime>
#include <queue>
#include <list>
#include <unordered_map>
#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<Neighbour>& peers);
2016-11-26 19:17:56 +01:00
Flooder(const Bytes& data, u64 datId, u32 seqno, Protocol* proto,
u64 peer);
~Flooder();
2016-11-26 16:19:38 +01:00
void update();
/** Call often enough (ie ~1s) */
void gotIHave(u64 id, u32 withSeqno);
/** Acknoledges the reception of the resource by peer `id`. */
2016-11-26 19:17:56 +01:00
void addPeer(u64 peer);
/** Notifies that a new peer must be flooded. */
2016-11-26 16:19:38 +01:00
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<u64, int> triesCount;
std::priority_queue<FloodEvt> toFlood;
Bytes data;
u64 datId;
u32 seqno;
Protocol* proto;
};