Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 1 | /* -*- 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 Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 23 | #ifndef SYNC_CCNX_WRAPPER_H |
| 24 | #define SYNC_CCNX_WRAPPER_H |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 25 | |
| 26 | extern "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 Bian | 49d4675 | 2012-03-07 10:35:21 -0800 | [diff] [blame] | 32 | #include <ccn/signing.h> |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 33 | } |
| 34 | |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 35 | #include <boost/thread/recursive_mutex.hpp> |
| 36 | #include <boost/thread/thread.hpp> |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 37 | #include <boost/function.hpp> |
Zhenkai Zhu | 774aa18 | 2012-03-05 19:54:38 -0800 | [diff] [blame] | 38 | #include <string> |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 39 | |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 40 | /** |
| 41 | * \defgroup sync SYNC protocol |
| 42 | * |
| 43 | * Implementation of SYNC protocol |
| 44 | */ |
| 45 | namespace Sync { |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 46 | |
| 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 Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 52 | class CcnxWrapper { |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 53 | public: |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 54 | typedef boost::function<void (std::string, std::string)> DataCallback; |
| 55 | typedef boost::function<void (std::string)> InterestCallback; |
| 56 | |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 57 | /** |
| 58 | * @brief initialize the wrapper; a lot of things needs to be done. 1) init |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 59 | * keystore 2) init keylocator 3) start a thread to hold a loop of ccn_run |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 60 | * |
| 61 | */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 62 | CcnxWrapper(); |
| 63 | ~CcnxWrapper(); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 64 | |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 65 | /** |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 66 | * @brief send Interest; need to grab lock m_mutex first |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 67 | * |
| 68 | * @param strInterest the Interest name |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 69 | * @param dataCallback the callback function to deal with the returned data |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 70 | * @return the return code of ccn_express_interest |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 71 | */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 72 | int |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 73 | sendInterest (const std::string &strInterest, const DataCallback &dataCallback); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 74 | |
| 75 | /** |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 76 | * @brief set Interest filter (specify what interest you want to receive |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 77 | * |
| 78 | * @param prefix the prefix of Interest |
| 79 | * @param interestCallback the callback function to deal with the returned data |
| 80 | * @return the return code of ccn_set_interest_filter |
| 81 | */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 82 | int |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 83 | setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 84 | |
| 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 Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 94 | int |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame^] | 95 | publishData (const std::string &name, const std::string &dataBuffer, int freshness); |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 96 | |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 97 | private: |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 98 | /// @cond include_hidden |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 99 | void |
| 100 | createKeyLocator (); |
| 101 | |
| 102 | void |
| 103 | initKeyStore (); |
| 104 | |
| 105 | const ccn_pkey * |
| 106 | getPrivateKey (); |
| 107 | |
| 108 | const unsigned char * |
| 109 | getPublicKeyDigest (); |
| 110 | |
| 111 | ssize_t |
| 112 | getPublicKeyDigestLength (); |
| 113 | |
| 114 | void |
| 115 | ccnLoop (); |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 116 | /// @endcond |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 117 | private: |
| 118 | ccn* m_handle; |
| 119 | ccn_keystore *m_keyStore; |
| 120 | ccn_charbuf *m_keyLoactor; |
| 121 | // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex); |
| 122 | boost::recursive_mutex m_mutex; |
| 123 | boost::thread m_thread; |
| 124 | bool m_running; |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 125 | }; |
| 126 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 127 | typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr; |
| 128 | |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 129 | } // Sync |
| 130 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 131 | #endif // SYNC_CCNX_WRAPPER_H |