Compare commits
2 commits
50975cd0e0
...
0289df4cdd
Author | SHA1 | Date | |
---|---|---|---|
0289df4cdd | |||
fa51b5c170 |
2 changed files with 20 additions and 33 deletions
|
@ -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);
|
||||
}
|
||||
|
|
16
dataStore.h
16
dataStore.h
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue