<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> <title>DTN Reference Implementation: AckList.cc Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.5.6 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="namespaces.html"><span>Namespaces</span></a></li> <li><a href="annotated.html"><span>Classes</span></a></li> <li class="current"><a href="files.html"><span>Files</span></a></li> </ul> </div> <h1>AckList.cc</h1><a href="AckList_8cc.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">/*</span> <a name="l00002"></a>00002 <span class="comment"> * Copyright 2007 Baylor University</span> <a name="l00003"></a>00003 <span class="comment"> *</span> <a name="l00004"></a>00004 <span class="comment"> * Licensed under the Apache License, Version 2.0 (the "License");</span> <a name="l00005"></a>00005 <span class="comment"> * you may not use this file except in compliance with the License.</span> <a name="l00006"></a>00006 <span class="comment"> * You may obtain a copy of the License at</span> <a name="l00007"></a>00007 <span class="comment"> *</span> <a name="l00008"></a>00008 <span class="comment"> * http://www.apache.org/licenses/LICENSE-2.0</span> <a name="l00009"></a>00009 <span class="comment"> *</span> <a name="l00010"></a>00010 <span class="comment"> * Unless required by applicable law or agreed to in writing, software</span> <a name="l00011"></a>00011 <span class="comment"> * distributed under the License is distributed on an "AS IS" BASIS,</span> <a name="l00012"></a>00012 <span class="comment"> * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span> <a name="l00013"></a>00013 <span class="comment"> * See the License for the specific language governing permissions and</span> <a name="l00014"></a>00014 <span class="comment"> * limitations under the License.</span> <a name="l00015"></a>00015 <span class="comment"> */</span> <a name="l00016"></a>00016 <a name="l00017"></a>00017 <span class="preprocessor">#include <time.h></span> <a name="l00018"></a>00018 <span class="preprocessor">#include "<a class="code" href="BundleCore_8h.html">BundleCore.h</a>"</span> <a name="l00019"></a>00019 <span class="preprocessor">#include "<a class="code" href="AckList_8h.html">AckList.h</a>"</span> <a name="l00020"></a>00020 <a name="l00021"></a>00021 <span class="keyword">namespace </span>prophet <a name="l00022"></a>00022 { <a name="l00023"></a>00023 <a name="l00024"></a><a class="code" href="classprophet_1_1AckList.html#441ea7efa90a3f97a16dce26fc27e60b">00024</a> <a class="code" href="classprophet_1_1AckList.html#441ea7efa90a3f97a16dce26fc27e60b" title="Destructor.">AckList::~AckList</a>() <a name="l00025"></a>00025 { <a name="l00026"></a>00026 <span class="keywordflow">for</span> (palist::iterator i = <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.begin(); i != <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.end(); i++) <a name="l00027"></a>00027 { <a name="l00028"></a>00028 <span class="keyword">delete</span> (*i); <a name="l00029"></a>00029 } <a name="l00030"></a>00030 <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.clear(); <a name="l00031"></a>00031 } <a name="l00032"></a>00032 <a name="l00033"></a>00033 <span class="keywordtype">bool</span> <a name="l00034"></a><a class="code" href="classprophet_1_1AckList.html#fa05576d340ed75b61a4652132410f05">00034</a> AckList::insert(<span class="keyword">const</span> std::string& dest_id, u_int32_t cts, <a name="l00035"></a>00035 u_int32_t seq, u_int32_t ets) <a name="l00036"></a>00036 { <a name="l00037"></a>00037 <span class="comment">// default to 1 day</span> <a name="l00038"></a>00038 <span class="keywordflow">if</span> (ets == 0) ets = 86400; <a name="l00039"></a>00039 <a name="l00040"></a>00040 <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a> a(dest_id,cts,seq,ets); <a name="l00041"></a>00041 <span class="keywordflow">return</span> <a class="code" href="classprophet_1_1AckList.html#fa05576d340ed75b61a4652132410f05" title="Convenience method for inserting Ack into list; return true upon success, else false...">insert</a>(&a); <a name="l00042"></a>00042 } <a name="l00043"></a>00043 <a name="l00044"></a>00044 <span class="keywordtype">bool</span> <a name="l00045"></a><a class="code" href="classprophet_1_1AckList.html#94243ba96f3107f48615da63a4f19642">00045</a> <a class="code" href="classprophet_1_1AckList.html#fa05576d340ed75b61a4652132410f05" title="Convenience method for inserting Ack into list; return true upon success, else false...">AckList::insert</a>(<span class="keyword">const</span> <a class="code" href="classprophet_1_1Bundle.html" title="Facade interface between Prophet router and host implmentation&#39;s Bundle representation...">Bundle</a>* b, <span class="keyword">const</span> <a class="code" href="classprophet_1_1BundleCore.html" title="Prophet facade&#39;s abstract interface into DTN host&#39;s bundle core.">BundleCore</a>* core) <a name="l00046"></a>00046 { <a name="l00047"></a>00047 <span class="keywordflow">if</span> (b == NULL || core == NULL) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00048"></a>00048 <a name="l00049"></a>00049 <span class="keywordflow">return</span> <a class="code" href="classprophet_1_1AckList.html#fa05576d340ed75b61a4652132410f05" title="Convenience method for inserting Ack into list; return true upon success, else false...">insert</a>(core-><a class="code" href="classprophet_1_1BundleCore.html#5ee789c3c5a49bbcb0624658e7f524c6" title="Given a Bundle&#39;s destination, return the route.">get_route</a>(b-><a class="code" href="classprophet_1_1Bundle.html#81f0a95589809a157829bacb4d3c5989" title="Accessors.">destination_id</a>()), <a name="l00050"></a>00050 b-><a class="code" href="classprophet_1_1Bundle.html#df5ff9aa8aba7ddd7a741d6143fe3d88" title="Accessors.">creation_ts</a>(), <a name="l00051"></a>00051 b-><a class="code" href="classprophet_1_1Bundle.html#8f4fad2c83696237207ba7a19acb9ca4" title="Accessors.">sequence_num</a>(), <a name="l00052"></a>00052 b-><a class="code" href="classprophet_1_1Bundle.html#83090d926a78f87026b328db7e5e56b5" title="Accessors.">expiration_ts</a>()); <a name="l00053"></a>00053 } <a name="l00054"></a>00054 <a name="l00055"></a>00055 <span class="keywordtype">bool</span> <a name="l00056"></a><a class="code" href="classprophet_1_1AckList.html#74e71d0c1d98c6ae50141d04215bfe68">00056</a> <a class="code" href="classprophet_1_1AckList.html#fa05576d340ed75b61a4652132410f05" title="Convenience method for inserting Ack into list; return true upon success, else false...">AckList::insert</a>(<span class="keyword">const</span> <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a>* a) <a name="l00057"></a>00057 { <a name="l00058"></a>00058 <span class="keywordflow">if</span> (a == NULL) <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00059"></a>00059 <a name="l00060"></a>00060 <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a>* na = <span class="keyword">new</span> <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a>(*a); <a name="l00061"></a>00061 <a name="l00062"></a>00062 <span class="comment">// attempt to insert</span> <a name="l00063"></a>00063 <span class="keywordflow">if</span> (<a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.insert(na).second) <a name="l00064"></a>00064 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="comment">// failed, so free up memory</span> <a name="l00067"></a>00067 <span class="keyword">delete</span> na; <a name="l00068"></a>00068 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00069"></a>00069 } <a name="l00070"></a>00070 <a name="l00071"></a>00071 <span class="keywordtype">size_t</span> <a name="l00072"></a><a class="code" href="classprophet_1_1AckList.html#0d8fe683b08216b60c55b03126cad1da">00072</a> <a class="code" href="classprophet_1_1AckList.html#0d8fe683b08216b60c55b03126cad1da" title="Export list of Acks to PointerList&lt;Ack&gt;, return number of elements exported...">AckList::clone</a>(<a class="code" href="classprophet_1_1PointerList.html" title="Auto deletes pointers in list destructor This object assumes ownership for member...">PointerList<Ack></a>& list)<span class="keyword"> const</span> <a name="l00073"></a>00073 <span class="keyword"></span>{ <a name="l00074"></a>00074 <span class="keywordtype">size_t</span> num = 0; <a name="l00075"></a>00075 <span class="comment">// clear the incoming</span> <a name="l00076"></a>00076 list.<a class="code" href="classprophet_1_1PointerList.html#8e42a99a3be3314a5cc84b08ee80d69e" title="Delete all member variables, then remove pointers from container.">clear</a>(); <a name="l00077"></a>00077 <span class="comment">// walk the internal, visiting each Ack</span> <a name="l00078"></a>00078 <span class="keywordflow">for</span> (palist::const_iterator i = <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.begin(); i != <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.end(); i++) <a name="l00079"></a>00079 { <a name="l00080"></a>00080 <span class="comment">// give away a copy of each visited Ack</span> <a name="l00081"></a>00081 list.push_back(<span class="keyword">new</span> <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a>(**i)); <a name="l00082"></a>00082 <span class="comment">// ... counting as we go</span> <a name="l00083"></a>00083 num++; <a name="l00084"></a>00084 } <a name="l00085"></a>00085 <span class="comment">// inform caller of how many elements</span> <a name="l00086"></a>00086 <span class="keywordflow">return</span> num; <a name="l00087"></a>00087 } <a name="l00088"></a>00088 <a name="l00089"></a>00089 <span class="keywordtype">size_t</span> <a name="l00090"></a><a class="code" href="classprophet_1_1AckList.html#616758f72d318b5514d26abb54984864">00090</a> <a class="code" href="classprophet_1_1AckList.html#616758f72d318b5514d26abb54984864" title="Given a destination ID, return the number of Acks that match (exact match only, no...">AckList::fetch</a>(<span class="keyword">const</span> std::string& dest_id, <a class="code" href="classprophet_1_1PointerList.html" title="Auto deletes pointers in list destructor This object assumes ownership for member...">PointerList<Ack></a>* list)<span class="keyword"> const</span> <a name="l00091"></a>00091 <span class="keyword"></span>{ <a name="l00092"></a>00092 <span class="keywordtype">size_t</span> num = 0; <a name="l00093"></a>00093 <a name="l00094"></a>00094 <span class="comment">// clear incoming, if set</span> <a name="l00095"></a>00095 <span class="keywordflow">if</span> (list != NULL) list-><a class="code" href="classprophet_1_1PointerList.html#8e42a99a3be3314a5cc84b08ee80d69e" title="Delete all member variables, then remove pointers from container.">clear</a>(); <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="comment">// create a search parameter of palist::key_type</span> <a name="l00098"></a>00098 <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a> a(dest_id); <a name="l00099"></a>00099 <a name="l00100"></a>00100 <span class="comment">// find the beginning of the sequence that matches dest_id</span> <a name="l00101"></a>00101 palist::const_iterator i = <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.lower_bound(&a); <a name="l00102"></a>00102 <a name="l00103"></a>00103 <span class="comment">// walk along until either the string no longer matches or</span> <a name="l00104"></a>00104 <span class="comment">// we fall off the end of the sequence</span> <a name="l00105"></a>00105 <span class="keywordflow">while</span> (i != <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.end() && (*i)->dest_id().compare(dest_id) == 0) <a name="l00106"></a>00106 { <a name="l00107"></a>00107 <span class="comment">// if set, give the list pointer a copy of each Ack we find</span> <a name="l00108"></a>00108 <span class="keywordflow">if</span> (list != NULL) list->push_back(<span class="keyword">new</span> <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a>(**(i++))); <a name="l00109"></a>00109 num++; <a name="l00110"></a>00110 } <a name="l00111"></a>00111 <a name="l00112"></a>00112 <span class="comment">// inform the caller of how many were found</span> <a name="l00113"></a>00113 <span class="keywordflow">return</span> num; <a name="l00114"></a>00114 } <a name="l00115"></a>00115 <a name="l00116"></a>00116 <span class="keywordtype">size_t</span> <a name="l00117"></a><a class="code" href="classprophet_1_1AckList.html#a14d15ee8e5335cf8f5ac8a030cd21ac">00117</a> <a class="code" href="classprophet_1_1AckList.html#a14d15ee8e5335cf8f5ac8a030cd21ac" title="Visit every ACK in the list, and delete those for which the expiration has passed;...">AckList::expire</a>() <a name="l00118"></a>00118 { <a name="l00119"></a>00119 <span class="keywordtype">size_t</span> num = 0; <a name="l00120"></a>00120 <a name="l00121"></a>00121 <span class="comment">// grab current epoch seconds</span> <a name="l00122"></a>00122 time_t now = time(0); <a name="l00123"></a>00123 <a name="l00124"></a>00124 <span class="comment">// walk the list of Acks, careful how we increment since</span> <a name="l00125"></a>00125 <span class="comment">// the erase() operation screws up iterator state</span> <a name="l00126"></a>00126 <span class="keywordflow">for</span> (palist::iterator i = <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.begin(); i != <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.end(); ) <a name="l00127"></a>00127 { <a name="l00128"></a>00128 <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a>* a = *i; <a name="l00129"></a>00129 <span class="comment">// test for expired condition</span> <a name="l00130"></a>00130 <span class="keywordflow">if</span> (now - a-><a class="code" href="classprophet_1_1Ack.html#d79e15f9fb5c20b4558cfdce00157785" title="Accessors.">cts</a>() > a-><a class="code" href="classprophet_1_1Ack.html#22e89f209804069923aa19f86d4d0dab" title="Accessors.">ets</a>()) <a name="l00131"></a>00131 { <a name="l00132"></a>00132 num++; <a name="l00133"></a>00133 <span class="comment">// post increment to sidestep iterator screwiness</span> <a name="l00134"></a>00134 <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.erase(i++); <a name="l00135"></a>00135 <span class="comment">// clear up memory </span> <a name="l00136"></a>00136 <span class="keyword">delete</span> a; <a name="l00137"></a>00137 } <a name="l00138"></a>00138 <span class="comment">// not expired, increment past this one</span> <a name="l00139"></a>00139 <span class="keywordflow">else</span> i++; <a name="l00140"></a>00140 } <a name="l00141"></a>00141 <span class="comment">// inform caller of how many were removed</span> <a name="l00142"></a>00142 <span class="keywordflow">return</span> num; <a name="l00143"></a>00143 } <a name="l00144"></a>00144 <a name="l00145"></a>00145 <span class="keywordtype">bool</span> <a name="l00146"></a><a class="code" href="classprophet_1_1AckList.html#217ee60142873856de9ffb877c95c3fb">00146</a> <a class="code" href="classprophet_1_1AckList.html#217ee60142873856de9ffb877c95c3fb" title="Convenience function to answer the question of whether this Bundle has been Ack&#39;d...">AckList::is_ackd</a>(<span class="keyword">const</span> std::string& dest_id, <a name="l00147"></a>00147 u_int32_t cts, u_int32_t seq)<span class="keyword"> const</span> <a name="l00148"></a>00148 <span class="keyword"></span>{ <a name="l00149"></a>00149 <span class="comment">// create search parameter of type palist::key_type</span> <a name="l00150"></a>00150 <a class="code" href="classprophet_1_1Ack.html" title="A Prophet ACK signals successful delivery of a Bundle to its final destination within...">Ack</a> a(dest_id); <a name="l00151"></a>00151 <a name="l00152"></a>00152 <span class="comment">// initialize iterator to beginning of matching sequence</span> <a name="l00153"></a>00153 palist::iterator i = <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.lower_bound(&a); <a name="l00154"></a>00154 <a name="l00155"></a>00155 <span class="comment">// walk the sequence until no more matches, or end of sequence</span> <a name="l00156"></a>00156 <span class="keywordflow">while</span> (i != <a class="code" href="classprophet_1_1AckList.html#6180c0f2187b6c5fb36e44b2f2044b9f">acks_</a>.end()) <a name="l00157"></a>00157 { <a name="l00158"></a>00158 <span class="comment">// test for no-more-matches</span> <a name="l00159"></a>00159 <span class="keywordflow">if</span> ((*i)->dest_id().compare(dest_id) != 0) <a name="l00160"></a>00160 <span class="keywordflow">break</span>; <a name="l00161"></a>00161 <span class="comment">// all parameters match?</span> <a name="l00162"></a>00162 <span class="keywordflow">if</span> ((*i)->cts() == cts && (*i)->seq() == seq) <a name="l00163"></a>00163 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00164"></a>00164 i++; <a name="l00165"></a>00165 } <a name="l00166"></a>00166 <span class="comment">// not found, therefore must not be Ack'd</span> <a name="l00167"></a>00167 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00168"></a>00168 } <a name="l00169"></a>00169 <a name="l00170"></a>00170 }; <span class="comment">// namespace prophet</span> </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Mon Jul 21 14:09:37 2008 for DTN Reference Implementation by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.5.6 </small></address> </body> </html>