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