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