IBR-DTNSuite  0.10
AcknowledgementSet.cpp
Go to the documentation of this file.
1 /*
2  * AcknowledgementSet.cpp
3  *
4  * Created on: 11.01.2013
5  * Author: morgenro
6  */
7 
9 #include "core/BundleCore.h"
10 #include <ibrdtn/utils/Clock.h>
11 
12 namespace dtn
13 {
14  namespace routing
15  {
17 
19  {
20  }
21 
23  : ibrcommon::Mutex(), _bundles(other._bundles)
24  {
25  }
26 
27  void AcknowledgementSet::add(const dtn::data::MetaBundle &bundle) throw ()
28  {
29  try {
30  // check if the bundle is valid
32 
33  // add the bundle to the set
34  _bundles.add(bundle);
36  // bundle rejected
37  }
38  }
39 
40  void AcknowledgementSet::expire(const dtn::data::Timestamp &timestamp) throw ()
41  {
42  _bundles.expire(timestamp);
43  }
44 
45  void AcknowledgementSet::merge(const AcknowledgementSet &other) throw ()
46  {
47  for(dtn::data::BundleList::const_iterator it = other._bundles.begin(); it != other._bundles.end(); ++it)
48  {
49  const dtn::data::MetaBundle &ack = (*it);
50  this->add(ack);
51  }
52  }
53 
54  bool AcknowledgementSet::has(const dtn::data::BundleID &id) const throw ()
55  {
57  return !(iter == _bundles.end());
58  }
59 
61  {
62  return identifier;
63  }
64 
66  {
67  std::stringstream ss;
68  serialize(ss);
69  return ss.str().length();
70  }
71 
72  std::ostream& AcknowledgementSet::serialize(std::ostream& stream) const
73  {
74  stream << (*this);
75  return stream;
76  }
77 
78  std::istream& AcknowledgementSet::deserialize(std::istream& stream)
79  {
80  stream >> (*this);
81  return stream;
82  }
83 
84  std::ostream& operator<<(std::ostream& stream, const AcknowledgementSet& ack_set)
85  {
86  dtn::data::Number ackset_size(ack_set._bundles.size());
87  ackset_size.encode(stream);
88  for (dtn::data::BundleList::const_iterator it = ack_set._bundles.begin(); it != ack_set._bundles.end(); ++it)
89  {
90  const dtn::data::MetaBundle &ack = (*it);
91  stream << (const dtn::data::BundleID&)ack;
92  ack.expiretime.encode(stream);
93  }
94 
95  return stream;
96  }
97 
98  std::istream& operator>>(std::istream &stream, AcknowledgementSet &ack_set)
99  {
100  // clear the ack set first
101  ack_set._bundles.clear();
102 
103  dtn::data::Number size;
104  size.decode(stream);
105 
106  for(size_t i = 0; size > i; ++i)
107  {
109  stream >> (dtn::data::BundleID&)ack;
110  ack.expiretime.decode(stream);
112 
113  ack_set.add(ack);
114  }
115  return stream;
116  }
117  } /* namespace routing */
118 } /* namespace dtn */