blob: 71ee3212fbad509f331252636145e5cf3fe2ca79 [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 -080053public:
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080054
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080055 /**
56 * @brief initialize the wrapper; a lot of things needs to be done. 1) init
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080057 * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080058 *
59 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080060 CcnxWrapper();
61 ~CcnxWrapper();
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080062
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080063 /**
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080064 * @brief send Interest; need to grab lock m_mutex first
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080065 *
66 * @param strInterest the Interest name
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080067 * @param dataCallback the callback function to deal with the returned data
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080068 * @return the return code of ccn_express_interest
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080069 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080070 int
71 sendInterest(std::string strInterest, boost::function<void (std::string)>
72 dataCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080073
74 /**
Chaoyi Bian3e6e5142012-03-06 22:32:19 -080075 * @brief set Interest filter (specify what interest you want to receive
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080076 *
77 * @param prefix the prefix of Interest
78 * @param interestCallback the callback function to deal with the returned data
79 * @return the return code of ccn_set_interest_filter
80 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080081 int
82 setInterestFilter(std::string prefix, boost::function<void (std::string)>
83 interestCallback);
Zhenkai Zhu46b26a12012-03-06 13:51:16 -080084
85 /**
86 * @brief publish data and put it to local ccn content store; need to grab
87 * lock m_mutex first
88 *
89 * @param name the name for the data object
90 * @param dataBuffer the data to be published
91 * @param freshness the freshness time for the data object
92 * @return code generated by ccnx library calls, >0 if success
93 */
Alexander Afanasyev1285b382012-03-08 16:40:27 -080094 int
95 publishData(std::string name, std::string dataBuffer, int freshness);
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -080096
Alexander Afanasyev1285b382012-03-08 16:40:27 -080097private:
98 void
99 createKeyLocator ();
100
101 void
102 initKeyStore ();
103
104 const ccn_pkey *
105 getPrivateKey ();
106
107 const unsigned char *
108 getPublicKeyDigest ();
109
110 ssize_t
111 getPublicKeyDigestLength ();
112
113 void
114 ccnLoop ();
115
116private:
117 ccn* m_handle;
118 ccn_keystore *m_keyStore;
119 ccn_charbuf *m_keyLoactor;
120 // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex);
121 boost::recursive_mutex m_mutex;
122 boost::thread m_thread;
123 bool m_running;
Zhenkai Zhu6cc2c812012-03-05 19:48:46 -0800124};
125
126} // Sync
127
Zhenkai Zhu1ac6f802012-03-06 17:40:27 -0800128#endif // SYNC_CCNX_WRAPPER_H