Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 1 | /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /* |
| 3 | * Copyright (c) 2013 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 | * Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 20 | */ |
| 21 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 22 | #include "ccnx-wrapper.h" |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 23 | extern "C" { |
| 24 | #include <ccn/fetch.h> |
| 25 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 26 | #include <poll.h> |
| 27 | #include <boost/throw_exception.hpp> |
| 28 | #include <boost/date_time/posix_time/posix_time.hpp> |
| 29 | #include <boost/random.hpp> |
Alexander Afanasyev | f278db3 | 2013-01-21 14:41:01 -0800 | [diff] [blame] | 30 | #include <boost/make_shared.hpp> |
Zhenkai Zhu | cbbb888 | 2013-01-25 13:49:12 -0800 | [diff] [blame] | 31 | #include <boost/algorithm/string.hpp> |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 32 | #include <sstream> |
| 33 | |
Alexander Afanasyev | 9c9c301 | 2013-01-25 23:44:25 -0800 | [diff] [blame] | 34 | #include "logging.h" |
| 35 | |
| 36 | INIT_LOGGER ("Ccnx.Wrapper"); |
| 37 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 38 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 39 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
| 40 | |
| 41 | using namespace std; |
| 42 | using namespace boost; |
| 43 | |
| 44 | namespace Ccnx { |
| 45 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 46 | static int |
| 47 | ccn_encode_Signature(struct ccn_charbuf *buf, |
| 48 | const char *digest_algorithm, |
| 49 | const void *witness, |
| 50 | size_t witness_size, |
| 51 | const struct ccn_signature *signature, |
| 52 | size_t signature_size) |
| 53 | { |
| 54 | int res = 0; |
| 55 | |
| 56 | if (signature == NULL) |
| 57 | return(-1); |
| 58 | |
| 59 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Signature, CCN_DTAG); |
| 60 | |
| 61 | if (digest_algorithm != NULL) { |
| 62 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_DigestAlgorithm, CCN_DTAG); |
| 63 | res |= ccn_charbuf_append_tt(buf, strlen(digest_algorithm), CCN_UDATA); |
| 64 | res |= ccn_charbuf_append_string(buf, digest_algorithm); |
| 65 | res |= ccn_charbuf_append_closer(buf); |
| 66 | } |
| 67 | |
| 68 | if (witness != NULL) { |
| 69 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_Witness, CCN_DTAG); |
| 70 | res |= ccn_charbuf_append_tt(buf, witness_size, CCN_BLOB); |
| 71 | res |= ccn_charbuf_append(buf, witness, witness_size); |
| 72 | res |= ccn_charbuf_append_closer(buf); |
| 73 | } |
| 74 | |
| 75 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_SignatureBits, CCN_DTAG); |
| 76 | res |= ccn_charbuf_append_tt(buf, signature_size, CCN_BLOB); |
| 77 | res |= ccn_charbuf_append(buf, signature, signature_size); |
| 78 | res |= ccn_charbuf_append_closer(buf); |
| 79 | |
| 80 | res |= ccn_charbuf_append_closer(buf); |
| 81 | |
| 82 | return(res == 0 ? 0 : -1); |
| 83 | } |
| 84 | |
| 85 | static int |
| 86 | ccn_pack_ContentObject(struct ccn_charbuf *buf, |
| 87 | const struct ccn_charbuf *Name, |
| 88 | const struct ccn_charbuf *SignedInfo, |
| 89 | const void *data, |
| 90 | size_t size, |
| 91 | const char *digest_algorithm, |
| 92 | const struct ccn_pkey *private_key |
| 93 | ) |
| 94 | { |
| 95 | int res = 0; |
| 96 | struct ccn_sigc *sig_ctx; |
| 97 | struct ccn_signature *signature; |
| 98 | struct ccn_charbuf *content_header; |
| 99 | size_t closer_start; |
| 100 | |
| 101 | content_header = ccn_charbuf_create(); |
| 102 | res |= ccn_charbuf_append_tt(content_header, CCN_DTAG_Content, CCN_DTAG); |
| 103 | if (size != 0) |
| 104 | res |= ccn_charbuf_append_tt(content_header, size, CCN_BLOB); |
| 105 | closer_start = content_header->length; |
| 106 | res |= ccn_charbuf_append_closer(content_header); |
| 107 | if (res < 0) |
| 108 | return(-1); |
| 109 | sig_ctx = ccn_sigc_create(); |
| 110 | size_t sig_size = ccn_sigc_signature_max_size(sig_ctx, private_key); |
| 111 | signature = (ccn_signature *)calloc(1, sig_size); |
| 112 | ccn_sigc_destroy(&sig_ctx); |
| 113 | res |= ccn_charbuf_append_tt(buf, CCN_DTAG_ContentObject, CCN_DTAG); |
| 114 | |
| 115 | res |= ccn_encode_Signature(buf, digest_algorithm, |
| 116 | NULL, 0, signature, sig_size); |
| 117 | res |= ccn_charbuf_append_charbuf(buf, Name); |
| 118 | res |= ccn_charbuf_append_charbuf(buf, SignedInfo); |
| 119 | res |= ccnb_append_tagged_blob(buf, CCN_DTAG_Content, data, size); |
| 120 | res |= ccn_charbuf_append_closer(buf); |
| 121 | free(signature); |
| 122 | ccn_charbuf_destroy(&content_header); |
| 123 | return(res == 0 ? 0 : -1); |
| 124 | } |
| 125 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 126 | CcnxWrapper::CcnxWrapper() |
| 127 | : m_handle (0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 128 | , m_running (true) |
| 129 | , m_connected (false) |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 130 | , m_executor (new Executor(1)) |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 131 | , m_keystore(NULL) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 132 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 133 | start (); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void |
| 137 | CcnxWrapper::connectCcnd() |
| 138 | { |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 139 | //if (m_handle != 0) { |
| 140 | //ccn_disconnect (m_handle); |
| 141 | //ccn_destroy (&m_handle); |
| 142 | //} |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 143 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 144 | m_handle = ccn_create (); |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 145 | //UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 146 | if (ccn_connect(m_handle, NULL) < 0) |
| 147 | { |
| 148 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("connection to ccnd failed")); |
| 149 | } |
| 150 | m_connected = true; |
| 151 | |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 152 | //if (!m_registeredInterests.empty()) |
| 153 | //{ |
| 154 | // for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it) |
| 155 | //{ |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 156 | // clearInterestFilter(it->first); |
Zhenkai Zhu | 3b82d43 | 2013-01-03 22:48:40 -0800 | [diff] [blame] | 157 | // setInterestFilter(it->first, it->second); |
| 158 | //} |
| 159 | //} |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | CcnxWrapper::~CcnxWrapper() |
| 163 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 164 | shutdown (); |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 165 | if (m_keystore != NULL) |
| 166 | { |
| 167 | ccn_keystore_destroy(&m_keystore); |
| 168 | } |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
| 172 | CcnxWrapper::start () // called automatically in constructor |
| 173 | { |
| 174 | connectCcnd(); |
| 175 | m_thread = thread (&CcnxWrapper::ccnLoop, this); |
| 176 | m_executor->start(); |
| 177 | } |
| 178 | |
| 179 | void |
| 180 | CcnxWrapper::shutdown () // called in destructor, but can called manually |
| 181 | { |
| 182 | m_executor->shutdown(); |
| 183 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 184 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 185 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 186 | m_running = false; |
| 187 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 188 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 189 | _LOG_DEBUG ("+++++++++SHUTDOWN+++++++"); |
| 190 | if (m_connected) |
| 191 | { |
| 192 | m_thread.join (); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 193 | |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 194 | ccn_disconnect (m_handle); |
| 195 | //ccn_destroy (&m_handle); |
| 196 | m_connected = false; |
| 197 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 201 | CcnxWrapper::ccnLoop () |
| 202 | { |
| 203 | static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0))); |
| 204 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000)); |
| 205 | |
| 206 | while (m_running) |
| 207 | { |
| 208 | try |
| 209 | { |
| 210 | int res = 0; |
| 211 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 212 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 213 | res = ccn_run (m_handle, 0); |
| 214 | } |
| 215 | |
| 216 | if (!m_running) break; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 217 | |
Alexander Afanasyev | 9c9c301 | 2013-01-25 23:44:25 -0800 | [diff] [blame] | 218 | if (res < 0) { |
| 219 | _LOG_ERROR ("ccn_run returned negative status: " << res); |
| 220 | usleep (100000); |
| 221 | continue; |
| 222 | // BOOST_THROW_EXCEPTION (CcnxOperationException() |
| 223 | // << errmsg_info_str("ccn_run returned error")); |
| 224 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 225 | |
| 226 | |
| 227 | pollfd pfds[1]; |
| 228 | { |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 229 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 230 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 231 | pfds[0].fd = ccn_get_connection_fd (m_handle); |
| 232 | pfds[0].events = POLLIN; |
| 233 | if (ccn_output_is_pending (m_handle)) |
| 234 | pfds[0].events |= POLLOUT; |
| 235 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 236 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 237 | int ret = poll (pfds, 1, 1); |
| 238 | if (ret < 0) |
| 239 | { |
| 240 | BOOST_THROW_EXCEPTION (CcnxOperationException() << errmsg_info_str("ccnd socket failed (probably ccnd got stopped)")); |
| 241 | } |
| 242 | } |
| 243 | catch (CcnxOperationException &e) |
| 244 | { |
| 245 | // do not try reconnect for now |
Zhenkai Zhu | 6e7d4d2 | 2013-01-15 18:18:18 -0800 | [diff] [blame] | 246 | cout << *get_error_info<errmsg_info_str> (e) << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 247 | throw e; |
| 248 | /* |
| 249 | m_connected = false; |
| 250 | // probably ccnd has been stopped |
| 251 | // try reconnect with sleep |
| 252 | int interval = 1; |
| 253 | int maxInterval = 32; |
| 254 | while (m_running) |
| 255 | { |
| 256 | try |
| 257 | { |
| 258 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 259 | |
| 260 | connectCcnd(); |
| 261 | _LOG_DEBUG("reconnect to ccnd succeeded"); |
| 262 | break; |
| 263 | } |
| 264 | catch (CcnxOperationException &e) |
| 265 | { |
| 266 | this_thread::sleep (boost::get_system_time () + TIME_SECONDS(interval) + TIME_MILLISECONDS (rangeUniformRandom ())); |
| 267 | |
| 268 | // do exponential backup for reconnect interval |
| 269 | if (interval < maxInterval) |
| 270 | { |
| 271 | interval *= 2; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | */ |
| 276 | } |
| 277 | catch (const std::exception &exc) |
| 278 | { |
| 279 | // catch anything thrown within try block that derives from std::exception |
| 280 | std::cerr << exc.what(); |
| 281 | } |
| 282 | catch (...) |
| 283 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 284 | cout << "UNKNOWN EXCEPTION !!!" << endl; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 285 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 286 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 289 | Bytes |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 290 | CcnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 291 | { |
Alexander Afanasyev | 1d1cc83 | 2013-02-05 20:03:36 -0800 | [diff] [blame] | 292 | { |
| 293 | UniqueRecLock lock(m_mutex); |
| 294 | if (!m_running || !m_connected) |
| 295 | { |
| 296 | _LOG_TRACE ("<< not running or connected"); |
| 297 | return Bytes (); |
| 298 | } |
| 299 | } |
| 300 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 301 | CcnxCharbufPtr ptr = name.toCcnxCharbuf(); |
| 302 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 303 | ccn_charbuf *content = ccn_charbuf_create(); |
| 304 | |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 305 | struct ccn_signing_params sp = CCN_SIGNING_PARAMS_INIT; |
| 306 | sp.freshness = freshness; |
| 307 | |
| 308 | if (ccn_sign_content(m_handle, content, pname, &sp, buf, len) != 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 309 | { |
Zhenkai Zhu | d9d03f6 | 2013-01-04 16:57:46 -0800 | [diff] [blame] | 310 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("sign content failed")); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 311 | } |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 312 | |
| 313 | Bytes bytes; |
| 314 | readRaw(bytes, content->buf, content->length); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 315 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 316 | ccn_charbuf_destroy (&content); |
| 317 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 318 | return bytes; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | int |
Zhenkai Zhu | bad089c | 2012-12-28 10:28:27 -0800 | [diff] [blame] | 322 | CcnxWrapper::putToCcnd (const Bytes &contentObject) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 323 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 324 | _LOG_TRACE (">> putToCcnd"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 325 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 326 | if (!m_running || !m_connected) |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 327 | { |
| 328 | _LOG_TRACE ("<< not running or connected"); |
| 329 | return -1; |
| 330 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 331 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 332 | |
Zhenkai Zhu | 974c5a6 | 2012-12-28 14:15:30 -0800 | [diff] [blame] | 333 | if (ccn_put(m_handle, head(contentObject), contentObject.size()) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 334 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 335 | _LOG_ERROR ("ccn_put failed"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 336 | // BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str("ccnput failed")); |
| 337 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 338 | else |
| 339 | { |
| 340 | _LOG_DEBUG ("<< putToCcnd"); |
| 341 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 342 | |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 347 | CcnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 348 | { |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 349 | Bytes co = createContentObject(name, buf, len, freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 350 | return putToCcnd(co); |
| 351 | } |
| 352 | |
Zhenkai Zhu | 43eb273 | 2012-12-28 00:48:26 -0800 | [diff] [blame] | 353 | int |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 354 | CcnxWrapper::publishData (const Name &name, const Bytes &content, int freshness) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 355 | { |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 356 | return publishData(name, head(content), content.size(), freshness); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 357 | } |
| 358 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 359 | int |
| 360 | CcnxWrapper::publishUnsignedData(const Name &name, const Bytes &content, int freshness) |
| 361 | { |
| 362 | return publishUnsignedData(name, head(content), content.size(), freshness); |
| 363 | } |
| 364 | |
| 365 | int |
| 366 | CcnxWrapper::publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness) |
| 367 | { |
| 368 | { |
| 369 | UniqueRecLock lock(m_mutex); |
| 370 | if (!m_running || !m_connected) |
| 371 | { |
| 372 | _LOG_TRACE ("<< not running or connected"); |
| 373 | return -1; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | CcnxCharbufPtr ptr = name.toCcnxCharbuf(); |
| 378 | ccn_charbuf *pname = ptr->getBuf(); |
| 379 | ccn_charbuf *content = ccn_charbuf_create(); |
| 380 | ccn_charbuf *signed_info = ccn_charbuf_create(); |
| 381 | |
| 382 | if (m_keystore == NULL) |
| 383 | { |
| 384 | m_keystore = ccn_keystore_create (); |
| 385 | string keystoreFile = string(getenv("HOME")) + string("/.ccnx/.ccnx_keystore"); |
| 386 | if (ccn_keystore_init (m_keystore, (char *)keystoreFile.c_str(), (char*)"Th1s1sn0t8g00dp8ssw0rd.") < 0) |
| 387 | BOOST_THROW_EXCEPTION(CcnxOperationException() << errmsg_info_str(keystoreFile.c_str())); |
| 388 | } |
| 389 | |
| 390 | int res = ccn_signed_info_create(signed_info, |
| 391 | ccn_keystore_public_key_digest(m_keystore), |
| 392 | ccn_keystore_public_key_digest_length(m_keystore), |
| 393 | NULL, |
| 394 | CCN_CONTENT_DATA, |
| 395 | freshness, |
| 396 | NULL, |
| 397 | NULL |
| 398 | ); |
| 399 | |
| 400 | ccn_pack_ContentObject(content, pname, signed_info, buf, len, NULL, ccn_keystore_private_key (m_keystore)); |
| 401 | |
| 402 | Bytes bytes; |
| 403 | readRaw(bytes, content->buf, content->length); |
| 404 | |
| 405 | ccn_charbuf_destroy (&content); |
| 406 | ccn_charbuf_destroy (&signed_info); |
| 407 | |
| 408 | return putToCcnd (bytes); |
| 409 | } |
| 410 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 411 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 412 | static void |
| 413 | deleterInInterestTuple (tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *tuple) |
| 414 | { |
| 415 | delete tuple->get<0> (); |
| 416 | delete tuple; |
| 417 | } |
| 418 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 419 | static ccn_upcall_res |
| 420 | incomingInterest(ccn_closure *selfp, |
| 421 | ccn_upcall_kind kind, |
| 422 | ccn_upcall_info *info) |
| 423 | { |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 424 | CcnxWrapper::InterestCallback *f; |
| 425 | ExecutorPtr executor; |
| 426 | tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<CcnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data); |
| 427 | tie (f, executor) = *realData; |
| 428 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 429 | switch (kind) |
| 430 | { |
| 431 | case CCN_UPCALL_FINAL: // effective in unit tests |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 432 | // delete closure; |
| 433 | executor->execute (bind (deleterInInterestTuple, realData)); |
| 434 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 435 | delete selfp; |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 436 | _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_FINAL"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 437 | return CCN_UPCALL_RESULT_OK; |
| 438 | |
| 439 | case CCN_UPCALL_INTEREST: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 440 | _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 441 | break; |
| 442 | |
| 443 | default: |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 444 | _LOG_TRACE ("<< incomingInterest with CCN_UPCALL_RESULT_OK: " << Name(info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 445 | return CCN_UPCALL_RESULT_OK; |
| 446 | } |
| 447 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 448 | Name interest(info->interest_ccnb, info->interest_comps); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 449 | Selectors selectors(info->pi); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 450 | |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 451 | executor->execute (bind (*f, interest, selectors)); |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 452 | // this will be run in executor |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 453 | // (*f) (interest); |
| 454 | // closure->runInterestCallback(interest); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 455 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 456 | return CCN_UPCALL_RESULT_OK; |
| 457 | } |
| 458 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 459 | static void |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 460 | deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple) |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 461 | { |
| 462 | delete tuple->get<0> (); |
| 463 | delete tuple; |
| 464 | } |
| 465 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 466 | static ccn_upcall_res |
| 467 | incomingData(ccn_closure *selfp, |
| 468 | ccn_upcall_kind kind, |
| 469 | ccn_upcall_info *info) |
| 470 | { |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 471 | // Closure *cp = static_cast<Closure *> (selfp->data); |
| 472 | Closure *cp; |
| 473 | ExecutorPtr executor; |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 474 | Selectors selectors; |
| 475 | tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data); |
| 476 | tie (cp, executor, selectors) = *realData; |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 477 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame^] | 478 | bool verified = false; |
| 479 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 480 | switch (kind) |
| 481 | { |
| 482 | case CCN_UPCALL_FINAL: // effecitve in unit tests |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 483 | executor->execute (bind (deleterInDataTuple, realData)); |
| 484 | |
Zhenkai Zhu | 19f81de | 2013-01-04 22:27:47 -0800 | [diff] [blame] | 485 | cp = NULL; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 486 | delete selfp; |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 487 | _LOG_TRACE ("<< incomingData with CCN_UPCALL_FINAL"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 488 | return CCN_UPCALL_RESULT_OK; |
| 489 | |
| 490 | case CCN_UPCALL_CONTENT: |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame^] | 491 | verified = true; |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 492 | _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ccnb, info->content_comps)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 493 | break; |
| 494 | |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 495 | case CCN_UPCALL_CONTENT_UNVERIFIED: |
| 496 | _LOG_TRACE (">> incomingData content unverified upcall: " << Name (info->content_ccnb, info->content_comps)); |
| 497 | break; |
| 498 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 499 | case CCN_UPCALL_INTEREST_TIMED_OUT: { |
Zhenkai Zhu | 8339e91 | 2013-01-18 18:10:17 -0800 | [diff] [blame] | 500 | if (cp != NULL) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 501 | { |
Zhenkai Zhu | 8339e91 | 2013-01-18 18:10:17 -0800 | [diff] [blame] | 502 | Name interest(info->interest_ccnb, info->interest_comps); |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 503 | _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ccnb, info->interest_comps)); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 504 | executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors)); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 505 | } |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 506 | else |
| 507 | { |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 508 | _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ccnb, info->interest_comps)); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 509 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 510 | return CCN_UPCALL_RESULT_OK; |
| 511 | } |
| 512 | |
| 513 | default: |
Zhenkai Zhu | 5acebd7 | 2013-02-07 19:59:25 -0800 | [diff] [blame] | 514 | _LOG_TRACE(">> unknown upcall type"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 515 | return CCN_UPCALL_RESULT_OK; |
| 516 | } |
| 517 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame^] | 518 | PcoPtr pco = make_shared<ParsedContentObject> (info->content_ccnb, info->pco->offset[CCN_PCO_E], verified); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 519 | |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 520 | // this will be run in executor |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 521 | executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco)); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 522 | _LOG_TRACE (">> incomingData"); |
| 523 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 524 | return CCN_UPCALL_RESULT_OK; |
| 525 | } |
| 526 | |
Alexander Afanasyev | d6c2a90 | 2013-01-19 21:24:30 -0800 | [diff] [blame] | 527 | int CcnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 528 | { |
Alexander Afanasyev | 6aec3bf | 2013-02-05 11:25:52 -0800 | [diff] [blame] | 529 | _LOG_TRACE (">> sendInterest: " << interest); |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 530 | { |
| 531 | UniqueRecLock lock(m_mutex); |
| 532 | if (!m_running || !m_connected) |
Alexander Afanasyev | b3d4cd5 | 2013-01-28 11:20:52 -0800 | [diff] [blame] | 533 | { |
| 534 | _LOG_ERROR ("<< sendInterest: not running or connected"); |
| 535 | return -1; |
| 536 | } |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 537 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 538 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 539 | CcnxCharbufPtr namePtr = interest.toCcnxCharbuf(); |
| 540 | ccn_charbuf *pname = namePtr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 541 | ccn_closure *dataClosure = new ccn_closure; |
| 542 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 543 | // Closure *myClosure = new ExecutorClosure(closure, m_executor); |
| 544 | Closure *myClosure = closure.dup (); |
Zhenkai Zhu | ff4fa8a | 2013-01-28 22:02:40 -0800 | [diff] [blame] | 545 | dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 546 | |
| 547 | dataClosure->p = &incomingData; |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 548 | |
| 549 | CcnxCharbufPtr selectorsPtr = selectors.toCcnxCharbuf(); |
| 550 | ccn_charbuf *templ = NULL; |
Alexander Afanasyev | 053e5ac | 2013-01-22 20:59:13 -0800 | [diff] [blame] | 551 | if (selectorsPtr) |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 552 | { |
| 553 | templ = selectorsPtr->getBuf(); |
| 554 | } |
| 555 | |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 556 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 557 | if (ccn_express_interest (m_handle, pname, dataClosure, templ) < 0) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 558 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 559 | _LOG_ERROR ("<< sendInterest: ccn_express_interest FAILED!!!"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 560 | } |
| 561 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 562 | return 0; |
| 563 | } |
| 564 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 565 | int CcnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 566 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 567 | _LOG_TRACE (">> setInterestFilter"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 568 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 569 | if (!m_running || !m_connected) |
Zhenkai Zhu | 1888f74 | 2013-01-28 12:47:33 -0800 | [diff] [blame] | 570 | { |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 571 | return -1; |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 572 | } |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 573 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 574 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 575 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 576 | ccn_closure *interestClosure = new ccn_closure; |
| 577 | |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 578 | // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor); |
| 579 | |
| 580 | interestClosure->data = new tuple<CcnxWrapper::InterestCallback *, ExecutorPtr> (new InterestCallback (interestCallback), m_executor); // should be removed when closure is removed |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 581 | interestClosure->p = &incomingInterest; |
Zhenkai Zhu | 7599db4 | 2013-01-28 10:32:53 -0800 | [diff] [blame] | 582 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 583 | int ret = ccn_set_interest_filter (m_handle, pname, interestClosure); |
| 584 | if (ret < 0) |
| 585 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 586 | _LOG_ERROR ("<< setInterestFilter: ccn_set_interest_filter FAILED"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 587 | } |
| 588 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 589 | m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback)); |
Alexander Afanasyev | 816251e | 2013-01-28 16:16:49 -0800 | [diff] [blame] | 590 | |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 591 | _LOG_TRACE ("<< setInterestFilter"); |
Alexander Afanasyev | dcfa963 | 2013-01-07 16:38:19 -0800 | [diff] [blame] | 592 | |
| 593 | return ret; |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | void |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 597 | CcnxWrapper::clearInterestFilter (const Name &prefix) |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 598 | { |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 599 | _LOG_TRACE (">> clearInterestFilter"); |
Alexander Afanasyev | 20bc8e2 | 2013-01-17 10:56:04 -0800 | [diff] [blame] | 600 | UniqueRecLock lock(m_mutex); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 601 | if (!m_running || !m_connected) |
| 602 | return; |
| 603 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 604 | CcnxCharbufPtr ptr = prefix.toCcnxCharbuf(); |
| 605 | ccn_charbuf *pname = ptr->getBuf(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 606 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 607 | int ret = ccn_set_interest_filter (m_handle, pname, 0); |
| 608 | if (ret < 0) |
| 609 | { |
| 610 | } |
| 611 | |
| 612 | m_registeredInterests.erase(prefix); |
Alexander Afanasyev | d982666 | 2013-01-28 11:18:06 -0800 | [diff] [blame] | 613 | |
| 614 | _LOG_TRACE ("<< clearInterestFilter"); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 615 | } |
| 616 | |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 617 | Name |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 618 | CcnxWrapper::getLocalPrefix () |
| 619 | { |
| 620 | struct ccn * tmp_handle = ccn_create (); |
| 621 | int res = ccn_connect (tmp_handle, NULL); |
| 622 | if (res < 0) |
| 623 | { |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 624 | return Name(); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 625 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 626 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 627 | string retval = ""; |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 628 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 629 | struct ccn_charbuf *templ = ccn_charbuf_create(); |
| 630 | ccn_charbuf_append_tt(templ, CCN_DTAG_Interest, CCN_DTAG); |
| 631 | ccn_charbuf_append_tt(templ, CCN_DTAG_Name, CCN_DTAG); |
| 632 | ccn_charbuf_append_closer(templ); /* </Name> */ |
| 633 | // XXX - use pubid if possible |
| 634 | ccn_charbuf_append_tt(templ, CCN_DTAG_MaxSuffixComponents, CCN_DTAG); |
| 635 | ccnb_append_number(templ, 1); |
| 636 | ccn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ |
| 637 | ccnb_tagged_putf(templ, CCN_DTAG_Scope, "%d", 2); |
| 638 | ccn_charbuf_append_closer(templ); /* </Interest> */ |
| 639 | |
| 640 | struct ccn_charbuf *name = ccn_charbuf_create (); |
| 641 | res = ccn_name_from_uri (name, "/local/ndn/prefix"); |
| 642 | if (res < 0) { |
| 643 | } |
| 644 | else |
| 645 | { |
| 646 | struct ccn_fetch *fetch = ccn_fetch_new (tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 647 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 648 | struct ccn_fetch_stream *stream = ccn_fetch_open (fetch, name, "/local/ndn/prefix", |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 649 | NULL, 4, CCN_V_HIGHEST, 0); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 650 | if (stream == NULL) { |
| 651 | } |
| 652 | else |
| 653 | { |
| 654 | ostringstream os; |
| 655 | |
| 656 | int counter = 0; |
| 657 | char buf[256]; |
| 658 | while (true) { |
| 659 | res = ccn_fetch_read (stream, buf, sizeof(buf)); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 660 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 661 | if (res == 0) { |
| 662 | break; |
| 663 | } |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 664 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 665 | if (res > 0) { |
| 666 | os << string(buf, res); |
| 667 | } else if (res == CCN_FETCH_READ_NONE) { |
| 668 | if (counter < 2) |
| 669 | { |
| 670 | ccn_run(tmp_handle, 1000); |
| 671 | counter ++; |
| 672 | } |
| 673 | else |
| 674 | { |
| 675 | break; |
| 676 | } |
| 677 | } else if (res == CCN_FETCH_READ_END) { |
| 678 | break; |
| 679 | } else if (res == CCN_FETCH_READ_TIMEOUT) { |
| 680 | break; |
| 681 | } else { |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | retval = os.str (); |
| 686 | stream = ccn_fetch_close(stream); |
| 687 | } |
| 688 | fetch = ccn_fetch_destroy(fetch); |
| 689 | } |
| 690 | |
| 691 | ccn_charbuf_destroy (&name); |
| 692 | |
| 693 | ccn_disconnect (tmp_handle); |
| 694 | ccn_destroy (&tmp_handle); |
Zhenkai Zhu | 0d8f5d5 | 2012-12-30 12:54:07 -0800 | [diff] [blame] | 695 | |
Zhenkai Zhu | cbbb888 | 2013-01-25 13:49:12 -0800 | [diff] [blame] | 696 | boost::algorithm::trim(retval); |
Zhenkai Zhu | cb2d0dd | 2013-01-03 14:10:48 -0800 | [diff] [blame] | 697 | return Name(retval); |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 698 | } |
| 699 | |
Zhenkai Zhu | 79264a4 | 2013-02-07 21:49:42 -0800 | [diff] [blame^] | 700 | bool |
| 701 | CcnxWrapper::verifyPco(PcoPtr &pco) |
| 702 | { |
| 703 | bool verified = ccn_verify_content(m_handle, pco->msg(), (ccn_parsed_ContentObject *)pco->pco()); |
| 704 | pco->setVerified(verified); |
| 705 | return verified; |
| 706 | } |
| 707 | |
Zhenkai Zhu | 1ddeb6f | 2012-12-27 14:04:18 -0800 | [diff] [blame] | 708 | } |