servlib/bundling/GbofId.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /* Copyright 2004-2006 BBN Technologies Corporation
       
     2  *
       
     3  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
       
     4  * use this file except in compliance with the License. You may obtain a copy
       
     5  * of the License at http://www.apache.org/licenses/LICENSE-2.0
       
     6  *
       
     7  * Unless required by applicable law or agreed to in writing, software
       
     8  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
       
     9  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       
    10  *
       
    11  * See the License for the specific language governing permissions and
       
    12  * limitations under the License.
       
    13  *
       
    14  *
       
    15  * $Id$
       
    16  */
       
    17 
       
    18 #ifdef HAVE_CONFIG_H
       
    19 #  include <dtn-config.h>
       
    20 #endif
       
    21 
       
    22 #include "GbofId.h"
       
    23 #include <sstream>
       
    24 
       
    25 // GBOFID -- Global Bundle Or Fragment ID
       
    26 
       
    27 namespace oasys {
       
    28 
       
    29 //----------------------------------------------------------------------
       
    30 template<>
       
    31 const char*
       
    32 InlineFormatter<dtn::GbofId>
       
    33 ::format(const dtn::GbofId& id)
       
    34 {
       
    35     if (! id.is_fragment_) {
       
    36         buf_.appendf("<%s, %llu.%llu>",
       
    37                      id.source_.c_str(),
       
    38                      id.creation_ts_.seconds_,
       
    39                      id.creation_ts_.seqno_);
       
    40     } else {
       
    41         buf_.appendf("<%s, %llu.%llu, FRAG len %u offset %u>",
       
    42                      id.source_.c_str(),
       
    43                      id.creation_ts_.seconds_,
       
    44                      id.creation_ts_.seqno_,
       
    45                      id.frag_length_, id.frag_offset_);
       
    46     }
       
    47     return buf_.c_str();
       
    48 }
       
    49 } // namespace oasys
       
    50 
       
    51 namespace dtn {
       
    52 
       
    53 //----------------------------------------------------------------------
       
    54 GbofId::GbofId()
       
    55 {
       
    56 }
       
    57 
       
    58 //----------------------------------------------------------------------
       
    59 GbofId::GbofId(EndpointID      source,
       
    60                BundleTimestamp creation_ts,
       
    61                bool            is_fragment,
       
    62                u_int32_t       frag_length,
       
    63                u_int32_t       frag_offset)
       
    64     : source_(source),
       
    65       creation_ts_(creation_ts),
       
    66       is_fragment_(is_fragment),
       
    67       frag_length_(frag_length),
       
    68       frag_offset_(frag_offset)
       
    69 {
       
    70 }
       
    71 
       
    72 //----------------------------------------------------------------------
       
    73 GbofId::~GbofId()
       
    74 {
       
    75 }
       
    76 
       
    77 //----------------------------------------------------------------------
       
    78 bool
       
    79 GbofId::equals(const GbofId& id) const
       
    80 {
       
    81     if (creation_ts_.seconds_ == id.creation_ts_.seconds_ &&
       
    82         creation_ts_.seqno_   == id.creation_ts_.seqno_ &&
       
    83         is_fragment_          == id.is_fragment_ &&
       
    84         (!is_fragment_ || 
       
    85          (frag_length_ == id.frag_length_ && frag_offset_ == id.frag_offset_)) &&
       
    86         source_.equals(id.source_)) 
       
    87     {
       
    88         return true;
       
    89     } else {
       
    90         return false;
       
    91     }
       
    92 }
       
    93 
       
    94 //----------------------------------------------------------------------
       
    95 bool
       
    96 GbofId::operator<(const GbofId& other) const
       
    97 {
       
    98     if (source_ < other.source_) return true;
       
    99     if (other.source_ < source_) return false;
       
   100 
       
   101     if (creation_ts_ < other.creation_ts_) return true;
       
   102     if (creation_ts_ > other.creation_ts_) return false;
       
   103 
       
   104     if (is_fragment_  && !other.is_fragment_) return true;
       
   105     if (!is_fragment_ && other.is_fragment_) return false;
       
   106     
       
   107     if (is_fragment_) {
       
   108         if (frag_length_ < other.frag_length_) return true;
       
   109         if (other.frag_length_ < frag_length_) return false;
       
   110 
       
   111         if (frag_offset_ < other.frag_offset_) return true;
       
   112         if (other.frag_offset_ < frag_offset_) return false;
       
   113     }
       
   114 
       
   115     return false; // all equal
       
   116 }
       
   117 
       
   118 //----------------------------------------------------------------------
       
   119 bool
       
   120 GbofId::equals(EndpointID source,
       
   121                BundleTimestamp creation_ts,
       
   122                bool is_fragment,
       
   123                u_int32_t frag_length,
       
   124                u_int32_t frag_offset) const
       
   125 {
       
   126     if (creation_ts_.seconds_ == creation_ts.seconds_ &&
       
   127 	creation_ts_.seqno_ == creation_ts.seqno_ &&
       
   128 	is_fragment_ == is_fragment &&
       
   129 	(!is_fragment || 
       
   130 	 (frag_length_ == frag_length && frag_offset_ == frag_offset)) &&
       
   131         source_.equals(source))
       
   132     {
       
   133         return true;
       
   134     } else {
       
   135         return false;
       
   136     }
       
   137 }
       
   138 
       
   139 //----------------------------------------------------------------------
       
   140 std::string
       
   141 GbofId::str() const
       
   142 {
       
   143         std::ostringstream oss;
       
   144 
       
   145         oss << source_.str() << ","
       
   146             << creation_ts_.seconds_ << "," 
       
   147             << creation_ts_.seqno_ << ","
       
   148             << is_fragment_ << ","
       
   149             << frag_length_ << ","
       
   150             << frag_offset_;
       
   151 
       
   152         return oss.str();
       
   153 }
       
   154 
       
   155 } // namespace dtn