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> |
Chaoyi Bian | 3e1eb16 | 2012-04-03 16:59:32 -0700 | [diff] [blame] | 19 | * Chaoyi Bian <bcy@pku.edu.cn> |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 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 | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 35 | #include <boost/exception/all.hpp> |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 36 | #include <boost/thread/recursive_mutex.hpp> |
| 37 | #include <boost/thread/thread.hpp> |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 38 | #include <boost/function.hpp> |
Zhenkai Zhu | 774aa18 | 2012-03-05 19:54:38 -0800 | [diff] [blame] | 39 | #include <string> |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 40 | #include <sstream> |
Zhenkai Zhu | 71bc0ba | 2012-10-05 14:37:44 -0700 | [diff] [blame] | 41 | #include <map> |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 42 | |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 43 | /** |
| 44 | * \defgroup sync SYNC protocol |
| 45 | * |
| 46 | * Implementation of SYNC protocol |
| 47 | */ |
| 48 | namespace Sync { |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 49 | |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 50 | struct CcnxOperationException : virtual boost::exception, virtual std::exception { }; |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 51 | /** |
| 52 | * \ingroup sync |
| 53 | * @brief A wrapper for ccnx library; clients of this code do not need to deal |
| 54 | * with ccnx library |
| 55 | */ |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 56 | class CcnxWrapper { |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 57 | public: |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 58 | typedef boost::function<void (std::string, std::string)> StringDataCallback; |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 59 | typedef boost::function<void (std::string, const char *buf, size_t len)> RawDataCallback; |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 60 | typedef boost::function<void (std::string)> InterestCallback; |
Alexander Afanasyev | 3aaea67 | 2012-10-05 15:25:11 -0700 | [diff] [blame] | 61 | |
Alexander Afanasyev | 60126e0 | 2012-10-05 16:54:42 -0700 | [diff] [blame] | 62 | public: |
Alexander Afanasyev | 60126e0 | 2012-10-05 16:54:42 -0700 | [diff] [blame] | 63 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 64 | #ifdef _DEBUG_WRAPPER_ |
| 65 | CcnxWrapper(char c='.'); |
| 66 | char m_c; |
| 67 | #else |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 68 | CcnxWrapper(); |
Alexander Afanasyev | 3aaea67 | 2012-10-05 15:25:11 -0700 | [diff] [blame] | 69 | #endif |
Alexander Afanasyev | 19d55ec | 2012-10-05 16:28:31 -0700 | [diff] [blame] | 70 | |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 71 | ~CcnxWrapper(); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 72 | |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 73 | /** |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 74 | * @brief send Interest; need to grab lock m_mutex first |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 75 | * |
| 76 | * @param strInterest the Interest name |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 77 | * @param dataCallback the callback function to deal with the returned data |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 78 | * @return the return code of ccn_express_interest |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 79 | */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 80 | int |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 81 | sendInterestForString (const std::string &strInterest, const StringDataCallback &strDataCallback, int retry = 0); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 82 | |
| 83 | int |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 84 | sendInterest (const std::string &strInterest, const RawDataCallback &rawDataCallback, int retry = 0); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 85 | |
| 86 | /** |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 87 | * @brief set Interest filter (specify what interest you want to receive) |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 88 | * |
| 89 | * @param prefix the prefix of Interest |
| 90 | * @param interestCallback the callback function to deal with the returned data |
| 91 | * @return the return code of ccn_set_interest_filter |
| 92 | */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 93 | int |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 94 | setInterestFilter (const std::string &prefix, const InterestCallback &interestCallback); |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 95 | |
| 96 | /** |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 97 | * @brief clear Interest filter |
| 98 | * @param prefix the prefix of Interest |
| 99 | */ |
| 100 | void |
| 101 | clearInterestFilter (const std::string &prefix); |
| 102 | |
| 103 | /** |
Zhenkai Zhu | 46b26a1 | 2012-03-06 13:51:16 -0800 | [diff] [blame] | 104 | * @brief publish data and put it to local ccn content store; need to grab |
| 105 | * lock m_mutex first |
| 106 | * |
| 107 | * @param name the name for the data object |
| 108 | * @param dataBuffer the data to be published |
| 109 | * @param freshness the freshness time for the data object |
| 110 | * @return code generated by ccnx library calls, >0 if success |
| 111 | */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 112 | int |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 113 | publishStringData (const std::string &name, const std::string &dataBuffer, int freshness); |
| 114 | |
| 115 | int |
| 116 | publishRawData (const std::string &name, const char *buf, size_t len, int freshness); |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 117 | |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 118 | std::string |
| 119 | getLocalPrefix (); |
| 120 | |
Zhenkai Zhu | 96974e0 | 2012-10-17 13:51:31 -0700 | [diff] [blame^] | 121 | protected: |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 122 | void |
| 123 | connectCcnd(); |
| 124 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 125 | /// @cond include_hidden |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 126 | void |
| 127 | createKeyLocator (); |
| 128 | |
| 129 | void |
| 130 | initKeyStore (); |
| 131 | |
| 132 | const ccn_pkey * |
| 133 | getPrivateKey (); |
| 134 | |
| 135 | const unsigned char * |
| 136 | getPublicKeyDigest (); |
| 137 | |
| 138 | ssize_t |
| 139 | getPublicKeyDigestLength (); |
| 140 | |
| 141 | void |
| 142 | ccnLoop (); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 143 | |
| 144 | int |
| 145 | sendInterest (const std::string &strInterest, void *dataPass); |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 146 | /// @endcond |
Zhenkai Zhu | 96974e0 | 2012-10-17 13:51:31 -0700 | [diff] [blame^] | 147 | protected: |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 148 | ccn* m_handle; |
| 149 | ccn_keystore *m_keyStore; |
| 150 | ccn_charbuf *m_keyLoactor; |
| 151 | // to lock, use "boost::recursive_mutex::scoped_lock scoped_lock(mutex); |
| 152 | boost::recursive_mutex m_mutex; |
| 153 | boost::thread m_thread; |
| 154 | bool m_running; |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 155 | bool m_connected; |
Zhenkai Zhu | 71bc0ba | 2012-10-05 14:37:44 -0700 | [diff] [blame] | 156 | std::map<std::string, InterestCallback> m_registeredInterests; |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 157 | // std::list< std::pair<std::string, InterestCallback> > m_registeredInterests; |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 158 | }; |
| 159 | |
Alexander Afanasyev | c103019 | 2012-03-08 22:21:28 -0800 | [diff] [blame] | 160 | typedef boost::shared_ptr<CcnxWrapper> CcnxWrapperPtr; |
| 161 | |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 162 | enum CallbackType { STRING_FORM, RAW_DATA}; |
| 163 | |
| 164 | class ClosurePass { |
| 165 | public: |
Chaoyi Bian | 3493889 | 2012-06-13 16:47:43 -0700 | [diff] [blame] | 166 | ClosurePass(CallbackType type, int retry): m_retry(retry), m_type(type) {} |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 167 | int getRetry() {return m_retry;} |
| 168 | void decRetry() { m_retry--;} |
| 169 | CallbackType getCallbackType() {return m_type;} |
| 170 | virtual ~ClosurePass(){} |
| 171 | virtual void runCallback(std::string name, const char *data, size_t len) = 0; |
| 172 | |
| 173 | protected: |
| 174 | int m_retry; |
| 175 | CallbackType m_type; |
| 176 | }; |
| 177 | |
| 178 | class DataClosurePass: public ClosurePass { |
| 179 | public: |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 180 | DataClosurePass(CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 181 | virtual ~DataClosurePass(); |
| 182 | virtual void runCallback(std::string name, const char *, size_t len); |
| 183 | private: |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 184 | CcnxWrapper::StringDataCallback * m_callback; |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 185 | }; |
| 186 | |
| 187 | class RawDataClosurePass: public ClosurePass { |
| 188 | public: |
| 189 | RawDataClosurePass(CallbackType type, int retry, const CcnxWrapper::RawDataCallback &RawDataCallback); |
| 190 | virtual ~RawDataClosurePass(); |
| 191 | virtual void runCallback(std::string name, const char *, size_t len); |
| 192 | private: |
| 193 | CcnxWrapper::RawDataCallback * m_callback; |
| 194 | }; |
| 195 | |
Zhenkai Zhu | 6cc2c81 | 2012-03-05 19:48:46 -0800 | [diff] [blame] | 196 | } // Sync |
| 197 | |
Zhenkai Zhu | 1ac6f80 | 2012-03-06 17:40:27 -0800 | [diff] [blame] | 198 | #endif // SYNC_CCNX_WRAPPER_H |