Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 1 | /* -*- 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 Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 22 | #ifndef CCNX_COMMON_H |
| 23 | #define CCNX_COMMON_H |
| 24 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 25 | extern "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 Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 33 | #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 Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 41 | #include <string.h> |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 42 | #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 | |
| 48 | namespace io = boost::iostreams; |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 49 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 50 | namespace Ccnx { |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 51 | typedef std::vector<unsigned char> Bytes; |
| 52 | typedef std::vector<std::string>Comps; |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 53 | |
Alexander Afanasyev | 68f2a95 | 2013-01-08 14:34:16 -0800 | [diff] [blame] | 54 | typedef boost::shared_ptr<Bytes> BytesPtr; |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 55 | |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 56 | inline |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 57 | const unsigned char * |
Alexander Afanasyev | dbc0671 | 2013-01-08 18:30:28 -0800 | [diff] [blame] | 58 | head(const Bytes &bytes) |
| 59 | { |
| 60 | return &bytes[0]; |
| 61 | } |
| 62 | |
| 63 | inline |
| 64 | unsigned char * |
| 65 | head (Bytes &bytes) |
| 66 | { |
| 67 | return &bytes[0]; |
| 68 | } |
| 69 | |
| 70 | // --- Bytes operations start --- |
| 71 | inline void |
| 72 | readRaw(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 | |
| 81 | inline BytesPtr |
| 82 | readRawPtr (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 Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 94 | |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 95 | template<class Msg> |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 96 | BytesPtr |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 97 | serializeMsg(const Msg &msg) |
| 98 | { |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 99 | int size = msg.ByteSize (); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 100 | BytesPtr bytes (new Bytes (size)); |
Alexander Afanasyev | 08aa70a | 2013-01-22 22:16:25 -0800 | [diff] [blame] | 101 | msg.SerializeToArray (head(*bytes), size); |
Alexander Afanasyev | 49a30d0 | 2013-01-21 21:38:48 -0800 | [diff] [blame] | 102 | return bytes; |
| 103 | } |
| 104 | |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 105 | template<class Msg> |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 106 | boost::shared_ptr<Msg> |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 107 | deserializeMsg (const Bytes &bytes) |
| 108 | { |
| 109 | boost::shared_ptr<Msg> retval (new Msg ()); |
Alexander Afanasyev | 428c53c | 2013-02-13 12:38:28 -0800 | [diff] [blame] | 110 | if (!retval->ParseFromArray (head (bytes), bytes.size ())) |
| 111 | { |
| 112 | // to indicate an error |
| 113 | return boost::shared_ptr<Msg> (); |
| 114 | } |
Alexander Afanasyev | a35756b | 2013-01-22 16:59:11 -0800 | [diff] [blame] | 115 | return retval; |
| 116 | } |
| 117 | |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 118 | template<class Msg> |
Alexander Afanasyev | 923f2f1 | 2013-02-23 16:36:31 -0800 | [diff] [blame] | 119 | boost::shared_ptr<Msg> |
| 120 | deserializeMsg (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 | |
| 132 | template<class Msg> |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 133 | BytesPtr |
| 134 | serializeGZipMsg(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 | |
| 149 | template<class Msg> |
| 150 | boost::shared_ptr<Msg> |
| 151 | deserializeGZipMsg(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 Afanasyev | 428c53c | 2013-02-13 12:38:28 -0800 | [diff] [blame] | 160 | if (!retval->ParseFromIstream(&in)) |
| 161 | { |
| 162 | // to indicate an error |
| 163 | return boost::shared_ptr<Msg> (); |
| 164 | } |
Zhenkai Zhu | 7c1cd8d | 2013-01-31 12:27:17 -0800 | [diff] [blame] | 165 | |
| 166 | return retval; |
| 167 | } |
| 168 | |
| 169 | |
Zhenkai Zhu | 9bad2bf | 2012-12-28 15:31:46 -0800 | [diff] [blame] | 170 | // --- Bytes operations end --- |
| 171 | |
Zhenkai Zhu | f47109b | 2013-01-02 19:41:34 -0800 | [diff] [blame] | 172 | // Exceptions |
| 173 | typedef boost::error_info<struct tag_errmsg, std::string> error_info_str; |
Zhenkai Zhu | 9bad2bf | 2012-12-28 15:31:46 -0800 | [diff] [blame] | 174 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 175 | } // Ccnx |
| 176 | #endif // CCNX_COMMON_H |