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