servlib/routing/ProphetBundleList.cc
changeset 0 2b3e5ec03512
equal deleted inserted replaced
-1:000000000000 0:2b3e5ec03512
       
     1 /*
       
     2  *    Copyright 2007 Baylor University
       
     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 
       
    17 #ifdef HAVE_CONFIG_H
       
    18 #include <dtn-config.h>
       
    19 #endif
       
    20 #include "ProphetBundleList.h"
       
    21 
       
    22 namespace dtn
       
    23 {
       
    24 
       
    25 BundleRef
       
    26 ProphetBundleList::NULL_BUNDLE("ProphetBundleList");
       
    27 
       
    28 ProphetBundleList::ProphetBundleList(prophet::Repository::BundleCoreRep* core)
       
    29     : list_(core) {}
       
    30 
       
    31 ProphetBundleList::~ProphetBundleList()
       
    32 {
       
    33     clear();
       
    34 }
       
    35 
       
    36 void
       
    37 ProphetBundleList::add(const BundleRef& b)
       
    38 {
       
    39     ProphetBundle* pb = new ProphetBundle(b);
       
    40     if (!list_.add(pb))
       
    41         delete pb;
       
    42 }
       
    43 
       
    44 void
       
    45 ProphetBundleList::add(const prophet::Bundle* b)
       
    46 {
       
    47     if (list_.add(b))
       
    48         return;
       
    49 
       
    50     prophet::Bundle* pb = const_cast<prophet::Bundle*>(b);
       
    51     delete pb;
       
    52 }
       
    53 
       
    54 void
       
    55 ProphetBundleList::del(const BundleRef& b)
       
    56 {
       
    57     const_iterator i;
       
    58     if (find(b->dest().str(),
       
    59              b->creation_ts().seconds_,
       
    60              b->creation_ts().seqno_, i))
       
    61     {
       
    62         prophet::Bundle* bundle = const_cast<prophet::Bundle*>(*i);
       
    63         list_.del(*i);
       
    64         delete bundle;
       
    65     }
       
    66 }
       
    67 
       
    68 void
       
    69 ProphetBundleList::del(const prophet::Bundle* b)
       
    70 {
       
    71     prophet::Bundle* bundle = const_cast<prophet::Bundle*>(b);
       
    72     list_.del(bundle);
       
    73     delete bundle;
       
    74 }
       
    75 
       
    76 const prophet::Bundle*
       
    77 ProphetBundleList::find(const std::string& dst,
       
    78                         u_int creation_ts,
       
    79                         u_int seqno) const
       
    80 {
       
    81     const_iterator i;
       
    82     if (find(dst,creation_ts,seqno,i))
       
    83         return *i;
       
    84     return NULL;
       
    85 }
       
    86 
       
    87 const BundleRef&
       
    88 ProphetBundleList::find_ref(const prophet::Bundle* b) const
       
    89 {
       
    90     if (b != NULL)
       
    91     {
       
    92         const_iterator i;
       
    93         if (find(b->destination_id(),
       
    94                  b->creation_ts(),
       
    95                  b->sequence_num(),i))
       
    96             return dynamic_cast<const ProphetBundle*>(*i)->ref();
       
    97     }
       
    98     return NULL_BUNDLE;
       
    99 }
       
   100 
       
   101 void
       
   102 ProphetBundleList::clear()
       
   103 {
       
   104     while (!list_.empty())
       
   105     {
       
   106         prophet::Bundle* b = const_cast<prophet::Bundle*>(
       
   107                 list_.get_bundles().front());
       
   108         list_.del(b);
       
   109         // clean up memory from Facade wrapper
       
   110         delete b;
       
   111     }
       
   112 }
       
   113 
       
   114 bool
       
   115 ProphetBundleList::find(const std::string& dst,
       
   116         u_int creation_ts, u_int seqno, const_iterator& i) const
       
   117 {
       
   118     i = list_.get_bundles().begin();
       
   119     while (i != list_.get_bundles().end())
       
   120     {
       
   121         if ((*i)->creation_ts() == creation_ts &&
       
   122             (*i)->sequence_num() == seqno &&
       
   123             (*i)->destination_id() == dst) break;
       
   124         i++;
       
   125     }
       
   126 
       
   127     if (i == list_.get_bundles().end()) return false;
       
   128 
       
   129     return ((*i)->creation_ts() == creation_ts &&
       
   130             (*i)->sequence_num() == seqno &&
       
   131             (*i)->destination_id() == dst);
       
   132 }
       
   133 
       
   134 }; // namespace dtn