IBR-DTNSuite
0.10
Main Page
Namespaces
Classes
Files
File List
File Members
ExtensionBlock.cpp
Go to the documentation of this file.
1
/*
2
* ExtensionBlock.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 "
ibrdtn/data/ExtensionBlock.h
"
23
#include "
ibrdtn/data/Exceptions.h
"
24
#include "
ibrdtn/data/ExtensionBlock.h
"
25
#include <
ibrcommon/thread/MutexLock.h
>
26
27
namespace
dtn
28
{
29
namespace
data
30
{
31
ExtensionBlock::FactoryList *
ExtensionBlock::factories
= NULL;
32
33
ExtensionBlock::FactoryList::FactoryList
()
34
{ }
35
36
ExtensionBlock::FactoryList::~FactoryList
()
37
{ }
38
39
void
ExtensionBlock::FactoryList::initialize
()
40
{
41
static
ibrcommon::Mutex
mutex;
42
ibrcommon::MutexLock
l(mutex);
43
44
if
(
ExtensionBlock::factories
== NULL)
45
{
46
ExtensionBlock::factories
=
new
ExtensionBlock::FactoryList
();
47
}
48
}
49
50
ExtensionBlock::Factory
&
ExtensionBlock::FactoryList::get
(
const
block_t
type)
throw
(
ibrcommon::Exception
)
51
{
52
std::map<block_t, ExtensionBlock::Factory*>::iterator iter = fmap.find(type);
53
54
if
(iter != fmap.end())
55
{
56
return
*(iter->second);
57
}
58
59
throw
ibrcommon::Exception
(
"Factory not available"
);
60
}
61
62
void
ExtensionBlock::FactoryList::add
(
const
block_t
type,
Factory
*f)
throw
(
ibrcommon::Exception
)
63
{
64
try
{
65
get
(type);
66
throw
ibrcommon::Exception
(
"extension block type already taken"
);
67
}
catch
(
const
ibrcommon::Exception
&) {
68
fmap[type] = f;
69
}
70
}
71
72
void
ExtensionBlock::FactoryList::remove
(
const
block_t
type)
73
{
74
fmap.erase(type);
75
}
76
77
ExtensionBlock::Factory::Factory
(
block_t
type)
78
: _type(type)
79
{
80
ExtensionBlock::FactoryList::initialize
();
81
ExtensionBlock::factories
->
add
(type,
this
);
82
}
83
84
ExtensionBlock::Factory::~Factory
()
85
{
86
ExtensionBlock::factories
->
remove
(_type);
87
}
88
89
ExtensionBlock::Factory
&
ExtensionBlock::Factory::get
(
block_t
type)
throw
(
ibrcommon::Exception
)
90
{
91
ExtensionBlock::FactoryList::initialize
();
92
return
ExtensionBlock::factories
->
get
(type);
93
}
94
95
ExtensionBlock::ExtensionBlock
()
96
:
Block
(0), _blobref(ibrcommon::BLOB::create())
97
{
98
}
99
100
ExtensionBlock::ExtensionBlock
(
ibrcommon::BLOB::Reference
ref)
101
:
Block
(0), _blobref(ref)
102
{
103
}
104
105
ExtensionBlock::~ExtensionBlock
()
106
{
107
}
108
109
ibrcommon::BLOB::Reference
ExtensionBlock::getBLOB
()
const
110
{
111
return
_blobref;
112
}
113
114
void
ExtensionBlock::setType
(
block_t
type)
115
{
116
_blocktype
= type;
117
}
118
119
Length
ExtensionBlock::getLength
()
const
120
{
121
ibrcommon::BLOB::Reference
blobref = _blobref;
122
ibrcommon::BLOB::iostream
io = blobref.
iostream
();
123
return
io.
size
();
124
}
125
126
std::ostream&
ExtensionBlock::serialize
(std::ostream &stream,
Length
&)
const
127
{
128
ibrcommon::BLOB::Reference
blobref = _blobref;
129
ibrcommon::BLOB::iostream
io = blobref.
iostream
();
130
131
try
{
132
ibrcommon::BLOB::copy
(stream, *io, io.
size
());
133
}
catch
(
const
ibrcommon::IOException
&ex) {
134
throw
dtn::SerializationFailedException
(ex.
what
());
135
}
136
137
return
stream;
138
}
139
140
std::istream&
ExtensionBlock::deserialize
(std::istream &stream,
const
Length
&length)
141
{
142
// lock the BLOB
143
ibrcommon::BLOB::iostream
io = _blobref.
iostream
();
144
145
// clear the blob
146
io.
clear
();
147
148
// check if the blob is ready
149
if
(!(*io).good())
throw
dtn::SerializationFailedException
(
"could not open BLOB for payload"
);
150
151
try
{
152
ibrcommon::BLOB::copy
(*io, stream, length);
153
}
catch
(
const
ibrcommon::IOException
&ex) {
154
throw
dtn::SerializationFailedException
(ex.
what
());
155
}
156
157
// set block not processed bit
158
set
(
dtn::data::Block::FORWARDED_WITHOUT_PROCESSED
,
true
);
159
160
return
stream;
161
}
162
}
163
}
ibrdtn
ibrdtn
data
ExtensionBlock.cpp
Generated on Mon Jul 22 2013 15:16:00 for IBR-DTNSuite by
1.8.3.1