blob: e45f067d735592255e9cd36481b8833bcc0f4165 [file] [log] [blame]
Jeff Thompsonfa306642013-06-17 15:06:57 -07001/* -*- 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
20namespace ndn
21{
22template<class T>
23struct Ptr : public boost::shared_ptr<T>
24{
25 Ptr () { }
26 Ptr (boost::shared_ptr<T> ptr) : boost::shared_ptr<T>(ptr) { }
27 Ptr (T *ptr) : boost::shared_ptr<T>(ptr) { }
28
29 template<class Y>
30 Ptr & operator = (boost::shared_ptr<Y> ptr)
31 {
32 boost::static_pointer_cast<T> (ptr).swap (*this);
33 // *this = boost::static_pointer_cast<T> (ptr);
34 return *this;
35 }
36
37 operator Ptr<const T> () const { return *this; }
38
39 static Ptr
40 Create () { return boost::make_shared<T> (); }
41};
42
43typedef boost::posix_time::ptime Time;
44typedef boost::posix_time::time_duration TimeInterval;
45
46namespace time
47{
48inline TimeInterval Seconds (int secs) { return boost::posix_time::seconds (secs); }
49inline TimeInterval Milliseconds (int msecs) { return boost::posix_time::milliseconds (msecs); }
50inline TimeInterval Microseconds (int musecs) { return boost::posix_time::microseconds (musecs); }
51
52inline TimeInterval Seconds (double fractionalSeconds)
53{
54 double seconds, microseconds;
55 seconds = std::modf (fractionalSeconds, &microseconds);
56 microseconds *= 1000000;
57
58 return time::Seconds((int)seconds) + time::Microseconds((int)microseconds);
59}
60
61inline Time Now () { return boost::posix_time::microsec_clock::universal_time (); }
62
63const Time UNIX_EPOCH_TIME = Time (boost::gregorian::date (1970, boost::gregorian::Jan, 1));
64inline TimeInterval NowUnixTimestamp ()
65{
66 return TimeInterval (time::Now () - UNIX_EPOCH_TIME);
67}
68} // time
69} // ndn
70
71
72extern "C" {
73#include <ccn/ccn.h>
74#include <ccn/charbuf.h>
75#include <ccn/keystore.h>
76#include <ccn/uri.h>
77#include <ccn/bloom.h>
78#include <ccn/signing.h>
79}
80#include <vector>
81#include <boost/shared_ptr.hpp>
82#include <boost/exception/all.hpp>
83#include <boost/function.hpp>
84#include <string>
85#include <sstream>
86#include <map>
87#include <utility>
88#include <string.h>
89#include <boost/iostreams/filter/gzip.hpp>
90#include <boost/iostreams/filtering_stream.hpp>
91#include <boost/iostreams/device/back_inserter.hpp>
92#include <boost/range/iterator_range.hpp>
93#include <boost/make_shared.hpp>
94
95namespace ndn {
96typedef std::vector<unsigned char> Bytes;
97typedef std::vector<std::string>Comps;
98
99typedef boost::shared_ptr<Bytes> BytesPtr;
100
101inline
102const unsigned char *
103head(const Bytes &bytes)
104{
105 return &bytes[0];
106}
107
108inline
109unsigned char *
110head (Bytes &bytes)
111{
112 return &bytes[0];
113}
114
115// --- Bytes operations start ---
116inline void
117readRaw(Bytes &bytes, const unsigned char *src, size_t len)
118{
119 if (len > 0)
120 {
121 bytes.resize(len);
122 memcpy (head (bytes), src, len);
123 }
124}
125
126inline BytesPtr
127readRawPtr (const unsigned char *src, size_t len)
128{
129 if (len > 0)
130 {
131 BytesPtr ret (new Bytes (len));
132 memcpy (head (*ret), src, len);
133
134 return ret;
135 }
136 else
137 return BytesPtr ();
138}
139
140template<class Msg>
141BytesPtr
142serializeMsg(const Msg &msg)
143{
144 int size = msg.ByteSize ();
145 BytesPtr bytes (new Bytes (size));
146 msg.SerializeToArray (head(*bytes), size);
147 return bytes;
148}
149
150template<class Msg>
151boost::shared_ptr<Msg>
152deserializeMsg (const Bytes &bytes)
153{
154 boost::shared_ptr<Msg> retval (new Msg ());
155 if (!retval->ParseFromArray (head (bytes), bytes.size ()))
156 {
157 // to indicate an error
158 return boost::shared_ptr<Msg> ();
159 }
160 return retval;
161}
162
163template<class Msg>
164boost::shared_ptr<Msg>
165deserializeMsg (const void *buf, size_t length)
166{
167 boost::shared_ptr<Msg> retval (new Msg ());
168 if (!retval->ParseFromArray (buf, length))
169 {
170 // to indicate an error
171 return boost::shared_ptr<Msg> ();
172 }
173 return retval;
174}
175
176
177template<class Msg>
178BytesPtr
179serializeGZipMsg(const Msg &msg)
180{
181 std::vector<char> bytes; // Bytes couldn't work
182 {
183 boost::iostreams::filtering_ostream out;
184 out.push(boost::iostreams::gzip_compressor()); // gzip filter
185 out.push(boost::iostreams::back_inserter(bytes)); // back_inserter sink
186
187 msg.SerializeToOstream(&out);
188 }
189 BytesPtr uBytes = boost::make_shared<Bytes>(bytes.size());
190 memcpy(&(*uBytes)[0], &bytes[0], bytes.size());
191 return uBytes;
192}
193
194template<class Msg>
195boost::shared_ptr<Msg>
196deserializeGZipMsg(const Bytes &bytes)
197{
198 std::vector<char> sBytes(bytes.size());
199 memcpy(&sBytes[0], &bytes[0], bytes.size());
200 boost::iostreams::filtering_istream in;
201 in.push(boost::iostreams::gzip_decompressor()); // gzip filter
202 in.push(boost::make_iterator_range(sBytes)); // source
203
204 boost::shared_ptr<Msg> retval = boost::make_shared<Msg>();
205 if (!retval->ParseFromIstream(&in))
206 {
207 // to indicate an error
208 return boost::shared_ptr<Msg> ();
209 }
210
211 return retval;
212}
213
214
215// --- Bytes operations end ---
216
217// Exceptions
218typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
219
220} // ndn
221#endif // NDN_COMMON_H