blob: 0f3890cc5f5ef5d5705e639be7b0439bd1860b83 [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
Alexander Afanasyev151a8552014-04-11 00:54:43 -07003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevf6468892014-01-29 01:04:14 -08004 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
Alexander Afanasyev151a8552014-04-11 00:54:43 -07005 * BSD License, see COPYING for copyright and distribution information.
Alexander Afanasyevf6468892014-01-29 01:04:14 -08006 */
7
8// correct way to include NDN-CPP headers
9// #include <ndn-cpp-dev/face.hpp>
Alexander Afanasyev151a8552014-04-11 00:54:43 -070010// #include <ndn-cpp-dev/util/scheduler.hpp>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080011#include "face.hpp"
12#include "util/scheduler.hpp"
13
Alexander Afanasyev151a8552014-04-11 00:54:43 -070014// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
15namespace ndn {
16// Additional nested namespace could be used to prevent/limit name contentions
17namespace examples {
18
Alexander Afanasyevf6468892014-01-29 01:04:14 -080019void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070020onData(Face& face,
21 const Interest& interest, Data& data)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080022{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080023 std::cout << "I: " << interest.toUri() << std::endl;
24 std::cout << "D: " << data.getName().toUri() << std::endl;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080025}
26
27void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070028onTimeout(Face& face,
29 const Interest& interest)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080030{
31 std::cout << "Timeout" << std::endl;
32}
33
34void
Alexander Afanasyev151a8552014-04-11 00:54:43 -070035delayedInterest(Face& face)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080036{
37 std::cout << "One more Interest, delayed by the scheduler" << std::endl;
Alexander Afanasyev151a8552014-04-11 00:54:43 -070038
39 Interest i(Name("/localhost/testApp/randomData"));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080040 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070041 i.setInterestLifetime(time::milliseconds(1000));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080042 i.setMustBeFresh(true);
43
44 face.expressInterest(i,
Alexander Afanasyev151a8552014-04-11 00:54:43 -070045 bind(&onData, boost::ref(face), _1, _2),
46 bind(&onTimeout, boost::ref(face), _1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080047}
48
Alexander Afanasyev151a8552014-04-11 00:54:43 -070049int
50main(int argc, char** argv)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080051{
52 try {
Alexander Afanasyev151a8552014-04-11 00:54:43 -070053 // 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 Afanasyevf6468892014-01-29 01:04:14 -080057 i.setScope(1);
Alexander Afanasyev151a8552014-04-11 00:54:43 -070058 i.setInterestLifetime(time::seconds(1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080059 i.setMustBeFresh(true);
60
Alexander Afanasyev151a8552014-04-11 00:54:43 -070061 // Create face with io_service object
62 Face face(io);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080063 face.expressInterest(i,
Alexander Afanasyev151a8552014-04-11 00:54:43 -070064 bind(&onData, boost::ref(face), _1, _2),
65 bind(&onTimeout, boost::ref(face), _1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080066
67
Alexander Afanasyev151a8552014-04-11 00:54:43 -070068 // 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 Afanasyevf6468892014-01-29 01:04:14 -080076 io->run();
Alexander Afanasyev151a8552014-04-11 00:54:43 -070077
Alexander Afanasyevf6468892014-01-29 01:04:14 -080078 // 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 Afanasyev151a8552014-04-11 00:54:43 -070082 catch(std::exception& e) {
Alexander Afanasyevf6468892014-01-29 01:04:14 -080083 std::cerr << "ERROR: " << e.what() << std::endl;
84 }
85 return 0;
86}
Alexander Afanasyev151a8552014-04-11 00:54:43 -070087
88} // namespace examples
89} // namespace ndn
90
91int
92main(int argc, char** argv)
93{
94 return ndn::examples::main(argc, argv);
95}