BlockInfo.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 <oasys/debug/Log.h>
00018 #include "BlockInfo.h"
00019 #include "BlockProcessor.h"
00020 #include "APIBlockProcessor.h"
00021 #include "BundleProtocol.h"
00022 
00023 namespace dtn {
00024 
00025 //----------------------------------------------------------------------
00026 BlockInfo::BlockInfo(BlockProcessor* owner, const BlockInfo* source)
00027     : SerializableObject(),
00028       owner_(owner),
00029       owner_type_(owner->block_type()),
00030       source_(source),
00031       contents_(),
00032       data_length_(0),
00033       data_offset_(0),
00034       complete_(false)
00035 {
00036 }
00037 
00038 //----------------------------------------------------------------------
00039 BlockInfo::BlockInfo(oasys::Builder& builder)
00040     : owner_(NULL),
00041       owner_type_(0),
00042       source_(NULL),
00043       contents_(),
00044       data_length_(0),
00045       data_offset_(0),
00046       complete_(false)
00047 {
00048     (void)builder;
00049 }
00050 
00051 //----------------------------------------------------------------------
00052 u_int8_t
00053 BlockInfo::type() const
00054 {
00055     if (owner_->block_type() == BundleProtocol::PRIMARY_BLOCK) {
00056         return BundleProtocol::PRIMARY_BLOCK;
00057     }
00058 
00059     if (contents_.len() == 0) {
00060         return 0xff;
00061     }
00062 
00063     return contents_.buf()[0];
00064 }
00065 
00066 //----------------------------------------------------------------------
00067 u_int8_t
00068 BlockInfo::flags() const
00069 {
00070     if (type() == BundleProtocol::PRIMARY_BLOCK) {
00071         return 0x0;
00072     }
00073 
00074     ASSERT(contents_.len() >= 2);
00075     return contents_.buf()[1];
00076 }
00077 
00078 //----------------------------------------------------------------------
00079 void
00080 BlockInfo::set_flag(u_int8_t flag)
00081 {
00082     ASSERT(contents_.len() >= 2);
00083     contents_.buf()[1] &= flag;
00084 }
00085 
00086 //----------------------------------------------------------------------
00087 bool
00088 BlockInfo::primary_block() const
00089 {
00090     return (type() == BundleProtocol::PRIMARY_BLOCK);
00091 }
00092 
00093 //----------------------------------------------------------------------
00094 bool
00095 BlockInfo::payload_block() const
00096 {
00097     return (type() == BundleProtocol::PAYLOAD_BLOCK);
00098 }
00099 
00100 //----------------------------------------------------------------------
00101 bool
00102 BlockInfo::last_block() const
00103 {
00104     return (flags() & BundleProtocol::BLOCK_FLAG_LAST_BLOCK);
00105 }
00106 
00107 //----------------------------------------------------------------------
00108 void
00109 BlockInfo::serialize(oasys::SerializeAction* a)
00110 {
00111     a->process("owner_type", &owner_type_);
00112     if (a->action_code() == oasys::Serialize::UNMARSHAL) {
00113         // need to re-assign the owner
00114         if (owner_type_ == BundleProtocol::API_EXTENSION_BLOCK) {
00115             owner_ = APIBlockProcessor::instance();
00116         } else {
00117             owner_ = BundleProtocol::find_processor(owner_type_);
00118         }
00119     }
00120     ASSERT(owner_type_ == owner_->block_type());
00121     
00122     u_int32_t length = contents_.len();
00123     a->process("length", &length);
00124     
00125     // when we're unserializing, we need to reserve space and set the
00126     // length of the contents buffer before we write into it
00127     if (a->action_code() == oasys::Serialize::UNMARSHAL) {
00128         contents_.reserve(length);
00129         contents_.set_len(length);
00130     }
00131 
00132     a->process("contents", contents_.buf(), length);
00133     a->process("data_length", &data_length_);
00134     a->process("data_offset", &data_offset_);
00135     a->process("complete", &complete_);
00136 }
00137 
00138 //----------------------------------------------------------------------
00139 const BlockInfo*
00140 BlockInfoVec::find_block(u_int8_t type) const
00141 {
00142     for (const_iterator iter = begin(); iter != end(); ++iter) {
00143         if (iter->type() == type ||
00144             iter->owner()->block_type() == type)
00145         {
00146             return &*iter;
00147         }
00148     }
00149     return false;
00150 }
00151 
00152 
00153 //----------------------------------------------------------------------
00154 LinkBlockSet::Entry::Entry(Link* link)
00155     : blocks_(NULL), link_(link)
00156 {
00157 }
00158 
00159 //----------------------------------------------------------------------
00160 LinkBlockSet::~LinkBlockSet()
00161 {
00162     for (iterator iter = entries_.begin();
00163          iter != entries_.end();
00164          ++iter)
00165     {
00166         delete iter->blocks_;
00167         iter->blocks_ = 0;
00168     }
00169 }
00170 
00171 //----------------------------------------------------------------------
00172 BlockInfoVec*
00173 LinkBlockSet::create_blocks(Link* link)
00174 {
00175     ASSERT(find_blocks(link) == NULL);
00176     entries_.push_back(Entry(link));
00177     entries_.back().blocks_ = new BlockInfoVec();
00178     return entries_.back().blocks_;
00179 }
00180 
00181 //----------------------------------------------------------------------
00182 BlockInfoVec*
00183 LinkBlockSet::find_blocks(Link* link)
00184 {
00185     for (iterator iter = entries_.begin();
00186          iter != entries_.end();
00187          ++iter)
00188     {
00189         if (iter->link_ == link) {
00190             return iter->blocks_;
00191         }
00192     }
00193     return NULL;
00194 }
00195 
00196 //----------------------------------------------------------------------
00197 void
00198 LinkBlockSet::delete_blocks(Link* link)
00199 {
00200     for (iterator iter = entries_.begin();
00201          iter != entries_.end();
00202          ++iter)
00203     {
00204         if (iter->link_ == link) {
00205             delete iter->blocks_;
00206             entries_.erase(iter);
00207             return;
00208         }
00209     }
00210     
00211     PANIC("LinkBlockVec::delete_blocks: "
00212           "no block vector for link *%p", link);
00213 }
00214 
00215 } // namespace dtn

Generated on Sat Sep 8 08:36:15 2007 for DTN Reference Implementation by  doxygen 1.5.3