blob: 6d17116e0514516a6f438d3ed3447cf7bd4d5fbd [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 Zhu974c5a62012-12-28 14:15:30 -080042
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080043namespace Ccnx {
Alexander Afanasyev053e5ac2013-01-22 20:59:13 -080044typedef std::vector<unsigned char> Bytes;
45typedef std::vector<std::string>Comps;
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080046
Alexander Afanasyev68f2a952013-01-08 14:34:16 -080047typedef boost::shared_ptr<Bytes> BytesPtr;
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080048
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080049inline
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080050const unsigned char *
Alexander Afanasyevdbc06712013-01-08 18:30:28 -080051head(const Bytes &bytes)
52{
53 return &bytes[0];
54}
55
56inline
57unsigned char *
58head (Bytes &bytes)
59{
60 return &bytes[0];
61}
62
63// --- Bytes operations start ---
64inline void
65readRaw(Bytes &bytes, const unsigned char *src, size_t len)
66{
67 if (len > 0)
68 {
69 bytes.resize(len);
70 memcpy (head (bytes), src, len);
71 }
72}
73
74inline BytesPtr
75readRawPtr (const unsigned char *src, size_t len)
76{
77 if (len > 0)
78 {
79 BytesPtr ret (new Bytes (len));
80 memcpy (head (*ret), src, len);
81
82 return ret;
83 }
84 else
85 return BytesPtr ();
86}
Zhenkai Zhu974c5a62012-12-28 14:15:30 -080087
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080088template<class Msg>
89inline BytesPtr
90serializeMsg(const Msg &msg)
91{
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -080092 int size = msg.ByteSize ();
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080093 BytesPtr bytes (new Bytes (size));
Alexander Afanasyev08aa70a2013-01-22 22:16:25 -080094 msg.SerializeToArray (head(*bytes), size);
Alexander Afanasyev49a30d02013-01-21 21:38:48 -080095 return bytes;
96}
97
Alexander Afanasyeva35756b2013-01-22 16:59:11 -080098template<class Msg>
99inline boost::shared_ptr<Msg>
100deserializeMsg (const Bytes &bytes)
101{
102 boost::shared_ptr<Msg> retval (new Msg ());
103 retval->ParseFromArray (head (bytes), bytes.size ());
104 return retval;
105}
106
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -0800107// --- Bytes operations end ---
108
Zhenkai Zhuf47109b2013-01-02 19:41:34 -0800109// Exceptions
110typedef boost::error_info<struct tag_errmsg, std::string> error_info_str;
Zhenkai Zhu9bad2bf2012-12-28 15:31:46 -0800111
Zhenkai Zhu974c5a62012-12-28 14:15:30 -0800112} // Ccnx
113#endif // CCNX_COMMON_H