blob: 9af81dac5f0273db9abe59287887c93c5eb55871 [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);
35 i.setInterestLifetime(1000);
36 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
43void
44BlockPrinter(const ndn::Block &block, const std::string &indent="")
45{
46 std::cout << indent << block.type() << " (" << block.value_size() << ") [[";
47 std::cout.write(reinterpret_cast<const char *>(block.value()), block.value_size());
48 std::cout<< "]]" << std::endl;
49
50 for(ndn::Block::element_const_iterator i = block.getAll().begin();
51 i != block.getAll().end();
52 ++i)
53 {
54 BlockPrinter(*i, indent+" ");
55 }
56}
57
58int main()
59{
60 try {
61 // Explicitly create io_service object, which can be shared between
62 // Face and Scheduler
63 ndn::shared_ptr<boost::asio::io_service> io =
64 ndn::make_shared<boost::asio::io_service>();
65
66 ndn::Interest i(ndn::Name("/localhost/testApp/randomData"));
67 i.setScope(1);
68 i.setInterestLifetime(1000);
69 i.setMustBeFresh(true);
70
71 ndn::Face face(io);
72 face.expressInterest(i,
73 ndn::bind(&onData, boost::ref(face), _1, _2),
74 ndn::bind(&onTimeout, boost::ref(face), _1));
75
76
77 ndn::Scheduler scheduler(*io);
78 scheduler.scheduleEvent(ndn::time::seconds(2),
79 ndn::bind(&delayedInterest, boost::ref(face)));
80
81 io->run();
82
83 // Alternatively, a helper face.processEvents() also can be called
84 // processEvents will block until the requested data received or timeout occurs
85 // face.processEvents();
86 }
87 catch(std::exception &e) {
88 std::cerr << "ERROR: " << e.what() << std::endl;
89 }
90 return 0;
91}