Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
| 2 | /** |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 4 | * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu> |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 5 | * BSD License, see COPYING for copyright and distribution information. |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | // correct way to include NDN-CPP headers |
| 9 | // #include <ndn-cpp-dev/face.hpp> |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 10 | // #include <ndn-cpp-dev/util/scheduler.hpp> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 11 | #include "face.hpp" |
| 12 | #include "util/scheduler.hpp" |
| 13 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 14 | // Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) |
| 15 | namespace ndn { |
| 16 | // Additional nested namespace could be used to prevent/limit name contentions |
| 17 | namespace examples { |
| 18 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 19 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 20 | onData(Face& face, |
| 21 | const Interest& interest, Data& data) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 22 | { |
Alexander Afanasyev | 0222fba | 2014-02-09 23:16:02 -0800 | [diff] [blame] | 23 | std::cout << "I: " << interest.toUri() << std::endl; |
| 24 | std::cout << "D: " << data.getName().toUri() << std::endl; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 28 | onTimeout(Face& face, |
| 29 | const Interest& interest) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 30 | { |
| 31 | std::cout << "Timeout" << std::endl; |
| 32 | } |
| 33 | |
| 34 | void |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 35 | delayedInterest(Face& face) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 36 | { |
| 37 | std::cout << "One more Interest, delayed by the scheduler" << std::endl; |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 38 | |
| 39 | Interest i(Name("/localhost/testApp/randomData")); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 40 | i.setScope(1); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 41 | i.setInterestLifetime(time::milliseconds(1000)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 42 | i.setMustBeFresh(true); |
| 43 | |
| 44 | face.expressInterest(i, |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 45 | bind(&onData, boost::ref(face), _1, _2), |
| 46 | bind(&onTimeout, boost::ref(face), _1)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 49 | int |
| 50 | main(int argc, char** argv) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 51 | { |
| 52 | try { |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 53 | // Explicitly create io_service object, which can be shared between Face and Scheduler |
| 54 | shared_ptr<boost::asio::io_service> io = make_shared<boost::asio::io_service>(); |
| 55 | |
| 56 | Interest i(Name("/localhost/testApp/randomData")); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 57 | i.setScope(1); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 58 | i.setInterestLifetime(time::seconds(1)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 59 | i.setMustBeFresh(true); |
| 60 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 61 | // Create face with io_service object |
| 62 | Face face(io); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 63 | face.expressInterest(i, |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 64 | bind(&onData, boost::ref(face), _1, _2), |
| 65 | bind(&onTimeout, boost::ref(face), _1)); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 66 | |
| 67 | |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 68 | // Create scheduler object |
| 69 | Scheduler scheduler(*io); |
| 70 | |
| 71 | // Schedule a new event |
| 72 | scheduler.scheduleEvent(time::seconds(2), |
| 73 | bind(&delayedInterest, boost::ref(face))); |
| 74 | |
| 75 | // io->run() will block until all events finished or io->stop() is called |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 76 | io->run(); |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 77 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 78 | // Alternatively, a helper face.processEvents() also can be called |
| 79 | // processEvents will block until the requested data received or timeout occurs |
| 80 | // face.processEvents(); |
| 81 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 82 | catch(std::exception& e) { |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 83 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 84 | } |
| 85 | return 0; |
| 86 | } |
Alexander Afanasyev | 151a855 | 2014-04-11 00:54:43 -0700 | [diff] [blame] | 87 | |
| 88 | } // namespace examples |
| 89 | } // namespace ndn |
| 90 | |
| 91 | int |
| 92 | main(int argc, char** argv) |
| 93 | { |
| 94 | return ndn::examples::main(argc, argv); |
| 95 | } |