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 "ProphetBundleList.h"
00021
00022 namespace dtn
00023 {
00024
00025 BundleRef
00026 ProphetBundleList::NULL_BUNDLE("ProphetBundleList");
00027
00028 ProphetBundleList::ProphetBundleList(prophet::Repository::BundleCoreRep* core)
00029 : list_(core) {}
00030
00031 ProphetBundleList::~ProphetBundleList()
00032 {
00033 clear();
00034 }
00035
00036 void
00037 ProphetBundleList::add(const BundleRef& b)
00038 {
00039 ProphetBundle* pb = new ProphetBundle(b);
00040 if (!list_.add(pb))
00041 delete pb;
00042 }
00043
00044 void
00045 ProphetBundleList::add(const prophet::Bundle* b)
00046 {
00047 if (list_.add(b))
00048 return;
00049
00050 prophet::Bundle* pb = const_cast<prophet::Bundle*>(b);
00051 delete pb;
00052 }
00053
00054 void
00055 ProphetBundleList::del(const BundleRef& b)
00056 {
00057 const_iterator i;
00058 if (find(b->dest().str(),
00059 b->creation_ts().seconds_,
00060 b->creation_ts().seqno_, i))
00061 {
00062 prophet::Bundle* bundle = const_cast<prophet::Bundle*>(*i);
00063 list_.del(*i);
00064 delete bundle;
00065 }
00066 }
00067
00068 void
00069 ProphetBundleList::del(const prophet::Bundle* b)
00070 {
00071 prophet::Bundle* bundle = const_cast<prophet::Bundle*>(b);
00072 list_.del(bundle);
00073 delete bundle;
00074 }
00075
00076 const prophet::Bundle*
00077 ProphetBundleList::find(const std::string& dst,
00078 u_int creation_ts,
00079 u_int seqno) const
00080 {
00081 const_iterator i;
00082 if (find(dst,creation_ts,seqno,i))
00083 return *i;
00084 return NULL;
00085 }
00086
00087 const BundleRef&
00088 ProphetBundleList::find_ref(const prophet::Bundle* b) const
00089 {
00090 if (b != NULL)
00091 {
00092 const_iterator i;
00093 if (find(b->destination_id(),
00094 b->creation_ts(),
00095 b->sequence_num(),i))
00096 return dynamic_cast<const ProphetBundle*>(*i)->ref();
00097 }
00098 return NULL_BUNDLE;
00099 }
00100
00101 void
00102 ProphetBundleList::clear()
00103 {
00104 while (!list_.empty())
00105 {
00106 prophet::Bundle* b = const_cast<prophet::Bundle*>(
00107 list_.get_bundles().front());
00108 list_.del(b);
00109
00110 delete b;
00111 }
00112 }
00113
00114 bool
00115 ProphetBundleList::find(const std::string& dst,
00116 u_int creation_ts, u_int seqno, const_iterator& i) const
00117 {
00118 i = list_.get_bundles().begin();
00119 while (i != list_.get_bundles().end())
00120 {
00121 if ((*i)->creation_ts() == creation_ts &&
00122 (*i)->sequence_num() == seqno &&
00123 (*i)->destination_id() == dst) break;
00124 i++;
00125 }
00126
00127 if (i == list_.get_bundles().end()) return false;
00128
00129 return ((*i)->creation_ts() == creation_ts &&
00130 (*i)->sequence_num() == seqno &&
00131 (*i)->destination_id() == dst);
00132 }
00133
00134 };