blob: 037d2d2033e3cfa9dccdcddf3ca8d9661efe8133 [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>
19 * 卞超轶 Chaoyi Bian <bcy@pku.edu.cn>
20 * 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 Zhu46b26a12012-03-06 13:51:16 -080035#include <boost/thread/recursive_mutex.hpp>
36#include <boost/thread/thread.hpp>
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080037#include <boost/function.hpp>
Zhenkai Zhu774aa182012-03-05 19:54:38 -080038#include <string>
Chaoyi Bian93d43102012-03-07 14:28:56 -080039
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080040/**
41 * \defgroup sync SYNC protocol
42 *
43 * Implementation of SYNC protocol
44 */
45namespace Sync {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080046
47/**
48 * \ingroup sync
49 * @brief A wrapper for ccnx library; clients of this code do not need to deal
50 * with ccnx library
51 */
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080052class CcnxWrapper {
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080053private:
Zhenkai Zhu406f37a2012-03-05 20:23:20 -080054 ccn* m_handle;
55 ccn_keystore *m_keyStore;
56 ccn_charbuf *m_keyLoactor;
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080057 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
58 boost::recursive_mutex m_mutex;
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080059 boost::thread m_thread;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080060
61private:
62 void createKeyLocator();
63 void initKeyStore();
64 const ccn_pkey *getPrivateKey();
65 const unsigned char *getPublicKeyDigest();
66 ssize_t getPublicKeyDigestLength();
67 void ccnLoop();
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080068 bool running;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080069
70public:
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080071
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080072 /**
73 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080074 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080075 *
76 */
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080077 CcnxWrapper();
78 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080079
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080080 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080081 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080082 *
83 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080084 * @param dataCallback the callback function to deal with the returned data
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080085 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080086 */
Chaoyi Bian93d43102012-03-07 14:28:56 -080087 int sendInterest(std::string strInterest, boost::function<void (std::string)>
88 dataCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080089
90 /**
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080091 * @brief set Interest filter (specify what interest you want to receive
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080092 *
93 * @param prefix the prefix of Interest
94 * @param interestCallback the callback function to deal with the returned data
95 * @return the return code of ccn_set_interest_filter
96 */
Zhenkai Zhub877e3f2012-03-06 14:02:44 -080097 int setInterestFilter(std::string prefix, boost::function<void (std::string)>
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080098 interestCallback);
99
100 /**
101 * @brief publish data and put it to local ccn content store; need to grab
102 * lock m_mutex first
103 *
104 * @param name the name for the data object
105 * @param dataBuffer the data to be published
106 * @param freshness the freshness time for the data object
107 * @return code generated by ccnx library calls, >0 if success
108 */
Chaoyi Bian93d43102012-03-07 14:28:56 -0800109 int publishData(std::string name, std::string dataBuffer, int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800110
111};
112
113} // Sync
114
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800115#endif // SYNC_CCNX_WRAPPER_H