servlib/bundling/MetadataBlock.h
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2007 The MITRE Corporation
       
     3  * 
       
     4  *    Licensed under the Apache License, Version 2.0 (the "License");
       
     5  *    you may not use this file except in compliance with the License.
       
     6  *    You may obtain a copy of the License at
       
     7  * 
       
     8  *        http://www.apache.org/licenses/LICENSE-2.0
       
     9  * 
       
    10  *    Unless required by applicable law or agreed to in writing, software
       
    11  *    distributed under the License is distributed on an "AS IS" BASIS,
       
    12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    13  *    See the License for the specific language governing permissions and
       
    14  *    limitations under the License.
       
    15  *
       
    16  *    The US Government will not be charged any license fee and/or royalties
       
    17  *    related to this software. Neither name of The MITRE Corporation; nor the
       
    18  *    names of its contributors may be used to endorse or promote products
       
    19  *    derived from this software without specific prior written permission.
       
    20  */
       
    21 
       
    22 #ifndef _METADATA_BLOCK_H_
       
    23 #define _METADATA_BLOCK_H_
       
    24 
       
    25 #include <oasys/thread/Mutex.h>
       
    26 
       
    27 #include "BlockInfo.h"
       
    28 #include "BlockProcessor.h"
       
    29 
       
    30 namespace dtn {
       
    31 
       
    32 /**
       
    33  * The representation of a metadata extension block.
       
    34  */
       
    35 class MetadataBlock : public BP_Local, public oasys::SerializableObject {
       
    36 public:
       
    37 
       
    38     // Constructors
       
    39     MetadataBlock(): lock_("MetadataBlock"),
       
    40         id_(MetadataBlock::get_next_index()),
       
    41         block_(NULL), generated_(false), error_(false),
       
    42         source_id_(0), source_(false), flags_(0),
       
    43         ontology_(0), metadata_(NULL), metadata_len_(0) {}
       
    44     
       
    45     MetadataBlock(BlockInfo *block): lock_("MetadataBlock"),
       
    46         id_(MetadataBlock::get_next_index()),
       
    47         block_(block), generated_(false), error_(false),
       
    48         source_id_(0), source_(false), flags_(0),
       
    49         ontology_(0), metadata_(NULL), metadata_len_(0) {}
       
    50     
       
    51     MetadataBlock(u_int64_t type, u_char* buf, u_int32_t len);
       
    52     
       
    53     MetadataBlock(unsigned int source_id, u_int64_t type,
       
    54                   u_char* buf, u_int32_t len);
       
    55     
       
    56     MetadataBlock(oasys::Builder& builder): lock_("MetadataBlock"),
       
    57         id_(MetadataBlock::get_next_index()),
       
    58         block_(NULL), generated_(false), error_(false),
       
    59         source_id_(0), source_(false), flags_(0),
       
    60         ontology_(0), metadata_(NULL), metadata_len_(0)
       
    61         { (void)builder; }
       
    62     
       
    63     MetadataBlock(const MetadataBlock& copy);
       
    64 
       
    65     // Destructor
       
    66     ~MetadataBlock();
       
    67 
       
    68     // Accessors
       
    69     unsigned int id()           const { return id_; }
       
    70     bool         generated()    const { return generated_; }
       
    71     bool         error()        const { return error_; }
       
    72     unsigned int source_id()    const { return source_id_; }
       
    73     bool         source()       const { return source_; }
       
    74     u_int64_t    flags()        const { return flags_; }
       
    75     u_int64_t    ontology()     const { return ontology_; }
       
    76     u_char *     metadata()     const { return metadata_; }
       
    77     u_int32_t    metadata_len() const { return metadata_len_; }
       
    78 
       
    79     oasys::Lock* lock() { return &lock_; }
       
    80 
       
    81     // Set class data.
       
    82     void set_flags(u_int64_t flags);
       
    83     void set_block_error()                { error_ = true; }
       
    84     void set_ontology(u_int64_t ontology) { ontology_ = ontology; }
       
    85     void set_metadata(u_char *buf, u_int32_t len);
       
    86     
       
    87     /**
       
    88      * Mark the metadata block for removal from the bundle on the
       
    89      * specified outgoing link.
       
    90      *
       
    91      * @returns true if the metadata block was successfully marked for
       
    92      * removal; otherwise false.
       
    93      */
       
    94     bool remove_outgoing_metadata(const LinkRef& link);
       
    95 
       
    96     /**
       
    97      * Modify the metadata block for the specified outgoing link.
       
    98      *
       
    99      * @returns true if the metadata block was successfully modified;
       
   100      * otherwise false.
       
   101      */
       
   102     bool modify_outgoing_metadata(const LinkRef& link,
       
   103                                   u_char* buf, u_int32_t len);
       
   104 
       
   105     /**
       
   106      * @returns true if the metadata block is marked for removal from
       
   107      * from the bundle on the specified outgoing link; otherwise false.
       
   108      */
       
   109     bool metadata_removed(const LinkRef& link);
       
   110 
       
   111     /**
       
   112      * @returns true if the metadata block is modified for the specified
       
   113      * outgoing link; otherwise false.
       
   114      */
       
   115     bool metadata_modified(const LinkRef& link);
       
   116 
       
   117     /**
       
   118      * @returns true if the metadata block is modified for the specified
       
   119      * outgoing link; otherwise false. The modified ontology data and
       
   120      * length are returned if the metadata block was modified.
       
   121      */
       
   122     bool metadata_modified(const LinkRef& link, u_char** buf, u_int32_t& len);
       
   123 
       
   124     /**
       
   125      * Remove any outgoing metadata state for the specified link.
       
   126      */
       
   127     void delete_outgoing_metadata(const LinkRef& link);
       
   128 
       
   129     /// Virtual from SerializableObject
       
   130     virtual void serialize(oasys::SerializeAction* action) { (void)action; }
       
   131     
       
   132     void operator=(const MetadataBlock& copy);
       
   133 
       
   134     static unsigned int get_next_index() { return index_++; }
       
   135 
       
   136 private:
       
   137     oasys::Mutex lock_;
       
   138 
       
   139     unsigned int id_;                ///< unique identifier
       
   140     BlockInfo *  block_;             ///< corresponding BlockInfo
       
   141 
       
   142     bool         generated_;         ///< flag that indicates if the metadata
       
   143                                      ///< was locally generated, and therefore
       
   144                                      ///< memory allocated to the ontology buf
       
   145 
       
   146     bool         error_;             ///< flag that indicates if the metadata
       
   147                                      ///< block was received with errors
       
   148 
       
   149     unsigned int source_id_;
       
   150     bool         source_;            // flag that indicates if the block
       
   151                                      // is a link-specific modification
       
   152                                      // of metadata generated at the API
       
   153 
       
   154     u_int64_t     flags_;            // flags specified at the API for
       
   155                                      // generated metadata
       
   156 
       
   157     u_int64_t    ontology_;
       
   158     u_char *     metadata_;
       
   159     u_int32_t    metadata_len_;
       
   160 
       
   161     class OutgoingMetadata {
       
   162     public:
       
   163         // Constructors
       
   164         OutgoingMetadata(const LinkRef& link):
       
   165             link_(link.object(), "OutgoingMetadata"), remove_(true),
       
   166             metadata_(NULL), metadata_len_(0) {}
       
   167         OutgoingMetadata(const LinkRef& link, u_char* buf, u_int32_t len);
       
   168         OutgoingMetadata(const OutgoingMetadata& copy);
       
   169 
       
   170         // Destructor
       
   171         ~OutgoingMetadata();
       
   172 
       
   173         void operator=(const OutgoingMetadata& copy);
       
   174 
       
   175         // Accessors
       
   176         const LinkRef& link()    const { return link_; }
       
   177         bool      remove()       const { return remove_; }
       
   178         u_char *  metadata()     const { return metadata_; }
       
   179         u_int32_t metadata_len() const { return metadata_len_; }
       
   180 
       
   181     private:
       
   182         LinkRef   link_;
       
   183         bool      remove_;
       
   184         u_char *  metadata_;
       
   185         u_int32_t metadata_len_;
       
   186     };
       
   187 
       
   188     std::vector<OutgoingMetadata> outgoing_metadata_;
       
   189 
       
   190     OutgoingMetadata* find_outgoing_metadata(const LinkRef& link);
       
   191     bool              has_outgoing_metadata(const LinkRef& link)
       
   192                           { return (find_outgoing_metadata(link) != NULL); }
       
   193 
       
   194     static unsigned int index_;
       
   195 };
       
   196 
       
   197 /**
       
   198  * Typedef for a reference to a MetadataBlock.
       
   199  */
       
   200 typedef oasys::Ref<MetadataBlock> MetadataBlockRef;
       
   201 
       
   202 /**
       
   203  * A vector of Metadata Block references.
       
   204  */
       
   205 class MetadataVec : public std::vector<MetadataBlockRef> {
       
   206 public:
       
   207     MetadataVec(const std::string& name) : name_(name) {}
       
   208 
       
   209     /**
       
   210      * Wrapper around push_back that takes a vanilla MetadataBlock
       
   211      * pointer.
       
   212      */
       
   213     void push_back(MetadataBlock* block) {
       
   214         std::vector<MetadataBlockRef>::
       
   215             push_back(MetadataBlockRef(block, name_.c_str()));
       
   216     }
       
   217 
       
   218 protected:
       
   219     std::string name_;
       
   220 };
       
   221 
       
   222 /**
       
   223  * Data structure to store a metadata block vector for each outgoing
       
   224  * link, similar to LinkBlockSet.
       
   225  */
       
   226 class LinkMetadataSet {
       
   227 public:
       
   228     // Destructor
       
   229     virtual ~LinkMetadataSet();
       
   230     
       
   231     MetadataVec* create_blocks(const LinkRef& link);
       
   232     MetadataVec* find_blocks(const LinkRef& link) const;
       
   233     void         delete_blocks(const LinkRef& link);
       
   234 
       
   235 private:
       
   236     struct Entry {
       
   237         Entry(const LinkRef& link);
       
   238 
       
   239         MetadataVec* blocks_;
       
   240         LinkRef      link_;
       
   241     };
       
   242 
       
   243     typedef std::vector<Entry> Vector;
       
   244     typedef std::vector<Entry>::iterator iterator;
       
   245     typedef std::vector<Entry>::const_iterator const_iterator;
       
   246     Vector entries_;
       
   247 };
       
   248 
       
   249 } // namespace dtn
       
   250 
       
   251 #endif /* _METADATA_BLOCK_H_ */