forest-net
an overlay networks for large-scale virtual worlds
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
LinkTable.h
Go to the documentation of this file.
1 
9 #ifndef LINKTABLE_H
10 #define LINKTABLE_H
11 
12 #include <set>
13 #include "Forest.h"
14 #include "Packet.h"
15 #include "RateSpec.h"
16 #include "Hash.h"
17 #include "HashSet.h"
18 #include "HashMap.h"
19 
20 namespace forest {
21 
24 class LinkTable {
25 public:
27  class Entry {
28  public:
29  int iface;
30  ipa_t peerIp;
31  ipp_t peerPort;
34  bool isConnected;
35  uint64_t nonce;
38 
39  Entry();
40  Entry(const Entry&);
41  ~Entry();
42 
43  string toString() const;
44  friend ostream& operator<<(ostream& out, const Entry& a) {
45  return out << a.toString();
46  }
47  };
48 
50  LinkTable(int);
51  ~LinkTable();
52 
53  // predicates
54  bool valid(int) const;
55  bool checkEntry(int);
56 
57  // iteration methods
58  int firstLink() const;
59  int nextLink(int) const;
60 
61  // access methods
62  int lookup(ipa_t, ipp_t) const;
63  int lookup(uint64_t) const;
64  int lookup(fAdr_t) const;
65 
66  Entry& getEntry(int) const;
67 
68  // modifiers
69  void setPeerAdr(int, fAdr_t);
70  int addEntry(int,ipa_t,ipp_t,uint64_t);
71  bool remapEntry(int,ipa_t,ipp_t);
72  bool remapEntry(int, ipa_t, ipp_t, uint64_t);
73  bool revertEntry(int);
74  bool removeEntry(int);
75  bool connect(int lnk, ipa_t peerIp, ipp_t peerPort);
76 
77  // io routines
78  bool read(istream&);
79  string link2string(int) const;
80  string toString() const;
81 
82  // packing entry in a packet
83  char* pack(int, char*) const;
84  char* unpack(int, char*);
85 
86 private:
87  int maxLnk;
88 
90  HashMap<uint64_t,Entry,Hash::u64> *map;
92  HashSet<fAdr_t,Hash::s32> *padrMap;
93 
94  // helper functions
95  uint64_t hashkey(ipa_t, ipp_t) const;
96  int readEntry(istream&);
97 };
98 
99 LinkTable::Entry::Entry() {
100  iface = 0;
101  peerIp = 0; peerPort = 0; peerType = Forest::UNDEF_NODE; peerAdr = 0;
102  isConnected = false; nonce = 0;
103 }
104 
105 LinkTable::Entry::Entry(const Entry& e) {
106  iface = e.iface;
107  peerIp = e.peerIp; peerPort = e.peerPort;
108  peerType = e.peerType; peerAdr = e.peerAdr;
109  isConnected = e.isConnected; nonce = e.nonce;
110  rates = e.rates; availRates = e.availRates;
111 }
112 
113 string LinkTable::Entry::toString() const {
114  stringstream ss;
115  ss << setw(6) << iface << " ";
116  ss << setw(12) << Np4d::ip2string(peerIp)
117  << ":" << setw(5) << left << peerPort << " ";
118  ss << setw(10) << left << Forest::nodeType2string(peerType);
119  ss << " " << setw(10) << left <<Forest::fAdr2string(peerAdr);
120  ss << " " << rates.toString();
121  ss << " " << availRates.toString();
122  return ss.str();
123 }
124 
130 inline bool LinkTable::valid(int lnk) const { return map->valid(lnk); }
131 
132 
138 inline int LinkTable::firstLink() const { return map->first(); }
139 
147 inline int LinkTable::nextLink(int lnk) const { return map->next(lnk); }
148 
153 inline uint64_t LinkTable::hashkey(ipa_t ipa, ipp_t ipp) const {
154  return (((uint64_t) ipa) << 32) | ((uint64_t) ipp);
155 }
156 
163 inline int LinkTable::lookup(ipa_t ipa, ipp_t ipp) const {
164  return map->find(hashkey(ipa,ipp));
165 }
166 
172 inline int LinkTable::lookup(uint64_t nonce) const {
173  return map->find(nonce);
174 }
175 
182 inline int LinkTable::lookup(fAdr_t peerAdr) const {
183  return padrMap->find(peerAdr);
184 }
185 
190 inline LinkTable::Entry& LinkTable::getEntry(int lnk) const {
191  return map->getValue(lnk);
192 }
193 
194 } // ends namespace
195 
196 
197 #endif