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