forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
RepeatHandler.cpp
Go to the documentation of this file.
1 
9 #include "RepeatHandler.h"
10 
11 namespace forest {
12 
14 RepeatHandler::RepeatHandler(int size) : n(size) {
15  pmap = new HashMap<Pair<fAdr_t,int64_t>, int, Hash::s32s64>(n);
16  deadlines = new Dheap<int64_t>(n);
17 }
18 
21 
27 int RepeatHandler::find(fAdr_t peerAdr, int64_t seqNum) {
28  int x = pmap->find(Pair<fAdr_t,int64_t>(peerAdr, seqNum));
29  if (x == 0) return 0;
30  return pmap->getValue(x);
31 }
32 
40 bool RepeatHandler::saveReq(int cx, fAdr_t peerAdr, int64_t seqNum,
41  int64_t now) {
42  if (pmap->size() == n) {
43  // when full, remove the oldest to make room
44  int x = deadlines->deleteMin();
45  pktx ox = pmap->getValue(x);
46  pmap->remove(pmap->getKey(x));
47  return ox;
48  }
49  int x = pmap->put(Pair<fAdr_t,int64_t>(peerAdr,seqNum),cx);
50  if (x != 0)
51  deadlines->insert(x, now + 20000000000); // keep for 20 seconds
52  return 0;
53 }
54 
65 int RepeatHandler::saveRep(int cx, fAdr_t peerAdr, int64_t seqNum) {
66  Pair<fAdr_t,int64_t> kee(peerAdr,seqNum);
67  int x = pmap->find(kee);
68  if (x == 0) return 0;
69  int px = pmap->getValue(x);
70  pmap->getValue(x) = cx;
71  deadlines->remove(x);
72  pmap->remove(kee);
73  return px;
74 }
75 
81 int RepeatHandler::expired(int64_t now) {
82  int x = deadlines->findmin();
83  if (x == 0 || now < deadlines->key(x)) return 0;
84  int px = pmap->getValue(x);
85  deadlines->remove(x);
86  pmap->remove(pmap->getKey(x));
87  return px;
88 }
89 
90 } // ends namespace