Alexander Afanasyev | 1dd37ed | 2013-08-14 18:08:09 -0700 | [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 | |
| 22 | #include "ndnx-wrapper.h" |
| 23 | extern "C" { |
| 24 | #include <ndn/fetch.h> |
| 25 | } |
| 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> |
| 30 | #include <boost/make_shared.hpp> |
| 31 | #include <boost/algorithm/string.hpp> |
| 32 | #include <sstream> |
| 33 | |
| 34 | #include "ndnx-verifier.h" |
| 35 | #include "logging.h" |
| 36 | |
| 37 | INIT_LOGGER ("Ndnx.Wrapper"); |
| 38 | |
| 39 | typedef boost::error_info<struct tag_errmsg, std::string> errmsg_info_str; |
| 40 | typedef boost::error_info<struct tag_errmsg, int> errmsg_info_int; |
| 41 | |
| 42 | using namespace std; |
| 43 | using namespace boost; |
| 44 | |
| 45 | namespace Ndnx { |
| 46 | |
| 47 | // hack to enable fake signatures |
| 48 | // min length for signature field is 16, as defined in ndn_buf_decoder.c:728 |
| 49 | const int DEFAULT_SIGNATURE_SIZE = 16; |
| 50 | |
| 51 | // Although ndn_buf_decoder.c:745 defines minimum length 16, something else is checking and only 32-byte fake value is accepted by ndnd |
| 52 | const int PUBLISHER_KEY_SIZE = 32; |
| 53 | |
| 54 | static int |
| 55 | ndn_encode_garbage_Signature(struct ndn_charbuf *buf) |
| 56 | { |
| 57 | int res = 0; |
| 58 | |
| 59 | res |= ndn_charbuf_append_tt(buf, NDN_DTAG_Signature, NDN_DTAG); |
| 60 | |
| 61 | // Let's cheat more. Default signing algorithm in ndnd is SHA256, so we just need add 32 bytes of garbage |
| 62 | static char garbage [DEFAULT_SIGNATURE_SIZE]; |
| 63 | |
| 64 | // digest and witness fields are optional, so use default ones |
| 65 | |
| 66 | res |= ndn_charbuf_append_tt(buf, NDN_DTAG_SignatureBits, NDN_DTAG); |
| 67 | res |= ndn_charbuf_append_tt(buf, DEFAULT_SIGNATURE_SIZE, NDN_BLOB); |
| 68 | res |= ndn_charbuf_append(buf, garbage, DEFAULT_SIGNATURE_SIZE); |
| 69 | res |= ndn_charbuf_append_closer(buf); |
| 70 | |
| 71 | res |= ndn_charbuf_append_closer(buf); |
| 72 | |
| 73 | return(res == 0 ? 0 : -1); |
| 74 | } |
| 75 | |
| 76 | static int |
| 77 | ndn_pack_unsigned_ContentObject(struct ndn_charbuf *buf, |
| 78 | const struct ndn_charbuf *Name, |
| 79 | const struct ndn_charbuf *SignedInfo, |
| 80 | const void *data, |
| 81 | size_t size) |
| 82 | { |
| 83 | int res = 0; |
| 84 | struct ndn_charbuf *content_header; |
| 85 | size_t closer_start; |
| 86 | |
| 87 | content_header = ndn_charbuf_create(); |
| 88 | res |= ndn_charbuf_append_tt(content_header, NDN_DTAG_Content, NDN_DTAG); |
| 89 | if (size != 0) |
| 90 | res |= ndn_charbuf_append_tt(content_header, size, NDN_BLOB); |
| 91 | closer_start = content_header->length; |
| 92 | res |= ndn_charbuf_append_closer(content_header); |
| 93 | if (res < 0) |
| 94 | return(-1); |
| 95 | |
| 96 | res |= ndn_charbuf_append_tt(buf, NDN_DTAG_ContentObject, NDN_DTAG); |
| 97 | |
| 98 | res |= ndn_encode_garbage_Signature(buf); |
| 99 | |
| 100 | res |= ndn_charbuf_append_charbuf(buf, Name); |
| 101 | res |= ndn_charbuf_append_charbuf(buf, SignedInfo); |
| 102 | res |= ndnb_append_tagged_blob(buf, NDN_DTAG_Content, data, size); |
| 103 | res |= ndn_charbuf_append_closer(buf); |
| 104 | |
| 105 | ndn_charbuf_destroy(&content_header); |
| 106 | return(res == 0 ? 0 : -1); |
| 107 | } |
| 108 | |
| 109 | NdnxWrapper::NdnxWrapper() |
| 110 | : m_handle (0) |
| 111 | , m_running (true) |
| 112 | , m_connected (false) |
| 113 | , m_executor (new Executor(1)) |
| 114 | , m_verifier(new Verifier(this)) |
| 115 | { |
| 116 | start (); |
| 117 | } |
| 118 | |
| 119 | void |
| 120 | NdnxWrapper::connectNdnd() |
| 121 | { |
| 122 | if (m_handle != 0) { |
| 123 | ndn_disconnect (m_handle); |
| 124 | //ndn_destroy (&m_handle); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | m_handle = ndn_create (); |
| 129 | } |
| 130 | |
| 131 | UniqueRecLock lock(m_mutex); |
| 132 | if (ndn_connect(m_handle, NULL) < 0) |
| 133 | { |
| 134 | BOOST_THROW_EXCEPTION (NdnxOperationException() << errmsg_info_str("connection to ndnd failed")); |
| 135 | } |
| 136 | m_connected = true; |
| 137 | |
| 138 | if (!m_registeredInterests.empty()) |
| 139 | { |
| 140 | for (map<Name, InterestCallback>::const_iterator it = m_registeredInterests.begin(); it != m_registeredInterests.end(); ++it) |
| 141 | { |
| 142 | clearInterestFilter(it->first, false); |
| 143 | setInterestFilter(it->first, it->second, false); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | NdnxWrapper::~NdnxWrapper() |
| 149 | { |
| 150 | shutdown (); |
| 151 | if (m_verifier != 0) |
| 152 | { |
| 153 | delete m_verifier; |
| 154 | m_verifier = 0; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | void |
| 159 | NdnxWrapper::start () // called automatically in constructor |
| 160 | { |
| 161 | connectNdnd(); |
| 162 | m_thread = thread (&NdnxWrapper::ndnLoop, this); |
| 163 | m_executor->start(); |
| 164 | } |
| 165 | |
| 166 | void |
| 167 | NdnxWrapper::shutdown () // called in destructor, but can called manually |
| 168 | { |
| 169 | m_executor->shutdown(); |
| 170 | |
| 171 | { |
| 172 | UniqueRecLock lock(m_mutex); |
| 173 | m_running = false; |
| 174 | } |
| 175 | |
| 176 | _LOG_DEBUG ("+++++++++SHUTDOWN+++++++"); |
| 177 | if (m_connected) |
| 178 | { |
| 179 | m_thread.join (); |
| 180 | |
| 181 | ndn_disconnect (m_handle); |
| 182 | //ndn_destroy (&m_handle); |
| 183 | m_connected = false; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | NdnxWrapper::ndnLoop () |
| 189 | { |
| 190 | static boost::mt19937 randomGenerator (static_cast<unsigned int> (std::time (0))); |
| 191 | static boost::variate_generator<boost::mt19937&, boost::uniform_int<> > rangeUniformRandom (randomGenerator, uniform_int<> (0,1000)); |
| 192 | |
| 193 | while (m_running) |
| 194 | { |
| 195 | try |
| 196 | { |
| 197 | int res = 0; |
| 198 | { |
| 199 | UniqueRecLock lock(m_mutex); |
| 200 | res = ndn_run (m_handle, 0); |
| 201 | } |
| 202 | |
| 203 | if (!m_running) break; |
| 204 | |
| 205 | if (res < 0) { |
| 206 | _LOG_ERROR ("ndn_run returned negative status: " << res); |
| 207 | |
| 208 | BOOST_THROW_EXCEPTION (NdnxOperationException() |
| 209 | << errmsg_info_str("ndn_run returned error")); |
| 210 | } |
| 211 | |
| 212 | |
| 213 | pollfd pfds[1]; |
| 214 | { |
| 215 | UniqueRecLock lock(m_mutex); |
| 216 | |
| 217 | pfds[0].fd = ndn_get_connection_fd (m_handle); |
| 218 | pfds[0].events = POLLIN; |
| 219 | if (ndn_output_is_pending (m_handle)) |
| 220 | pfds[0].events |= POLLOUT; |
| 221 | } |
| 222 | |
| 223 | int ret = poll (pfds, 1, 1); |
| 224 | if (ret < 0) |
| 225 | { |
| 226 | BOOST_THROW_EXCEPTION (NdnxOperationException() << errmsg_info_str("ndnd socket failed (probably ndnd got stopped)")); |
| 227 | } |
| 228 | } |
| 229 | catch (NdnxOperationException &e) |
| 230 | { |
| 231 | m_connected = false; |
| 232 | // probably ndnd has been stopped |
| 233 | // try reconnect with sleep |
| 234 | int interval = 1; |
| 235 | int maxInterval = 32; |
| 236 | while (m_running) |
| 237 | { |
| 238 | try |
| 239 | { |
| 240 | this_thread::sleep (boost::get_system_time () + boost::posix_time::seconds (interval) + boost::posix_time::milliseconds (rangeUniformRandom ())); |
| 241 | |
| 242 | connectNdnd (); |
| 243 | _LOG_DEBUG("reconnect to ndnd succeeded"); |
| 244 | break; |
| 245 | } |
| 246 | catch (NdnxOperationException &e) |
| 247 | { |
| 248 | this_thread::sleep (boost::get_system_time () + boost::posix_time::seconds (interval) + boost::posix_time::milliseconds (rangeUniformRandom ())); |
| 249 | |
| 250 | // do exponential backup for reconnect interval |
| 251 | if (interval < maxInterval) |
| 252 | { |
| 253 | interval *= 2; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | } |
| 258 | catch (const std::exception &exc) |
| 259 | { |
| 260 | // catch anything thrown within try block that derives from std::exception |
| 261 | std::cerr << exc.what(); |
| 262 | } |
| 263 | catch (...) |
| 264 | { |
| 265 | cout << "UNKNOWN EXCEPTION !!!" << endl; |
| 266 | } |
| 267 | } |
| 268 | } |
| 269 | |
| 270 | Bytes |
| 271 | NdnxWrapper::createContentObject(const Name &name, const void *buf, size_t len, int freshness, const Name &keyNameParam) |
| 272 | { |
| 273 | { |
| 274 | UniqueRecLock lock(m_mutex); |
| 275 | if (!m_running || !m_connected) |
| 276 | { |
| 277 | _LOG_TRACE ("<< not running or connected"); |
| 278 | return Bytes (); |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | NdnxCharbufPtr ptr = name.toNdnxCharbuf(); |
| 283 | ndn_charbuf *pname = ptr->getBuf(); |
| 284 | ndn_charbuf *content = ndn_charbuf_create(); |
| 285 | |
| 286 | struct ndn_signing_params sp = NDN_SIGNING_PARAMS_INIT; |
| 287 | sp.freshness = freshness; |
| 288 | |
| 289 | Name keyName; |
| 290 | |
| 291 | if (keyNameParam.size() == 0) |
| 292 | { |
| 293 | // use default key name |
| 294 | NdnxCharbufPtr defaultKeyNamePtr = boost::make_shared<NdnxCharbuf>(); |
| 295 | ndn_get_public_key_and_name(m_handle, &sp, NULL, NULL, defaultKeyNamePtr->getBuf()); |
| 296 | keyName = Name(*defaultKeyNamePtr); |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | keyName = keyNameParam; |
| 301 | } |
| 302 | |
| 303 | if (sp.template_ndnb == NULL) |
| 304 | { |
| 305 | sp.template_ndnb = ndn_charbuf_create(); |
| 306 | ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_SignedInfo, NDN_DTAG); |
| 307 | } |
| 308 | // no idea what the following 3 lines do, but it was there |
| 309 | else if (sp.template_ndnb->length > 0) { |
| 310 | sp.template_ndnb->length--; |
| 311 | } |
| 312 | ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_KeyLocator, NDN_DTAG); |
| 313 | ndn_charbuf_append_tt(sp.template_ndnb, NDN_DTAG_KeyName, NDN_DTAG); |
| 314 | NdnxCharbufPtr keyPtr = keyName.toNdnxCharbuf(); |
| 315 | ndn_charbuf *keyBuf = keyPtr->getBuf(); |
| 316 | ndn_charbuf_append(sp.template_ndnb, keyBuf->buf, keyBuf->length); |
| 317 | ndn_charbuf_append_closer(sp.template_ndnb); // </KeyName> |
| 318 | ndn_charbuf_append_closer(sp.template_ndnb); // </KeyLocator> |
| 319 | sp.sp_flags |= NDN_SP_TEMPL_KEY_LOCATOR; |
| 320 | ndn_charbuf_append_closer(sp.template_ndnb); // </SignedInfo> |
| 321 | |
| 322 | if (ndn_sign_content(m_handle, content, pname, &sp, buf, len) != 0) |
| 323 | { |
| 324 | BOOST_THROW_EXCEPTION(NdnxOperationException() << errmsg_info_str("sign content failed")); |
| 325 | } |
| 326 | |
| 327 | Bytes bytes; |
| 328 | readRaw(bytes, content->buf, content->length); |
| 329 | |
| 330 | ndn_charbuf_destroy (&content); |
| 331 | if (sp.template_ndnb != NULL) |
| 332 | { |
| 333 | ndn_charbuf_destroy (&sp.template_ndnb); |
| 334 | } |
| 335 | |
| 336 | return bytes; |
| 337 | } |
| 338 | |
| 339 | int |
| 340 | NdnxWrapper::putToNdnd (const Bytes &contentObject) |
| 341 | { |
| 342 | _LOG_TRACE (">> putToNdnd"); |
| 343 | UniqueRecLock lock(m_mutex); |
| 344 | if (!m_running || !m_connected) |
| 345 | { |
| 346 | _LOG_TRACE ("<< not running or connected"); |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | |
| 351 | if (ndn_put(m_handle, head(contentObject), contentObject.size()) < 0) |
| 352 | { |
| 353 | _LOG_ERROR ("ndn_put failed"); |
| 354 | // BOOST_THROW_EXCEPTION(NdnxOperationException() << errmsg_info_str("ndnput failed")); |
| 355 | } |
| 356 | else |
| 357 | { |
| 358 | _LOG_DEBUG ("<< putToNdnd"); |
| 359 | } |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | int |
| 365 | NdnxWrapper::publishData (const Name &name, const unsigned char *buf, size_t len, int freshness, const Name &keyName) |
| 366 | { |
| 367 | Bytes co = createContentObject(name, buf, len, freshness, keyName); |
| 368 | return putToNdnd(co); |
| 369 | } |
| 370 | |
| 371 | int |
| 372 | NdnxWrapper::publishUnsignedData(const Name &name, const unsigned char *buf, size_t len, int freshness) |
| 373 | { |
| 374 | { |
| 375 | UniqueRecLock lock(m_mutex); |
| 376 | if (!m_running || !m_connected) |
| 377 | { |
| 378 | _LOG_TRACE ("<< not running or connected"); |
| 379 | return -1; |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | NdnxCharbufPtr ptr = name.toNdnxCharbuf(); |
| 384 | ndn_charbuf *pname = ptr->getBuf(); |
| 385 | ndn_charbuf *content = ndn_charbuf_create(); |
| 386 | ndn_charbuf *signed_info = ndn_charbuf_create(); |
| 387 | |
| 388 | static char fakeKey[PUBLISHER_KEY_SIZE]; |
| 389 | |
| 390 | int res = ndn_signed_info_create(signed_info, |
| 391 | fakeKey, PUBLISHER_KEY_SIZE, |
| 392 | NULL, |
| 393 | NDN_CONTENT_DATA, |
| 394 | freshness, |
| 395 | NULL, |
| 396 | NULL // ndnd is happy with absent key locator and key itself... ha ha |
| 397 | ); |
| 398 | ndn_pack_unsigned_ContentObject(content, pname, signed_info, buf, len); |
| 399 | |
| 400 | Bytes bytes; |
| 401 | readRaw(bytes, content->buf, content->length); |
| 402 | |
| 403 | ndn_charbuf_destroy (&content); |
| 404 | ndn_charbuf_destroy (&signed_info); |
| 405 | |
| 406 | return putToNdnd (bytes); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | static void |
| 411 | deleterInInterestTuple (tuple<NdnxWrapper::InterestCallback *, ExecutorPtr> *tuple) |
| 412 | { |
| 413 | delete tuple->get<0> (); |
| 414 | delete tuple; |
| 415 | } |
| 416 | |
| 417 | static ndn_upcall_res |
| 418 | incomingInterest(ndn_closure *selfp, |
| 419 | ndn_upcall_kind kind, |
| 420 | ndn_upcall_info *info) |
| 421 | { |
| 422 | NdnxWrapper::InterestCallback *f; |
| 423 | ExecutorPtr executor; |
| 424 | tuple<NdnxWrapper::InterestCallback *, ExecutorPtr> *realData = reinterpret_cast< tuple<NdnxWrapper::InterestCallback *, ExecutorPtr>* > (selfp->data); |
| 425 | tie (f, executor) = *realData; |
| 426 | |
| 427 | switch (kind) |
| 428 | { |
| 429 | case NDN_UPCALL_FINAL: // effective in unit tests |
| 430 | // delete closure; |
| 431 | executor->execute (bind (deleterInInterestTuple, realData)); |
| 432 | |
| 433 | delete selfp; |
| 434 | _LOG_TRACE ("<< incomingInterest with NDN_UPCALL_FINAL"); |
| 435 | return NDN_UPCALL_RESULT_OK; |
| 436 | |
| 437 | case NDN_UPCALL_INTEREST: |
| 438 | _LOG_TRACE (">> incomingInterest upcall: " << Name(info->interest_ndnb, info->interest_comps)); |
| 439 | break; |
| 440 | |
| 441 | default: |
| 442 | _LOG_TRACE ("<< incomingInterest with NDN_UPCALL_RESULT_OK: " << Name(info->interest_ndnb, info->interest_comps)); |
| 443 | return NDN_UPCALL_RESULT_OK; |
| 444 | } |
| 445 | |
| 446 | Name interest(info->interest_ndnb, info->interest_comps); |
| 447 | Selectors selectors(info->pi); |
| 448 | |
| 449 | executor->execute (bind (*f, interest, selectors)); |
| 450 | // this will be run in executor |
| 451 | // (*f) (interest); |
| 452 | // closure->runInterestCallback(interest); |
| 453 | |
| 454 | return NDN_UPCALL_RESULT_OK; |
| 455 | } |
| 456 | |
| 457 | static void |
| 458 | deleterInDataTuple (tuple<Closure *, ExecutorPtr, Selectors> *tuple) |
| 459 | { |
| 460 | delete tuple->get<0> (); |
| 461 | delete tuple; |
| 462 | } |
| 463 | |
| 464 | static ndn_upcall_res |
| 465 | incomingData(ndn_closure *selfp, |
| 466 | ndn_upcall_kind kind, |
| 467 | ndn_upcall_info *info) |
| 468 | { |
| 469 | // Closure *cp = static_cast<Closure *> (selfp->data); |
| 470 | Closure *cp; |
| 471 | ExecutorPtr executor; |
| 472 | Selectors selectors; |
| 473 | tuple<Closure *, ExecutorPtr, Selectors> *realData = reinterpret_cast< tuple<Closure*, ExecutorPtr, Selectors>* > (selfp->data); |
| 474 | tie (cp, executor, selectors) = *realData; |
| 475 | |
| 476 | switch (kind) |
| 477 | { |
| 478 | case NDN_UPCALL_FINAL: // effecitve in unit tests |
| 479 | executor->execute (bind (deleterInDataTuple, realData)); |
| 480 | |
| 481 | cp = NULL; |
| 482 | delete selfp; |
| 483 | _LOG_TRACE ("<< incomingData with NDN_UPCALL_FINAL"); |
| 484 | return NDN_UPCALL_RESULT_OK; |
| 485 | |
| 486 | case NDN_UPCALL_CONTENT: |
| 487 | _LOG_TRACE (">> incomingData content upcall: " << Name (info->content_ndnb, info->content_comps)); |
| 488 | break; |
| 489 | |
| 490 | // this is the case where the intentionally unsigned packets coming (in Encapsulation case) |
| 491 | case NDN_UPCALL_CONTENT_BAD: |
| 492 | _LOG_TRACE (">> incomingData content bad upcall: " << Name (info->content_ndnb, info->content_comps)); |
| 493 | break; |
| 494 | |
| 495 | // always ask ndnd to try to fetch the key |
| 496 | case NDN_UPCALL_CONTENT_UNVERIFIED: |
| 497 | _LOG_TRACE (">> incomingData content unverified upcall: " << Name (info->content_ndnb, info->content_comps)); |
| 498 | break; |
| 499 | |
| 500 | case NDN_UPCALL_INTEREST_TIMED_OUT: { |
| 501 | if (cp != NULL) |
| 502 | { |
| 503 | Name interest(info->interest_ndnb, info->interest_comps); |
| 504 | _LOG_TRACE ("<< incomingData timeout: " << Name (info->interest_ndnb, info->interest_comps)); |
| 505 | executor->execute (bind (&Closure::runTimeoutCallback, cp, interest, *cp, selectors)); |
| 506 | } |
| 507 | else |
| 508 | { |
| 509 | _LOG_TRACE ("<< incomingData timeout, but callback is not set...: " << Name (info->interest_ndnb, info->interest_comps)); |
| 510 | } |
| 511 | return NDN_UPCALL_RESULT_OK; |
| 512 | } |
| 513 | |
| 514 | default: |
| 515 | _LOG_TRACE(">> unknown upcall type"); |
| 516 | return NDN_UPCALL_RESULT_OK; |
| 517 | } |
| 518 | |
| 519 | PcoPtr pco = make_shared<ParsedContentObject> (info->content_ndnb, info->pco->offset[NDN_PCO_E]); |
| 520 | |
| 521 | // this will be run in executor |
| 522 | executor->execute (bind (&Closure::runDataCallback, cp, pco->name (), pco)); |
| 523 | _LOG_TRACE (">> incomingData"); |
| 524 | |
| 525 | return NDN_UPCALL_RESULT_OK; |
| 526 | } |
| 527 | |
| 528 | int NdnxWrapper::sendInterest (const Name &interest, const Closure &closure, const Selectors &selectors) |
| 529 | { |
| 530 | _LOG_TRACE (">> sendInterest: " << interest); |
| 531 | { |
| 532 | UniqueRecLock lock(m_mutex); |
| 533 | if (!m_running || !m_connected) |
| 534 | { |
| 535 | _LOG_ERROR ("<< sendInterest: not running or connected"); |
| 536 | return -1; |
| 537 | } |
| 538 | } |
| 539 | |
| 540 | NdnxCharbufPtr namePtr = interest.toNdnxCharbuf(); |
| 541 | ndn_charbuf *pname = namePtr->getBuf(); |
| 542 | ndn_closure *dataClosure = new ndn_closure; |
| 543 | |
| 544 | // Closure *myClosure = new ExecutorClosure(closure, m_executor); |
| 545 | Closure *myClosure = closure.dup (); |
| 546 | dataClosure->data = new tuple<Closure*, ExecutorPtr, Selectors> (myClosure, m_executor, selectors); |
| 547 | |
| 548 | dataClosure->p = &incomingData; |
| 549 | |
| 550 | NdnxCharbufPtr selectorsPtr = selectors.toNdnxCharbuf(); |
| 551 | ndn_charbuf *templ = NULL; |
| 552 | if (selectorsPtr) |
| 553 | { |
| 554 | templ = selectorsPtr->getBuf(); |
| 555 | } |
| 556 | |
| 557 | UniqueRecLock lock(m_mutex); |
| 558 | if (ndn_express_interest (m_handle, pname, dataClosure, templ) < 0) |
| 559 | { |
| 560 | _LOG_ERROR ("<< sendInterest: ndn_express_interest FAILED!!!"); |
| 561 | } |
| 562 | |
| 563 | return 0; |
| 564 | } |
| 565 | |
| 566 | int NdnxWrapper::setInterestFilter (const Name &prefix, const InterestCallback &interestCallback, bool record/* = true*/) |
| 567 | { |
| 568 | _LOG_TRACE (">> setInterestFilter"); |
| 569 | UniqueRecLock lock(m_mutex); |
| 570 | if (!m_running || !m_connected) |
| 571 | { |
| 572 | return -1; |
| 573 | } |
| 574 | |
| 575 | NdnxCharbufPtr ptr = prefix.toNdnxCharbuf(); |
| 576 | ndn_charbuf *pname = ptr->getBuf(); |
| 577 | ndn_closure *interestClosure = new ndn_closure; |
| 578 | |
| 579 | // interestClosure->data = new ExecutorInterestClosure(interestCallback, m_executor); |
| 580 | |
| 581 | interestClosure->data = new tuple<NdnxWrapper::InterestCallback *, ExecutorPtr> (new InterestCallback (interestCallback), m_executor); // should be removed when closure is removed |
| 582 | interestClosure->p = &incomingInterest; |
| 583 | |
| 584 | int ret = ndn_set_interest_filter (m_handle, pname, interestClosure); |
| 585 | if (ret < 0) |
| 586 | { |
| 587 | _LOG_ERROR ("<< setInterestFilter: ndn_set_interest_filter FAILED"); |
| 588 | } |
| 589 | |
| 590 | if (record) |
| 591 | { |
| 592 | m_registeredInterests.insert(pair<Name, InterestCallback>(prefix, interestCallback)); |
| 593 | } |
| 594 | |
| 595 | _LOG_TRACE ("<< setInterestFilter"); |
| 596 | |
| 597 | return ret; |
| 598 | } |
| 599 | |
| 600 | void |
| 601 | NdnxWrapper::clearInterestFilter (const Name &prefix, bool record/* = true*/) |
| 602 | { |
| 603 | _LOG_TRACE (">> clearInterestFilter"); |
| 604 | UniqueRecLock lock(m_mutex); |
| 605 | if (!m_running || !m_connected) |
| 606 | return; |
| 607 | |
| 608 | NdnxCharbufPtr ptr = prefix.toNdnxCharbuf(); |
| 609 | ndn_charbuf *pname = ptr->getBuf(); |
| 610 | |
| 611 | int ret = ndn_set_interest_filter (m_handle, pname, 0); |
| 612 | if (ret < 0) |
| 613 | { |
| 614 | } |
| 615 | |
| 616 | if (record) |
| 617 | { |
| 618 | m_registeredInterests.erase(prefix); |
| 619 | } |
| 620 | |
| 621 | _LOG_TRACE ("<< clearInterestFilter"); |
| 622 | } |
| 623 | |
| 624 | Name |
| 625 | NdnxWrapper::getLocalPrefix () |
| 626 | { |
| 627 | struct ndn * tmp_handle = ndn_create (); |
| 628 | int res = ndn_connect (tmp_handle, NULL); |
| 629 | if (res < 0) |
| 630 | { |
| 631 | return Name(); |
| 632 | } |
| 633 | |
| 634 | string retval = ""; |
| 635 | |
| 636 | struct ndn_charbuf *templ = ndn_charbuf_create(); |
| 637 | ndn_charbuf_append_tt(templ, NDN_DTAG_Interest, NDN_DTAG); |
| 638 | ndn_charbuf_append_tt(templ, NDN_DTAG_Name, NDN_DTAG); |
| 639 | ndn_charbuf_append_closer(templ); /* </Name> */ |
| 640 | // XXX - use pubid if possible |
| 641 | ndn_charbuf_append_tt(templ, NDN_DTAG_MaxSuffixComponents, NDN_DTAG); |
| 642 | ndnb_append_number(templ, 1); |
| 643 | ndn_charbuf_append_closer(templ); /* </MaxSuffixComponents> */ |
| 644 | ndnb_tagged_putf(templ, NDN_DTAG_Scope, "%d", 2); |
| 645 | ndn_charbuf_append_closer(templ); /* </Interest> */ |
| 646 | |
| 647 | struct ndn_charbuf *name = ndn_charbuf_create (); |
| 648 | res = ndn_name_from_uri (name, "/local/ndn/prefix"); |
| 649 | if (res < 0) { |
| 650 | } |
| 651 | else |
| 652 | { |
| 653 | struct ndn_fetch *fetch = ndn_fetch_new (tmp_handle); |
| 654 | |
| 655 | struct ndn_fetch_stream *stream = ndn_fetch_open (fetch, name, "/local/ndn/prefix", |
| 656 | NULL, 4, NDN_V_HIGHEST, 0); |
| 657 | if (stream == NULL) { |
| 658 | } |
| 659 | else |
| 660 | { |
| 661 | ostringstream os; |
| 662 | |
| 663 | int counter = 0; |
| 664 | char buf[256]; |
| 665 | while (true) { |
| 666 | res = ndn_fetch_read (stream, buf, sizeof(buf)); |
| 667 | |
| 668 | if (res == 0) { |
| 669 | break; |
| 670 | } |
| 671 | |
| 672 | if (res > 0) { |
| 673 | os << string(buf, res); |
| 674 | } else if (res == NDN_FETCH_READ_NONE) { |
| 675 | if (counter < 2) |
| 676 | { |
| 677 | ndn_run(tmp_handle, 1000); |
| 678 | counter ++; |
| 679 | } |
| 680 | else |
| 681 | { |
| 682 | break; |
| 683 | } |
| 684 | } else if (res == NDN_FETCH_READ_END) { |
| 685 | break; |
| 686 | } else if (res == NDN_FETCH_READ_TIMEOUT) { |
| 687 | break; |
| 688 | } else { |
| 689 | break; |
| 690 | } |
| 691 | } |
| 692 | retval = os.str (); |
| 693 | stream = ndn_fetch_close(stream); |
| 694 | } |
| 695 | fetch = ndn_fetch_destroy(fetch); |
| 696 | } |
| 697 | |
| 698 | ndn_charbuf_destroy (&name); |
| 699 | |
| 700 | ndn_disconnect (tmp_handle); |
| 701 | ndn_destroy (&tmp_handle); |
| 702 | |
| 703 | boost::algorithm::trim(retval); |
| 704 | return Name(retval); |
| 705 | } |
| 706 | |
| 707 | bool |
| 708 | NdnxWrapper::verify(PcoPtr &pco, double maxWait) |
| 709 | { |
| 710 | return m_verifier->verify(pco, maxWait); |
| 711 | } |
| 712 | |
| 713 | // This is needed just for get function implementation |
| 714 | struct GetState |
| 715 | { |
| 716 | GetState (double maxWait) |
| 717 | { |
| 718 | double intPart, fraction; |
| 719 | fraction = modf (std::abs(maxWait), &intPart); |
| 720 | |
| 721 | m_maxWait = date_time::second_clock<boost::posix_time::ptime>::universal_time() |
| 722 | + boost::posix_time::seconds (intPart) |
| 723 | + boost::posix_time::microseconds (fraction * 1000000); |
| 724 | } |
| 725 | |
| 726 | PcoPtr |
| 727 | WaitForResult () |
| 728 | { |
| 729 | //_LOG_TRACE("GetState::WaitForResult start"); |
| 730 | boost::unique_lock<boost::mutex> lock (m_mutex); |
| 731 | m_cond.timed_wait (lock, m_maxWait); |
| 732 | //_LOG_TRACE("GetState::WaitForResult finish"); |
| 733 | |
| 734 | return m_retval; |
| 735 | } |
| 736 | |
| 737 | void |
| 738 | DataCallback (Name name, PcoPtr pco) |
| 739 | { |
| 740 | //_LOG_TRACE("GetState::DataCallback, Name [" << name << "]"); |
| 741 | boost::unique_lock<boost::mutex> lock (m_mutex); |
| 742 | m_retval = pco; |
| 743 | m_cond.notify_one (); |
| 744 | } |
| 745 | |
| 746 | void |
| 747 | TimeoutCallback (Name name) |
| 748 | { |
| 749 | boost::unique_lock<boost::mutex> lock (m_mutex); |
| 750 | m_cond.notify_one (); |
| 751 | } |
| 752 | |
| 753 | private: |
| 754 | boost::posix_time::ptime m_maxWait; |
| 755 | |
| 756 | boost::mutex m_mutex; |
| 757 | boost::condition_variable m_cond; |
| 758 | |
| 759 | PcoPtr m_retval; |
| 760 | }; |
| 761 | |
| 762 | |
| 763 | PcoPtr |
| 764 | NdnxWrapper::get(const Name &interest, const Selectors &selectors, double maxWait/* = 4.0*/) |
| 765 | { |
| 766 | _LOG_TRACE (">> get: " << interest); |
| 767 | { |
| 768 | UniqueRecLock lock(m_mutex); |
| 769 | if (!m_running || !m_connected) |
| 770 | { |
| 771 | _LOG_ERROR ("<< get: not running or connected"); |
| 772 | return PcoPtr (); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | GetState state (maxWait); |
| 777 | this->sendInterest (interest, Closure (boost::bind (&GetState::DataCallback, &state, _1, _2), |
| 778 | boost::bind (&GetState::TimeoutCallback, &state, _1)), |
| 779 | selectors); |
| 780 | return state.WaitForResult (); |
| 781 | } |
| 782 | |
| 783 | } |