IBR-DTNSuite
0.12
Main Page
Namespaces
Classes
Files
File List
File Members
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
"
25
#include "
core/BundleGeneratedEvent.h
"
26
#include <
ibrdtn/data/PayloadBlock.h
>
27
#include <
ibrdtn/data/BundleID.h
>
28
#include <
ibrcommon/thread/MutexLock.h
>
29
#include <
ibrcommon/Logger.h
>
30
31
namespace
dtn
32
{
33
namespace
storage
34
{
35
BundleStorage::BundleStorage
(
const
dtn::data::Length
&maxsize)
36
: _faulty(false), _maxsize(maxsize), _currentsize(0)
37
{
38
}
39
40
BundleStorage::~BundleStorage
()
41
{
42
}
43
44
void
BundleStorage::remove
(
const
dtn::data::Bundle
&b)
45
{
46
remove
(
dtn::data::BundleID
(b));
47
}
48
49
const
dtn::data::EID
BundleStorage::acceptCustody
(
const
dtn::data::MetaBundle
&meta)
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
77
custody_bundle.
set
(
dtn::data::PrimaryBlock::APPDATA_IS_ADMRECORD
,
true
);
78
custody_bundle.
set
(
dtn::data::PrimaryBlock::DESTINATION_IS_SINGLETON
,
true
);
79
custody_bundle.
destination
= meta.
custodian
;
80
custody_bundle.
source
=
dtn::core::BundleCore::local
;
81
82
// send the custody accepted bundle
83
dtn::core::BundleGeneratedEvent::raise
(custody_bundle);
84
85
// raise the custody accepted event
86
dtn::core::CustodyEvent::raise
(meta,
dtn::core::CUSTODY_ACCEPT
);
87
88
return
dtn::core::BundleCore::local
;
89
}
90
91
void
BundleStorage::rejectCustody
(
const
dtn::data::MetaBundle
&meta,
dtn::data::CustodySignalBlock::REASON_CODE
reason)
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
112
dtn::data::PayloadBlock
&payload = b.
push_back
<
dtn::data::PayloadBlock
>();
113
signal.
write
(payload);
114
115
b.
set
(
dtn::data::PrimaryBlock::APPDATA_IS_ADMRECORD
,
true
);
116
b.
destination
= meta.
custodian
;
117
b.
source
=
dtn::core::BundleCore::local
;
118
119
// send the custody rejected bundle
120
dtn::core::BundleGeneratedEvent::raise
(b);
121
122
// raise the custody rejected event
123
dtn::core::CustodyEvent::raise
(
dtn::data::MetaBundle::create
(b),
dtn::core::CUSTODY_REJECT
);
124
}
125
126
dtn::data::Length
BundleStorage::size
()
const
127
{
128
return
_currentsize;
129
}
130
131
void
BundleStorage::allocSpace
(
const
dtn::data::Length
&size)
throw
(
StorageSizeExeededException
)
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
{
138
throw
StorageSizeExeededException
();
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
159
void
BundleStorage::clearSpace
() throw ()
160
{
161
ibrcommon::MutexLock
l(_sizelock);
162
_currentsize = 0;
163
}
164
165
void
BundleStorage::eventBundleAdded
(
const
dtn::data::MetaBundle
&b)
throw
()
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
175
void
BundleStorage::eventBundleRemoved
(
const
dtn::data::BundleID
&
id
)
throw
()
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
185
void
BundleStorage::attach
(
dtn::storage::BundleIndex
*index)
186
{
187
ibrcommon::MutexLock
l(_index_lock);
188
_indexes.insert(index);
189
}
190
191
void
BundleStorage::detach
(
dtn::storage::BundleIndex
*index)
192
{
193
ibrcommon::MutexLock
l(_index_lock);
194
_indexes.erase(index);
195
}
196
}
197
}
daemon
src
storage
BundleStorage.cpp
Generated on Thu Mar 27 2014 09:26:20 for IBR-DTNSuite by
1.8.4