NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.0: NDN, CCN, CCNx, content centric networks
API Documentation
io.hpp
Go to the documentation of this file.
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22
#ifndef NDN_UTIL_IO_HPP
23
#define NDN_UTIL_IO_HPP
24
25
#include "../common.hpp"
26
27
#include "../encoding/block.hpp"
28
#include "../encoding/buffer-stream.hpp"
29
30
#include <iostream>
31
#include <fstream>
32
#include "../security/cryptopp.hpp"
33
34
35
namespace
ndn
{
36
namespace
io {
37
38
class
Error
:
public
std::runtime_error
39
{
40
public
:
41
explicit
42
Error
(
const
std::string& what)
43
:
std
::runtime_error(what)
44
{
45
}
46
};
47
48
enum
IoEncoding
{
49
NO_ENCODING
,
50
BASE_64
,
51
HEX
52
};
53
54
template
<
typename
T>
55
shared_ptr<T>
56
load
(std::istream& is,
IoEncoding
encoding =
BASE_64
)
57
{
58
typedef
typename
T::Error TypeError;
59
try
60
{
61
using namespace
CryptoPP
;
62
63
shared_ptr<T>
object
= make_shared<T>();
64
65
OBufferStream
os;
66
67
switch
(encoding)
68
{
69
case
NO_ENCODING
:
70
{
71
FileSource ss(is,
true
,
new
FileSink(os));
72
break
;
73
}
74
case
BASE_64
:
75
{
76
FileSource ss(is,
true
,
new
Base64Decoder(
new
FileSink(os)));
77
break
;
78
}
79
case
HEX
:
80
{
81
FileSource ss(is,
true
,
new
HexDecoder(
new
FileSink(os)));
82
break
;
83
}
84
default
:
85
return
shared_ptr<T>();
86
}
87
88
object
->wireDecode(
Block
(os.buf()));
89
return
object;
90
}
91
catch
(TypeError& e)
92
{
93
return
shared_ptr<T>();
94
}
95
catch
(CryptoPP::Exception& e)
96
{
97
return
shared_ptr<T>();
98
}
99
catch
(
tlv::Error
& e)
100
{
101
return
shared_ptr<T>();
102
}
103
}
104
105
template
<
typename
T>
106
shared_ptr<T>
107
load
(
const
std::string&
file
,
IoEncoding
encoding =
BASE_64
)
108
{
109
std::ifstream is(file.c_str());
110
return
load<T>(is, encoding);
111
}
112
113
template
<
typename
T>
114
void
115
save
(
const
T&
object
, std::ostream& os,
IoEncoding
encoding =
BASE_64
)
116
{
117
typedef
typename
T::Error TypeError;
118
try
119
{
120
using namespace
CryptoPP
;
121
122
Block
block =
object
.wireEncode();
123
124
switch
(encoding)
125
{
126
case
NO_ENCODING
:
127
{
128
StringSource ss(block.
wire
(), block.
size
(),
true
,
129
new
FileSink(os));
130
break
;
131
}
132
case
BASE_64
:
133
{
134
StringSource ss(block.
wire
(), block.
size
(),
true
,
135
new
Base64Encoder(
new
FileSink(os),
true
, 64));
136
break
;
137
}
138
case
HEX
:
139
{
140
StringSource ss(block.
wire
(), block.
size
(),
true
,
141
new
HexEncoder(
new
FileSink(os)));
142
break
;
143
}
144
default
:
145
return
;
146
}
147
return
;
148
}
149
catch
(TypeError& e)
150
{
151
BOOST_THROW_EXCEPTION(
Error
(e.what()));
152
}
153
catch
(CryptoPP::Exception& e)
154
{
155
BOOST_THROW_EXCEPTION(
Error
(e.what()));
156
}
157
catch
(
tlv::Error
& e)
158
{
159
BOOST_THROW_EXCEPTION(
Error
(e.what()));
160
}
161
}
162
163
template
<
typename
T>
164
void
165
save
(
const
T&
object
,
const
std::string&
file
,
IoEncoding
encoding =
BASE_64
)
166
{
167
std::ofstream os(file.c_str());
168
save
(
object
, os, encoding);
169
}
170
171
}
// namespace io
172
}
// namespace ndn
173
174
#endif // NDN_UTIL_IO_HPP
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition:
ndn-strategy-choice-helper.hpp:34
CryptoPP
Copyright (c) 2013-2014 Regents of the University of California.
Definition:
oid.hpp:29
ndn::Block::wire
const uint8_t * wire() const
Definition:
block.cpp:495
std
STL namespace.
ndn::io::Error
Definition:
io.hpp:38
ndn::Block
Class representing a wire element of NDN-TLV packet format.
Definition:
block.hpp:43
ndn::io::load
shared_ptr< T > load(std::istream &is, IoEncoding encoding=BASE_64)
Definition:
io.hpp:56
ndn::Block::size
size_t size() const
Definition:
block.cpp:504
ndn::io::HEX
Definition:
io.hpp:51
ndn::io::NO_ENCODING
Definition:
io.hpp:49
ndn::security::file
tpm file
Definition:
key-chain.cpp:70
ndn::io::Error::Error
Error(const std::string &what)
Definition:
io.hpp:42
ndn::OBufferStream
Class implementing interface similar to ostringstream, but to construct ndn::Buffer.
Definition:
buffer-stream.hpp:79
ndn::tlv::Error
represents an error in TLV encoding or decoding
Definition:
tlv.hpp:50
ndn::io::BASE_64
Definition:
io.hpp:50
ndn::io::save
void save(const T &object, std::ostream &os, IoEncoding encoding=BASE_64)
Definition:
io.hpp:115
ndn::io::IoEncoding
IoEncoding
Definition:
io.hpp:48
ndnSIM
ndn-cxx
src
util
io.hpp
Generated on Sat Nov 12 2016 16:02:53 for ndnSIM by
1.8.12