00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <time.h>
00018 #include "BundleCore.h"
00019 #include "AckList.h"
00020
00021 namespace prophet
00022 {
00023
00024 AckList::~AckList()
00025 {
00026 for (palist::iterator i = acks_.begin(); i != acks_.end(); i++)
00027 {
00028 delete (*i);
00029 }
00030 acks_.clear();
00031 }
00032
00033 bool
00034 AckList::insert(const std::string& dest_id, u_int32_t cts,
00035 u_int32_t seq, u_int32_t ets)
00036 {
00037
00038 if (ets == 0) ets = 86400;
00039
00040 Ack a(dest_id,cts,seq,ets);
00041 return insert(&a);
00042 }
00043
00044 bool
00045 AckList::insert(const Bundle* b, const BundleCore* core)
00046 {
00047 if (b == NULL || core == NULL) return false;
00048
00049 return insert(core->get_route(b->destination_id()),
00050 b->creation_ts(),
00051 b->sequence_num(),
00052 b->expiration_ts());
00053 }
00054
00055 bool
00056 AckList::insert(const Ack* a)
00057 {
00058 if (a == NULL) return false;
00059
00060 Ack* na = new Ack(*a);
00061
00062
00063 if (acks_.insert(na).second)
00064 return true;
00065
00066
00067 delete na;
00068 return false;
00069 }
00070
00071 size_t
00072 AckList::clone(PointerList<Ack>& list) const
00073 {
00074 size_t num = 0;
00075
00076 list.clear();
00077
00078 for (palist::const_iterator i = acks_.begin(); i != acks_.end(); i++)
00079 {
00080
00081 list.push_back(new Ack(**i));
00082
00083 num++;
00084 }
00085
00086 return num;
00087 }
00088
00089 size_t
00090 AckList::fetch(const std::string& dest_id, PointerList<Ack>* list) const
00091 {
00092 size_t num = 0;
00093
00094
00095 if (list != NULL) list->clear();
00096
00097
00098 Ack a(dest_id);
00099
00100
00101 palist::const_iterator i = acks_.lower_bound(&a);
00102
00103
00104
00105 while (i != acks_.end() && (*i)->dest_id().compare(dest_id) == 0)
00106 {
00107
00108 if (list != NULL) list->push_back(new Ack(**(i++)));
00109 num++;
00110 }
00111
00112
00113 return num;
00114 }
00115
00116 size_t
00117 AckList::expire()
00118 {
00119 size_t num = 0;
00120
00121
00122 time_t now = time(0);
00123
00124
00125
00126 for (palist::iterator i = acks_.begin(); i != acks_.end(); )
00127 {
00128 Ack* a = *i;
00129
00130 if (now - a->cts() > a->ets())
00131 {
00132 num++;
00133
00134 acks_.erase(i++);
00135
00136 delete a;
00137 }
00138
00139 else i++;
00140 }
00141
00142 return num;
00143 }
00144
00145 bool
00146 AckList::is_ackd(const std::string& dest_id,
00147 u_int32_t cts, u_int32_t seq) const
00148 {
00149
00150 Ack a(dest_id);
00151
00152
00153 palist::iterator i = acks_.lower_bound(&a);
00154
00155
00156 while (i != acks_.end())
00157 {
00158
00159 if ((*i)->dest_id().compare(dest_id) != 0)
00160 break;
00161
00162 if ((*i)->cts() == cts && (*i)->seq() == seq)
00163 return true;
00164 i++;
00165 }
00166
00167 return false;
00168 }
00169
00170 };