Compare commits

..

2 commits

Author SHA1 Message Date
0289df4cdd Fix > → >= 2016-11-28 18:53:49 +01:00
fa51b5c170 Datastore: no priority queue anymore for republish
Now handled only by last republish time.
2016-11-28 18:39:34 +01:00
2 changed files with 20 additions and 33 deletions

View file

@ -10,28 +10,25 @@
#include <cstdlib>
using namespace std;
DataStore::DataStore(Protocol* proto) : proto(proto)
DataStore::DataStore(Protocol* proto) :
proto(proto), myData(0), hasOwnData(false)
{}
DataStore::~DataStore() {
}
void DataStore::update() {
while(!timeEvents.empty()) {
TimeEvent evt = timeEvents.top();
while(!expireEvents.empty()) {
TimeEvent evt = expireEvents.front();
if(evt.time > time(NULL)) // We're done for now.
break;
timeEvents.pop();
switch(evt.type) {
case EV_REPUBLISH:
handleRepublish(evt.id);
break;
case EV_EXPIRES:
handleExpire(evt.id, evt.seqno);
break;
}
expireEvents.pop();
handleExpire(evt.id, evt.seqno);
}
if(hasOwnData) {
if(time(NULL) - recvTime[myData] >= csts::TIME_REPUBLISH_DATA)
handleRepublish(myData);
}
}
@ -43,14 +40,13 @@ void DataStore::addData(Bytes pck, u32 seqno, u64 id, bool mine) {
curSeqno[id] = seqno;
if(mine) {
timeEvents.push(TimeEvent(
time(NULL) + csts::TIME_REPUBLISH_DATA,
seqno, id, EV_REPUBLISH));
hasOwnData = true;
myData = id;
}
else {
timeEvents.push(TimeEvent(
expireEvents.push(TimeEvent(
time(NULL) + csts::TIMEOUT_DATA,
seqno, id, EV_EXPIRES));
seqno, id));
// If it is not renewed, it will expire.
}
@ -128,10 +124,7 @@ void DataStore::handleRepublish(u64 datId) {
}
curSeqno[datId] = time(NULL);
timeEvents.push(TimeEvent(
time(NULL) + csts::TIME_REPUBLISH_DATA,
curSeqno[datId], datId, EV_REPUBLISH));
recvTime[datId] = time(NULL);
handleFlood(datId);
}

View file

@ -66,20 +66,12 @@ class DataStore {
void cleanData(u64 id);
private:
enum EvType {
EV_REPUBLISH, EV_EXPIRES
};
struct TimeEvent {
TimeEvent(time_t time, u32 seqno, u64 id, EvType type) :
time(time), seqno(seqno), id(id), type(type) {}
TimeEvent(time_t time, u32 seqno, u64 id) :
time(time), seqno(seqno), id(id) {}
time_t time;
u32 seqno;
u64 id;
EvType type;
bool operator<(const TimeEvent& e) const {
return time > e.time; // Max-priority queue
}
};
struct Data {
@ -90,8 +82,10 @@ class DataStore {
bool isMine;
};
std::priority_queue<TimeEvent> timeEvents;
std::queue<TimeEvent> expireEvents;
Protocol* proto;
u64 myData;
bool hasOwnData;
std::unordered_map<u64, Data> data;
std::unordered_map<u64, u32> curSeqno;
std::unordered_map<u64, time_t> recvTime;