00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef _PROPHET_BUNDLE_ENTRY_LIST_H_
00018 #define _PROPHET_BUNDLE_ENTRY_LIST_H_
00019
00020 #include <map>
00021 #include <string>
00022
00023 #include "PointerList.h"
00024 #include "BundleTLVEntry.h"
00025 #include "Dictionary.h"
00026 #include "Table.h"
00027
00028 namespace prophet
00029 {
00030
00035 template <class BundleEntryType>
00036 class BundleEntryList
00037 {
00038 public:
00039 typedef PointerList<BundleEntryType> List;
00040 typedef typename PointerList<BundleEntryType>::iterator
00041 iterator;
00042 typedef typename PointerList<BundleEntryType>::const_iterator
00043 const_iterator;
00044
00048 BundleEntryList(BundleTLVEntry::bundle_entry_t type =
00049 BundleTLVEntry::UNDEFINED)
00050 : type_(type) {}
00051
00055 BundleEntryList(const BundleEntryList& list)
00056 : type_(list.type_), list_(list.list_) {}
00057
00061 virtual ~BundleEntryList() { list_.clear(); }
00062
00066 size_t size() const { return list_.size(); }
00067
00071 bool empty() const { return list_.empty(); }
00072
00076 void clear() { list_.clear(); }
00077
00081 bool add_entry(const BundleEntryType* entry)
00082 {
00083
00084 if (entry == NULL) return false;
00085
00086
00087 return add_entry(entry->type(), entry->creation_ts(),
00088 entry->seqno(), entry->sid(),
00089 entry->custody(), entry->accept(),
00090 entry->ack());
00091 }
00092
00097 bool remove_entry(u_int32_t cts, u_int32_t seq, u_int16_t sid)
00098 {
00099
00100 for (iterator i = list_.begin(); i != list_.end(); i++)
00101 {
00102 if ((*i)->creation_ts() == cts &&
00103 (*i)->seqno() == seq &&
00104 (*i)->sid() == sid)
00105 {
00106
00107 list_.erase(i);
00108 return true;
00109 }
00110 }
00111
00112 return false;
00113 }
00114
00118 const BundleEntryType* find(u_int32_t cts,
00119 u_int32_t seq,
00120 u_int16_t sid) const
00121 {
00122 BundleEntryType* bo = NULL;
00123 for (const_iterator i = list_.begin(); i != list_.end(); i++)
00124 {
00125 if ((*i)->creation_ts() == cts &&
00126 (*i)->seqno() == seq &&
00127 (*i)->sid() == sid)
00128 {
00129 bo = *i;
00130 break;
00131 }
00132 }
00133 return bo;
00134 }
00135
00139 BundleTLVEntry::bundle_entry_t type() const { return type_; }
00140
00142 iterator begin() { return list_.begin(); }
00143 iterator end() { return list_.end(); }
00144 const_iterator begin() const { return (const_iterator) list_.begin(); }
00145 const_iterator end() const { return (const_iterator) list_.end(); }
00147
00151 const BundleEntryType* front() const { return list_.front(); }
00152
00156 const BundleEntryType* back() const { return list_.back(); }
00157
00161 size_t guess_size(size_t BOEsz) const { return BOEsz * size(); }
00162
00166 BundleEntryList& operator= (const BundleEntryList& list)
00167 {
00168 list_ = list.list_;
00169 type_ = list.type_;
00170 return *this;
00171 }
00172
00173 protected:
00178 bool add_entry(BundleTLVEntry::bundle_entry_t type,
00179 u_int32_t cts, u_int32_t seq, u_int16_t sid,
00180 bool custody=false,
00181 bool accept=false,
00182 bool ack=false)
00183 {
00184
00185
00186 if (type_ == BundleTLVEntry::UNDEFINED)
00187 return false;
00188 else if (type_ != type)
00189 return false;
00190
00191
00192 BundleTLVEntry* b =
00193 BundleTLVEntry::create_entry(cts,seq,sid,custody,accept,ack);
00194
00195
00196 if (b == NULL)
00197 return false;
00198 else
00199 if (type_ != b->type())
00200 {
00201 delete b;
00202 return false;
00203 }
00204
00205
00206 return push_back(dynamic_cast<BundleEntryType*>(b));
00207 }
00208
00212 virtual bool push_back(BundleEntryType* bo) = 0;
00213
00214 BundleTLVEntry::bundle_entry_t type_;
00215 List list_;
00216 };
00217
00222 class BundleOfferList : public BundleEntryList<BundleOfferEntry>
00223 {
00224 public:
00228 BundleOfferList()
00229 : BundleEntryList<BundleOfferEntry>(BundleTLVEntry::OFFER) {}
00230
00234 virtual ~BundleOfferList() {}
00235
00240 bool add_offer(const Bundle* b, u_int16_t sid)
00241 {
00242
00243 if (b == NULL) return false;
00244
00245 return add_offer(b->creation_ts(),
00246 b->sequence_num(),
00247 sid,
00248 b->custody_requested(),
00249 false);
00250 }
00251
00255 bool add_offer(u_int32_t cts,
00256 u_int32_t seq,
00257 u_int16_t sid,
00258 bool custody,
00259 bool ack)
00260 {
00261 return
00262 add_entry(BundleTLVEntry::OFFER,cts,seq,sid,custody,false,ack);
00263 }
00264 protected:
00265 virtual bool push_back(BundleOfferEntry* b)
00266 {
00267 if (b == NULL) return false;
00268 if (find(b->creation_ts(),b->seqno(),b->sid()) == NULL)
00269 {
00270 list_.push_back(b);
00271 return true;
00272 }
00273 return false;
00274 }
00275 };
00276
00281 class BundleResponseList : public BundleEntryList<BundleResponseEntry>
00282 {
00283 public:
00287 BundleResponseList()
00288 : BundleEntryList<BundleResponseEntry>(BundleTLVEntry::RESPONSE) {}
00289
00293 virtual ~BundleResponseList() {}
00294
00298 bool add_response(u_int32_t cts,
00299 u_int32_t seq,
00300 u_int16_t sid,
00301 bool custody,
00302 bool accept = true)
00303 {
00304 return
00305 add_entry(BundleTLVEntry::RESPONSE,cts,seq,sid,custody,accept,false);
00306 }
00307 protected:
00308 virtual bool push_back(BundleResponseEntry* b)
00309 {
00310 if (b == NULL) return false;
00311 if (find(b->creation_ts(),b->seqno(),b->sid()) == NULL)
00312 {
00313 list_.push_back(b);
00314 return true;
00315 }
00316 return false;
00317 }
00318 };
00319
00320 };
00321
00322 #endif // _PROPHET_BUNDLE_ENTRY_LIST_H_