IBR-DTNSuite  0.12
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"
27 #include <ibrdtn/data/BundleID.h>
29 #include <ibrcommon/Logger.h>
30 
31 namespace dtn
32 {
33  namespace storage
34  {
36  : _faulty(false), _maxsize(maxsize), _currentsize(0)
37  {
38  }
39 
41  {
42  }
43 
45  {
46  remove(dtn::data::BundleID(b));
47  }
48 
50  {
51  if (!meta.get(Bundle::CUSTODY_REQUESTED))
52  throw ibrcommon::Exception("custody transfer is not requested for this bundle.");
53 
54  if (meta.custodian == EID())
55  throw ibrcommon::Exception("no previous custodian is set.");
56 
57  // create a new bundle
58  Bundle custody_bundle;
59 
60  // set priority to HIGH
61  custody_bundle.set(dtn::data::PrimaryBlock::PRIORITY_BIT1, false);
62  custody_bundle.set(dtn::data::PrimaryBlock::PRIORITY_BIT2, true);
63 
64  // create a custody signal with accept flag
65  CustodySignalBlock signal;
66 
67  // set the bundle to match
68  signal.setMatch(meta);
69 
70  // set accepted
71  signal.custody_accepted = true;
72 
73  // write the custody data to a payload block
74  dtn::data::PayloadBlock &payload = custody_bundle.push_back<dtn::data::PayloadBlock>();
75  signal.write(payload);
76 
79  custody_bundle.destination = meta.custodian;
80  custody_bundle.source = dtn::core::BundleCore::local;
81 
82  // send the custody accepted bundle
84 
85  // raise the custody accepted event
87 
89  }
90 
92  {
93  if (!meta.get(Bundle::CUSTODY_REQUESTED))
94  throw ibrcommon::Exception("custody transfer is not requested for this bundle.");
95 
96  if (meta.custodian == EID())
97  throw ibrcommon::Exception("no previous custodian is set.");
98 
99  // create a new bundle
100  Bundle b;
101 
102  // create a custody signal with reject flag
103  CustodySignalBlock signal;
104 
105  // set the bundle to match
106  signal.setMatch(meta);
107 
108  // set reason code
109  signal.reason = reason;
110 
111  // write the custody data to a payload block
113  signal.write(payload);
114 
116  b.destination = meta.custodian;
118 
119  // send the custody rejected bundle
121 
122  // raise the custody rejected event
124  }
125 
127  {
128  return _currentsize;
129  }
130 
132  {
133  ibrcommon::MutexLock l(_sizelock);
134 
135  // check if this container is too big for us.
136  if ((_maxsize > 0) && (_currentsize + size > _maxsize))
137  {
139  }
140 
141  // increment the storage size
142  _currentsize += size;
143  }
144 
145  void BundleStorage::freeSpace(const dtn::data::Length &size) throw ()
146  {
147  ibrcommon::MutexLock l(_sizelock);
148  if (size > _currentsize)
149  {
150  _currentsize = 0;
151  IBRCOMMON_LOGGER_TAG("BundleStorage", critical) << "More space to free than allocated." << IBRCOMMON_LOGGER_ENDL;
152  }
153  else
154  {
155  _currentsize -= size;
156  }
157  }
158 
160  {
161  ibrcommon::MutexLock l(_sizelock);
162  _currentsize = 0;
163  }
164 
166  {
167  IBRCOMMON_LOGGER_DEBUG_TAG("BundleStorage", 2) << "add bundle to index: " << b.toString() << IBRCOMMON_LOGGER_ENDL;
168 
169  for (index_list::iterator it = _indexes.begin(); it != _indexes.end(); ++it) {
170  BundleIndex &index = (**it);
171  index.add(b);
172  }
173  }
174 
176  {
177  IBRCOMMON_LOGGER_DEBUG_TAG("BundleStorage", 2) << "remove bundle from index: " << id.toString() << IBRCOMMON_LOGGER_ENDL;
178 
179  for (index_list::iterator it = _indexes.begin(); it != _indexes.end(); ++it) {
180  BundleIndex &index = (**it);
181  index.remove(id);
182  }
183  }
184 
186  {
187  ibrcommon::MutexLock l(_index_lock);
188  _indexes.insert(index);
189  }
190 
192  {
193  ibrcommon::MutexLock l(_index_lock);
194  _indexes.erase(index);
195  }
196  }
197 }