forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
Repeater.cpp
Go to the documentation of this file.
1 
9 #include "Repeater.h"
10 
11 namespace forest {
12 
15 Repeater::Repeater(int size) : n(size) {
16  pmap = new HashMap<int64_t, Pair<int,int>, Hash::s64>(n);
17  deadlines = new Dheap<int64_t>(n);
18 }
19 
21 Repeater::~Repeater() { delete pmap; delete deadlines; }
22 
32 int Repeater::saveReq(int cx, int64_t seqNum, int64_t now, int idx) {
33  int x = pmap->put(seqNum, Pair<int,int>(cx,3), idx);
34  if (x == 0) return 0;
35  deadlines->insert(x, now + 1000000000);
36  return x;
37 }
38 
43 pair<int,int> Repeater::deleteMatch(int64_t seqNum) {
44  int idx = pmap->find(seqNum);
45  if (idx == 0) return pair<int,int>(0,0);
46  Pair<int,int>& vp = pmap->getValue(idx);
47  deadlines->remove(idx);
48  pmap->remove(seqNum);
49  return pair<int,int>(vp.first,idx);
50 }
51 
60 pair<int,int> Repeater::overdue(int64_t now) {
61  int idx = deadlines->findmin();
62  if (idx == 0 || now < deadlines->key(idx))
63  return pair<int,int>(0,0);
64  Pair<int, int>& vp = pmap->getValue(idx);
65  if (vp.second == 0) {
66  deadlines->remove(idx);
67  pmap->remove(pmap->getKey(idx));
68  return pair<int,int>(-vp.first,idx);
69  }
70  // push deadline back by one second and decrement repeat count
71  deadlines->changekey(idx, now + 1000000000); vp.second--;
72  return pair<int,int>(vp.first,idx);
73 }
74 
75 } // ends namespace