IBR-DTNSuite  0.10
BundleList.cpp
Go to the documentation of this file.
1 /*
2  * BundleList.cpp
3  *
4  * Copyright (C) 2011 IBR, TU Braunschweig
5  *
6  * Written-by: Johannes Morgenroth <morgenroth@ibr.cs.tu-bs.de>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 
22 #include "ibrdtn/data/BundleList.h"
23 #include <algorithm>
24 
25 namespace dtn
26 {
27  namespace data
28  {
30 
32  : _listener(listener)
33  { }
34 
36  { }
37 
38  void BundleList::add(const dtn::data::MetaBundle &bundle) throw ()
39  {
40  // insert the bundle to the public list
41  pair<iterator,bool> ret = _meta_bundles.insert(bundle);
42 
43  if (ret.second) {
44  ExpiringBundle exb(*ret.first);
45  _bundles.insert(exb);
46  }
47  }
48 
49  void BundleList::remove(const dtn::data::MetaBundle &bundle) throw ()
50  {
51  // delete bundle id in the private list
52  _bundles.erase(bundle);
53 
54  // delete bundle id in the public list
55  _meta_bundles.erase(bundle);
56  }
57 
58  void BundleList::clear() throw ()
59  {
60  _bundles.clear();
61  _meta_bundles.clear();
62  }
63 
64  void BundleList::expire(const Timestamp &timestamp) throw ()
65  {
66  // we can not expire bundles if we have no idea of time
67  if (timestamp == 0) return;
68 
69  std::set<ExpiringBundle>::iterator iter = _bundles.begin();
70 
71  while (iter != _bundles.end())
72  {
73  const ExpiringBundle &b = (*iter);
74 
75  if ( b.bundle.expiretime >= timestamp ) break;
76 
77  // raise expired event
78  if (_listener != NULL)
79  _listener->eventBundleExpired( b.bundle );
80 
81  // remove this item in public list
82  _meta_bundles.erase( b.bundle );
83 
84  // remove this item in private list
85  _bundles.erase( iter++ );
86  }
87  }
88 
89  BundleList::ExpiringBundle::ExpiringBundle(const MetaBundle &b)
90  : bundle(b)
91  { }
92 
93  BundleList::ExpiringBundle::~ExpiringBundle()
94  { }
95 
96  bool BundleList::ExpiringBundle::operator!=(const ExpiringBundle& other) const
97  {
98  return !(other == *this);
99  }
100 
101  bool BundleList::ExpiringBundle::operator==(const ExpiringBundle& other) const
102  {
103  return (other.bundle == this->bundle);
104  }
105 
106  bool BundleList::ExpiringBundle::operator<(const ExpiringBundle& other) const
107  {
108  if (bundle.expiretime < other.bundle.expiretime) return true;
109  if (bundle.expiretime != other.bundle.expiretime) return false;
110 
111  if (bundle < other.bundle) return true;
112 
113  return false;
114  }
115 
116  bool BundleList::ExpiringBundle::operator>(const ExpiringBundle& other) const
117  {
118  return !(((*this) < other) || ((*this) == other));
119  }
120  }
121 }