IBR-DTNSuite  0.12
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  ack.lifetime.encode(stream);
94  }
95 
96  return stream;
97  }
98 
99  std::istream& operator>>(std::istream &stream, AcknowledgementSet &ack_set)
100  {
101  // clear the ack set first
102  ack_set._bundles.clear();
103 
104  dtn::data::Number size;
105  size.decode(stream);
106 
107  for(size_t i = 0; size > i; ++i)
108  {
110  stream >> (dtn::data::BundleID&)ack;
111  ack.expiretime.decode(stream);
112  ack.lifetime.decode(stream);
113 
114  ack_set.add(ack);
115  }
116  return stream;
117  }
118  } /* namespace routing */
119 } /* namespace dtn */