Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -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 | |
Alexander Afanasyev | 09c613f | 2014-01-29 00:23:58 -0800 | [diff] [blame] | 8 | // correct way to include NDN-CPP headers |
| 9 | // #include <ndn-cpp-dev/face.hpp> |
| 10 | #include "face.hpp" |
| 11 | |
Alexander Afanasyev | c4b7598 | 2014-01-09 14:51:45 -0800 | [diff] [blame] | 12 | #include <stdexcept> |
| 13 | |
| 14 | #if NDN_CPP_HAVE_CXX11 |
| 15 | // In the std library, the placeholders are in a different namespace than boost. |
| 16 | using namespace ndn::func_lib::placeholders; |
| 17 | #endif |
| 18 | |
| 19 | void |
| 20 | onData(ndn::Face &face, |
| 21 | const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest, const ndn::ptr_lib::shared_ptr<ndn::Data> &data) |
| 22 | { |
| 23 | std::cout << "I: " << interest->toUri() << std::endl; |
| 24 | std::cout << "D: " << data->getName().toUri() << std::endl; |
| 25 | } |
| 26 | |
| 27 | void |
| 28 | onTimeout(ndn::Face &face, |
| 29 | const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest) |
| 30 | { |
| 31 | std::cout << "Timeout" << std::endl; |
| 32 | } |
| 33 | |
| 34 | void |
| 35 | BlockPrinter(const ndn::Block &block, const std::string &indent="") |
| 36 | { |
| 37 | std::cout << indent << block.type() << " (" << block.value_size() << ") [["; |
| 38 | std::cout.write(reinterpret_cast<const char *>(block.value()), block.value_size()); |
| 39 | std::cout<< "]]" << std::endl; |
| 40 | |
| 41 | for(ndn::Block::element_const_iterator i = block.getAll().begin(); |
| 42 | i != block.getAll().end(); |
| 43 | ++i) |
| 44 | { |
| 45 | BlockPrinter(*i, indent+" "); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | int main() |
| 50 | { |
| 51 | try { |
| 52 | ndn::Interest i(ndn::Name("/localhost/testApp/randomData")); |
| 53 | i.setScope(1); |
| 54 | i.setInterestLifetime(1000); |
| 55 | i.setMustBeFresh(true); |
| 56 | |
| 57 | ndn::Face face; |
| 58 | face.expressInterest(i, |
| 59 | ndn::func_lib::bind(onData, boost::ref(face), _1, _2), |
| 60 | ndn::func_lib::bind(onTimeout, boost::ref(face), _1)); |
| 61 | |
| 62 | // processEvents will block until the requested data received or timeout occurs |
| 63 | face.processEvents(); |
| 64 | } |
| 65 | catch(std::exception &e) { |
| 66 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 67 | } |
| 68 | return 0; |
| 69 | } |