blob: 36f4b6b836c89f0280c173ea0af4805d921c1433 [file] [log] [blame]
Eric Newberrya9907782016-11-30 13:37:14 -07001/**
2 * Consumer for the NextHopFaceId test (test_nexthopfaceid)
3 *
Eric Newberry8b367fb2018-06-06 22:48:28 -07004 * Author: Eric Newberry <enewberry@cs.arizona.edu>
Eric Newberrya9907782016-11-30 13:37:14 -07005 *
6 * Based on ndn-cxx example consumer
7 */
8
Eric Newberry3d5b55d2017-08-31 13:07:06 -07009#include <ndn-cxx/face.hpp>
10#include <ndn-cxx/lp/nack.hpp>
Eric Newberrya9907782016-11-30 13:37:14 -070011#include <ndn-cxx/lp/tags.hpp>
12#include <ndn-cxx/mgmt/nfd/controller.hpp>
13
14#include <cstring>
15#include <cstdlib>
Eric Newberry8b367fb2018-06-06 22:48:28 -070016#include <iostream>
Eric Newberrya9907782016-11-30 13:37:14 -070017
18using namespace ndn;
19
20class TestNextHopFaceIdConsumer : noncopyable
21{
22public:
23 void
24 run(Name name, bool enableLocalFields, int nextHopFaceId)
25 {
26 nfd::ControlParameters params;
27 params.setFlagBit(nfd::BIT_LOCAL_FIELDS_ENABLED, enableLocalFields);
28
Eric Newberry3d5b55d2017-08-31 13:07:06 -070029 nfd::Controller controller(m_face, m_keyChain);
30 controller.start<nfd::FaceUpdateCommand>(params,
31 nullptr,
32 [] (const nfd::ControlResponse& resp) {
33 BOOST_THROW_EXCEPTION(
34 std::runtime_error("Unable to toggle local fields"));
35 });
Eric Newberrya9907782016-11-30 13:37:14 -070036 m_face.processEvents();
37
38 // Now, send test case Interest
39 Interest interest(name);
40
41 if (nextHopFaceId != -1) {
42 interest.setTag(make_shared<lp::NextHopFaceIdTag>(nextHopFaceId));
43 }
44
45 m_face.expressInterest(interest,
46 [] (const Interest& interest, const Data& data) {
47 std::cout.write(reinterpret_cast<const char*>(data.getContent().value()),
48 data.getContent().value_size());
49 std::cout << std::endl;
50 },
51 [] (const Interest& interest, const lp::Nack& nack) {
52 std::cout << "Nack" << std::endl;
53 },
54 [] (const Interest& interest) {
55 std::cout << "Timeout" << std::endl;
56 });
57 m_face.processEvents();
58 }
59
60private:
61 Face m_face;
62 KeyChain m_keyChain;
63};
64
65static void
66usage(const char* name)
67{
68 std::cerr << "Usage: " << name
69 << " [prefix] [enableLocalFields - t/f] [NextHopFaceId (-1 if not set)]" << std::endl;
70}
71
72int
73main(int argc, char** argv)
74{
75 if (argc != 4) {
76 usage(argv[0]);
77 return -1;
78 }
79
80 Name name(argv[1]);
81
82 bool enableLocalFields = false;
83 if (std::strcmp(argv[2], "t") == 0) {
84 enableLocalFields = true;
85 }
86 else if (std::strcmp(argv[2], "f") == 0) {
87 enableLocalFields = false;
88 }
89 else {
90 usage(argv[0]);
91 return -1;
92 }
93
94 int nextHopFaceId = std::atoi(argv[3]);
95
96 try {
97 TestNextHopFaceIdConsumer consumer;
98 consumer.run(name, enableLocalFields, nextHopFaceId);
99 }
100 catch (const std::exception& e) {
101 std::cerr << "ERROR: " << e.what() << std::endl;
102 return -2;
103 }
104
105 return 0;
106}