PreviousHopBlockProcessor.cc

Go to the documentation of this file.
00001 /*
00002  *    Copyright 2006 Intel Corporation
00003  * 
00004  *    Licensed under the Apache License, Version 2.0 (the "License");
00005  *    you may not use this file except in compliance with the License.
00006  *    You may obtain a copy of the License at
00007  * 
00008  *        http://www.apache.org/licenses/LICENSE-2.0
00009  * 
00010  *    Unless required by applicable law or agreed to in writing, software
00011  *    distributed under the License is distributed on an "AS IS" BASIS,
00012  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  *    See the License for the specific language governing permissions and
00014  *    limitations under the License.
00015  */
00016 
00017 #include "PreviousHopBlockProcessor.h"
00018 #include "Bundle.h"
00019 #include "BundleDaemon.h"
00020 #include "BundleProtocol.h"
00021 #include "contacts/Link.h"
00022 
00023 namespace dtn {
00024 
00025 //----------------------------------------------------------------------
00026 PreviousHopBlockProcessor::PreviousHopBlockProcessor()
00027     : BlockProcessor(BundleProtocol::PREVIOUS_HOP_BLOCK)
00028 {
00029 }
00030 
00031 //----------------------------------------------------------------------
00032 void
00033 PreviousHopBlockProcessor::prepare(const Bundle*    bundle,
00034                                    Link*            link,
00035                                    BlockInfoVec*    blocks,
00036                                    const BlockInfo* source)
00037 {
00038     if (link == NULL || !link->params().prevhop_hdr_) {
00039         return;
00040     }
00041     
00042     BlockProcessor::prepare(bundle, link, blocks, source);
00043 }
00044 
00045 //----------------------------------------------------------------------
00046 void
00047 PreviousHopBlockProcessor::generate(const Bundle* bundle,
00048                                     Link*         link,
00049                                     BlockInfo*    block,
00050                                     bool          last)
00051 {
00052     (void)bundle;
00053     (void)link;
00054 
00055     // XXX/demmer this is not the right protocol spec'd format since
00056     // it's supposed to use the dictionary
00057     
00058 
00059     // if the flag isn't set, we shouldn't have a block
00060     ASSERT(link->params().prevhop_hdr_);
00061 
00062     BundleDaemon* bd = BundleDaemon::instance();
00063     size_t length = bd->local_eid().length();
00064     
00065     generate_preamble(block,
00066                       BundleProtocol::PREVIOUS_HOP_BLOCK,
00067                       BundleProtocol::BLOCK_FLAG_DISCARD_BUNDLE_ONERROR |
00068                         (last ? BundleProtocol::BLOCK_FLAG_LAST_BLOCK : 0),
00069                       length);
00070 
00071     BlockInfo::DataBuffer* contents = block->writable_contents();
00072     contents->reserve(block->data_offset() + length);
00073     contents->set_len(block->data_offset() + length);
00074     memcpy(contents->buf() + block->data_offset(),
00075            bd->local_eid().data(), length);
00076 }
00077 
00078 //----------------------------------------------------------------------
00079 int
00080 PreviousHopBlockProcessor::consume(Bundle* bundle, BlockInfo* block,
00081                                    u_char* buf, size_t len)
00082 {
00083     int cc = BlockProcessor::consume(bundle, block, buf, len);
00084 
00085     if (cc == -1) {
00086         return -1; // protocol error
00087     }
00088     
00089     if (! block->complete()) {
00090         ASSERT(cc == (int)len);
00091         return cc;
00092     }
00093 
00094     if (! bundle->prevhop_.assign((char*)block->data(), block->data_length())) {
00095         log_err_p("/dtn/bundle/protocol",
00096                   "error parsing previous hop eid '%.*s",
00097                   block->data_length(), block->data());
00098         return -1;
00099     }
00100     
00101     return cc;
00102 }
00103 
00104 
00105 } // namespace dtn

Generated on Thu Jun 7 16:56:51 2007 for DTN Reference Implementation by  doxygen 1.5.1