blob: 96b2e00b562f9a3a6069f8bfa861f844c5fb1059 [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * 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 Afanasyevf6468892014-01-29 01:04:14 -080013 */
14
Alexander Afanasyev766cea72014-04-24 19:16:42 -070015// correct way to include ndn-cxx headers
16// #include <ndn-cxx/face.hpp>
17// #include <ndn-cxx/util/scheduler.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080018#include "face.hpp"
19#include "util/scheduler.hpp"
20
Alexander Afanasyev151a8552014-04-11 00:54:43 -070021// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
22namespace ndn {
23// Additional nested namespace could be used to prevent/limit name contentions
24namespace examples {
25
Alexander Afanasyevf6468892014-01-29 01:04:14 -080026void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070027onData(Face& face,
28 const Interest& interest, Data& data)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080029{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080030 std::cout << "I: " << interest.toUri() << std::endl;
31 std::cout << "D: " << data.getName().toUri() << std::endl;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080032}
33
34void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070035onTimeout(Face& face,
36 const Interest& interest)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080037{
38 std::cout << "Timeout" << std::endl;
39}
40
41void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070042delayedInterest(Face& face)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080043{
44 std::cout << "One more Interest, delayed by the scheduler" << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070045
46 Interest i(Name("/localhost/testApp/randomData"));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080047 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070048 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080049 i.setMustBeFresh(true);
50
51 face.expressInterest(i,
Alexander Afanasyev151a8552014-04-11 00:54:43 -070052 bind(&onData, boost::ref(face), _1, _2),
53 bind(&onTimeout, boost::ref(face), _1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054}
55
Alexander Afanasyev151a8552014-04-11 00:54:43 -070056int
57main(int argc, char** argv)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080058{
59 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070060 // Explicitly create io_service object, which can be shared between Face and Scheduler
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070061 boost::asio::io_service ioService;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070062
63 Interest i(Name("/localhost/testApp/randomData"));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080064 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070065 i.setInterestLifetime(time::seconds(1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066 i.setMustBeFresh(true);
67
Alexander Afanasyev151a8552014-04-11 00:54:43 -070068 // Create face with io_service object
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070069 Face face(ioService);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080070 face.expressInterest(i,
Alexander Afanasyev151a8552014-04-11 00:54:43 -070071 bind(&onData, boost::ref(face), _1, _2),
72 bind(&onTimeout, boost::ref(face), _1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080073
74
Alexander Afanasyev151a8552014-04-11 00:54:43 -070075 // Create scheduler object
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070076 Scheduler scheduler(ioService);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070077
78 // Schedule a new event
79 scheduler.scheduleEvent(time::seconds(2),
80 bind(&delayedInterest, boost::ref(face)));
81
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070082 // ioService.run() will block until all events finished or ioService.stop() is called
83 ioService.run();
Alexander Afanasyev151a8552014-04-11 00:54:43 -070084
Alexander Afanasyevf6468892014-01-29 01:04:14 -080085 // 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 Afanasyev151a8552014-04-11 00:54:43 -070089 catch(std::exception& e) {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080090 std::cerr << "ERROR: " << e.what() << std::endl;
91 }
92 return 0;
93}
Alexander Afanasyev151a8552014-04-11 00:54:43 -070094
95} // namespace examples
96} // namespace ndn
97
98int
99main(int argc, char** argv)
100{
101 return ndn::examples::main(argc, argv);
102}