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