IBR-DTNSuite  0.12
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  // search for the bundle in the public list
52  const std::set<dtn::data::MetaBundle>::iterator it = _meta_bundles.find(bundle);
53 
54  // only if the bundle is in the public list
55  if (it != _meta_bundles.end())
56  {
57  // remove the bundle from the set of expiring bundles
58  _bundles.erase(*it);
59 
60  // remove the bundle from the public list
61  _meta_bundles.erase(it);
62  }
63  }
64 
65  void BundleList::clear() throw ()
66  {
67  _bundles.clear();
68  _meta_bundles.clear();
69  }
70 
71  void BundleList::expire(const Timestamp &timestamp) throw ()
72  {
73  // we can not expire bundles if we have no idea of time
74  if (timestamp == 0) return;
75 
76  std::set<ExpiringBundle>::iterator iter = _bundles.begin();
77 
78  while (iter != _bundles.end())
79  {
80  const ExpiringBundle &b = (*iter);
81 
82  if ( b.bundle.expiretime >= timestamp ) break;
83 
84  // raise expired event
85  if (_listener != NULL)
86  _listener->eventBundleExpired( b.bundle );
87 
88  // remove this item in public list
89  _meta_bundles.erase( b.bundle );
90 
91  // remove this item in private list
92  _bundles.erase( iter++ );
93  }
94  }
95 
96  BundleList::ExpiringBundle::ExpiringBundle(const MetaBundle &b)
97  : bundle(b)
98  { }
99 
100  BundleList::ExpiringBundle::~ExpiringBundle()
101  { }
102 
103  bool BundleList::ExpiringBundle::operator!=(const ExpiringBundle& other) const
104  {
105  return !(other == *this);
106  }
107 
108  bool BundleList::ExpiringBundle::operator==(const ExpiringBundle& other) const
109  {
110  return (other.bundle == this->bundle);
111  }
112 
113  bool BundleList::ExpiringBundle::operator<(const ExpiringBundle& other) const
114  {
115  if (bundle.expiretime < other.bundle.expiretime) return true;
116  if (bundle.expiretime != other.bundle.expiretime) return false;
117 
118  if (bundle < other.bundle) return true;
119 
120  return false;
121  }
122 
123  bool BundleList::ExpiringBundle::operator>(const ExpiringBundle& other) const
124  {
125  return !(((*this) < other) || ((*this) == other));
126  }
127  }
128 }