Project

General

Profile

Bug #2629 ยป consumer-exclude.cpp

Ilya Moiseenko, 03/09/2015 05:22 PM

 
1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2
/**
3
 * Copyright (c) 2013-2014 Regents of the University of California.
4
 *
5
 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6
 *
7
 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8
 * terms of the GNU Lesser General Public License as published by the Free Software
9
 * Foundation, either version 3 of the License, or (at your option) any later version.
10
 *
11
 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13
 * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
14
 *
15
 * You should have received copies of the GNU General Public License and GNU Lesser
16
 * General Public License along with ndn-cxx, e.g., in COPYING.md file.  If not, see
17
 * <http://www.gnu.org/licenses/>.
18
 *
19
 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20
 *
21
 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22
 */
23

    
24
// correct way to include ndn-cxx headers
25
// #include <ndn-cxx/face.hpp>
26
#include "face.hpp"
27
#include "util/crypto.hpp"
28

    
29
// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`)
30
namespace ndn {
31
// Additional nested namespace could be used to prevent/limit name contentions
32
namespace examples {
33

    
34
void
35
onData(Face& face,
36
       const Interest& interest, Data& data)
37
{
38
  std::cout << "I: " << interest.toUri() << std::endl;
39
  std::cout << "D: " << data.getName().toUri() << std::endl;
40
}
41

    
42
void
43
onTimeout(Face& face,
44
          const Interest& interest)
45
{
46
  std::cout << "Timeout" << std::endl;
47
}
48

    
49
int
50
main(int argc, char** argv)
51
{
52
  try {
53
    Interest i(Name("/example/testApp/randomData"));
54
    i.setScope(1);
55
    i.setInterestLifetime(time::milliseconds(1000));
56
    i.setMustBeFresh(true);
57

    
58
    std::string content = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
59
    ndn::ConstBufferPtr implicitDigestBuffer = ndn::crypto::sha256((uint8_t*)content.c_str(), content.size());
60
    name::Component implicitDigest = name::Component::fromImplicitSha256Digest(implicitDigestBuffer);
61
    
62
    Exclude exclusion;
63
    exclusion.appendExclude(implicitDigest, false);
64
    i.setExclude(exclusion);
65

    
66
    Face face;
67
    face.expressInterest(i,
68
                         bind(onData, ref(face), _1, _2),
69
                         bind(onTimeout, ref(face), _1));
70

    
71
    // processEvents will block until the requested data received or timeout occurs
72
    face.processEvents();
73
  }
74
  catch(std::exception& e) {
75
    std::cerr << "ERROR: " << e.what() << std::endl;
76
    return 1;
77
  }
78
  return 0;
79
}
80

    
81
} // namespace examples
82
} // namespace ndn
83

    
84
int
85
main(int argc, char** argv)
86
{
87
  return ndn::examples::main(argc, argv);
88
}
89

    
    (1-1/1)