blob: 0ccbec464b1882315782ee66cd4ada05654aed6a [file] [log] [blame]
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -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>
Chaoyi Bian3e1eb162012-04-03 16:59:32 -070019 * Chaoyi Bian <bcy@pku.edu.cn>
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080020 * Alexander Afanasyev <alexander.afanasyev@ucla.edu>
21 */
22
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -080023#ifndef SYNC_CCNX_WRAPPER_H
24#define SYNC_CCNX_WRAPPER_H
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080025
26extern "C" {
27#include <ccn/ccn.h>
28#include <ccn/charbuf.h>
29#include <ccn/keystore.h>
30#include <ccn/uri.h>
31#include <ccn/bloom.h>
Chaoyi Bian49d46752012-03-07 10:35:21 -080032#include <ccn/signing.h>
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080033}
34
Zhenkai Zhu009ff792012-03-09 12:37:52 -080035#include <boost/exception/all.hpp>
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080036#include <boost/thread/recursive_mutex.hpp>
37#include <boost/thread/thread.hpp>
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080038#include <boost/function.hpp>
Zhenkai Zhu774aa182012-03-05 19:54:38 -080039#include <string>
Chaoyi Bian93d43102012-03-07 14:28:56 -080040
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080041/**
42 * \defgroup sync SYNC protocol
43 *
44 * Implementation of SYNC protocol
45 */
46namespace Sync {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080047
Zhenkai Zhu009ff792012-03-09 12:37:52 -080048struct CcnxOperationException : virtual boost::exception, virtual std::exception { };
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080049/**
50 * \ingroup sync
51 * @brief A wrapper for ccnx library; clients of this code do not need to deal
52 * with ccnx library
53 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080054class CcnxWrapper {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080055public:
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080056 typedef boost::function<void (std::string, std::string)> DataCallback;
57 typedef boost::function<void (std::string)> InterestCallback;
58
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080059 /**
60 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080061 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080062 *
63 */
Alexander Afanasyev2247b302012-03-14 14:11:54 -070064#ifdef _DEBUG_WRAPPER_
65 CcnxWrapper(char c='.');
66 char m_c;
67#else
Alexander Afanasyev1285b382012-03-08 16:40:27 -080068 CcnxWrapper();
Alexander Afanasyev2247b302012-03-14 14:11:54 -070069#endif
Alexander Afanasyev1285b382012-03-08 16:40:27 -080070 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080071
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080072 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080073 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080074 *
75 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080076 * @param dataCallback the callback function to deal with the returned data
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080077 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080078 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080079 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080080 sendInterest (const std::string &strInterest, const DataCallback &dataCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080081
82 /**
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070083 * @brief set Interest filter (specify what interest you want to receive)
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080084 *
85 * @param prefix the prefix of Interest
86 * @param interestCallback the callback function to deal with the returned data
87 * @return the return code of ccn_set_interest_filter
88 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080089 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -080090 setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080091
92 /**
Alexander Afanasyev1b449c42012-03-13 20:24:07 -070093 * @brief clear Interest filter
94 * @param prefix the prefix of Interest
95 */
96 void
97 clearInterestFilter (const std::string &prefix);
98
99 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -0800100 * @brief publish data and put it to local ccn content store; need to grab
101 * lock m_mutex first
102 *
103 * @param name the name for the data object
104 * @param dataBuffer the data to be published
105 * @param freshness the freshness time for the data object
106 * @return code generated by ccnx library calls, >0 if success
107 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800108 int
Alexander Afanasyev172d2b72012-03-08 23:43:39 -0800109 publishData (const std::string &name, const std::string &dataBuffer, int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800110
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800111private:
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800112 /// @cond include_hidden
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800113 void
114 createKeyLocator ();
115
116 void
117 initKeyStore ();
118
119 const ccn_pkey *
120 getPrivateKey ();
121
122 const unsigned char *
123 getPublicKeyDigest ();
124
125 ssize_t
126 getPublicKeyDigestLength ();
127
128 void
129 ccnLoop ();
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800130 /// @endcond
Alexander Afanasyev1285b382012-03-08 16:40:27 -0800131private:
132 ccn* m_handle;
133 ccn_keystore *m_keyStore;
134 ccn_charbuf *m_keyLoactor;
135 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
136 boost::recursive_mutex m_mutex;
137 boost::thread m_thread;
138 bool m_running;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800139};
140
Alexander Afanasyevc1030192012-03-08 22:21:28 -0800141typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr;
142
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800143} // Sync
144
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800145#endif // SYNC_CCNX_WRAPPER_H