blob: bba9a7f1bc34f2db18e9c890f4a6242e23c02f69 [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 -080012#include <stdexcept>
13
14#if NDN_CPP_HAVE_CXX11
15// In the std library, the placeholders are in a different namespace than boost.
16using namespace ndn::func_lib::placeholders;
17#endif
18
19void
20onData(ndn::Face &face,
21 const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest, const ndn::ptr_lib::shared_ptr<ndn::Data> &data)
22{
23 std::cout << "I: " << interest->toUri() << std::endl;
24 std::cout << "D: " << data->getName().toUri() << std::endl;
25}
26
27void
28onTimeout(ndn::Face &face,
29 const ndn::ptr_lib::shared_ptr<const ndn::Interest> &interest)
30{
31 std::cout << "Timeout" << std::endl;
32}
33
34void
35BlockPrinter(const ndn::Block &block, const std::string &indent="")
36{
37 std::cout << indent << block.type() << " (" << block.value_size() << ") [[";
38 std::cout.write(reinterpret_cast<const char *>(block.value()), block.value_size());
39 std::cout<< "]]" << std::endl;
40
41 for(ndn::Block::element_const_iterator i = block.getAll().begin();
42 i != block.getAll().end();
43 ++i)
44 {
45 BlockPrinter(*i, indent+" ");
46 }
47}
48
49int main()
50{
51 try {
52 ndn::Interest i(ndn::Name("/localhost/testApp/randomData"));
53 i.setScope(1);
54 i.setInterestLifetime(1000);
55 i.setMustBeFresh(true);
56
57 ndn::Face face;
58 face.expressInterest(i,
59 ndn::func_lib::bind(onData, boost::ref(face), _1, _2),
60 ndn::func_lib::bind(onTimeout, boost::ref(face), _1));
61
62 // processEvents will block until the requested data received or timeout occurs
63 face.processEvents();
64 }
65 catch(std::exception &e) {
66 std::cerr << "ERROR: " << e.what() << std::endl;
67 }
68 return 0;
69}