Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013, Regents of the University of California |
| 4 | * Alexander Afanasyev |
| 5 | * Zhenkai Zhu |
| 6 | * |
| 7 | * BSD license, See the LICENSE file for more information |
| 8 | * |
| 9 | * Author: Zhenkai Zhu <zhenkai@cs.ucla.edu> |
| 10 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 11 | */ |
| 12 | |
| 13 | #ifndef NDN_COMMON_H |
| 14 | #define NDN_COMMON_H |
| 15 | |
| 16 | #include <boost/shared_ptr.hpp> |
| 17 | #include <boost/make_shared.hpp> |
| 18 | #include <boost/date_time/posix_time/posix_time_types.hpp> |
| 19 | |
| 20 | namespace ndn |
| 21 | { |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 22 | typedef boost::posix_time::ptime Time; |
| 23 | typedef boost::posix_time::time_duration TimeInterval; |
| 24 | |
| 25 | namespace time |
| 26 | { |
| 27 | inline TimeInterval Seconds (int secs) { return boost::posix_time::seconds (secs); } |
| 28 | inline TimeInterval Milliseconds (int msecs) { return boost::posix_time::milliseconds (msecs); } |
| 29 | inline TimeInterval Microseconds (int musecs) { return boost::posix_time::microseconds (musecs); } |
| 30 | |
| 31 | inline TimeInterval Seconds (double fractionalSeconds) |
| 32 | { |
| 33 | double seconds, microseconds; |
| 34 | seconds = std::modf (fractionalSeconds, µseconds); |
| 35 | microseconds *= 1000000; |
| 36 | |
| 37 | return time::Seconds((int)seconds) + time::Microseconds((int)microseconds); |
| 38 | } |
| 39 | |
| 40 | inline Time Now () { return boost::posix_time::microsec_clock::universal_time (); } |
| 41 | |
| 42 | const Time UNIX_EPOCH_TIME = Time (boost::gregorian::date (1970, boost::gregorian::Jan, 1)); |
| 43 | inline TimeInterval NowUnixTimestamp () |
| 44 | { |
| 45 | return TimeInterval (time::Now () - UNIX_EPOCH_TIME); |
| 46 | } |
| 47 | } // time |
| 48 | } // ndn |
| 49 | |
| 50 | |
| 51 | extern "C" { |
| 52 | #include <ccn/ccn.h> |
| 53 | #include <ccn/charbuf.h> |
| 54 | #include <ccn/keystore.h> |
| 55 | #include <ccn/uri.h> |
| 56 | #include <ccn/bloom.h> |
| 57 | #include <ccn/signing.h> |
| 58 | } |
| 59 | #include <vector> |
| 60 | #include <boost/shared_ptr.hpp> |
| 61 | #include <boost/exception/all.hpp> |
| 62 | #include <boost/function.hpp> |
| 63 | #include <string> |
| 64 | #include <sstream> |
| 65 | #include <map> |
| 66 | #include <utility> |
| 67 | #include <string.h> |
| 68 | #include <boost/iostreams/filter/gzip.hpp> |
| 69 | #include <boost/iostreams/filtering_stream.hpp> |
| 70 | #include <boost/iostreams/device/back_inserter.hpp> |
| 71 | #include <boost/range/iterator_range.hpp> |
| 72 | #include <boost/make_shared.hpp> |
| 73 | |
| 74 | namespace ndn { |
| 75 | typedef std::vector<unsigned char> Bytes; |
| 76 | typedef std::vector<std::string>Comps; |
| 77 | |
| 78 | typedef boost::shared_ptr<Bytes> BytesPtr; |
Jeff Thompson | 4454bf7 | 2013-06-18 13:33:12 -0700 | [diff] [blame^] | 79 | typedef boost::shared_ptr<const Bytes> ConstBytesPtr; |
Jeff Thompson | fa30664 | 2013-06-17 15:06:57 -0700 | [diff] [blame] | 80 | |
| 81 | inline |
| 82 | const unsigned char * |
| 83 | head(const Bytes &bytes) |
| 84 | { |
| 85 | return &bytes[0]; |
| 86 | } |
| 87 | |
| 88 | inline |
| 89 | unsigned char * |
| 90 | head (Bytes &bytes) |
| 91 | { |
| 92 | return &bytes[0]; |
| 93 | } |
| 94 | |
| 95 | // --- Bytes operations start --- |
| 96 | inline void |
| 97 | readRaw(Bytes &bytes, const unsigned char *src, size_t len) |
| 98 | { |
| 99 | if (len > 0) |
| 100 | { |
| 101 | bytes.resize(len); |
| 102 | memcpy (head (bytes), src, len); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | inline BytesPtr |
| 107 | readRawPtr (const unsigned char *src, size_t len) |
| 108 | { |
| 109 | if (len > 0) |
| 110 | { |
| 111 | BytesPtr ret (new Bytes (len)); |
| 112 | memcpy (head (*ret), src, len); |
| 113 | |
| 114 | return ret; |
| 115 | } |
| 116 | else |
| 117 | return BytesPtr (); |
| 118 | } |
| 119 | |
| 120 | template<class Msg> |
| 121 | BytesPtr |
| 122 | serializeMsg(const Msg &msg) |
| 123 | { |
| 124 | int size = msg.ByteSize (); |
| 125 | BytesPtr bytes (new Bytes (size)); |
| 126 | msg.SerializeToArray (head(*bytes), size); |
| 127 | return bytes; |
| 128 | } |
| 129 | |
| 130 | template<class Msg> |
| 131 | boost::shared_ptr<Msg> |
| 132 | deserializeMsg (const Bytes &bytes) |
| 133 | { |
| 134 | boost::shared_ptr<Msg> retval (new Msg ()); |
| 135 | if (!retval->ParseFromArray (head (bytes), bytes.size ())) |
| 136 | { |
| 137 | // to indicate an error |
| 138 | return boost::shared_ptr<Msg> (); |
| 139 | } |
| 140 | return retval; |
| 141 | } |
| 142 | |
| 143 | template<class Msg> |
| 144 | boost::shared_ptr<Msg> |
| 145 | deserializeMsg (const void *buf, size_t length) |
| 146 | { |
| 147 | boost::shared_ptr<Msg> retval (new Msg ()); |
| 148 | if (!retval->ParseFromArray (buf, length)) |
| 149 | { |
| 150 | // to indicate an error |
| 151 | return boost::shared_ptr<Msg> (); |
| 152 | } |
| 153 | return retval; |
| 154 | } |
| 155 | |
| 156 | |
| 157 | template<class Msg> |
| 158 | BytesPtr |
| 159 | serializeGZipMsg(const Msg &msg) |
| 160 | { |
| 161 | std::vector<char> bytes; // Bytes couldn't work |
| 162 | { |
| 163 | boost::iostreams::filtering_ostream out; |
| 164 | out.push(boost::iostreams::gzip_compressor()); // gzip filter |
| 165 | out.push(boost::iostreams::back_inserter(bytes)); // back_inserter sink |
| 166 | |
| 167 | msg.SerializeToOstream(&out); |
| 168 | } |
| 169 | BytesPtr uBytes = boost::make_shared<Bytes>(bytes.size()); |
| 170 | memcpy(&(*uBytes)[0], &bytes[0], bytes.size()); |
| 171 | return uBytes; |
| 172 | } |
| 173 | |
| 174 | template<class Msg> |
| 175 | boost::shared_ptr<Msg> |
| 176 | deserializeGZipMsg(const Bytes &bytes) |
| 177 | { |
| 178 | std::vector<char> sBytes(bytes.size()); |
| 179 | memcpy(&sBytes[0], &bytes[0], bytes.size()); |
| 180 | boost::iostreams::filtering_istream in; |
| 181 | in.push(boost::iostreams::gzip_decompressor()); // gzip filter |
| 182 | in.push(boost::make_iterator_range(sBytes)); // source |
| 183 | |
| 184 | boost::shared_ptr<Msg> retval = boost::make_shared<Msg>(); |
| 185 | if (!retval->ParseFromIstream(&in)) |
| 186 | { |
| 187 | // to indicate an error |
| 188 | return boost::shared_ptr<Msg> (); |
| 189 | } |
| 190 | |
| 191 | return retval; |
| 192 | } |
| 193 | |
| 194 | |
| 195 | // --- Bytes operations end --- |
| 196 | |
| 197 | // Exceptions |
| 198 | typedef boost::error_info<struct tag_errmsg, std::string> error_info_str; |
| 199 | |
| 200 | } // ndn |
| 201 | #endif // NDN_COMMON_H |