00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifdef HAVE_CONFIG_H
00018 # include <dtn-config.h>
00019 #endif
00020 #include "ProphetNodeList.h"
00021
00022 namespace dtn
00023 {
00024
00025 ProphetNodeList::ProphetNodeList()
00026 {
00027 }
00028
00029 ProphetNodeList::~ProphetNodeList()
00030 {
00031 clear();
00032 }
00033
00034 void
00035 ProphetNodeList::load(const prophet::Node* n)
00036 {
00037 log_debug_p("/dtn/route/nodelist","load");
00038 iterator i;
00039 ASSERT(n != NULL);
00040 ASSERT(! find(n->dest_id(), i));
00041
00042 log_debug_p("/dtn/route/nodelist",
00043 "add new node for %s (age %u pv %.2f flags %s%s%s)",
00044 n->dest_id(),
00045 n->age(),
00046 n->p_value(),
00047 n->relay() ? "R" : "-",
00048 n->custody() ? "C" : "-",
00049 n->internet_gw() ? "I" : "-");
00050
00051 ProphetNode* a = new ProphetNode(*n);
00052
00053 list_.insert(i,a);
00054 }
00055
00056 void
00057 ProphetNodeList::update(const prophet::Node* n)
00058 {
00059 log_debug_p("/dtn/route/nodelist","update");
00060 iterator i;
00061 ASSERT(n != NULL);
00062 if (! find(n->dest_id(), i))
00063 {
00064 log_debug_p("/dtn/route/nodelist",
00065 "add new node for %s",n->dest_id());
00066
00067 ProphetNode* a = new ProphetNode(*n);
00068
00069 list_.insert(i,a);
00070 ProphetStore::instance()->add(a);
00071 }
00072 else
00073 {
00074 log_debug_p("/dtn/route/nodelist",
00075 "update existing node for %s",n->dest_id());
00076
00077 ProphetNode* a = static_cast<ProphetNode*>(*i);
00078 a->set_pvalue( n->p_value() );
00079 ProphetStore::instance()->update(a);
00080 }
00081 }
00082
00083 void
00084 ProphetNodeList::del(const prophet::Node* n)
00085 {
00086 iterator i;
00087 ASSERT(n != NULL);
00088 if (find(n->dest_id(), i))
00089 {
00090
00091 ProphetNode* a = static_cast<ProphetNode*>(*i);
00092 list_.erase(i);
00093 ProphetStore::instance()->del(a);
00094 delete a;
00095 }
00096 }
00097
00098 const prophet::Node*
00099 ProphetNodeList::find(const std::string& dest_id) const
00100 {
00101 iterator i;
00102 ProphetNodeList* me = const_cast<ProphetNodeList*>(this);
00103 if (me == NULL) return NULL;
00104 if (me->find(dest_id,i))
00105 return *i;
00106 return NULL;
00107 }
00108
00109 void
00110 ProphetNodeList::clone(prophet::Table* nodes,
00111 const prophet::NodeParams* params)
00112 {
00113 if (nodes == NULL || params == NULL) return;
00114
00115 std::list<const prophet::Node*> list(list_.begin(),list_.end());
00116 nodes->assign(list,params);
00117 }
00118
00119 void
00120 ProphetNodeList::clear()
00121 {
00122 while (!list_.empty())
00123 {
00124 delete list_.front();
00125 list_.pop_front();
00126 }
00127 }
00128
00129 bool
00130 ProphetNodeList::find(const std::string& dest_id, iterator& i)
00131 {
00132 i = list_.begin();
00133 while (i != list_.end() && (*i)->dest_id() < dest_id)
00134 i++;
00135 return (i != list_.end() && (*i)->dest_id() == dest_id);
00136 }
00137
00138 };