PeerStorage.h

Go to the documentation of this file.
00001 //
00002 // Copyright (C) 2010 Karlsruhe Institute of Technology (KIT),
00003 //                    Institute of Telematics
00004 //
00005 // This program is free software; you can redistribute it and/or
00006 // modify it under the terms of the GNU General Public License
00007 // as published by the Free Software Foundation; either version 2
00008 // of the License, or (at your option) any later version.
00009 //
00010 // This program is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with this program; if not, write to the Free Software
00017 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018 //
00019 
00025 #ifndef __PEERSTORAGE_H__
00026 #define __PEERSTORAGE_H__
00027 
00028 #include <vector>
00029 #include <list>
00030 
00031 #include <oversim_mapset.h>
00032 #include <IPvXAddress.h>
00033 #include <TransportAddress.h>
00034 #include <HashFunc.h>
00035 #include <PeerInfo.h>
00036 
00043 struct BootstrapEntry
00044 {
00045     TransportAddress* node;
00046     PeerInfo* info;
00047     uint32_t peerVectorIndex;
00048     friend std::ostream& operator<<(std::ostream& Stream, const BootstrapEntry entry);
00049 };
00050 
00051 typedef UNORDERED_MAP<IPvXAddress, BootstrapEntry> PeerHashMap;
00052 
00053 
00058 class PeerStorage
00059 {
00060 public:
00061     ~PeerStorage();
00062     size_t size();
00063     const PeerHashMap::iterator find(const IPvXAddress& ip);
00064     const PeerHashMap::iterator begin();
00065     const PeerHashMap::iterator end();
00066     std::pair<const PeerHashMap::iterator, bool> insert(const std::pair<IPvXAddress, BootstrapEntry>& element);
00067     void erase(const PeerHashMap::iterator it);
00068     const PeerHashMap::iterator getRandomNode(int32_t nodeType,
00069                                               bool bootstrappedNeeded,
00070                                               bool inoffensiveNeeded);
00071     void setMalicious(const PeerHashMap::iterator it, bool malicious);
00072     void setBootstrapped(const PeerHashMap::iterator it, bool bootstrapped);
00073     const PeerHashMap& getPeerHashMap() { return peerHashMap; };
00074 
00075 private:
00076     void insertMapIteratorIntoVector(PeerHashMap::iterator it);
00077     void removeMapIteratorFromVector(PeerHashMap::iterator it);
00078     inline size_t offsetSize();
00079     inline uint8_t calcOffset(bool bootstrapped, bool malicious);
00080 
00081     PeerHashMap peerHashMap;
00082     std::vector<std::vector<PeerHashMap::iterator> > peerVector;
00083     std::vector<std::vector<uint32_t> >freeVector;
00084 };
00085 
00086 #endif