<!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: FragmentManager.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>FragmentManager.cc</h1><a href="FragmentManager_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 2004-2006 Intel Corporation</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">#ifdef HAVE_CONFIG_H</span> <a name="l00018"></a>00018 <span class="preprocessor"></span><span class="preprocessor"># include <<a class="code" href="dtn-config_8h.html">dtn-config.h</a>></span> <a name="l00019"></a>00019 <span class="preprocessor">#endif</span> <a name="l00020"></a>00020 <span class="preprocessor"></span> <a name="l00021"></a>00021 <span class="preprocessor">#include <list></span> <a name="l00022"></a>00022 <a name="l00023"></a>00023 <span class="preprocessor">#include "<a class="code" href="bundling_2Bundle_8h.html">Bundle.h</a>"</span> <a name="l00024"></a>00024 <span class="preprocessor">#include "<a class="code" href="BundleEvent_8h.html">BundleEvent.h</a>"</span> <a name="l00025"></a>00025 <span class="preprocessor">#include "<a class="code" href="BundleDaemon_8h.html">BundleDaemon.h</a>"</span> <a name="l00026"></a>00026 <span class="preprocessor">#include "<a class="code" href="bundling_2BundleList_8h.html">BundleList.h</a>"</span> <a name="l00027"></a>00027 <span class="preprocessor">#include "<a class="code" href="BundleRef_8h.html">BundleRef.h</a>"</span> <a name="l00028"></a>00028 <span class="preprocessor">#include "<a class="code" href="FragmentManager_8h.html">FragmentManager.h</a>"</span> <a name="l00029"></a>00029 <span class="preprocessor">#include "<a class="code" href="FragmentState_8h.html">FragmentState.h</a>"</span> <a name="l00030"></a>00030 <span class="preprocessor">#include "<a class="code" href="BlockInfo_8h.html">BlockInfo.h</a>"</span> <a name="l00031"></a>00031 <span class="preprocessor">#include "<a class="code" href="BundleProtocol_8h.html">BundleProtocol.h</a>"</span> <a name="l00032"></a>00032 <a name="l00033"></a>00033 <span class="keyword">namespace </span>dtn { <a name="l00034"></a>00034 <a name="l00035"></a><a class="code" href="classdtn_1_1BlockInfoPointerList.html">00035</a> <span class="keyword">class </span><a class="code" href="classdtn_1_1BlockInfoPointerList.html">BlockInfoPointerList</a> : <span class="keyword">public</span> std::list<BlockInfo*> { }; <a name="l00036"></a>00036 <a name="l00037"></a>00037 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00038"></a><a class="code" href="classdtn_1_1FragmentManager.html#694dece64b76aaddff128b23e3c5a06f">00038</a> <a class="code" href="classdtn_1_1FragmentManager.html#694dece64b76aaddff128b23e3c5a06f" title="Constructor.">FragmentManager::FragmentManager</a>() <a name="l00039"></a>00039 : <a class="code" href="classoasys_1_1Logger.html">Logger</a>(<span class="stringliteral">"FragmentManager"</span>, <span class="stringliteral">"/dtn/bundle/fragmentation"</span>) <a name="l00040"></a>00040 { <a name="l00041"></a>00041 } <a name="l00042"></a>00042 <a name="l00043"></a>00043 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00044"></a>00044 <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* <a name="l00045"></a><a class="code" href="classdtn_1_1FragmentManager.html#f6f29fb926b821b27d8d6cda64ffa96a">00045</a> FragmentManager::create_fragment(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00046"></a>00046 <a class="code" href="classdtn_1_1BlockInfoVec.html" title="Class for a vector of BlockInfo structures.">BlockInfoVec</a> *blocks, <a name="l00047"></a>00047 <span class="keywordtype">size_t</span> offset, <a name="l00048"></a>00048 <span class="keywordtype">size_t</span> length) <a name="l00049"></a>00049 { <a name="l00050"></a>00050 <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* fragment = <span class="keyword">new</span> <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>(); <a name="l00051"></a>00051 <a name="l00052"></a>00052 <span class="comment">// copy the metadata into the new fragment (which can be further fragmented)</span> <a name="l00053"></a>00053 bundle-><a class="code" href="classdtn_1_1Bundle.html#659027ae11e78a7527b3e31e7ebf0eb1" title="Copy the metadata from one bundle to another (used in fragmentation).">copy_metadata</a>(fragment); <a name="l00054"></a>00054 fragment-><a class="code" href="classdtn_1_1Bundle.html#fc3bc80899d4f0c92b163b2d662cd637" title="Setters and mutable accessors.">set_is_fragment</a>(<span class="keyword">true</span>); <a name="l00055"></a>00055 fragment-><a class="code" href="classdtn_1_1Bundle.html#ce75b35784c110dc09986632e9d9eba1" title="Setters and mutable accessors.">set_do_not_fragment</a>(<span class="keyword">false</span>); <a name="l00056"></a>00056 <a name="l00057"></a>00057 <span class="comment">// initialize the fragment's orig_length and figure out the offset</span> <a name="l00058"></a>00058 <span class="comment">// into the payload</span> <a name="l00059"></a>00059 <span class="keywordflow">if</span> (! bundle-><a class="code" href="classdtn_1_1Bundle.html#d6c74eba219bfe55854cf7a90e210cca" title="Accessors.">is_fragment</a>()) { <a name="l00060"></a>00060 fragment-><a class="code" href="classdtn_1_1Bundle.html#ad892616cfb568fbe458106f589ce8d3" title="Setters and mutable accessors.">set_orig_length</a>(bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>()); <a name="l00061"></a>00061 fragment-><a class="code" href="classdtn_1_1Bundle.html#7e050ba82bf916772a0971e118dc31ee" title="Setters and mutable accessors.">set_frag_offset</a>(offset); <a name="l00062"></a>00062 } <span class="keywordflow">else</span> { <a name="l00063"></a>00063 fragment-><a class="code" href="classdtn_1_1Bundle.html#ad892616cfb568fbe458106f589ce8d3" title="Setters and mutable accessors.">set_orig_length</a>(bundle-><a class="code" href="classdtn_1_1Bundle.html#e32f3c57057214a1763dfe7709160cf8" title="Accessors.">orig_length</a>()); <a name="l00064"></a>00064 fragment-><a class="code" href="classdtn_1_1Bundle.html#7e050ba82bf916772a0971e118dc31ee" title="Setters and mutable accessors.">set_frag_offset</a>(bundle-><a class="code" href="classdtn_1_1Bundle.html#76cf6a44f566aa22e2a98007bf233ee2" title="Accessors.">frag_offset</a>() + offset); <a name="l00065"></a>00065 } <a name="l00066"></a>00066 <a name="l00067"></a>00067 <span class="comment">// check for overallocated length</span> <a name="l00068"></a>00068 <span class="keywordflow">if</span> ((offset + length) > fragment-><a class="code" href="classdtn_1_1Bundle.html#e32f3c57057214a1763dfe7709160cf8" title="Accessors.">orig_length</a>()) { <a name="l00069"></a>00069 PANIC(<span class="stringliteral">"fragment length overrun: "</span> <a name="l00070"></a>00070 <span class="stringliteral">"orig_length %u frag_offset %u requested offset %zu length %zu"</span>, <a name="l00071"></a>00071 fragment-><a class="code" href="classdtn_1_1Bundle.html#e32f3c57057214a1763dfe7709160cf8" title="Accessors.">orig_length</a>(), fragment-><a class="code" href="classdtn_1_1Bundle.html#76cf6a44f566aa22e2a98007bf233ee2" title="Accessors.">frag_offset</a>(), <a name="l00072"></a>00072 offset, length); <a name="l00073"></a>00073 } <a name="l00074"></a>00074 <a name="l00075"></a>00075 <span class="comment">// initialize payload</span> <a name="l00076"></a>00076 fragment-><a class="code" href="classdtn_1_1Bundle.html#857079bad68497626f75c2985b69052f" title="Setters and mutable accessors.">mutable_payload</a>()-><a class="code" href="classdtn_1_1BundlePayload.html#af1ede6096a6d625fb83c001a9d490d6" title="Set the payload length in preparation for filling in with data.">set_length</a>(length); <a name="l00077"></a>00077 fragment-><a class="code" href="classdtn_1_1Bundle.html#857079bad68497626f75c2985b69052f" title="Setters and mutable accessors.">mutable_payload</a>()-><a class="code" href="classdtn_1_1BundlePayload.html#7f5185039b03f0c875a12136b6b24862" title="Write a chunk of payload data at the specified offset.">write_data</a>(bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>(), offset, length, 0); <a name="l00078"></a>00078 <a name="l00079"></a>00079 <span class="comment">// copy all blocks that follow the payload, and all those before</span> <a name="l00080"></a>00080 <span class="comment">// the payload that are marked with the "must be replicated in every</span> <a name="l00081"></a>00081 <span class="comment">// fragment" bit</span> <a name="l00082"></a>00082 BlockInfoVec::iterator iter; <a name="l00083"></a>00083 <span class="keywordtype">bool</span> found_payload = <span class="keyword">false</span>; <a name="l00084"></a>00084 <span class="keywordflow">for</span> (iter = blocks->begin(); iter != blocks->end(); iter++) { <a name="l00085"></a>00085 <span class="keywordtype">int</span> type = iter->type(); <a name="l00086"></a>00086 <span class="keywordflow">if</span> (type == <a class="code" href="classdtn_1_1BundleProtocol.html#54a44c247b3c20b4ed11e29494284ca753097979cfe600d2c1dfb5cf3629b195" title="INTERNAL ONLY -- NOT IN SPEC.">BundleProtocol::PRIMARY_BLOCK</a> <a name="l00087"></a>00087 || type == <a class="code" href="classdtn_1_1BundleProtocol.html#54a44c247b3c20b4ed11e29494284ca711ba3c0a764c41add8e738d757393d60">BundleProtocol::PAYLOAD_BLOCK</a> <a name="l00088"></a>00088 || found_payload <a name="l00089"></a>00089 || iter->flags() & <a class="code" href="classdtn_1_1BundleProtocol.html#cd56fabd4393bd86ffc295bafaba540b615404458f982d50e1fea01b9d383146">BundleProtocol::BLOCK_FLAG_REPLICATE</a>) { <a name="l00090"></a>00090 <a name="l00091"></a>00091 <span class="comment">// we need to include this block; copy the BlockInfo into the</span> <a name="l00092"></a>00092 <span class="comment">// fragment</span> <a name="l00093"></a>00093 fragment-><a class="code" href="classdtn_1_1Bundle.html#5c48f51fba15c89a82d5d98c9881f0bd" title="Setters and mutable accessors.">mutable_recv_blocks</a>()->push_back(*iter); <a name="l00094"></a>00094 <span class="keywordflow">if</span> (type == <a class="code" href="classdtn_1_1BundleProtocol.html#54a44c247b3c20b4ed11e29494284ca711ba3c0a764c41add8e738d757393d60">BundleProtocol::PAYLOAD_BLOCK</a>) { <a name="l00095"></a>00095 found_payload = <span class="keyword">true</span>; <a name="l00096"></a>00096 } <a name="l00097"></a>00097 } <a name="l00098"></a>00098 } <a name="l00099"></a>00099 <a name="l00100"></a>00100 <span class="keywordflow">return</span> fragment; <a name="l00101"></a>00101 } <a name="l00102"></a>00102 <a name="l00103"></a>00103 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00104"></a>00104 <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* <a name="l00105"></a><a class="code" href="classdtn_1_1FragmentManager.html#240ed97b1cdf027902042e9571b9beec">00105</a> <a class="code" href="classdtn_1_1FragmentManager.html#f6f29fb926b821b27d8d6cda64ffa96a" title="Create a bundle fragment from another bundle.">FragmentManager::create_fragment</a>(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00106"></a>00106 <span class="keyword">const</span> <a class="code" href="namespacedtn.html#6efb37e503f8062c537b022eb755b94e" title="Typedef for a reference on a link.">LinkRef</a>& link, <a name="l00107"></a>00107 <span class="keyword">const</span> <a class="code" href="classdtn_1_1BlockInfoPointerList.html">BlockInfoPointerList</a>& blocks_to_copy, <a name="l00108"></a>00108 <span class="keywordtype">size_t</span> offset, <a name="l00109"></a>00109 <span class="keywordtype">size_t</span> max_length) <a name="l00110"></a>00110 { <a name="l00111"></a>00111 <span class="keywordtype">size_t</span> block_length = 0; <a name="l00112"></a>00112 BlockInfoPointerList::const_iterator block_i; <a name="l00113"></a>00113 <a name="l00114"></a>00114 <span class="keywordflow">for</span> (block_i = blocks_to_copy.begin(); <a name="l00115"></a>00115 block_i != blocks_to_copy.end(); <a name="l00116"></a>00116 ++block_i) { <a name="l00117"></a>00117 block_length += (*block_i)->contents().len(); <a name="l00118"></a>00118 } <a name="l00119"></a>00119 <a name="l00120"></a>00120 <span class="keywordflow">if</span> (block_length > max_length) { <a name="l00121"></a>00121 log_err(<span class="stringliteral">"unable to create a fragment of length %zu; minimum length "</span> <a name="l00122"></a>00122 <span class="stringliteral">"required is %zu"</span>, max_length, block_length); <a name="l00123"></a>00123 <span class="keywordflow">return</span> NULL; <a name="l00124"></a>00124 } <a name="l00125"></a>00125 <a name="l00126"></a>00126 <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* fragment = <span class="keyword">new</span> <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>(); <a name="l00127"></a>00127 <a name="l00128"></a>00128 <span class="comment">// copy the metadata into the new fragment (which can be further fragmented)</span> <a name="l00129"></a>00129 bundle-><a class="code" href="classdtn_1_1Bundle.html#659027ae11e78a7527b3e31e7ebf0eb1" title="Copy the metadata from one bundle to another (used in fragmentation).">copy_metadata</a>(fragment); <a name="l00130"></a>00130 fragment-><a class="code" href="classdtn_1_1Bundle.html#fc3bc80899d4f0c92b163b2d662cd637" title="Setters and mutable accessors.">set_is_fragment</a>(<span class="keyword">true</span>); <a name="l00131"></a>00131 fragment-><a class="code" href="classdtn_1_1Bundle.html#ce75b35784c110dc09986632e9d9eba1" title="Setters and mutable accessors.">set_do_not_fragment</a>(<span class="keyword">false</span>); <a name="l00132"></a>00132 <a name="l00133"></a>00133 <span class="comment">// initialize the fragment's orig_length and figure out the offset</span> <a name="l00134"></a>00134 <span class="comment">// into the payload</span> <a name="l00135"></a>00135 <span class="keywordflow">if</span> (! bundle-><a class="code" href="classdtn_1_1Bundle.html#d6c74eba219bfe55854cf7a90e210cca" title="Accessors.">is_fragment</a>()) { <a name="l00136"></a>00136 fragment-><a class="code" href="classdtn_1_1Bundle.html#ad892616cfb568fbe458106f589ce8d3" title="Setters and mutable accessors.">set_orig_length</a>(bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>()); <a name="l00137"></a>00137 fragment-><a class="code" href="classdtn_1_1Bundle.html#7e050ba82bf916772a0971e118dc31ee" title="Setters and mutable accessors.">set_frag_offset</a>(offset); <a name="l00138"></a>00138 } <span class="keywordflow">else</span> { <a name="l00139"></a>00139 fragment-><a class="code" href="classdtn_1_1Bundle.html#ad892616cfb568fbe458106f589ce8d3" title="Setters and mutable accessors.">set_orig_length</a>(bundle-><a class="code" href="classdtn_1_1Bundle.html#e32f3c57057214a1763dfe7709160cf8" title="Accessors.">orig_length</a>()); <a name="l00140"></a>00140 fragment-><a class="code" href="classdtn_1_1Bundle.html#7e050ba82bf916772a0971e118dc31ee" title="Setters and mutable accessors.">set_frag_offset</a>(bundle-><a class="code" href="classdtn_1_1Bundle.html#76cf6a44f566aa22e2a98007bf233ee2" title="Accessors.">frag_offset</a>() + offset); <a name="l00141"></a>00141 } <a name="l00142"></a>00142 <a name="l00143"></a>00143 <span class="comment">// initialize payload</span> <a name="l00144"></a>00144 <span class="keywordtype">size_t</span> to_copy = std::min(max_length - block_length, <a name="l00145"></a>00145 bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>() - offset); <a name="l00146"></a>00146 fragment-><a class="code" href="classdtn_1_1Bundle.html#857079bad68497626f75c2985b69052f" title="Setters and mutable accessors.">mutable_payload</a>()-><a class="code" href="classdtn_1_1BundlePayload.html#af1ede6096a6d625fb83c001a9d490d6" title="Set the payload length in preparation for filling in with data.">set_length</a>(to_copy); <a name="l00147"></a>00147 fragment-><a class="code" href="classdtn_1_1Bundle.html#857079bad68497626f75c2985b69052f" title="Setters and mutable accessors.">mutable_payload</a>()-><a class="code" href="classdtn_1_1BundlePayload.html#7f5185039b03f0c875a12136b6b24862" title="Write a chunk of payload data at the specified offset.">write_data</a>(bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>(), offset, to_copy, 0); <a name="l00148"></a>00148 <a class="code" href="classdtn_1_1BlockInfoVec.html" title="Class for a vector of BlockInfo structures.">BlockInfoVec</a>* xmit_blocks = fragment-><a class="code" href="classdtn_1_1Bundle.html#0ce0961036dd0da1fd322721cbb794aa" title="Setters and mutable accessors.">xmit_blocks</a>()-><a class="code" href="classdtn_1_1LinkBlockSet.html#7fd0f75b4caa20c373c532cc8256e86c" title="Create a new BlockInfoVec for the given link.">create_blocks</a>(link); <a name="l00149"></a>00149 <a name="l00150"></a>00150 <span class="keywordflow">for</span> (block_i = blocks_to_copy.begin(); <a name="l00151"></a>00151 block_i != blocks_to_copy.end(); <a name="l00152"></a>00152 ++block_i) { <a name="l00153"></a>00153 xmit_blocks->push_back(<a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a>(*(*block_i))); <a name="l00154"></a>00154 } <a name="l00155"></a>00155 <a name="l00156"></a>00156 log_debug(<span class="stringliteral">"created %zu byte fragment bundle with %zu bytes of payload"</span>, <a name="l00157"></a>00157 to_copy + block_length, to_copy); <a name="l00158"></a>00158 <a name="l00159"></a>00159 <span class="keywordflow">return</span> fragment; <a name="l00160"></a>00160 } <a name="l00161"></a>00161 <a name="l00162"></a>00162 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00163"></a>00163 <span class="keywordtype">bool</span> <a name="l00164"></a><a class="code" href="classdtn_1_1FragmentManager.html#d3a2b8e4ecd2838da9caee845f47dccb">00164</a> <a class="code" href="classdtn_1_1FragmentManager.html#d3a2b8e4ecd2838da9caee845f47dccb" title="Convert a partially received bundle into a fragment.">FragmentManager::try_to_convert_to_fragment</a>(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle) <a name="l00165"></a>00165 { <a name="l00166"></a>00166 <span class="keyword">const</span> <a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a> *payload_block <a name="l00167"></a>00167 = bundle-><a class="code" href="classdtn_1_1Bundle.html#ec3c2ef7670fb2d34d46ab547bf9fb80" title="Accessors.">recv_blocks</a>().<a class="code" href="classdtn_1_1BlockInfoVec.html#646316c3b0fb301712bb39fdbfd3207e" title="Find the block for the given type.">find_block</a>(<a class="code" href="classdtn_1_1BundleProtocol.html#54a44c247b3c20b4ed11e29494284ca711ba3c0a764c41add8e738d757393d60">BundleProtocol::PAYLOAD_BLOCK</a>); <a name="l00168"></a>00168 <span class="keywordflow">if</span> (!payload_block) { <a name="l00169"></a>00169 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// can't do anything</span> <a name="l00170"></a>00170 } <a name="l00171"></a>00171 <span class="keywordflow">if</span> (payload_block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>() == 0) { <a name="l00172"></a>00172 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// there is not even enough data for the preamble</span> <a name="l00173"></a>00173 } <a name="l00174"></a>00174 <a name="l00175"></a>00175 <span class="keywordflow">if</span> (bundle-><a class="code" href="classdtn_1_1Bundle.html#1931c2eeed81432743735dd245c8b95d" title="Accessors.">do_not_fragment</a>()) { <a name="l00176"></a>00176 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// can't do anything</span> <a name="l00177"></a>00177 } <a name="l00178"></a>00178 <a name="l00179"></a>00179 <span class="comment">// the payload is already truncated to the length that was received</span> <a name="l00180"></a>00180 <span class="keywordtype">size_t</span> payload_len = payload_block-><a class="code" href="classdtn_1_1BlockInfo.html#cb7676dfe53630699317388ad32e8d99" title="Accessors.">data_length</a>(); <a name="l00181"></a>00181 <span class="keywordtype">size_t</span> payload_rcvd = bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>(); <a name="l00182"></a>00182 <a name="l00183"></a>00183 <span class="comment">// A fragment cannot be created with only one byte of payload</span> <a name="l00184"></a>00184 <span class="comment">// available.</span> <a name="l00185"></a>00185 <span class="keywordflow">if</span> (payload_len <= 1) { <a name="l00186"></a>00186 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00187"></a>00187 } <a name="l00188"></a>00188 <a name="l00189"></a>00189 <span class="keywordflow">if</span> (payload_rcvd >= payload_len) { <a name="l00190"></a>00190 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(payload_block-><a class="code" href="classdtn_1_1BlockInfo.html#a70c73cf28ec51a265dc5ac077c56975" title="Accessors.">complete</a>() || payload_len == 0); <a name="l00191"></a>00191 <a name="l00192"></a>00192 <span class="keywordflow">if</span> (payload_block-><a class="code" href="classdtn_1_1BlockInfo.html#9688c4db3afee01ca64d83029358c387" title="Accessors.">last_block</a>()) { <a name="l00193"></a>00193 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// nothing to do - whole bundle present</span> <a name="l00194"></a>00194 } <a name="l00195"></a>00195 <a name="l00196"></a>00196 <span class="comment">// If the payload block is not the last block, there are extension</span> <a name="l00197"></a>00197 <span class="comment">// blocks following it. See if they all appear to be present.</span> <a name="l00198"></a>00198 BlockInfoVec::const_iterator last_block = <a name="l00199"></a>00199 bundle-><a class="code" href="classdtn_1_1Bundle.html#ec3c2ef7670fb2d34d46ab547bf9fb80" title="Accessors.">recv_blocks</a>().end() - 1; <a name="l00200"></a>00200 <a name="l00201"></a>00201 <span class="keywordflow">if</span> (last_block->data_offset() != 0 && last_block->complete() <a name="l00202"></a>00202 && last_block->last_block()) { <a name="l00203"></a>00203 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// nothing to do - whole bundle present</span> <a name="l00204"></a>00204 } <a name="l00205"></a>00205 <a name="l00206"></a>00206 <span class="comment">// At this point the payload is complete but the bundle is not,</span> <a name="l00207"></a>00207 <span class="comment">// so force the creation of a fragment by dropping a byte.</span> <a name="l00208"></a>00208 payload_rcvd--; <a name="l00209"></a>00209 bundle-><a class="code" href="classdtn_1_1Bundle.html#857079bad68497626f75c2985b69052f" title="Setters and mutable accessors.">mutable_payload</a>()-><a class="code" href="classdtn_1_1BundlePayload.html#571daa527ed47d397080c96603d72b5c" title="Truncate the payload.">truncate</a>(payload_rcvd); <a name="l00210"></a>00210 } <a name="l00211"></a>00211 <a name="l00212"></a>00212 log_debug(<span class="stringliteral">"partial bundle *%p, making reactive fragment of %zu bytes"</span>, <a name="l00213"></a>00213 bundle, payload_rcvd); <a name="l00214"></a>00214 <a name="l00215"></a>00215 <span class="keywordflow">if</span> (! bundle-><a class="code" href="classdtn_1_1Bundle.html#d6c74eba219bfe55854cf7a90e210cca" title="Accessors.">is_fragment</a>()) { <a name="l00216"></a>00216 bundle-><a class="code" href="classdtn_1_1Bundle.html#fc3bc80899d4f0c92b163b2d662cd637" title="Setters and mutable accessors.">set_is_fragment</a>(<span class="keyword">true</span>); <a name="l00217"></a>00217 bundle-><a class="code" href="classdtn_1_1Bundle.html#ad892616cfb568fbe458106f589ce8d3" title="Setters and mutable accessors.">set_orig_length</a>(payload_len); <a name="l00218"></a>00218 bundle-><a class="code" href="classdtn_1_1Bundle.html#7e050ba82bf916772a0971e118dc31ee" title="Setters and mutable accessors.">set_frag_offset</a>(0); <a name="l00219"></a>00219 } <span class="keywordflow">else</span> { <a name="l00220"></a>00220 <span class="comment">// if it was already a fragment, the fragment headers are</span> <a name="l00221"></a>00221 <span class="comment">// already correct</span> <a name="l00222"></a>00222 } <a name="l00223"></a>00223 bundle-><a class="code" href="classdtn_1_1Bundle.html#cf35d164ad5f7249e22d6bd631a2f944" title="Setters and mutable accessors.">set_fragmented_incoming</a>(<span class="keyword">true</span>); <a name="l00224"></a>00224 <a name="l00225"></a>00225 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00226"></a>00226 } <a name="l00227"></a>00227 <a name="l00228"></a>00228 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00229"></a>00229 <span class="keywordtype">void</span> <a name="l00230"></a><a class="code" href="classdtn_1_1FragmentManager.html#14e6759dca799277da24a3425274d9f9">00230</a> <a class="code" href="classdtn_1_1FragmentManager.html#14e6759dca799277da24a3425274d9f9" title="Calculate a hash table key from a bundle.">FragmentManager::get_hash_key</a>(<span class="keyword">const</span> <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, std::string* key) <a name="l00231"></a>00231 { <a name="l00232"></a>00232 <span class="keywordtype">char</span> <a class="code" href="num2sdnv_8c.html#a81cdcc7ff6987bc85c073253e32715f">buf</a>[128]; <a name="l00233"></a>00233 snprintf(buf, 128, <span class="stringliteral">"%u.%u"</span>, <a name="l00234"></a>00234 bundle-><a class="code" href="classdtn_1_1Bundle.html#13cd9ab7b010f09562170e2a57242209" title="Accessors.">creation_ts</a>().<a class="code" href="structdtn_1_1BundleTimestamp.html#02c8a347150620243240655bdcd2f42d" title="Seconds since 1/1/2000.">seconds_</a>, <a name="l00235"></a>00235 bundle-><a class="code" href="classdtn_1_1Bundle.html#13cd9ab7b010f09562170e2a57242209" title="Accessors.">creation_ts</a>().<a class="code" href="structdtn_1_1BundleTimestamp.html#ebc8cea66862d7f6056da450db410347" title="Sub-second sequence number.">seqno_</a>); <a name="l00236"></a>00236 <a name="l00237"></a>00237 key->append(buf); <a name="l00238"></a>00238 key->append(bundle-><a class="code" href="classdtn_1_1Bundle.html#2dae8a97270a95799acf45d859eaf866" title="Accessors.">source</a>().<a class="code" href="classdtn_1_1EndpointID.html#c9d8a72806359621f4d2850c1e78269c">c_str</a>()); <a name="l00239"></a>00239 key->append(bundle-><a class="code" href="classdtn_1_1Bundle.html#507241e8ac766e1900009ed6243e2aa3" title="Accessors.">dest</a>().<a class="code" href="classdtn_1_1EndpointID.html#c9d8a72806359621f4d2850c1e78269c">c_str</a>()); <a name="l00240"></a>00240 } <a name="l00241"></a>00241 <a name="l00242"></a>00242 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00243"></a>00243 <a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>* <a name="l00244"></a><a class="code" href="classdtn_1_1FragmentManager.html#fbc6aa92f31a9e8a72cadb302e9c8a54">00244</a> <a class="code" href="classdtn_1_1FragmentManager.html#fbc6aa92f31a9e8a72cadb302e9c8a54" title="Given the given fragmentation threshold, determine whether the given bundle should...">FragmentManager::proactively_fragment</a>(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00245"></a>00245 <span class="keyword">const</span> <a class="code" href="namespacedtn.html#6efb37e503f8062c537b022eb755b94e" title="Typedef for a reference on a link.">LinkRef</a>& link, <a name="l00246"></a>00246 <span class="keywordtype">size_t</span> max_length) <a name="l00247"></a>00247 { <a name="l00248"></a>00248 <span class="keywordtype">size_t</span> payload_len = bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>(); <a name="l00249"></a>00249 <a name="l00250"></a>00250 <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* fragment; <a name="l00251"></a>00251 <a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>* state = <span class="keyword">new</span> <a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>(bundle); <a name="l00252"></a>00252 <a name="l00253"></a>00253 <span class="keywordtype">size_t</span> todo = payload_len; <a name="l00254"></a>00254 <span class="keywordtype">size_t</span> offset = 0; <a name="l00255"></a>00255 <span class="keywordtype">size_t</span> <a class="code" href="dtnping_8c.html#d43c3812e6d13e0518d9f8b8f463ffcf">count</a> = 0; <a name="l00256"></a>00256 <a name="l00257"></a>00257 <a class="code" href="classdtn_1_1BlockInfoPointerList.html">BlockInfoPointerList</a> first_frag_blocks; <a name="l00258"></a>00258 <a class="code" href="classdtn_1_1BlockInfoPointerList.html">BlockInfoPointerList</a> all_frag_blocks; <a name="l00259"></a>00259 <a class="code" href="classdtn_1_1BlockInfoPointerList.html">BlockInfoPointerList</a>& this_frag_blocks = first_frag_blocks; <a name="l00260"></a>00260 <a class="code" href="classdtn_1_1BlockInfoVec.html" title="Class for a vector of BlockInfo structures.">BlockInfoVec</a>* blocks = bundle-><a class="code" href="classdtn_1_1Bundle.html#0ce0961036dd0da1fd322721cbb794aa" title="Setters and mutable accessors.">xmit_blocks</a>()-><a class="code" href="classdtn_1_1LinkBlockSet.html#07658a9543619b3ab3f1493a2dd68e85" title="Find the BlockInfoVec for the given link.">find_blocks</a>(link); <a name="l00261"></a>00261 <a name="l00262"></a>00262 BlockInfoVec::iterator block_i; <a name="l00263"></a>00263 <span class="keywordflow">for</span> (block_i = blocks->begin(); block_i != blocks->end(); ++block_i) { <a name="l00264"></a>00264 <a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a>* block_info = &(*block_i); <a name="l00265"></a>00265 <a name="l00266"></a>00266 <span class="keywordflow">if</span> (block_info-><a class="code" href="classdtn_1_1BlockInfo.html#46e4157d30528eae60a1cd240e58b495">type</a>() == <a class="code" href="classdtn_1_1BundleProtocol.html#54a44c247b3c20b4ed11e29494284ca753097979cfe600d2c1dfb5cf3629b195" title="INTERNAL ONLY -- NOT IN SPEC.">BundleProtocol::PRIMARY_BLOCK</a> || <a name="l00267"></a>00267 block_info-><a class="code" href="classdtn_1_1BlockInfo.html#46e4157d30528eae60a1cd240e58b495">type</a>() == <a class="code" href="classdtn_1_1BundleProtocol.html#54a44c247b3c20b4ed11e29494284ca711ba3c0a764c41add8e738d757393d60">BundleProtocol::PAYLOAD_BLOCK</a>) { <a name="l00268"></a>00268 all_frag_blocks.push_back(block_info); <a name="l00269"></a>00269 first_frag_blocks.push_back(block_info); <a name="l00270"></a>00270 } <a name="l00271"></a>00271 <a name="l00272"></a>00272 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (block_info-><a class="code" href="classdtn_1_1BlockInfo.html#51f21d5cf4aef2550b605ab90961e045">flags</a>() & <a class="code" href="classdtn_1_1BundleProtocol.html#cd56fabd4393bd86ffc295bafaba540b615404458f982d50e1fea01b9d383146">BundleProtocol::BLOCK_FLAG_REPLICATE</a>) <a name="l00273"></a>00273 all_frag_blocks.push_back(block_info); <a name="l00274"></a>00274 <span class="keywordflow">else</span> <a name="l00275"></a>00275 first_frag_blocks.push_back(block_info); <a name="l00276"></a>00276 } <a name="l00277"></a>00277 <a name="l00278"></a>00278 <span class="keywordflow">do</span> { <a name="l00279"></a>00279 fragment = <a class="code" href="classdtn_1_1FragmentManager.html#f6f29fb926b821b27d8d6cda64ffa96a" title="Create a bundle fragment from another bundle.">create_fragment</a>(bundle, link, this_frag_blocks, <a name="l00280"></a>00280 offset, max_length); <a name="l00281"></a>00281 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(fragment); <a name="l00282"></a>00282 <a name="l00283"></a>00283 state-><a class="code" href="classdtn_1_1FragmentState.html#cec4c98230bf6a2f84184d227329f36a">add_fragment</a>(fragment); <a name="l00284"></a>00284 offset += fragment-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>(); <a name="l00285"></a>00285 todo -= fragment-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>(); <a name="l00286"></a>00286 this_frag_blocks = all_frag_blocks; <a name="l00287"></a>00287 ++count; <a name="l00288"></a>00288 <a name="l00289"></a>00289 } <span class="keywordflow">while</span> (todo > 0); <a name="l00290"></a>00290 <a name="l00291"></a>00291 log_info(<span class="stringliteral">"proactively fragmenting "</span> <a name="l00292"></a>00292 <span class="stringliteral">"%zu byte payload into %zu %zu byte fragments"</span>, <a name="l00293"></a>00293 payload_len, count, max_length); <a name="l00294"></a>00294 <a name="l00295"></a>00295 std::string hash_key; <a name="l00296"></a>00296 <a class="code" href="classdtn_1_1FragmentManager.html#14e6759dca799277da24a3425274d9f9" title="Calculate a hash table key from a bundle.">get_hash_key</a>(fragment, &hash_key); <a name="l00297"></a>00297 <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>[hash_key] = state; <a name="l00298"></a>00298 <a name="l00299"></a>00299 <span class="keywordflow">return</span> state; <a name="l00300"></a>00300 } <a name="l00301"></a>00301 <a name="l00302"></a>00302 <a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>* <a name="l00303"></a><a class="code" href="classdtn_1_1FragmentManager.html#6d648fcee877721cf9e6448b24641246">00303</a> <a class="code" href="classdtn_1_1FragmentManager.html#6d648fcee877721cf9e6448b24641246">FragmentManager::get_fragment_state</a>(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle) <a name="l00304"></a>00304 { <a name="l00305"></a>00305 std::string hash_key; <a name="l00306"></a>00306 <a class="code" href="classdtn_1_1FragmentManager.html#14e6759dca799277da24a3425274d9f9" title="Calculate a hash table key from a bundle.">get_hash_key</a>(bundle, &hash_key); <a name="l00307"></a>00307 FragmentTable::iterator iter = <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.find(hash_key); <a name="l00308"></a>00308 <a name="l00309"></a>00309 <span class="keywordflow">if</span> (iter == <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.end()) { <a name="l00310"></a>00310 <span class="keywordflow">return</span> NULL; <a name="l00311"></a>00311 } <span class="keywordflow">else</span> { <a name="l00312"></a>00312 <span class="keywordflow">return</span> iter->second; <a name="l00313"></a>00313 } <a name="l00314"></a>00314 } <a name="l00315"></a>00315 <a name="l00316"></a>00316 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00317"></a>00317 <span class="keywordtype">void</span> <a name="l00318"></a><a class="code" href="classdtn_1_1FragmentManager.html#1e7b45257f6281030c8fe71fa2f809db">00318</a> <a class="code" href="classdtn_1_1FragmentManager.html#1e7b45257f6281030c8fe71fa2f809db">FragmentManager::erase_fragment_state</a>(<a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>* state) <a name="l00319"></a>00319 { <a name="l00320"></a>00320 std::string hash_key; <a name="l00321"></a>00321 <a class="code" href="classdtn_1_1FragmentManager.html#14e6759dca799277da24a3425274d9f9" title="Calculate a hash table key from a bundle.">get_hash_key</a>(state-><a class="code" href="classdtn_1_1FragmentState.html#78e1546a266b3a986808eca4e420c6cc">bundle</a>().object(), &hash_key); <a name="l00322"></a>00322 <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.erase(hash_key); <a name="l00323"></a>00323 } <a name="l00324"></a>00324 <a name="l00325"></a>00325 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00326"></a>00326 <span class="keywordtype">bool</span> <a name="l00327"></a><a class="code" href="classdtn_1_1FragmentManager.html#2f9394ef9f40337b95fed1f6cca381ac">00327</a> <a class="code" href="classdtn_1_1FragmentManager.html#2f9394ef9f40337b95fed1f6cca381ac" title="If only part of the given bundle was sent successfully, split it into two.">FragmentManager::try_to_reactively_fragment</a>(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00328"></a>00328 <a class="code" href="classdtn_1_1BlockInfoVec.html" title="Class for a vector of BlockInfo structures.">BlockInfoVec</a> *blocks, <a name="l00329"></a>00329 <span class="keywordtype">size_t</span> bytes_sent) <a name="l00330"></a>00330 { <a name="l00331"></a>00331 <span class="keywordflow">if</span> (bundle-><a class="code" href="classdtn_1_1Bundle.html#1931c2eeed81432743735dd245c8b95d" title="Accessors.">do_not_fragment</a>()) { <a name="l00332"></a>00332 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// can't do anything</span> <a name="l00333"></a>00333 } <a name="l00334"></a>00334 <a name="l00335"></a>00335 <span class="keywordtype">size_t</span> payload_offset = <a class="code" href="classdtn_1_1BundleProtocol.html#1b5ecc82d428705f3ba067622faace2c" title="Temporary helper function to find the offset of the first byte of the payload in...">BundleProtocol::payload_offset</a>(blocks); <a name="l00336"></a>00336 <span class="keywordtype">size_t</span> total_length = <a class="code" href="classdtn_1_1BundleProtocol.html#a1dedd9ea4300339bc2a3f7a231ca5dd" title="Return the total length of the formatted bundle block data.">BundleProtocol::total_length</a>(blocks); <a name="l00337"></a>00337 <a name="l00338"></a>00338 <span class="keywordflow">if</span> (bytes_sent <= payload_offset) { <a name="l00339"></a>00339 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// can't do anything</span> <a name="l00340"></a>00340 } <a name="l00341"></a>00341 <a name="l00342"></a>00342 <span class="keywordflow">if</span> (bytes_sent >= total_length) { <a name="l00343"></a>00343 <span class="keywordflow">return</span> <span class="keyword">false</span>; <span class="comment">// nothing to do</span> <a name="l00344"></a>00344 } <a name="l00345"></a>00345 <a name="l00346"></a>00346 <span class="keyword">const</span> <a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a> *payload_block <a name="l00347"></a>00347 = blocks-><a class="code" href="classdtn_1_1BlockInfoVec.html#646316c3b0fb301712bb39fdbfd3207e" title="Find the block for the given type.">find_block</a>(<a class="code" href="classdtn_1_1BundleProtocol.html#54a44c247b3c20b4ed11e29494284ca711ba3c0a764c41add8e738d757393d60">BundleProtocol::PAYLOAD_BLOCK</a>); <a name="l00348"></a>00348 <a name="l00349"></a>00349 <span class="keywordtype">size_t</span> payload_len = bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>(); <a name="l00350"></a>00350 <span class="keywordtype">size_t</span> payload_sent = std::min(payload_len, bytes_sent - payload_offset); <a name="l00351"></a>00351 <a name="l00352"></a>00352 <span class="comment">// A fragment cannot be created with only one byte of payload</span> <a name="l00353"></a>00353 <span class="comment">// available.</span> <a name="l00354"></a>00354 <span class="keywordflow">if</span> (payload_len <= 1) { <a name="l00355"></a>00355 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00356"></a>00356 } <a name="l00357"></a>00357 <a name="l00358"></a>00358 <span class="keywordtype">size_t</span> frag_off, frag_len; <a name="l00359"></a>00359 <a name="l00360"></a>00360 <span class="keywordflow">if</span> (payload_sent >= payload_len) { <a name="l00361"></a>00361 <span class="comment">// this means some but not all data after the payload was transmitted</span> <a name="l00362"></a>00362 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(! payload_block-><a class="code" href="classdtn_1_1BlockInfo.html#9688c4db3afee01ca64d83029358c387" title="Accessors.">last_block</a>()); <a name="l00363"></a>00363 <a name="l00364"></a>00364 <span class="comment">// keep a byte to put with the trailing blocks</span> <a name="l00365"></a>00365 frag_off = payload_len - 1; <a name="l00366"></a>00366 frag_len = 1; <a name="l00367"></a>00367 } <a name="l00368"></a>00368 <span class="keywordflow">else</span> { <a name="l00369"></a>00369 frag_off = payload_sent; <a name="l00370"></a>00370 frag_len = payload_len - payload_sent; <a name="l00371"></a>00371 } <a name="l00372"></a>00372 <a name="l00373"></a>00373 log_debug(<span class="stringliteral">"creating reactive fragment (offset %zu len %zu/%zu)"</span>, <a name="l00374"></a>00374 frag_off, frag_len, payload_len); <a name="l00375"></a>00375 <a name="l00376"></a>00376 <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* tail = <a class="code" href="classdtn_1_1FragmentManager.html#f6f29fb926b821b27d8d6cda64ffa96a" title="Create a bundle fragment from another bundle.">create_fragment</a>(bundle, blocks, frag_off, frag_len); <a name="l00377"></a>00377 <a name="l00378"></a>00378 <span class="comment">// treat the new fragment as if it just arrived</span> <a name="l00379"></a>00379 <a class="code" href="classdtn_1_1BundleDaemon.html#c7f41d180e271e74b1c7cd71f2577686" title="Queues the event at the head of the queue for processing by the daemon thread.">BundleDaemon::post_at_head</a>( <a name="l00380"></a>00380 <span class="keyword">new</span> <a class="code" href="classdtn_1_1BundleReceivedEvent.html" title="Event class for new bundle arrivals.">BundleReceivedEvent</a>(tail, <a class="code" href="namespacedtn.html#60ee39c9672604f59fe1b12d4e1d142b9b7a83778d3a7c1243e8f95c6445c88a" title="the fragmentation engine">EVENTSRC_FRAGMENTATION</a>)); <a name="l00381"></a>00381 <a name="l00382"></a>00382 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00383"></a>00383 } <a name="l00384"></a>00384 <a name="l00385"></a>00385 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00386"></a>00386 <span class="keywordtype">void</span> <a name="l00387"></a><a class="code" href="classdtn_1_1FragmentManager.html#06ba9686e9d9a7fca8cdc31c3ffca187">00387</a> <a class="code" href="classdtn_1_1FragmentManager.html#06ba9686e9d9a7fca8cdc31c3ffca187" title="Given a newly arrived bundle fragment, append it to the table of fragments and see...">FragmentManager::process_for_reassembly</a>(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* fragment) <a name="l00388"></a>00388 { <a name="l00389"></a>00389 <a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>* state; <a name="l00390"></a>00390 FragmentTable::iterator iter; <a name="l00391"></a>00391 <a name="l00392"></a>00392 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(fragment-><a class="code" href="classdtn_1_1Bundle.html#d6c74eba219bfe55854cf7a90e210cca" title="Accessors.">is_fragment</a>()); <a name="l00393"></a>00393 <a name="l00394"></a>00394 <span class="comment">// cons up the key to do the table lookup and look for reassembly state</span> <a name="l00395"></a>00395 std::string hash_key; <a name="l00396"></a>00396 <a class="code" href="classdtn_1_1FragmentManager.html#14e6759dca799277da24a3425274d9f9" title="Calculate a hash table key from a bundle.">get_hash_key</a>(fragment, &hash_key); <a name="l00397"></a>00397 iter = <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.find(hash_key); <a name="l00398"></a>00398 <a name="l00399"></a>00399 log_debug(<span class="stringliteral">"processing bundle fragment id=%u hash=%s %d"</span>, <a name="l00400"></a>00400 fragment-><a class="code" href="classdtn_1_1Bundle.html#7031e2e5e4078445bf4ad39cdcdea3b2" title="Accessors.">bundleid</a>(), hash_key.c_str(), <a name="l00401"></a>00401 fragment-><a class="code" href="classdtn_1_1Bundle.html#d6c74eba219bfe55854cf7a90e210cca" title="Accessors.">is_fragment</a>()); <a name="l00402"></a>00402 <a name="l00403"></a>00403 <span class="keywordflow">if</span> (iter == <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.end()) { <a name="l00404"></a>00404 log_debug(<span class="stringliteral">"no reassembly state for key %s -- creating new state"</span>, <a name="l00405"></a>00405 hash_key.c_str()); <a name="l00406"></a>00406 state = <span class="keyword">new</span> <a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>(); <a name="l00407"></a>00407 <a name="l00408"></a>00408 <span class="comment">// copy the metadata from the first fragment to arrive, but</span> <a name="l00409"></a>00409 <span class="comment">// make sure we mark the bundle that it's not a fragment (or</span> <a name="l00410"></a>00410 <span class="comment">// at least won't be for long)</span> <a name="l00411"></a>00411 fragment-><a class="code" href="classdtn_1_1Bundle.html#659027ae11e78a7527b3e31e7ebf0eb1" title="Copy the metadata from one bundle to another (used in fragmentation).">copy_metadata</a>(state->bundle().object()); <a name="l00412"></a>00412 state->bundle()->set_is_fragment(<span class="keyword">false</span>); <a name="l00413"></a>00413 state->bundle()->mutable_payload()-> <a name="l00414"></a>00414 set_length(fragment-><a class="code" href="classdtn_1_1Bundle.html#e32f3c57057214a1763dfe7709160cf8" title="Accessors.">orig_length</a>()); <a name="l00415"></a>00415 <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>[hash_key] = state; <a name="l00416"></a>00416 } <span class="keywordflow">else</span> { <a name="l00417"></a>00417 state = iter->second; <a name="l00418"></a>00418 log_debug(<span class="stringliteral">"found reassembly state for key %s (%zu fragments)"</span>, <a name="l00419"></a>00419 hash_key.c_str(), state-><a class="code" href="classdtn_1_1FragmentState.html#297d53afd37b9504916aef8968117407">fragment_list</a>().<a class="code" href="classdtn_1_1BundleList.html#3c50c6f421631a4dd72b7b77b0660bc3" title="Return the size of the list.">size</a>()); <a name="l00420"></a>00420 } <a name="l00421"></a>00421 <a name="l00422"></a>00422 <span class="comment">// stick the fragment on the reassembly list</span> <a name="l00423"></a>00423 state-><a class="code" href="classdtn_1_1FragmentState.html#cec4c98230bf6a2f84184d227329f36a">add_fragment</a>(fragment); <a name="l00424"></a>00424 <a name="l00425"></a>00425 <span class="comment">// store the fragment data in the partially reassembled bundle file</span> <a name="l00426"></a>00426 <span class="keywordtype">size_t</span> fraglen = fragment-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#56b3851ad789c4dbf019a32d557b7453" title="The payload length.">length</a>(); <a name="l00427"></a>00427 <a name="l00428"></a>00428 log_debug(<span class="stringliteral">"write_data: length_=%zu src_offset=%u dst_offset=%u len %zu"</span>, <a name="l00429"></a>00429 state-><a class="code" href="classdtn_1_1FragmentState.html#78e1546a266b3a986808eca4e420c6cc">bundle</a>()->payload().length(), <a name="l00430"></a>00430 0, fragment-><a class="code" href="classdtn_1_1Bundle.html#76cf6a44f566aa22e2a98007bf233ee2" title="Accessors.">frag_offset</a>(), fraglen); <a name="l00431"></a>00431 <a name="l00432"></a>00432 state-><a class="code" href="classdtn_1_1FragmentState.html#78e1546a266b3a986808eca4e420c6cc">bundle</a>()->mutable_payload()-> <a name="l00433"></a>00433 write_data(fragment-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>(), 0, fraglen, <a name="l00434"></a>00434 fragment-><a class="code" href="classdtn_1_1Bundle.html#76cf6a44f566aa22e2a98007bf233ee2" title="Accessors.">frag_offset</a>()); <a name="l00435"></a>00435 <a name="l00436"></a>00436 <span class="comment">// XXX/jmmikkel this ensures that we have a set of blocks in the</span> <a name="l00437"></a>00437 <span class="comment">// reassembled bundle, but eventually reassembly will have to do much more</span> <a name="l00438"></a>00438 <span class="keywordflow">if</span> (fragment-><a class="code" href="classdtn_1_1Bundle.html#76cf6a44f566aa22e2a98007bf233ee2" title="Accessors.">frag_offset</a>() == 0 && <a name="l00439"></a>00439 !state-><a class="code" href="classdtn_1_1FragmentState.html#78e1546a266b3a986808eca4e420c6cc">bundle</a>()->recv_blocks().empty()) <a name="l00440"></a>00440 { <a name="l00441"></a>00441 BlockInfoVec::const_iterator block_i; <a name="l00442"></a>00442 <span class="keywordflow">for</span> (block_i = fragment-><a class="code" href="classdtn_1_1Bundle.html#ec3c2ef7670fb2d34d46ab547bf9fb80" title="Accessors.">recv_blocks</a>().begin(); <a name="l00443"></a>00443 block_i != fragment-><a class="code" href="classdtn_1_1Bundle.html#ec3c2ef7670fb2d34d46ab547bf9fb80" title="Accessors.">recv_blocks</a>().end(); ++block_i) <a name="l00444"></a>00444 { <a name="l00445"></a>00445 state-><a class="code" href="classdtn_1_1FragmentState.html#78e1546a266b3a986808eca4e420c6cc">bundle</a>()->mutable_recv_blocks()-> <a name="l00446"></a>00446 push_back(<a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a>(*block_i)); <a name="l00447"></a>00447 } <a name="l00448"></a>00448 } <a name="l00449"></a>00449 <a name="l00450"></a>00450 <span class="comment">// check see if we're done</span> <a name="l00451"></a>00451 <span class="keywordflow">if</span> (! state-><a class="code" href="classdtn_1_1FragmentState.html#fe6b05c70df4f3c855e8368cd59e2e66">check_completed</a>()) { <a name="l00452"></a>00452 <span class="keywordflow">return</span>; <a name="l00453"></a>00453 } <a name="l00454"></a>00454 <a name="l00455"></a>00455 <a class="code" href="classdtn_1_1BundleDaemon.html#c7f41d180e271e74b1c7cd71f2577686" title="Queues the event at the head of the queue for processing by the daemon thread.">BundleDaemon::post_at_head</a> <a name="l00456"></a>00456 (<span class="keyword">new</span> <a class="code" href="classdtn_1_1ReassemblyCompletedEvent.html" title="Event class for reassembly completion.">ReassemblyCompletedEvent</a>(state-><a class="code" href="classdtn_1_1FragmentState.html#78e1546a266b3a986808eca4e420c6cc">bundle</a>().object(), <a name="l00457"></a>00457 &state-><a class="code" href="classdtn_1_1FragmentState.html#297d53afd37b9504916aef8968117407">fragment_list</a>())); <a name="l00458"></a>00458 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(state-><a class="code" href="classdtn_1_1FragmentState.html#297d53afd37b9504916aef8968117407">fragment_list</a>().<a class="code" href="classdtn_1_1BundleList.html#3c50c6f421631a4dd72b7b77b0660bc3" title="Return the size of the list.">size</a>() == 0); <span class="comment">// moved into the event</span> <a name="l00459"></a>00459 <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.erase(hash_key); <a name="l00460"></a>00460 <span class="keyword">delete</span> state; <a name="l00461"></a>00461 } <a name="l00462"></a>00462 <a name="l00463"></a>00463 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00464"></a>00464 <span class="keywordtype">void</span> <a name="l00465"></a><a class="code" href="classdtn_1_1FragmentManager.html#900e9f477dc6ff5aac6cd6621daf04d9">00465</a> <a class="code" href="classdtn_1_1FragmentManager.html#900e9f477dc6ff5aac6cd6621daf04d9" title="Delete any fragments that are no longer needed given the incoming (non-fragment)...">FragmentManager::delete_obsoleted_fragments</a>(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle) <a name="l00466"></a>00466 { <a name="l00467"></a>00467 <a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>* state; <a name="l00468"></a>00468 FragmentTable::iterator iter; <a name="l00469"></a>00469 <a name="l00470"></a>00470 <span class="comment">// cons up the key to do the table lookup and look for reassembly state</span> <a name="l00471"></a>00471 std::string hash_key; <a name="l00472"></a>00472 <a class="code" href="classdtn_1_1FragmentManager.html#14e6759dca799277da24a3425274d9f9" title="Calculate a hash table key from a bundle.">get_hash_key</a>(bundle, &hash_key); <a name="l00473"></a>00473 iter = <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.find(hash_key); <a name="l00474"></a>00474 <a name="l00475"></a>00475 log_debug(<span class="stringliteral">"checking for obsolete fragments id=%u hash=%s..."</span>, <a name="l00476"></a>00476 bundle-><a class="code" href="classdtn_1_1Bundle.html#7031e2e5e4078445bf4ad39cdcdea3b2" title="Accessors.">bundleid</a>(), hash_key.c_str()); <a name="l00477"></a>00477 <a name="l00478"></a>00478 <span class="keywordflow">if</span> (iter == <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.end()) { <a name="l00479"></a>00479 log_debug(<span class="stringliteral">"no reassembly state for key %s"</span>, <a name="l00480"></a>00480 hash_key.c_str()); <a name="l00481"></a>00481 <span class="keywordflow">return</span>; <a name="l00482"></a>00482 } <a name="l00483"></a>00483 <a name="l00484"></a>00484 state = iter->second; <a name="l00485"></a>00485 log_debug(<span class="stringliteral">"found reassembly state... deleting %zu fragments"</span>, <a name="l00486"></a>00486 state-><a class="code" href="classdtn_1_1FragmentState.html#cc22443cf764e6eded3b91eff47eaf3f">num_fragments</a>()); <a name="l00487"></a>00487 <a name="l00488"></a>00488 <a class="code" href="namespacedtn.html#87094f3b26b9f1d469e616d311ae57b1" title="Class definition for a Bundle reference.">BundleRef</a> fragment(<span class="stringliteral">"FragmentManager::delete_obsoleted_fragments"</span>); <a name="l00489"></a>00489 <a class="code" href="classdtn_1_1BundleList.html#1d52b8d6bb7d8a07e5d4f5fc4b5b61d2" title="Type for an iterator, which just wraps an stl iterator.">BundleList::iterator</a> i; <a name="l00490"></a>00490 oasys::ScopeLock l(state-><a class="code" href="classdtn_1_1FragmentState.html#297d53afd37b9504916aef8968117407">fragment_list</a>().<a class="code" href="classdtn_1_1BundleList.html#0d0c9f789f0f7f7d1cda3b4ecf32d2bc" title="Return the internal lock on this list.">lock</a>(), <a name="l00491"></a>00491 <span class="stringliteral">"FragmentManager::delete_obsoleted_fragments"</span>); <a name="l00492"></a>00492 <span class="keywordflow">while</span> (! state-><a class="code" href="classdtn_1_1FragmentState.html#297d53afd37b9504916aef8968117407">fragment_list</a>().<a class="code" href="classdtn_1_1BundleList.html#3ca5e173510164b46c8de9a95147f20d" title="Return whether or not the list is empty.">empty</a>()) { <a name="l00493"></a>00493 <a class="code" href="classdtn_1_1BundleDaemon.html#370a80f7dd9fd9297e76d7f830d737a8" title="Queues the event at the tail of the queue for processing by the daemon thread.">BundleDaemon::post</a>(<span class="keyword">new</span> <a class="code" href="classdtn_1_1BundleDeleteRequest.html" title="Event class for requestion deletion of a bundle.">BundleDeleteRequest</a>(state-><a class="code" href="classdtn_1_1FragmentState.html#297d53afd37b9504916aef8968117407">fragment_list</a>().<a class="code" href="classdtn_1_1BundleList.html#3429d8c70e63925609713c6d6fbd51fe" title="Remove (and return) the last bundle on the list.">pop_back</a>(), <a name="l00494"></a>00494 <a class="code" href="classdtn_1_1BundleProtocol.html#37b000d94172c06970bb8af32889f917eac137290a0338e66ed4a0c1cc09c2f2">BundleProtocol::REASON_NO_ADDTL_INFO</a>)); <a name="l00495"></a>00495 } <a name="l00496"></a>00496 <a name="l00497"></a>00497 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(state-><a class="code" href="classdtn_1_1FragmentState.html#297d53afd37b9504916aef8968117407">fragment_list</a>().<a class="code" href="classdtn_1_1BundleList.html#3c50c6f421631a4dd72b7b77b0660bc3" title="Return the size of the list.">size</a>() == 0); <span class="comment">// moved into events</span> <a name="l00498"></a>00498 l.unlock(); <a name="l00499"></a>00499 <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.erase(hash_key); <a name="l00500"></a>00500 <span class="keyword">delete</span> state; <a name="l00501"></a>00501 } <a name="l00502"></a>00502 <a name="l00503"></a>00503 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00504"></a>00504 <span class="keywordtype">void</span> <a name="l00505"></a><a class="code" href="classdtn_1_1FragmentManager.html#3cf63b648553dda12d36e0f05d17380e">00505</a> <a class="code" href="classdtn_1_1FragmentManager.html#3cf63b648553dda12d36e0f05d17380e" title="Delete reassembly state for a bundle.">FragmentManager::delete_fragment</a>(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* fragment) <a name="l00506"></a>00506 { <a name="l00507"></a>00507 <a class="code" href="classdtn_1_1FragmentState.html">FragmentState</a>* state; <a name="l00508"></a>00508 FragmentTable::iterator iter; <a name="l00509"></a>00509 <a name="l00510"></a>00510 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(fragment-><a class="code" href="classdtn_1_1Bundle.html#d6c74eba219bfe55854cf7a90e210cca" title="Accessors.">is_fragment</a>()); <a name="l00511"></a>00511 <a name="l00512"></a>00512 <span class="comment">// cons up the key to do the table lookup and look for reassembly state</span> <a name="l00513"></a>00513 std::string hash_key; <a name="l00514"></a>00514 <a class="code" href="classdtn_1_1FragmentManager.html#14e6759dca799277da24a3425274d9f9" title="Calculate a hash table key from a bundle.">get_hash_key</a>(fragment, &hash_key); <a name="l00515"></a>00515 iter = <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.find(hash_key); <a name="l00516"></a>00516 <a name="l00517"></a>00517 <span class="comment">// no reassembly state, simply return</span> <a name="l00518"></a>00518 <span class="keywordflow">if</span> (iter == <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.end()) { <a name="l00519"></a>00519 <span class="keywordflow">return</span>; <a name="l00520"></a>00520 } <a name="l00521"></a>00521 <a name="l00522"></a>00522 state = iter->second; <a name="l00523"></a>00523 <a name="l00524"></a>00524 <span class="comment">// remove the fragment from the reassembly list</span> <a name="l00525"></a>00525 <span class="keywordtype">bool</span> erased = state-><a class="code" href="classdtn_1_1FragmentState.html#3ae540418ab1e8bd1e60dab0a299d9f9">erase_fragment</a>(fragment); <a name="l00526"></a>00526 <a name="l00527"></a>00527 <span class="comment">// fragment was not in reassembly list, simply return</span> <a name="l00528"></a>00528 <span class="keywordflow">if</span> (!erased) { <a name="l00529"></a>00529 <span class="keywordflow">return</span>; <a name="l00530"></a>00530 } <a name="l00531"></a>00531 <a name="l00532"></a>00532 <span class="comment">// note that the old fragment data is still kept in the</span> <a name="l00533"></a>00533 <span class="comment">// partially-reassembled bundle file, but there won't be metadata</span> <a name="l00534"></a>00534 <span class="comment">// to indicate as such</span> <a name="l00535"></a>00535 <a name="l00536"></a>00536 <span class="comment">// delete reassembly state if no fragments now exist</span> <a name="l00537"></a>00537 <span class="keywordflow">if</span> (state-><a class="code" href="classdtn_1_1FragmentState.html#cc22443cf764e6eded3b91eff47eaf3f">num_fragments</a>() == 0) { <a name="l00538"></a>00538 <a class="code" href="classdtn_1_1FragmentManager.html#c16a79e94b6eb8ac6aeed3f52752022b">fragment_table_</a>.erase(hash_key); <a name="l00539"></a>00539 <span class="keyword">delete</span> state; <a name="l00540"></a>00540 } <a name="l00541"></a>00541 } <a name="l00542"></a>00542 <a name="l00543"></a>00543 } <span class="comment">// namespace dtn</span> </pre></div></div> <hr size="1"><address style="text-align: right;"><small>Generated on Mon Jul 21 14:09:45 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>