<!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: PayloadBlockProcessor.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>PayloadBlockProcessor.cc</h1><a href="PayloadBlockProcessor_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 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 "<a class="code" href="PayloadBlockProcessor_8h.html">PayloadBlockProcessor.h</a>"</span> <a name="l00022"></a>00022 <span class="preprocessor">#include "<a class="code" href="bundling_2Bundle_8h.html">Bundle.h</a>"</span> <a name="l00023"></a>00023 <span class="preprocessor">#include "<a class="code" href="BundleProtocol_8h.html">BundleProtocol.h</a>"</span> <a name="l00024"></a>00024 <a name="l00025"></a>00025 <span class="keyword">namespace </span>dtn { <a name="l00026"></a>00026 <a name="l00027"></a>00027 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00028"></a><a class="code" href="classdtn_1_1PayloadBlockProcessor.html#3b97e3201b6049c18e06d2746cc97760">00028</a> <a class="code" href="classdtn_1_1PayloadBlockProcessor.html#3b97e3201b6049c18e06d2746cc97760" title="Constructor.">PayloadBlockProcessor::PayloadBlockProcessor</a>() <a name="l00029"></a>00029 : <a class="code" href="classdtn_1_1BlockProcessor.html" title="Base class for the protocol handling of bundle blocks, including the core primary...">BlockProcessor</a>(<a class="code" href="classdtn_1_1BundleProtocol.html" title="Centralized class used to convert a Bundle to / from the bundle protocol specification...">BundleProtocol</a>::PAYLOAD_BLOCK) <a name="l00030"></a>00030 { <a name="l00031"></a>00031 } <a name="l00032"></a>00032 <a name="l00033"></a>00033 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00034"></a>00034 <span class="keywordtype">int</span> <a name="l00035"></a><a class="code" href="classdtn_1_1PayloadBlockProcessor.html#ac4a81f447e0ae6b4ef471d5a1810809">00035</a> PayloadBlockProcessor::consume(<a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00036"></a>00036 <a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a>* block, <a name="l00037"></a>00037 u_char* <a class="code" href="num2sdnv_8c.html#a81cdcc7ff6987bc85c073253e32715f">buf</a>, <a name="l00038"></a>00038 <span class="keywordtype">size_t</span> <a class="code" href="num2sdnv_8c.html#fed088663f8704004425cdae2120b9b3">len</a>) <a name="l00039"></a>00039 { <a name="l00040"></a>00040 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="namespacedtn.html#68bf4270dd0a7e5e8390869b83889922">log</a> = <span class="stringliteral">"/dtn/bundle/protocol"</span>; <a name="l00041"></a>00041 (void)log; <a name="l00042"></a>00042 <a name="l00043"></a>00043 <a class="code" href="classdtn_1_1BlockInfoVec.html" title="Class for a vector of BlockInfo structures.">BlockInfoVec</a>* recv_blocks = bundle-><a class="code" href="classdtn_1_1Bundle.html#5c48f51fba15c89a82d5d98c9881f0bd" title="Setters and mutable accessors.">mutable_recv_blocks</a>(); <a name="l00044"></a>00044 <span class="keywordtype">size_t</span> consumed = 0; <a name="l00045"></a>00045 <span class="keywordflow">if</span> (block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>() == 0) { <a name="l00046"></a>00046 <span class="keywordtype">int</span> cc = <a class="code" href="classdtn_1_1BlockProcessor.html#16d1b8a2805b0888536bd001ce5e9e5b" title="Consume a block preamble consisting of type, flags(SDNV), EID-list (composite field...">consume_preamble</a>(recv_blocks, block, buf, len); <a name="l00047"></a>00047 <span class="keywordflow">if</span> (cc == -1) { <a name="l00048"></a>00048 <span class="keywordflow">return</span> -1; <a name="l00049"></a>00049 } <a name="l00050"></a>00050 <a name="l00051"></a>00051 buf += cc; <a name="l00052"></a>00052 len -= cc; <a name="l00053"></a>00053 <a name="l00054"></a>00054 consumed += cc; <a name="l00055"></a>00055 <a name="l00056"></a>00056 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</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>() == 0); <a name="l00057"></a>00057 } <a name="l00058"></a>00058 <a name="l00059"></a>00059 <span class="comment">// If we still don't know the data offset, we must have consumed</span> <a name="l00060"></a>00060 <span class="comment">// the whole buffer</span> <a name="l00061"></a>00061 <span class="keywordflow">if</span> (block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>() == 0) { <a name="l00062"></a>00062 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(len == 0); <a name="l00063"></a>00063 <span class="keywordflow">return</span> consumed; <a name="l00064"></a>00064 } <a name="l00065"></a>00065 <a name="l00066"></a>00066 <span class="comment">// Special case for the simulator -- if the payload location is</span> <a name="l00067"></a>00067 <span class="comment">// NODATA, then we're done.</span> <a name="l00068"></a>00068 <span class="keywordflow">if</span> (bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#d6d6293001575473f0a1a3df3d83ed85" title="The payload location.">location</a>() == <a class="code" href="classdtn_1_1BundlePayload.html#f87102863631e39f673458a7527ae352136a5a00db709c66557035a52134298f" title="on disk">BundlePayload::NODATA</a>) { <a name="l00069"></a>00069 block-><a class="code" href="classdtn_1_1BlockInfo.html#78731d47494fb3e29e818379f627d3e0" title="Mutating accessors.">set_complete</a>(<span class="keyword">true</span>); <a name="l00070"></a>00070 <span class="keywordflow">return</span> consumed; <a name="l00071"></a>00071 } <a name="l00072"></a>00072 <a name="l00073"></a>00073 <span class="comment">// If we've consumed the length (because the data_offset is</span> <a name="l00074"></a>00074 <span class="comment">// non-zero) and the length is zero, then we're done.</span> <a name="l00075"></a>00075 <span class="keywordflow">if</span> (block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>() != 0 && block-><a class="code" href="classdtn_1_1BlockInfo.html#cb7676dfe53630699317388ad32e8d99" title="Accessors.">data_length</a>() == 0) { <a name="l00076"></a>00076 block-><a class="code" href="classdtn_1_1BlockInfo.html#78731d47494fb3e29e818379f627d3e0" title="Mutating accessors.">set_complete</a>(<span class="keyword">true</span>); <a name="l00077"></a>00077 <span class="keywordflow">return</span> consumed; <a name="l00078"></a>00078 } <a name="l00079"></a>00079 <a name="l00080"></a>00080 <span class="comment">// Also bail if there's nothing left to do</span> <a name="l00081"></a>00081 <span class="keywordflow">if</span> (len == 0) { <a name="l00082"></a>00082 <span class="keywordflow">return</span> consumed; <a name="l00083"></a>00083 } <a name="l00084"></a>00084 <a name="l00085"></a>00085 <span class="comment">// Otherwise, the buffer should always hold just the preamble</span> <a name="l00086"></a>00086 <span class="comment">// since we store the rest in the payload file</span> <a name="l00087"></a>00087 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(block-><a class="code" href="classdtn_1_1BlockInfo.html#40fee1299938523234d3ae8c80661e22" title="Accessors.">contents</a>().len() == block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>()); <a name="l00088"></a>00088 <a name="l00089"></a>00089 <span class="comment">// Now make sure there's still something left to do for the block,</span> <a name="l00090"></a>00090 <span class="comment">// otherwise it should have been marked as complete</span> <a name="l00091"></a>00091 <a class="code" href="SDNV_8cc.html#ca68c0d4ac8df0838e209fb5300f7be3">ASSERT</a>(block-><a class="code" href="classdtn_1_1BlockInfo.html#cb7676dfe53630699317388ad32e8d99" title="Accessors.">data_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="l00092"></a>00092 <a name="l00093"></a>00093 <span class="keywordtype">size_t</span> 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="l00094"></a>00094 <span class="keywordtype">size_t</span> remainder = block-><a class="code" href="classdtn_1_1BlockInfo.html#cb7676dfe53630699317388ad32e8d99" title="Accessors.">data_length</a>() - rcvd; <a name="l00095"></a>00095 <span class="keywordtype">size_t</span> tocopy; <a name="l00096"></a>00096 <a name="l00097"></a>00097 <span class="keywordflow">if</span> (len >= remainder) { <a name="l00098"></a>00098 block-><a class="code" href="classdtn_1_1BlockInfo.html#78731d47494fb3e29e818379f627d3e0" title="Mutating accessors.">set_complete</a>(<span class="keyword">true</span>); <a name="l00099"></a>00099 tocopy = remainder; <a name="l00100"></a>00100 } <span class="keywordflow">else</span> { <a name="l00101"></a>00101 tocopy = len; <a name="l00102"></a>00102 } <a name="l00103"></a>00103 <a name="l00104"></a>00104 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#af1ede6096a6d625fb83c001a9d490d6" title="Set the payload length in preparation for filling in with data.">set_length</a>(rcvd + tocopy); <a name="l00105"></a>00105 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#7f5185039b03f0c875a12136b6b24862" title="Write a chunk of payload data at the specified offset.">write_data</a>(buf, rcvd, tocopy); <a name="l00106"></a>00106 <a name="l00107"></a>00107 consumed += tocopy; <a name="l00108"></a>00108 <a name="l00109"></a>00109 log_debug_p(log, <span class="stringliteral">"PayloadBlockProcessor consumed %zu/%u (%s)"</span>, <a name="l00110"></a>00110 consumed, block-><a class="code" href="classdtn_1_1BlockInfo.html#2045184fe07026a47e1226bf7142f89a" title="Accessors.">full_length</a>(), <a name="l00111"></a>00111 block-><a class="code" href="classdtn_1_1BlockInfo.html#a70c73cf28ec51a265dc5ac077c56975" title="Accessors.">complete</a>() ? <span class="stringliteral">"complete"</span> : <span class="stringliteral">"not complete"</span>); <a name="l00112"></a>00112 <a name="l00113"></a>00113 <span class="keywordflow">return</span> consumed; <a name="l00114"></a>00114 } <a name="l00115"></a>00115 <a name="l00116"></a>00116 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00117"></a>00117 <span class="keywordtype">bool</span> <a name="l00118"></a><a class="code" href="classdtn_1_1PayloadBlockProcessor.html#27c437be9873cec504bc8d54f6e1fd3c">00118</a> <a class="code" href="classdtn_1_1PayloadBlockProcessor.html#27c437be9873cec504bc8d54f6e1fd3c" title="Virtual from BlockProcessor.">PayloadBlockProcessor::validate</a>(<span class="keyword">const</span> <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00119"></a>00119 <a class="code" href="classdtn_1_1BlockInfoVec.html" title="Class for a vector of BlockInfo structures.">BlockInfoVec</a>* block_list, <a name="l00120"></a>00120 <a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a>* block, <a name="l00121"></a>00121 <a class="code" href="classdtn_1_1BundleProtocol.html#37b000d94172c06970bb8af32889f917" title="Bundle Status Report &quot;Reason Code&quot; flags.">status_report_reason_t</a>* reception_reason, <a name="l00122"></a>00122 <a class="code" href="classdtn_1_1BundleProtocol.html#37b000d94172c06970bb8af32889f917" title="Bundle Status Report &quot;Reason Code&quot; flags.">status_report_reason_t</a>* deletion_reason) <a name="l00123"></a>00123 { <a name="l00124"></a>00124 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span>* <a class="code" href="namespacedtn.html#68bf4270dd0a7e5e8390869b83889922">log</a> = <span class="stringliteral">"/dtn/bundle/protocol"</span>; <a name="l00125"></a>00125 <a name="l00126"></a>00126 <span class="comment">// check for generic block errors</span> <a name="l00127"></a>00127 <span class="keywordflow">if</span> (!<a class="code" href="classdtn_1_1BlockProcessor.html#14242dcdb094a179f718a5eaee23cabd" title="Validate the block.">BlockProcessor::validate</a>(bundle, block_list, block, <a name="l00128"></a>00128 reception_reason, deletion_reason)) { <a name="l00129"></a>00129 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00130"></a>00130 } <a name="l00131"></a>00131 <a name="l00132"></a>00132 <span class="keywordflow">if</span> (!block-><a class="code" href="classdtn_1_1BlockInfo.html#a70c73cf28ec51a265dc5ac077c56975" title="Accessors.">complete</a>()) { <a name="l00133"></a>00133 <span class="comment">// We do not need the block to be complete because we may be</span> <a name="l00134"></a>00134 <span class="comment">// able to reactively fragment it, but we must have at least</span> <a name="l00135"></a>00135 <span class="comment">// the full preamble to do so.</span> <a name="l00136"></a>00136 <span class="keywordflow">if</span> (block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>() == 0 <a name="l00137"></a>00137 <a name="l00138"></a>00138 <span class="comment">// There is not much value in a 0-byte payload fragment so</span> <a name="l00139"></a>00139 <span class="comment">// discard those as well.</span> <a name="l00140"></a>00140 || (block-><a class="code" href="classdtn_1_1BlockInfo.html#cb7676dfe53630699317388ad32e8d99" title="Accessors.">data_length</a>() != 0 && <a name="l00141"></a>00141 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>() == 0) <a name="l00142"></a>00142 <a name="l00143"></a>00143 <span class="comment">// If the bundle should not be fragmented and the payload</span> <a name="l00144"></a>00144 <span class="comment">// block is not complete, we must discard the bundle.</span> <a name="l00145"></a>00145 || bundle-><a class="code" href="classdtn_1_1Bundle.html#1931c2eeed81432743735dd245c8b95d" title="Accessors.">do_not_fragment</a>()) <a name="l00146"></a>00146 { <a name="l00147"></a>00147 <a class="code" href="SDNV_8cc.html#3f24cadc9f28aba33472df0500092dcb">log_err_p</a>(log, <span class="stringliteral">"payload incomplete and cannot be fragmented"</span>); <a name="l00148"></a>00148 *deletion_reason = <a class="code" href="classdtn_1_1BundleProtocol.html#37b000d94172c06970bb8af32889f917a86c9900fe5641754acdcdb326b79b2c">BundleProtocol::REASON_BLOCK_UNINTELLIGIBLE</a>; <a name="l00149"></a>00149 <span class="keywordflow">return</span> <span class="keyword">false</span>; <a name="l00150"></a>00150 } <a name="l00151"></a>00151 } <a name="l00152"></a>00152 <a name="l00153"></a>00153 <span class="keywordflow">return</span> <span class="keyword">true</span>; <a name="l00154"></a>00154 } <a name="l00155"></a>00155 <a name="l00156"></a>00156 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00157"></a>00157 <span class="keywordtype">int</span> <a name="l00158"></a><a class="code" href="classdtn_1_1PayloadBlockProcessor.html#89d655b936e37b55f3b080d23f2a2e7c">00158</a> <a class="code" href="classdtn_1_1PayloadBlockProcessor.html#89d655b936e37b55f3b080d23f2a2e7c" title="Virtual from BlockProcessor.">PayloadBlockProcessor::generate</a>(<span class="keyword">const</span> <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00159"></a>00159 <a class="code" href="classdtn_1_1BlockInfoVec.html" title="Class for a vector of BlockInfo structures.">BlockInfoVec</a>* xmit_blocks, <a name="l00160"></a>00160 <a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a>* block, <a name="l00161"></a>00161 <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="l00162"></a>00162 <span class="keywordtype">bool</span> last) <a name="l00163"></a>00163 { <a name="l00164"></a>00164 (void)link; <a name="l00165"></a>00165 (void)xmit_blocks; <a name="l00166"></a>00166 <a name="l00167"></a>00167 <span class="comment">// in the ::generate pass, we just need to set up the preamble,</span> <a name="l00168"></a>00168 <span class="comment">// since the payload stays on disk</span> <a name="l00169"></a>00169 <a class="code" href="classdtn_1_1BlockProcessor.html#c08f7b82f6049f30710a5dcdebb61ce7" title="Generate the standard preamble for the given block type, flags, EID-list and content...">generate_preamble</a>(xmit_blocks, <a name="l00170"></a>00170 block, <a name="l00171"></a>00171 <a class="code" href="classdtn_1_1BundleProtocol.html#54a44c247b3c20b4ed11e29494284ca711ba3c0a764c41add8e738d757393d60">BundleProtocol::PAYLOAD_BLOCK</a>, <a name="l00172"></a>00172 last ? <a class="code" href="classdtn_1_1BundleProtocol.html#cd56fabd4393bd86ffc295bafaba540b60a3533536c98ae651d4533edd238037">BundleProtocol::BLOCK_FLAG_LAST_BLOCK</a> : 0, <a name="l00173"></a>00173 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="l00174"></a>00174 <a name="l00175"></a>00175 <span class="keywordflow">return</span> <a class="code" href="BlockProcessor_8h.html#446572d61c813971cb4d5ce4ee1b9c0b">BP_SUCCESS</a>; <a name="l00176"></a>00176 } <a name="l00177"></a>00177 <a name="l00178"></a>00178 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00179"></a>00179 <span class="keywordtype">void</span> <a name="l00180"></a><a class="code" href="classdtn_1_1PayloadBlockProcessor.html#4a0313f82c40de4dc6198ac6edba1523">00180</a> <a class="code" href="classdtn_1_1PayloadBlockProcessor.html#4a0313f82c40de4dc6198ac6edba1523" title="Virtual from BlockProcessor.">PayloadBlockProcessor::produce</a>(<span class="keyword">const</span> <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00181"></a>00181 <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>* block, <a name="l00182"></a>00182 u_char* <a class="code" href="num2sdnv_8c.html#a81cdcc7ff6987bc85c073253e32715f">buf</a>, <a name="l00183"></a>00183 <span class="keywordtype">size_t</span> offset, <a name="l00184"></a>00184 <span class="keywordtype">size_t</span> <a class="code" href="num2sdnv_8c.html#fed088663f8704004425cdae2120b9b3">len</a>) <a name="l00185"></a>00185 { <a name="l00186"></a>00186 <span class="comment">// First copy out the specified range of the preamble</span> <a name="l00187"></a>00187 <span class="keywordflow">if</span> (offset < block->data_offset()) { <a name="l00188"></a>00188 <span class="keywordtype">size_t</span> tocopy = std::min(len, block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>() - offset); <a name="l00189"></a>00189 memcpy(buf, block-><a class="code" href="classdtn_1_1BlockInfo.html#40fee1299938523234d3ae8c80661e22" title="Accessors.">contents</a>().buf() + offset, tocopy); <a name="l00190"></a>00190 buf += tocopy; <a name="l00191"></a>00191 offset += tocopy; <a name="l00192"></a>00192 len -= tocopy; <a name="l00193"></a>00193 } <a name="l00194"></a>00194 <a name="l00195"></a>00195 <span class="keywordflow">if</span> (len == 0) <a name="l00196"></a>00196 <span class="keywordflow">return</span>; <a name="l00197"></a>00197 <a name="l00198"></a>00198 <span class="comment">// Adjust offset to account for the preamble</span> <a name="l00199"></a>00199 <span class="keywordtype">size_t</span> payload_offset = offset - block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>(); <a name="l00200"></a>00200 <a name="l00201"></a>00201 <span class="keywordtype">size_t</span> tocopy = std::min(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>() - payload_offset); <a name="l00202"></a>00202 bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#c4ce25f2d2d7ad54a9306ea2599731b4" title="Copy out a chunk of payload data into the supplied buffer.">read_data</a>(payload_offset, tocopy, buf); <a name="l00203"></a>00203 <a name="l00204"></a>00204 <span class="keywordflow">return</span>; <a name="l00205"></a>00205 } <a name="l00206"></a>00206 <a name="l00207"></a>00207 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00208"></a>00208 <span class="keywordtype">void</span> <a name="l00209"></a><a class="code" href="classdtn_1_1PayloadBlockProcessor.html#2f698522931b3cc8704e607aa567c01c">00209</a> <a class="code" href="classdtn_1_1PayloadBlockProcessor.html#2f698522931b3cc8704e607aa567c01c" title="Virtual from BlockProcessor.">PayloadBlockProcessor::process</a>(<a class="code" href="classdtn_1_1BlockProcessor.html#65e98938e66022edf2971fa59721a782" title="Typedef for a process function pointer.">process_func</a>* func, <a name="l00210"></a>00210 <span class="keyword">const</span> <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00211"></a>00211 <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>* caller_block, <a name="l00212"></a>00212 <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>* target_block, <a name="l00213"></a>00213 <span class="keywordtype">size_t</span> offset, <a name="l00214"></a>00214 <span class="keywordtype">size_t</span> <a class="code" href="num2sdnv_8c.html#fed088663f8704004425cdae2120b9b3">len</a>, <a name="l00215"></a>00215 OpaqueContext* context) <a name="l00216"></a>00216 { <a name="l00217"></a>00217 <span class="keyword">const</span> u_char* <a class="code" href="num2sdnv_8c.html#a81cdcc7ff6987bc85c073253e32715f">buf</a>; <a name="l00218"></a>00218 u_char work[1024]; <span class="comment">// XXX/pl TODO rework buffer usage</span> <a name="l00219"></a>00219 <span class="comment">// and look at type for the payload data</span> <a name="l00220"></a>00220 <span class="keywordtype">size_t</span> len_to_do = 0; <a name="l00221"></a>00221 <a name="l00222"></a>00222 <span class="comment">// re-do these appropriately for the payload</span> <a name="l00223"></a>00223 <span class="comment">//ASSERT(offset < target_block->contents().len());</span> <a name="l00224"></a>00224 <span class="comment">//ASSERT(target_block->contents().len() >= offset + len);</span> <a name="l00225"></a>00225 <a name="l00226"></a>00226 <span class="comment">// First work on specified range of the preamble</span> <a name="l00227"></a>00227 <span class="keywordflow">if</span> (offset < target_block->data_offset()) { <a name="l00228"></a>00228 len_to_do = std::min(len, target_block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>() - offset); <a name="l00229"></a>00229 <a name="l00230"></a>00230 <span class="comment">// convert the offset to a pointer in the target block</span> <a name="l00231"></a>00231 buf = target_block-><a class="code" href="classdtn_1_1BlockInfo.html#40fee1299938523234d3ae8c80661e22" title="Accessors.">contents</a>().buf() + offset; <a name="l00232"></a>00232 <a name="l00233"></a>00233 <span class="comment">// call the processing function to do the work</span> <a name="l00234"></a>00234 (*func)(bundle, caller_block, target_block, buf, len_to_do, context); <a name="l00235"></a>00235 buf += len_to_do; <a name="l00236"></a>00236 offset += len_to_do; <a name="l00237"></a>00237 len -= len_to_do; <a name="l00238"></a>00238 } <a name="l00239"></a>00239 <a name="l00240"></a>00240 <span class="keywordflow">if</span> (len == 0) <a name="l00241"></a>00241 <span class="keywordflow">return</span>; <a name="l00242"></a>00242 <a name="l00243"></a>00243 <span class="comment">// Adjust offset to account for the preamble</span> <a name="l00244"></a>00244 <span class="keywordtype">size_t</span> payload_offset = offset - target_block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>(); <a name="l00245"></a>00245 <span class="keywordtype">size_t</span> remaining = std::min(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>() - payload_offset); <a name="l00246"></a>00246 <span class="keywordtype">size_t</span> outlen; <a name="l00247"></a>00247 <a name="l00248"></a>00248 buf = work; <span class="comment">// use local buffer</span> <a name="l00249"></a>00249 <a name="l00250"></a>00250 <span class="keywordflow">while</span> ( remaining > 0 ) { <a name="l00251"></a>00251 outlen = 0; <a name="l00252"></a>00252 len_to_do = std::min(remaining, <span class="keyword">sizeof</span>(work)); <a name="l00253"></a>00253 buf = bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#c4ce25f2d2d7ad54a9306ea2599731b4" title="Copy out a chunk of payload data into the supplied buffer.">read_data</a>(payload_offset, len_to_do, work); <a name="l00254"></a>00254 <a name="l00255"></a>00255 <span class="comment">// call the processing function to do the work</span> <a name="l00256"></a>00256 (*func)(bundle, caller_block, target_block, buf, len_to_do, context); <a name="l00257"></a>00257 <a name="l00258"></a>00258 payload_offset += len_to_do; <a name="l00259"></a>00259 remaining -= len_to_do; <a name="l00260"></a>00260 } <a name="l00261"></a>00261 } <a name="l00262"></a>00262 <a name="l00263"></a>00263 <span class="comment">//----------------------------------------------------------------------</span> <a name="l00264"></a>00264 <span class="keywordtype">bool</span> <a name="l00265"></a><a class="code" href="classdtn_1_1PayloadBlockProcessor.html#70cec440e670c7380531344e7cccd272">00265</a> <a class="code" href="classdtn_1_1PayloadBlockProcessor.html#70cec440e670c7380531344e7cccd272" title="Virtual from BlockProcessor.">PayloadBlockProcessor::mutate</a>(<a class="code" href="classdtn_1_1BlockProcessor.html#17f2dd5627da39aab829ef8dfc37b252" title="Typedef for a mutate function pointer.">mutate_func</a>* func, <a name="l00266"></a>00266 <a class="code" href="classdtn_1_1Bundle.html" title="The internal representation of a bundle.">Bundle</a>* bundle, <a name="l00267"></a>00267 <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>* caller_block, <a name="l00268"></a>00268 <a class="code" href="classdtn_1_1BlockInfo.html" title="Class used to store unparsed bundle blocks and associated metadata about them.">BlockInfo</a>* target_block, <a name="l00269"></a>00269 <span class="keywordtype">size_t</span> offset, <a name="l00270"></a>00270 <span class="keywordtype">size_t</span> <a class="code" href="num2sdnv_8c.html#fed088663f8704004425cdae2120b9b3">len</a>, <a name="l00271"></a>00271 OpaqueContext* r) <a name="l00272"></a>00272 { <a name="l00273"></a>00273 <span class="keywordtype">bool</span> changed = <span class="keyword">false</span>; <a name="l00274"></a>00274 u_char* <a class="code" href="num2sdnv_8c.html#a81cdcc7ff6987bc85c073253e32715f">buf</a>; <a name="l00275"></a>00275 u_char work[1024]; <span class="comment">// XXX/pl TODO rework buffer usage</span> <a name="l00276"></a>00276 <span class="comment">// and look at type for the payload data</span> <a name="l00277"></a>00277 <span class="keywordtype">size_t</span> len_to_do = 0; <a name="l00278"></a>00278 <a name="l00279"></a>00279 <span class="comment">// re-do these appropriately for the payload</span> <a name="l00280"></a>00280 <span class="comment">//ASSERT(offset < target_block->contents().len());</span> <a name="l00281"></a>00281 <span class="comment">//ASSERT(target_block->contents().len() >= offset + len);</span> <a name="l00282"></a>00282 <a name="l00283"></a>00283 <span class="comment">// First work on specified range of the preamble</span> <a name="l00284"></a>00284 <span class="keywordflow">if</span> (offset < target_block->data_offset()) { <a name="l00285"></a>00285 len_to_do = std::min(len, target_block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>() - offset); <a name="l00286"></a>00286 <span class="comment">// convert the offset to a pointer in the target block</span> <a name="l00287"></a>00287 buf = target_block-><a class="code" href="classdtn_1_1BlockInfo.html#40fee1299938523234d3ae8c80661e22" title="Accessors.">contents</a>().buf() + offset; <a name="l00288"></a>00288 <span class="comment">// call the processing function to do the work</span> <a name="l00289"></a>00289 changed = (*func)(bundle, caller_block, target_block, buf, len_to_do, r); <a name="l00290"></a>00290 buf += len_to_do; <a name="l00291"></a>00291 offset += len_to_do; <a name="l00292"></a>00292 len -= len_to_do; <a name="l00293"></a>00293 } <a name="l00294"></a>00294 <a name="l00295"></a>00295 <span class="keywordflow">if</span> (len == 0) <a name="l00296"></a>00296 <span class="keywordflow">return</span> changed; <a name="l00297"></a>00297 <a name="l00298"></a>00298 <span class="comment">// Adjust offset to account for the preamble</span> <a name="l00299"></a>00299 <span class="keywordtype">size_t</span> payload_offset = offset - target_block-><a class="code" href="classdtn_1_1BlockInfo.html#590c483ae6cc403c5062b8b3acc51ae3" title="Accessors.">data_offset</a>(); <a name="l00300"></a>00300 <span class="keywordtype">size_t</span> remaining = std::min(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>() - payload_offset); <a name="l00301"></a>00301 <span class="keywordtype">size_t</span> outlen; <a name="l00302"></a>00302 <a name="l00303"></a>00303 buf = work; <span class="comment">// use local buffer</span> <a name="l00304"></a>00304 <a name="l00305"></a>00305 <span class="keywordflow">while</span> ( remaining > 0 ) { <a name="l00306"></a>00306 outlen = 0; <a name="l00307"></a>00307 len_to_do = std::min(remaining, <span class="keyword">sizeof</span>(work)); <a name="l00308"></a>00308 bundle-><a class="code" href="classdtn_1_1Bundle.html#3bc0b88b547620b4029eba4809a73447" title="Accessors.">payload</a>().<a class="code" href="classdtn_1_1BundlePayload.html#c4ce25f2d2d7ad54a9306ea2599731b4" title="Copy out a chunk of payload data into the supplied buffer.">read_data</a>(payload_offset, len_to_do, buf); <a name="l00309"></a>00309 <a name="l00310"></a>00310 <span class="comment">// call the processing function to do the work</span> <a name="l00311"></a>00311 <span class="keywordtype">bool</span> chunk_changed = <a name="l00312"></a>00312 (*func)(bundle, caller_block, target_block, buf, len_to_do, r); <a name="l00313"></a>00313 <a name="l00314"></a>00314 <span class="comment">// if we need to flush changed content back to disk, do it </span> <a name="l00315"></a>00315 <span class="keywordflow">if</span> ( chunk_changed ) <a name="l00316"></a>00316 bundle->mutable_payload()-> <a name="l00317"></a>00317 write_data(buf, payload_offset, len_to_do); <a name="l00318"></a>00318 <a name="l00319"></a>00319 changed |= chunk_changed; <a name="l00320"></a>00320 <a name="l00321"></a>00321 payload_offset += len_to_do; <a name="l00322"></a>00322 remaining -= len_to_do; <a name="l00323"></a>00323 } <a name="l00324"></a>00324 <a name="l00325"></a>00325 <span class="keywordflow">return</span> changed; <a name="l00326"></a>00326 } <a name="l00327"></a>00327 <a name="l00328"></a>00328 } <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:46 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>