IBR-DTN  1.0.0
BundleStorage.cpp
Go to the documentation of this file.
1 /*
2  * BundleStorage.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 "core/BundleCore.h"
23 #include "storage/BundleStorage.h"
24 #include "core/CustodyEvent.h"
26 #include <ibrdtn/data/BundleID.h>
27 #include <ibrcommon/thread/MutexLock.h>
28 #include <ibrcommon/Logger.h>
29 
30 namespace dtn
31 {
32  namespace storage
33  {
35  : _faulty(false), _maxsize(maxsize), _currentsize(0)
36  {
37  }
38 
40  {
41  }
42 
44  {
45  remove(dtn::data::BundleID(b));
46  }
47 
49  {
50  if (!meta.get(Bundle::CUSTODY_REQUESTED))
51  throw ibrcommon::Exception("custody transfer is not requested for this bundle.");
52 
53  if (meta.custodian == EID())
54  throw ibrcommon::Exception("no previous custodian is set.");
55 
56  // create a new bundle
57  Bundle custody_bundle;
58 
59  // set priority to HIGH
60  custody_bundle.set(dtn::data::PrimaryBlock::PRIORITY_BIT1, false);
61  custody_bundle.set(dtn::data::PrimaryBlock::PRIORITY_BIT2, true);
62 
63  // create a custody signal with accept flag
64  CustodySignalBlock signal;
65 
66  // set the bundle to match
67  signal.setMatch(meta);
68 
69  // set accepted
70  signal.custody_accepted = true;
71 
72  // write the custody data to a payload block
73  dtn::data::PayloadBlock &payload = custody_bundle.push_back<dtn::data::PayloadBlock>();
74  signal.write(payload);
75 
78  custody_bundle.destination = meta.custodian;
79  custody_bundle.source = dtn::core::BundleCore::local;
80 
81  // always sign custody signals
83 
84  // send the custody accepted bundle
86 
87  // raise the custody accepted event
89 
91  }
92 
94  {
95  if (!meta.get(Bundle::CUSTODY_REQUESTED))
96  throw ibrcommon::Exception("custody transfer is not requested for this bundle.");
97 
98  if (meta.custodian == EID())
99  throw ibrcommon::Exception("no previous custodian is set.");
100 
101  // create a new bundle
102  Bundle b;
103 
104  // create a custody signal with reject flag
105  CustodySignalBlock signal;
106 
107  // set the bundle to match
108  signal.setMatch(meta);
109 
110  // set reason code
111  signal.reason = reason;
112 
113  // write the custody data to a payload block
115  signal.write(payload);
116 
118  b.destination = meta.custodian;
120 
121  // always sign custody signals
123 
124  // send the custody rejected bundle
126 
127  // raise the custody rejected event
129  }
130 
132  {
133  return _currentsize;
134  }
135 
137  {
138  ibrcommon::MutexLock l(_sizelock);
139 
140  // check if this container is too big for us.
141  if ((_maxsize > 0) && (_currentsize + size > _maxsize))
142  {
144  }
145 
146  // increment the storage size
147  _currentsize += size;
148  }
149 
150  void BundleStorage::freeSpace(const dtn::data::Length &size) throw ()
151  {
152  ibrcommon::MutexLock l(_sizelock);
153  if (size > _currentsize)
154  {
155  _currentsize = 0;
156  IBRCOMMON_LOGGER_TAG("BundleStorage", critical) << "More space to free than allocated." << IBRCOMMON_LOGGER_ENDL;
157  }
158  else
159  {
160  _currentsize -= size;
161  }
162  }
163 
165  {
166  ibrcommon::MutexLock l(_sizelock);
167  _currentsize = 0;
168  }
169 
171  {
172  IBRCOMMON_LOGGER_DEBUG_TAG("BundleStorage", 2) << "add bundle to index: " << b.toString() << IBRCOMMON_LOGGER_ENDL;
173 
174  for (index_list::iterator it = _indexes.begin(); it != _indexes.end(); ++it) {
175  BundleIndex &index = (**it);
176  index.add(b);
177  }
178  }
179 
181  {
182  IBRCOMMON_LOGGER_DEBUG_TAG("BundleStorage", 2) << "remove bundle from index: " << id.toString() << IBRCOMMON_LOGGER_ENDL;
183 
184  for (index_list::iterator it = _indexes.begin(); it != _indexes.end(); ++it) {
185  BundleIndex &index = (**it);
186  index.remove(id);
187  }
188  }
189 
191  {
192  ibrcommon::MutexLock l(_index_lock);
193  _indexes.insert(index);
194  }
195 
197  {
198  ibrcommon::MutexLock l(_index_lock);
199  _indexes.erase(index);
200  }
201  }
202 }
dtn::data::EID custodian
Definition: MetaBundle.h:62
static void inject(const dtn::data::EID &source, dtn::data::Bundle &bundle)
Definition: BundleCore.cpp:706
static dtn::data::EID local
Definition: BundleCore.h:79
bool get(dtn::data::PrimaryBlock::FLAGS flag) const
Definition: MetaBundle.cpp:160
dtn::data::Length size() const
void attach(dtn::storage::BundleIndex *index)
size_t Length
Definition: Number.h:33
virtual void write(dtn::data::PayloadBlock &p) const
const dtn::data::EID acceptCustody(const dtn::data::MetaBundle &meta)
virtual void add(const dtn::data::MetaBundle &b)=0
static void raise(const dtn::data::MetaBundle &bundle, const EventCustodyAction action)
virtual void remove(const dtn::data::BundleID &id)=0
void detach(dtn::storage::BundleIndex *index)
void setMatch(const dtn::data::MetaBundle &other)
void rejectCustody(const dtn::data::MetaBundle &meta, dtn::data::CustodySignalBlock::REASON_CODE reason=dtn::data::CustodySignalBlock::NO_ADDITIONAL_INFORMATION)
void freeSpace(const dtn::data::Length &size)
virtual void remove(const dtn::data::BundleID &id)=0
void allocSpace(const dtn::data::Length &size)
void eventBundleAdded(const dtn::data::MetaBundle &b)
void eventBundleRemoved(const dtn::data::BundleID &id)
BundleStorage(const dtn::data::Length &maxsize)
T & push_back()
Definition: Bundle.h:180
static MetaBundle create(const dtn::data::BundleID &id)
Definition: MetaBundle.cpp:34
void set(FLAGS flag, bool value)
dtn::data::EID source
Definition: BundleID.h:53