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 | */ |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 22 | |
| 23 | #include "sync-ccnx-wrapper.h" |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 24 | |
| 25 | extern "C" { |
| 26 | #include <ccn/fetch.h> |
| 27 | } |
| 28 | |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 29 | #include "sync-logging.h" |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 30 | #include <poll.h> |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 31 | #include <boost/throw_exception.hpp> |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 32 | #include <boost/date_time/posix_time/posix_time.hpp> |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 33 | #include <boost/random.hpp> |
| 34 | |
| 35 | #include "sync-scheduler.h" |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 36 | |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 37 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 38 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 39 | |
Alexander Afanasyev | 60126e0 | 2012-10-05 16:54:42 -0700 | [diff] [blame] | 40 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 41 | using namespace std; |
| 42 | using namespace boost; |
| 43 | |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 44 | INIT_LOGGER ("CcnxWrapper"); |
| 45 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 46 | namespace Sync { |
| 47 | |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 48 | #ifdef _DEBUG_WRAPPER_ |
| 49 | CcnxWrapper::CcnxWrapper(char c) |
| 50 | #else |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 51 | CcnxWrapper::CcnxWrapper() |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 52 | #endif |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 53 | : m_handle (0) |
| 54 | , m_keyStore (0) |
| 55 | , m_keyLoactor (0) |
| 56 | , m_running (true) |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 57 | , m_connected (false) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 58 | { |
Alexander Afanasyev | 2247b30 | 2012-03-14 14:11:54 -0700 | [diff] [blame] | 59 | #ifdef _DEBUG_WRAPPER_ |
| 60 | m_c = c; |
| 61 | #endif |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 62 | connectCcnd(); |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 63 | initKeyStore (); |
| 64 | createKeyLocator (); |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 65 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 66 | } |
| 67 | |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 68 | void |
| 69 | CcnxWrapper::connectCcnd() |
| 70 | { |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 71 | recursive_mutex::scoped_lock lock (m_mutex); |
| 72 | |
Alexander Afanasyev | 3aaea67 | 2012-10-05 15:25:11 -0700 | [diff] [blame] | 73 | if (m_handle != 0) { |
| 74 | ccn_disconnect (m_handle); |
| 75 | ccn_destroy (&m_handle); |
| 76 | } |
| 77 | |
| 78 | m_handle = ccn_create (); |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 79 | _LOG_DEBUG("<<< connecting to ccnd"); |
| 80 | if (ccn_connect(m_handle, NULL) < 0) |
| 81 | { |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 82 | _LOG_DEBUG("<<< connecting to ccnd failed"); |
| 83 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed")); |
| 84 | } |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 85 | m_connected = true; |
| 86 | |
Zhenkai Zhu | 71bc0ba | 2012-10-05 14:37:44 -0700 | [diff] [blame] | 87 | if (!m_registeredInterests.empty()) |
| 88 | { |
| 89 | for (map<std::string, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it) |
| 90 | { |
Alexander Afanasyev | 3aaea67 | 2012-10-05 15:25:11 -0700 | [diff] [blame] | 91 | // clearInterestFilter(it->first); |
Zhenkai Zhu | 71bc0ba | 2012-10-05 14:37:44 -0700 | [diff] [blame] | 92 | setInterestFilter(it->first, it->second); |
| 93 | _LOG_DEBUG("<<< registering interest filter for: " << it->first); |
| 94 | } |
| 95 | } |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 98 | CcnxWrapper::~CcnxWrapper() |
| 99 | { |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 100 | // std::cout << "CcnxWrapper::~CcnxWrapper()" << std::endl; |
| 101 | { |
| 102 | recursive_mutex::scoped_lock lock(m_mutex); |
| 103 | m_running = false; |
| 104 | } |
| 105 | |
| 106 | m_thread.join (); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 107 | ccn_disconnect (m_handle); |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 108 | ccn_destroy (&m_handle); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 109 | ccn_charbuf_destroy (&m_keyLoactor); |
| 110 | ccn_keystore_destroy (&m_keyStore); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 111 | } |
| 112 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 113 | /// @cond include_hidden |
| 114 | |
| 115 | void |
| 116 | CcnxWrapper::createKeyLocator () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 117 | { |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 118 | m_keyLoactor = ccn_charbuf_create(); |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 119 | ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_KeyLocator, CCN_DTAG); |
| 120 | ccn_charbuf_append_tt (m_keyLoactor, CCN_DTAG_Key, CCN_DTAG); |
| 121 | int res = ccn_append_pubkey_blob (m_keyLoactor, ccn_keystore_public_key(m_keyStore)); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 122 | if (res >= 0) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 123 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 124 | ccn_charbuf_append_closer (m_keyLoactor); /* </Key> */ |
| 125 | ccn_charbuf_append_closer (m_keyLoactor); /* </KeyLocator> */ |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 126 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 127 | } |
| 128 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 129 | const ccn_pkey* |
| 130 | CcnxWrapper::getPrivateKey () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 131 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 132 | return ccn_keystore_private_key (m_keyStore); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 135 | const unsigned char* |
| 136 | CcnxWrapper::getPublicKeyDigest () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 137 | { |
| 138 | return ccn_keystore_public_key_digest(m_keyStore); |
| 139 | } |
| 140 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 141 | ssize_t |
| 142 | CcnxWrapper::getPublicKeyDigestLength () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 143 | { |
| 144 | return ccn_keystore_public_key_digest_length(m_keyStore); |
| 145 | } |
| 146 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 147 | void |
| 148 | CcnxWrapper::initKeyStore () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 149 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 150 | m_keyStore = ccn_keystore_create (); |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 151 | string keyStoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore"); |
| 152 | if (ccn_keystore_init (m_keyStore, (char *)keyStoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0) |
| 153 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keyStoreFile.c_str())); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 156 | void |
| 157 | CcnxWrapper::ccnLoop () |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 158 | { |
Alexander Afanasyev | 4f9ea48 | 2012-03-15 11:57:29 -0700 | [diff] [blame] | 159 | _LOG_FUNCTION (this); |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 160 | static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0))); |
| 161 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000)); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 162 | |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 163 | while (m_running) |
| 164 | { |
| 165 | try |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 166 | { |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 167 | #ifdef _DEBUG_WRAPPER_ |
| 168 | std::cout << m_c << flush; |
| 169 | #endif |
| 170 | int res = 0; |
| 171 | { |
| 172 | recursive_mutex::scoped_lock lock (m_mutex); |
| 173 | res = ccn_run (m_handle, 0); |
| 174 | } |
| 175 | |
| 176 | if (!m_running) break; |
| 177 | |
| 178 | if (res < 0) |
| 179 | BOOST_THROW_EXCEPTION (CcnxOperationException() |
| 180 | << errmsg_info_str("ccn_run returned error")); |
| 181 | |
| 182 | |
| 183 | pollfd pfds[1]; |
| 184 | { |
| 185 | recursive_mutex::scoped_lock lock (m_mutex); |
| 186 | |
| 187 | pfds[0].fd = ccn_get_connection_fd (m_handle); |
| 188 | pfds[0].events = POLLIN; |
| 189 | if (ccn_output_is_pending (m_handle)) |
| 190 | pfds[0].events |= POLLOUT; |
| 191 | } |
| 192 | |
| 193 | int ret = poll (pfds, 1, 1); |
| 194 | if (ret < 0) |
| 195 | { |
| 196 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)")); |
| 197 | } |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 198 | } |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 199 | catch (CcnxOperationException &e) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 200 | { |
Zhenkai Zhu | 9587cd6 | 2012-10-17 11:58:37 -0700 | [diff] [blame] | 201 | // do not try reconnect for now |
| 202 | throw e; |
| 203 | /* |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 204 | m_connected = false; |
| 205 | // probably ccnd has been stopped |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 206 | // try reconnect with sleep |
| 207 | int interval = 1; |
| 208 | int maxInterval = 32; |
| 209 | while (m_running) |
| 210 | { |
| 211 | try |
| 212 | { |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 213 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 214 | |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 215 | connectCcnd(); |
| 216 | _LOG_DEBUG("reconnect to ccnd succeeded"); |
| 217 | break; |
| 218 | } |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 219 | catch (CcnxOperationException &e) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 220 | { |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 221 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 222 | |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 223 | // do exponential backup for reconnect interval |
| 224 | if (interval < maxInterval) |
| 225 | { |
| 226 | interval *= 2; |
| 227 | } |
| 228 | } |
| 229 | } |
Zhenkai Zhu | 9587cd6 | 2012-10-17 11:58:37 -0700 | [diff] [blame] | 230 | */ |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 231 | } |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 232 | catch (const std::exception &exc) |
| 233 | { |
| 234 | // catch anything thrown within try block that derives from std::exception |
| 235 | std::cerr << exc.what(); |
| 236 | } |
| 237 | catch (...) |
| 238 | { |
| 239 | cout << "UNKNOWN EXCEPTION !!!" << endl; |
| 240 | } |
| 241 | |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 242 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 243 | } |
| 244 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 245 | /// @endcond |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 246 | int |
| 247 | CcnxWrapper::publishStringData (const string &name, const string &dataBuffer, int freshness) { |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 248 | return publishRawData(name, dataBuffer.c_str(), dataBuffer.length(), freshness); |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 249 | } |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 250 | |
| 251 | int |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 252 | CcnxWrapper::publishRawData (const string &name, const char *buf, size_t len, int freshness) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 253 | { |
Zhenkai Zhu | dc70a29 | 2012-06-01 14:00:59 -0700 | [diff] [blame] | 254 | |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 255 | recursive_mutex::scoped_lock lock(m_mutex); |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 256 | if (!m_running || !m_connected) |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 257 | return -1; |
| 258 | |
Alexander Afanasyev | 45fba08 | 2012-03-12 18:05:24 -0700 | [diff] [blame] | 259 | // cout << "Publish: " << name << endl; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 260 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 261 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 262 | ccn_charbuf *content = ccn_charbuf_create(); |
| 263 | |
| 264 | ccn_name_from_uri(pname, name.c_str()); |
| 265 | ccn_signed_info_create(signed_info, |
| 266 | getPublicKeyDigest(), |
| 267 | getPublicKeyDigestLength(), |
| 268 | NULL, |
| 269 | CCN_CONTENT_DATA, |
| 270 | freshness, |
| 271 | NULL, |
| 272 | m_keyLoactor); |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 273 | if(ccn_encode_ContentObject(content, pname, signed_info, |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 274 | (const unsigned char *)buf, len, |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 275 | NULL, getPrivateKey()) < 0) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 276 | { |
| 277 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("encode content failed")); |
| 278 | _LOG_ERROR("<<< Encode content failed " << name); |
| 279 | } |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 280 | |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 281 | if (ccn_put(m_handle, content->buf, content->length) < 0) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 282 | { |
| 283 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed")); |
| 284 | _LOG_ERROR("<<< ccnput content failed " << name); |
| 285 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 286 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 287 | ccn_charbuf_destroy (&pname); |
| 288 | ccn_charbuf_destroy (&signed_info); |
| 289 | ccn_charbuf_destroy (&content); |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 290 | return 0; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 294 | static ccn_upcall_res |
| 295 | incomingInterest(ccn_closure *selfp, |
| 296 | ccn_upcall_kind kind, |
| 297 | ccn_upcall_info *info) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 298 | { |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 299 | CcnxWrapper::InterestCallback *f = static_cast<CcnxWrapper::InterestCallback*> (selfp->data); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 300 | |
| 301 | switch (kind) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 302 | { |
Zhenkai Zhu | cd74759 | 2012-03-09 12:08:17 -0800 | [diff] [blame] | 303 | case CCN_UPCALL_FINAL: // effective in unit tests |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 304 | delete f; |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 305 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 306 | return CCN_UPCALL_RESULT_OK; |
| 307 | |
| 308 | case CCN_UPCALL_INTEREST: |
| 309 | break; |
| 310 | |
| 311 | default: |
| 312 | return CCN_UPCALL_RESULT_OK; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 313 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 314 | |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 315 | string interest; |
Chaoyi Bian | 02dba3c | 2012-03-07 21:45:22 -0800 | [diff] [blame] | 316 | for (int i = 0; i < info->interest_comps->n - 1; i++) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 317 | { |
| 318 | char *comp; |
| 319 | size_t size; |
| 320 | interest += "/"; |
| 321 | ccn_name_comp_get(info->interest_ccnb, info->interest_comps, i, (const unsigned char **)&comp, &size); |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 322 | string compStr(comp, size); |
| 323 | interest += compStr; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 324 | } |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 325 | (*f) (interest); |
Zhenkai Zhu | 71bc0ba | 2012-10-05 14:37:44 -0700 | [diff] [blame] | 326 | _LOG_DEBUG("<<< processed interest: " << interest); |
Chaoyi Bian | 93d4310 | 2012-03-07 14:28:56 -0800 | [diff] [blame] | 327 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 328 | } |
| 329 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 330 | static ccn_upcall_res |
| 331 | incomingData(ccn_closure *selfp, |
| 332 | ccn_upcall_kind kind, |
| 333 | ccn_upcall_info *info) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 334 | { |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 335 | ClosurePass *cp = static_cast<ClosurePass *> (selfp->data); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 336 | |
| 337 | switch (kind) |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 338 | { |
Zhenkai Zhu | cd74759 | 2012-03-09 12:08:17 -0800 | [diff] [blame] | 339 | case CCN_UPCALL_FINAL: // effecitve in unit tests |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 340 | delete cp; |
| 341 | cp = NULL; |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 342 | delete selfp; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 343 | return CCN_UPCALL_RESULT_OK; |
| 344 | |
| 345 | case CCN_UPCALL_CONTENT: |
| 346 | break; |
| 347 | |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 348 | case CCN_UPCALL_INTEREST_TIMED_OUT: { |
| 349 | if (cp != NULL && cp->getRetry() > 0) { |
| 350 | cp->decRetry(); |
| 351 | return CCN_UPCALL_RESULT_REEXPRESS; |
| 352 | } |
| 353 | return CCN_UPCALL_RESULT_OK; |
| 354 | } |
| 355 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 356 | default: |
| 357 | return CCN_UPCALL_RESULT_OK; |
Alexander Afanasyev | 1285b38 | 2012-03-08 16:40:27 -0800 | [diff] [blame] | 358 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 359 | |
| 360 | char *pcontent; |
| 361 | size_t len; |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 362 | if (ccn_content_get_value(info->content_ccnb, info->pco->offset[CCN_PCO_E], info->pco, (const unsigned char **)&pcontent, &len) < 0) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 363 | { |
| 364 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("decode ContentObject failed")); |
| 365 | _LOG_ERROR("<<< Decode content failed "); |
| 366 | } |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 367 | |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 368 | string name; |
| 369 | for (int i = 0; i < info->content_comps->n - 1; i++) |
| 370 | { |
| 371 | char *comp; |
| 372 | size_t size; |
| 373 | name += "/"; |
| 374 | ccn_name_comp_get(info->content_ccnb, info->content_comps, i, (const unsigned char **)&comp, &size); |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 375 | string compStr(comp, size); |
| 376 | name += compStr; |
Chaoyi Bian | 4194b74 | 2012-03-08 17:21:35 -0800 | [diff] [blame] | 377 | } |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 378 | |
| 379 | cp->runCallback(name, pcontent, len); |
| 380 | |
Chaoyi Bian | 11f294f | 2012-03-08 14:28:06 -0800 | [diff] [blame] | 381 | return CCN_UPCALL_RESULT_OK; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 382 | } |
| 383 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 384 | int CcnxWrapper::sendInterestForString (const string &strInterest, const StringDataCallback &strDataCallback, int retry) |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 385 | { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 386 | DataClosurePass * pass = new DataClosurePass(STRING_FORM, retry, strDataCallback); |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 387 | return sendInterest(strInterest, pass); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 390 | int CcnxWrapper::sendInterest (const string &strInterest, const RawDataCallback &rawDataCallback, int retry) |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 391 | { |
| 392 | RawDataClosurePass * pass = new RawDataClosurePass(RAW_DATA, retry, rawDataCallback); |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 393 | return sendInterest(strInterest, pass); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | int CcnxWrapper::sendInterest (const string &strInterest, void *dataPass) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 397 | { |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 398 | recursive_mutex::scoped_lock lock(m_mutex); |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 399 | if (!m_running || !m_connected) |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 400 | return -1; |
| 401 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 402 | // std::cout << "Send interests for " << strInterest << std::endl; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 403 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 404 | ccn_closure *dataClosure = new ccn_closure; |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 405 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 406 | ccn_name_from_uri (pname, strInterest.c_str()); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 407 | dataClosure->data = dataPass; |
Chaoyi Bian | 95a58c3 | 2012-03-09 15:43:59 -0800 | [diff] [blame] | 408 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 409 | dataClosure->p = &incomingData; |
Zhenkai Zhu | 009ff79 | 2012-03-09 12:37:52 -0800 | [diff] [blame] | 410 | if (ccn_express_interest (m_handle, pname, dataClosure, NULL) < 0) |
Zhenkai Zhu | 26cd6e3 | 2012-10-05 13:10:58 -0700 | [diff] [blame] | 411 | { |
| 412 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("express interest failed")); |
| 413 | _LOG_ERROR("<<< Express interest failed: " << strInterest); |
| 414 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 415 | |
Zhenkai Zhu | 71bc0ba | 2012-10-05 14:37:44 -0700 | [diff] [blame] | 416 | _LOG_DEBUG("<<< Sending interest: " << strInterest); |
| 417 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 418 | ccn_charbuf_destroy (&pname); |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 419 | return 0; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 420 | } |
| 421 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 422 | int CcnxWrapper::setInterestFilter (const string &prefix, const InterestCallback &interestCallback) |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 423 | { |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 424 | recursive_mutex::scoped_lock lock(m_mutex); |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 425 | if (!m_running || !m_connected) |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 426 | return -1; |
| 427 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 428 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 429 | ccn_closure *interestClosure = new ccn_closure; |
| 430 | |
Alexander Afanasyev | 172d2b7 | 2012-03-08 23:43:39 -0800 | [diff] [blame] | 431 | ccn_name_from_uri (pname, prefix.c_str()); |
| 432 | interestClosure->data = new InterestCallback (interestCallback); // should be removed when closure is removed |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 433 | interestClosure->p = &incomingInterest; |
Alexander Afanasyev | 387ac95 | 2012-03-11 23:49:27 -0700 | [diff] [blame] | 434 | int ret = ccn_set_interest_filter (m_handle, pname, interestClosure); |
| 435 | if (ret < 0) |
| 436 | { |
| 437 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret)); |
| 438 | } |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 439 | |
Zhenkai Zhu | 71bc0ba | 2012-10-05 14:37:44 -0700 | [diff] [blame] | 440 | m_registeredInterests.insert(pair<std::string, InterestCallback>(prefix, interestCallback)); |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 441 | ccn_charbuf_destroy(&pname); |
Alexander Afanasyev | ce00169 | 2013-07-14 11:34:41 -0700 | [diff] [blame] | 442 | |
| 443 | return 0; |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 444 | } |
| 445 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 446 | void |
| 447 | CcnxWrapper::clearInterestFilter (const std::string &prefix) |
| 448 | { |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 449 | recursive_mutex::scoped_lock lock(m_mutex); |
Alexander Afanasyev | 3a6c225 | 2012-10-05 16:10:44 -0700 | [diff] [blame] | 450 | if (!m_running || !m_connected) |
Alexander Afanasyev | 2c18077 | 2012-03-13 23:58:46 -0700 | [diff] [blame] | 451 | return; |
| 452 | |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 453 | std::cout << "clearInterestFilter" << std::endl; |
| 454 | ccn_charbuf *pname = ccn_charbuf_create(); |
| 455 | |
| 456 | ccn_name_from_uri (pname, prefix.c_str()); |
| 457 | int ret = ccn_set_interest_filter (m_handle, pname, 0); |
| 458 | if (ret < 0) |
| 459 | { |
| 460 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("set interest filter failed") << errmsg_info_int (ret)); |
| 461 | } |
| 462 | |
Zhenkai Zhu | 71bc0ba | 2012-10-05 14:37:44 -0700 | [diff] [blame] | 463 | m_registeredInterests.erase(prefix); |
Alexander Afanasyev | 1b449c4 | 2012-03-13 20:24:07 -0700 | [diff] [blame] | 464 | ccn_charbuf_destroy(&pname); |
| 465 | } |
| 466 | |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 467 | string |
| 468 | CcnxWrapper::getLocalPrefix () |
| 469 | { |
Alexander Afanasyev | 7f68029 | 2012-10-08 14:30:42 -0700 | [diff] [blame] | 470 | struct ccn * tmp_handle = ccn_create (); |
| 471 | int res = ccn_connect (tmp_handle, NULL); |
| 472 | if (res < 0) |
| 473 | { |
| 474 | _LOG_ERROR ("connecting to ccnd failed"); |
| 475 | return ""; |
| 476 | } |
| 477 | |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 478 | string retval = ""; |
| 479 | |
| 480 | struct ccn_charbuf *templ = ccn_charbuf_create(); |
| 481 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 482 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 483 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 484 | // XXX - use pubid if possible |
| 485 | ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); |
| 486 | ccnb_append_number(templ, 1); |
| 487 | ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ |
| 488 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2); |
| 489 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 490 | |
| 491 | struct ccn_charbuf *name = ccn_charbuf_create (); |
Alexander Afanasyev | 7f68029 | 2012-10-08 14:30:42 -0700 | [diff] [blame] | 492 | res = ccn_name_from_uri (name, "/local/ndn/prefix"); |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 493 | if (res < 0) { |
| 494 | _LOG_ERROR ("Something wrong with `ccn_name_from_uri` call"); |
| 495 | } |
| 496 | else |
| 497 | { |
Alexander Afanasyev | 7f68029 | 2012-10-08 14:30:42 -0700 | [diff] [blame] | 498 | struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle); |
Alexander Afanasyev | cf30196 | 2012-10-08 13:25:35 -0700 | [diff] [blame] | 499 | |
Alexander Afanasyev | 7f68029 | 2012-10-08 14:30:42 -0700 | [diff] [blame] | 500 | struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix", |
| 501 | NULL, 4, CCN_V_HIGHEST, 0); |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 502 | if (stream == NULL) { |
| 503 | _LOG_ERROR ("Cannot create ccn_fetch_stream"); |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | ostringstream os; |
Alexander Afanasyev | 7f68029 | 2012-10-08 14:30:42 -0700 | [diff] [blame] | 508 | |
| 509 | int counter = 0; |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 510 | char buf[256]; |
Alexander Afanasyev | cf30196 | 2012-10-08 13:25:35 -0700 | [diff] [blame] | 511 | while (true) { |
Alexander Afanasyev | cf30196 | 2012-10-08 13:25:35 -0700 | [diff] [blame] | 512 | res = ccn_fetch_read (stream, buf, sizeof(buf)); |
Alexander Afanasyev | cf30196 | 2012-10-08 13:25:35 -0700 | [diff] [blame] | 513 | |
| 514 | if (res == 0) { |
| 515 | break; |
| 516 | } |
| 517 | |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 518 | if (res > 0) { |
| 519 | os << string(buf, res); |
| 520 | } else if (res == CCN_FETCH_READ_NONE) { |
Alexander Afanasyev | 7f68029 | 2012-10-08 14:30:42 -0700 | [diff] [blame] | 521 | if (counter < 2) |
| 522 | { |
| 523 | ccn_run(tmp_handle, 1000); |
| 524 | counter ++; |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | break; |
| 529 | } |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 530 | } else if (res == CCN_FETCH_READ_END) { |
| 531 | break; |
| 532 | } else if (res == CCN_FETCH_READ_TIMEOUT) { |
| 533 | break; |
| 534 | } else { |
| 535 | _LOG_ERROR ("fatal error. should be reported"); |
| 536 | break; |
| 537 | } |
| 538 | } |
| 539 | retval = os.str (); |
| 540 | stream = ccn_fetch_close(stream); |
| 541 | } |
| 542 | fetch = ccn_fetch_destroy(fetch); |
| 543 | } |
| 544 | |
| 545 | ccn_charbuf_destroy (&name); |
| 546 | |
Alexander Afanasyev | 7f68029 | 2012-10-08 14:30:42 -0700 | [diff] [blame] | 547 | ccn_disconnect (tmp_handle); |
| 548 | ccn_destroy (&tmp_handle); |
| 549 | |
Alexander Afanasyev | 50f7e9f | 2012-10-08 00:20:42 -0700 | [diff] [blame] | 550 | return retval; |
| 551 | } |
| 552 | |
| 553 | |
| 554 | |
| 555 | |
| 556 | |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 557 | DataClosurePass::DataClosurePass (CallbackType type, int retry, const CcnxWrapper::StringDataCallback &strDataCallback): ClosurePass(type, retry), m_callback(NULL) |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 558 | { |
Zhenkai Zhu | 1cb2929 | 2012-05-31 22:54:34 -0700 | [diff] [blame] | 559 | m_callback = new CcnxWrapper::StringDataCallback (strDataCallback); |
Zhenkai Zhu | 80af0e0 | 2012-05-31 15:49:07 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | DataClosurePass::~DataClosurePass () |
| 563 | { |
| 564 | delete m_callback; |
| 565 | m_callback = NULL; |
| 566 | } |
| 567 | |
| 568 | void |
| 569 | DataClosurePass::runCallback(std::string name, const char *data, size_t len) |
| 570 | { |
| 571 | string content(data, len); |
| 572 | if (m_callback != NULL) { |
| 573 | (*m_callback)(name, content); |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | |
| 578 | RawDataClosurePass::RawDataClosurePass (CallbackType type, int retry, const CcnxWrapper::RawDataCallback &rawDataCallback): ClosurePass(type, retry), m_callback(NULL) |
| 579 | { |
| 580 | m_callback = new CcnxWrapper::RawDataCallback (rawDataCallback); |
| 581 | } |
| 582 | |
| 583 | RawDataClosurePass::~RawDataClosurePass () |
| 584 | { |
| 585 | delete m_callback; |
| 586 | m_callback = NULL; |
| 587 | } |
| 588 | |
| 589 | void |
| 590 | RawDataClosurePass::runCallback(std::string name, const char *data, size_t len) |
| 591 | { |
| 592 | if (m_callback != NULL) { |
| 593 | (*m_callback)(name, data, len); |
| 594 | } |
| 595 | } |
| 596 | |
Chaoyi Bian | 3e6e514 | 2012-03-06 22:32:19 -0800 | [diff] [blame] | 597 | } |