OverSim
PeerStorage.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2010 Karlsruhe Institute of Technology (KIT),
3 // Institute of Telematics
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 
25 #ifndef __PEERSTORAGE_H__
26 #define __PEERSTORAGE_H__
27 
28 #include <vector>
29 #include <list>
30 
31 #include <oversim_mapset.h>
32 #include <IPvXAddress.h>
33 #include <TransportAddress.h>
34 #include <HashFunc.h>
35 #include <PeerInfo.h>
36 
43 {
44  int32_t overlayId;
46  bool bootstrapped;
48 };
49 
50 class AddrPerOverlayVector : public std::vector<AddrPerOverlay>
51 {
52 public:
54  for (iterator it = begin(); it != end(); it++) {
55  delete it->ta;
56  }
57  }
58 
59  const AddrPerOverlayVector::iterator getIterForOverlayId(int32_t overlayId) {
60  iterator it;
61  for (it = begin(); it != end(); it++) {
62  if (it->overlayId == overlayId) return it;
63  }
64 
65  return it;
66  };
67 
68  TransportAddress* getAddrForOverlayId(int32_t overlayId) {
69  iterator it = getIterForOverlayId(overlayId);
70 
71  if (it != end()) {
72  return it->ta;
73  }
74 
75  return NULL;
76  };
77 
78  void setAddrForOverlayId(TransportAddress* addr, int32_t overlayId) {
79  for (iterator it = begin(); it != end(); it++) {
80  if (it->overlayId == overlayId) {
81  delete it->ta;
82  it->ta = addr;
83  return;
84  }
85  }
86 
87  AddrPerOverlay apo;
88  apo.overlayId = overlayId;
89  apo.bootstrapped = false;
90  apo.ta = addr;
91  push_back(apo);
92  };
93 };
94 
102 {
105  uint32_t peerVectorIndex;
106  friend std::ostream& operator<<(std::ostream& Stream, const BootstrapEntry& entry);
107 };
108 
109 typedef UNORDERED_MAP<IPvXAddress, BootstrapEntry> PeerHashMap;
110 
111 
117 {
118 public:
119  ~PeerStorage();
120 
121  size_t size();
122  const PeerHashMap::iterator find(const IPvXAddress& ip);
123  const PeerHashMap::iterator begin();
124  const PeerHashMap::iterator end();
125 
126  std::pair<const PeerHashMap::iterator, bool> insert(const std::pair<IPvXAddress, BootstrapEntry>& element);
127  void erase(const PeerHashMap::iterator it);
128 
129  void registerOverlay(const PeerHashMap::iterator it,
130  const NodeHandle& peer,
131  int32_t overlayId);
132 
133  const PeerHashMap::iterator getRandomNode(int32_t overlayId,
134  int32_t nodeType,
135  bool bootstrappedNeeded,
136  bool inoffensiveNeeded);
137 
138  void setMalicious(const PeerHashMap::iterator it, bool malicious);
139 
140  void setBootstrapped(const PeerHashMap::iterator it, int32_t overlayId,
141  bool bootstrapped);
142 
143  const PeerHashMap& getPeerHashMap() { return peerHashMap; };
144 
145 private:
146  typedef std::vector<std::vector<PeerHashMap::iterator> > PeerVector;
147 
148  void insertMapIteratorIntoVector(PeerVector& peerVector,
149  PeerHashMap::iterator it);
150 
151  void removeMapIteratorFromVector(PeerVector& peerVector,
152  PeerHashMap::iterator it);
153 
154  inline size_t offsetSize();
155  inline uint8_t calcOffset(bool bootstrapped, bool malicious);
156 
157  PeerHashMap peerHashMap; /* hashmap contain all nodes */
158  PeerVector globalPeerVector; /* vector with iterators to peerHashMap */
159  std::map<int32_t, PeerVector> overlayPeerVectorMap; /* vector of vectors (for each overlayId) with iterators to peerHashMap */
160  std::vector<std::vector<uint32_t> >freeVector;
161 };
162 
163 #endif