blob: 4dd1036e834bbc021c94a8b81dcb643e77c42cfb [file] [log] [blame]
Alexander Afanasyevc4b75982014-01-09 14:51:45 -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
Alexander Afanasyev09c613f2014-01-29 00:23:58 -08008// correct way to include NDN-CPP headers
9// #include <ndn-cpp-dev/face.hpp>
10#include "face.hpp"
11
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080012void
13onData(ndn::Face &face,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080014 const ndn::Interest& interest, ndn::Data& data)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080015{
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080016 std::cout << "I: " << interest.toUri() << std::endl;
17 std::cout << "D: " << data.getName().toUri() << std::endl;
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080018}
19
20void
21onTimeout(ndn::Face &face,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080022 const ndn::Interest& interest)
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080023{
24 std::cout << "Timeout" << std::endl;
25}
26
27void
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080028BlockPrinter(const ndn::Block& block, const std::string& indent="")
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080029{
30 std::cout << indent << block.type() << " (" << block.value_size() << ") [[";
31 std::cout.write(reinterpret_cast<const char *>(block.value()), block.value_size());
32 std::cout<< "]]" << std::endl;
33
34 for(ndn::Block::element_const_iterator i = block.getAll().begin();
35 i != block.getAll().end();
36 ++i)
37 {
38 BlockPrinter(*i, indent+" ");
39 }
40}
41
42int main()
43{
44 try {
45 ndn::Interest i(ndn::Name("/localhost/testApp/randomData"));
46 i.setScope(1);
47 i.setInterestLifetime(1000);
48 i.setMustBeFresh(true);
49
50 ndn::Face face;
51 face.expressInterest(i,
Alexander Afanasyev0222fba2014-02-09 23:16:02 -080052 ndn::bind(onData, boost::ref(face), _1, _2),
53 ndn::bind(onTimeout, boost::ref(face), _1));
Alexander Afanasyevc4b75982014-01-09 14:51:45 -080054
55 // processEvents will block until the requested data received or timeout occurs
56 face.processEvents();
57 }
58 catch(std::exception &e) {
59 std::cerr << "ERROR: " << e.what() << std::endl;
60 }
61 return 0;
62}