blob: 579128f1f19127193030173bdc9e72de9f687f32 [file] [log] [blame]
Alexander Afanasyev68f2a952013-01-08 14:34:16 -08001/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */
2/*
3 * Copyright (c) 2012 University of California, Los Angeles
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu>
19 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
20 */
21
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080022#ifndef CCNX_COMMON_H
23#define CCNX_COMMON_H
24
Zhenkai Zhuf47109b2013-01-02 19:41:34 -080025extern "C" {
26#include <ccn/ccn.h>
27#include <ccn/charbuf.h>
28#include <ccn/keystore.h>
29#include <ccn/uri.h>
30#include <ccn/bloom.h>
31#include <ccn/signing.h>
32}
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080033#include <vector>
34#include <boost/shared_ptr.hpp>
35#include <boost/exception/all.hpp>
36#include <boost/function.hpp>
37#include <string>
38#include <sstream>
39#include <map>
40#include <utility>
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080041#include <string.h>
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -080042#include <boost/iostreams/filter/gzip.hpp>
43#include <boost/iostreams/filtering_stream.hpp>
44#include <boost/iostreams/device/back_inserter.hpp>
45#include <boost/range/iterator_range.hpp>
46#include <boost/make_shared.hpp>
47
48namespace io = boost::iostreams;
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080049
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080050namespace Ccnx {
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080051typedef std::vector<unsigned char> Bytes;
52typedef std::vector<std::string>Comps;
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080053
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080054typedef boost::shared_ptr<Bytes> BytesPtr;
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080055
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080056inline
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080057const unsigned char *
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080058head(const Bytes &bytes)
59{
60 return &bytes[0];
61}
62
63inline
64unsigned char *
65head (Bytes &bytes)
66{
67 return &bytes[0];
68}
69
70// --- Bytes operations start ---
71inline void
72readRaw(Bytes &bytes, const unsigned char *src, size_t len)
73{
74 if (len > 0)
75 {
76 bytes.resize(len);
77 memcpy (head (bytes), src, len);
78 }
79}
80
81inline BytesPtr
82readRawPtr (const unsigned char *src, size_t len)
83{
84 if (len > 0)
85 {
86 BytesPtr ret (new Bytes (len));
87 memcpy (head (*ret), src, len);
88
89 return ret;
90 }
91 else
92 return BytesPtr ();
93}
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080094
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080095template<class Msg>
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -080096BytesPtr
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080097serializeMsg(const Msg &msg)
98{
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -080099 int size = msg.ByteSize ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800100 BytesPtr bytes (new Bytes (size));
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -0800101 msg.SerializeToArray (head(*bytes), size);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -0800102 return bytes;
103}
104
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800105template<class Msg>
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800106boost::shared_ptr<Msg>
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800107deserializeMsg (const Bytes &bytes)
108{
109 boost::shared_ptr<Msg> retval (new Msg ());
Alexander Afanasyev428c53c2013-02-13 12:38:28 -0800110 if (!retval->ParseFromArray (head (bytes), bytes.size ()))
111 {
112 // to indicate an error
113 return boost::shared_ptr<Msg> ();
114 }
Alexander Afanasyeva35756b2013-01-22 16:59:11 -0800115 return retval;
116}
117
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800118template<class Msg>
Alexander Afanasyev923f2f12013-02-23 16:36:31 -0800119boost::shared_ptr<Msg>
120deserializeMsg (const void *buf, size_t length)
121{
122 boost::shared_ptr<Msg> retval (new Msg ());
123 if (!retval->ParseFromArray (buf, length))
124 {
125 // to indicate an error
126 return boost::shared_ptr<Msg> ();
127 }
128 return retval;
129}
130
131
132template<class Msg>
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800133BytesPtr
134serializeGZipMsg(const Msg &msg)
135{
136 std::vector<char> bytes; // Bytes couldn't work
137 {
138 boost::iostreams::filtering_ostream out;
139 out.push(boost::iostreams::gzip_compressor()); // gzip filter
140 out.push(boost::iostreams::back_inserter(bytes)); // back_inserter sink
141
142 msg.SerializeToOstream(&out);
143 }
144 BytesPtr uBytes = boost::make_shared<Bytes>(bytes.size());
145 memcpy(&(*uBytes)[0], &bytes[0], bytes.size());
146 return uBytes;
147}
148
149template<class Msg>
150boost::shared_ptr<Msg>
151deserializeGZipMsg(const Bytes &bytes)
152{
153 std::vector<char> sBytes(bytes.size());
154 memcpy(&sBytes[0], &bytes[0], bytes.size());
155 boost::iostreams::filtering_istream in;
156 in.push(boost::iostreams::gzip_decompressor()); // gzip filter
157 in.push(boost::make_iterator_range(sBytes)); // source
158
159 boost::shared_ptr<Msg> retval = boost::make_shared<Msg>();
Alexander Afanasyev428c53c2013-02-13 12:38:28 -0800160 if (!retval->ParseFromIstream(&in))
161 {
162 // to indicate an error
163 return boost::shared_ptr<Msg> ();
164 }
Zhenkai Zhu7c1cd8d2013-01-31 12:27:17 -0800165
166 return retval;
167}
168
169
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -0800170// --- Bytes operations end ---
171
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800172// Exceptions
173typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -0800174
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800175} // Ccnx
176#endif // CCNX_COMMON_H