Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
| 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
| 8 | // correct way to include NDN-CPP headers |
| 9 | // #include <ndn-cpp-dev/face.hpp> |
| 10 | #include "face.hpp" |
| 11 | #include "util/scheduler.hpp" |
| 12 | |
| 13 | #include <stdexcept> |
| 14 | |
| 15 | #if NDN_CPP_HAVE_CXX11 |
| 16 | // In the std library, the placeholders are in a different namespace than boost. |
| 17 | using namespace ndn::func_lib::placeholders; |
| 18 | #endif |
| 19 | |
| 20 | void |
| 21 | onData(ndn::Face &face, |
| 22 | const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest, const ndn::ptr_lib::shared_ptr<ndn::Data> &data) |
| 23 | { |
| 24 | std::cout << "I: " << interest->toUri() << std::endl; |
| 25 | std::cout << "D: " << data->getName().toUri() << std::endl; |
| 26 | } |
| 27 | |
| 28 | void |
| 29 | onTimeout(ndn::Face &face, |
| 30 | const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest) |
| 31 | { |
| 32 | std::cout << "Timeout" << std::endl; |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | delayedInterest(ndn::Face &face) |
| 37 | { |
| 38 | std::cout << "One more Interest, delayed by the scheduler" << std::endl; |
| 39 | |
| 40 | ndn::Interest i(ndn::Name("/localhost/testApp/randomData")); |
| 41 | i.setScope(1); |
| 42 | i.setInterestLifetime(1000); |
| 43 | i.setMustBeFresh(true); |
| 44 | |
| 45 | face.expressInterest(i, |
| 46 | ndn::bind(&onData, boost::ref(face), _1, _2), |
| 47 | ndn::bind(&onTimeout, boost::ref(face), _1)); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | BlockPrinter(const ndn::Block &block, const std::string &indent="") |
| 52 | { |
| 53 | std::cout << indent << block.type() << " (" << block.value_size() << ") [["; |
| 54 | std::cout.write(reinterpret_cast<const char *>(block.value()), block.value_size()); |
| 55 | std::cout<< "]]" << std::endl; |
| 56 | |
| 57 | for(ndn::Block::element_const_iterator i = block.getAll().begin(); |
| 58 | i != block.getAll().end(); |
| 59 | ++i) |
| 60 | { |
| 61 | BlockPrinter(*i, indent+" "); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | int main() |
| 66 | { |
| 67 | try { |
| 68 | // Explicitly create io_service object, which can be shared between |
| 69 | // Face and Scheduler |
| 70 | ndn::shared_ptr<boost::asio::io_service> io = |
| 71 | ndn::make_shared<boost::asio::io_service>(); |
| 72 | |
| 73 | ndn::Interest i(ndn::Name("/localhost/testApp/randomData")); |
| 74 | i.setScope(1); |
| 75 | i.setInterestLifetime(1000); |
| 76 | i.setMustBeFresh(true); |
| 77 | |
| 78 | ndn::Face face(io); |
| 79 | face.expressInterest(i, |
| 80 | ndn::bind(&onData, boost::ref(face), _1, _2), |
| 81 | ndn::bind(&onTimeout, boost::ref(face), _1)); |
| 82 | |
| 83 | |
| 84 | ndn::Scheduler scheduler(*io); |
| 85 | scheduler.scheduleEvent(ndn::time::seconds(2), |
| 86 | ndn::bind(&delayedInterest, boost::ref(face))); |
| 87 | |
| 88 | io->run(); |
| 89 | |
| 90 | // Alternatively, a helper face.processEvents() also can be called |
| 91 | // processEvents will block until the requested data received or timeout occurs |
| 92 | // face.processEvents(); |
| 93 | } |
| 94 | catch(std::exception &e) { |
| 95 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 96 | } |
| 97 | return 0; |
| 98 | } |