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 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 13 | void |
| 14 | onData(ndn::Face &face, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 15 | const ndn::Interest& interest, ndn::Data& data) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 16 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 17 | std::cout << "I: " << interest.toUri() << std::endl; |
| 18 | std::cout << "D: " << data.getName().toUri() << std::endl; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 19 | } |
| 20 | |
| 21 | void |
| 22 | onTimeout(ndn::Face &face, |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 23 | const ndn::Interest& interest) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 24 | { |
| 25 | std::cout << "Timeout" << std::endl; |
| 26 | } |
| 27 | |
| 28 | void |
| 29 | delayedInterest(ndn::Face &face) |
| 30 | { |
| 31 | std::cout << "One more Interest, delayed by the scheduler" << std::endl; |
| 32 | |
| 33 | ndn::Interest i(ndn::Name("/localhost/testApp/randomData")); |
| 34 | i.setScope(1); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 35 | i.setInterestLifetime(ndn::time::milliseconds(1000)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 36 | i.setMustBeFresh(true); |
| 37 | |
| 38 | face.expressInterest(i, |
| 39 | ndn::bind(&onData, boost::ref(face), _1, _2), |
| 40 | ndn::bind(&onTimeout, boost::ref(face), _1)); |
| 41 | } |
| 42 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 43 | int main() |
| 44 | { |
| 45 | try { |
| 46 | // Explicitly create io_service object, which can be shared between |
| 47 | // Face and Scheduler |
| 48 | ndn::shared_ptr<boost::asio::io_service> io = |
| 49 | ndn::make_shared<boost::asio::io_service>(); |
| 50 | |
| 51 | ndn::Interest i(ndn::Name("/localhost/testApp/randomData")); |
| 52 | i.setScope(1); |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 53 | i.setInterestLifetime(ndn::time::seconds(1)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 54 | i.setMustBeFresh(true); |
| 55 | |
| 56 | ndn::Face face(io); |
| 57 | face.expressInterest(i, |
| 58 | ndn::bind(&onData, boost::ref(face), _1, _2), |
| 59 | ndn::bind(&onTimeout, boost::ref(face), _1)); |
| 60 | |
| 61 | |
| 62 | ndn::Scheduler scheduler(*io); |
| 63 | scheduler.scheduleEvent(ndn::time::seconds(2), |
| 64 | ndn::bind(&delayedInterest, boost::ref(face))); |
| 65 | |
| 66 | io->run(); |
| 67 | |
| 68 | // Alternatively, a helper face.processEvents() also can be called |
| 69 | // processEvents will block until the requested data received or timeout occurs |
| 70 | // face.processEvents(); |
| 71 | } |
| 72 | catch(std::exception &e) { |
| 73 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 74 | } |
| 75 | return 0; |
| 76 | } |