00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _PROPHET_TABLE_H_
00018 #define _PROPHET_TABLE_H_
00019
00020 #include "Util.h"
00021 #include "Node.h"
00022 #include "Bundle.h"
00023 #include "BundleCore.h"
00024
00025 #include <map>
00026 #include <list>
00027
00028 namespace prophet
00029 {
00030
00031 class Dictionary;
00032
00036 struct heap_compare {
00037 bool operator() (Node* a, Node* b) {
00038 if (a == NULL || b == NULL) return false;
00039 return ((*a).p_value() > (*b).p_value());
00040 }
00041 };
00042
00046 struct heap_pos {
00047 inline void operator()(Node* a, size_t pos)
00048 {
00049 a->set_heap_pos(pos);
00050 }
00051 };
00052
00057 class Table
00058 {
00059 public:
00060 typedef std::map<std::string,Node*,less_string> rib_table;
00061 typedef std::map<std::string,Node*,less_string>::const_iterator
00062 const_iterator;
00063 typedef std::vector<Node*> Sequence;
00064 typedef Sequence::const_iterator heap_iterator;
00065
00069 Table(BundleCore* core,
00070 const std::string& name,
00071 bool persistent = false);
00072
00076 Table(const Table& t);
00077
00081 ~Table();
00082
00086 const Node* find(const std::string& dest_id) const;
00087
00092 void update(Node* n);
00093
00100 void update_route(const std::string& dest_id,
00101 bool relay = Node::DEFAULT_RELAY,
00102 bool custody = Node::DEFAULT_CUSTODY,
00103 bool internet = Node::DEFAULT_INTERNET);
00104
00111 void update_transitive(const std::string& dest_id,
00112 const std::string& peer_id,
00113 double peer_pvalue,
00114 bool relay = Node::DEFAULT_RELAY,
00115 bool custody = Node::DEFAULT_CUSTODY,
00116 bool internet = Node::DEFAULT_INTERNET);
00117
00121 void update_transitive(const std::string& peer_id,
00122 const RIBNodeList& nodes,
00123 const Dictionary& ribd);
00124
00128 double p_value(const std::string& dest_id) const;
00129
00133 double p_value(const Bundle* b) const;
00134
00138 size_t clone(NodeList& list) const;
00139
00143 size_t size() const { return table_.size(); }
00144
00148 void set_max_route(u_int max_route)
00149 {
00150 max_route_ = max_route;
00151 enforce_quota();
00152 }
00153
00163 size_t truncate(double epsilon);
00164
00168 void assign(const RIBNodeList& list,
00169 const Dictionary& ribd);
00170
00175 void assign(const std::list<const Node*>& list,
00176 const NodeParams* params);
00177
00182 size_t age_nodes();
00183
00185 const_iterator begin() const { return table_.begin(); }
00186 const_iterator end() const { return table_.end(); }
00187 heap_iterator heap_begin() const { return heap_.sequence().begin(); }
00188 heap_iterator heap_end() const { return heap_.sequence().end(); }
00190
00191 protected:
00192 typedef rib_table::iterator iterator;
00193 typedef Heap<Node*,
00194 std::vector<Node*>,
00195 struct heap_compare,
00196 struct heap_pos> NodeHeap;
00197
00201 void heap_add(Node* n);
00202
00206 void heap_del(Node* n);
00207
00211 void enforce_quota();
00212
00217 void remove(iterator* i);
00218
00222 void free();
00223
00227 bool find(const std::string& dest_id, iterator* i);
00228
00229 BundleCore* const core_;
00230 rib_table table_;
00231 NodeHeap heap_;
00232 bool persistent_;
00233
00234 std::string name_;
00235 u_int max_route_;
00236 };
00237
00238 };
00239
00240 #endif // _PROPHET_TABLE_H_