blob: 83bb8587d0ede3bd16c4462b9f0c44c4a7960648 [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
5 * See COPYING for copyright and distribution information.
6 */
7
8// correct way to include NDN-CPP headers
9// #include <ndn-cpp-dev/face.hpp>
10#include "face.hpp"
11#include "util/scheduler.hpp"
12
Alexander Afanasyevf6468892014-01-29 01:04:14 -080013void
14onData(ndn::Face &face,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080015 const ndn::Interest& interest, ndn::Data& data)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080016{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080017 std::cout << "I: " << interest.toUri() << std::endl;
18 std::cout << "D: " << data.getName().toUri() << std::endl;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080019}
20
21void
22onTimeout(ndn::Face &face,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080023 const ndn::Interest& interest)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080024{
25 std::cout << "Timeout" << std::endl;
26}
27
28void
29delayedInterest(ndn::Face &face)
30{
31 std::cout << "One more Interest, delayed by the scheduler" << std::endl;
32
33 ndn::Interest i(ndn::Name("/localhost/testApp/randomData"));
34 i.setScope(1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070035 i.setInterestLifetime(ndn::time::milliseconds(1000));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080036 i.setMustBeFresh(true);
37
38 face.expressInterest(i,
39 ndn::bind(&onData, boost::ref(face), _1, _2),
40 ndn::bind(&onTimeout, boost::ref(face), _1));
41}
42
Alexander Afanasyevf6468892014-01-29 01:04:14 -080043int main()
44{
45 try {
46 // Explicitly create io_service object, which can be shared between
47 // Face and Scheduler
48 ndn::shared_ptr<boost::asio::io_service> io =
49 ndn::make_shared<boost::asio::io_service>();
50
51 ndn::Interest i(ndn::Name("/localhost/testApp/randomData"));
52 i.setScope(1);
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053 i.setInterestLifetime(ndn::time::seconds(1));
Alexander Afanasyevf6468892014-01-29 01:04:14 -080054 i.setMustBeFresh(true);
55
56 ndn::Face face(io);
57 face.expressInterest(i,
58 ndn::bind(&onData, boost::ref(face), _1, _2),
59 ndn::bind(&onTimeout, boost::ref(face), _1));
60
61
62 ndn::Scheduler scheduler(*io);
63 scheduler.scheduleEvent(ndn::time::seconds(2),
64 ndn::bind(&delayedInterest, boost::ref(face)));
65
66 io->run();
67
68 // Alternatively, a helper face.processEvents() also can be called
69 // processEvents will block until the requested data received or timeout occurs
70 // face.processEvents();
71 }
72 catch(std::exception &e) {
73 std::cerr << "ERROR: " << e.what() << std::endl;
74 }
75 return 0;
76}