blob: 713e203e57a8614096107318b9ed164efed1f671 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080022 */
23
Alexander Afanasyev766cea72014-04-24 19:16:42 -070024// correct way to include ndn-cxx headers
25// #include <ndn-cxx/face.hpp>
26// #include <ndn-cxx/util/scheduler.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080027#include "face.hpp"
28#include "util/scheduler.hpp"
29
Alexander Afanasyev151a8552014-04-11 00:54:43 -070030// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
31namespace ndn {
32// Additional nested namespace could be used to prevent/limit name contentions
33namespace examples {
34
Alexander Afanasyevf6468892014-01-29 01:04:14 -080035void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070036onData(Face& face,
37 const Interest& interest, Data& data)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080038{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080039 std::cout << "I: " << interest.toUri() << std::endl;
40 std::cout << "D: " << data.getName().toUri() << std::endl;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080041}
42
43void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070044onTimeout(Face& face,
45 const Interest& interest)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080046{
47 std::cout << "Timeout" << std::endl;
48}
49
50void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070051delayedInterest(Face& face)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080052{
53 std::cout << "One more Interest, delayed by the scheduler" << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070054
55 Interest i(Name("/localhost/testApp/randomData"));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080056 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070057 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080058 i.setMustBeFresh(true);
59
60 face.expressInterest(i,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070061 bind(&onData, ref(face), _1, _2),
62 bind(&onTimeout, ref(face), _1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080063}
64
Alexander Afanasyev151a8552014-04-11 00:54:43 -070065int
66main(int argc, char** argv)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080067{
68 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070069 // Explicitly create io_service object, which can be shared between Face and Scheduler
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070070 boost::asio::io_service ioService;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070071
72 Interest i(Name("/localhost/testApp/randomData"));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080073 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070074 i.setInterestLifetime(time::seconds(1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080075 i.setMustBeFresh(true);
76
Alexander Afanasyev151a8552014-04-11 00:54:43 -070077 // Create face with io_service object
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070078 Face face(ioService);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080079 face.expressInterest(i,
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070080 bind(&onData, ref(face), _1, _2),
81 bind(&onTimeout, ref(face), _1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080082
83
Alexander Afanasyev151a8552014-04-11 00:54:43 -070084 // Create scheduler object
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070085 Scheduler scheduler(ioService);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070086
87 // Schedule a new event
88 scheduler.scheduleEvent(time::seconds(2),
Alexander Afanasyevb67090a2014-04-29 22:31:01 -070089 bind(&delayedInterest, ref(face)));
Alexander Afanasyev151a8552014-04-11 00:54:43 -070090
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070091 // ioService.run() will block until all events finished or ioService.stop() is called
92 ioService.run();
Alexander Afanasyev151a8552014-04-11 00:54:43 -070093
Alexander Afanasyevf6468892014-01-29 01:04:14 -080094 // Alternatively, a helper face.processEvents() also can be called
95 // processEvents will block until the requested data received or timeout occurs
96 // face.processEvents();
97 }
Alexander Afanasyev151a8552014-04-11 00:54:43 -070098 catch(std::exception& e) {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080099 std::cerr << "ERROR: " << e.what() << std::endl;
100 }
101 return 0;
102}
Alexander Afanasyev151a8552014-04-11 00:54:43 -0700103
104} // namespace examples
105} // namespace ndn
106
107int
108main(int argc, char** argv)
109{
110 return ndn::examples::main(argc, argv);
111}