Eric Newberry | 608211e | 2017-11-27 21:59:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Producer for CongestionMark test (test_congestionmark) |
| 3 | * |
| 4 | * Receives Interests for a specified prefix and returns a Data packet with the same congestion mark |
| 5 | * as the corresponding Interest. |
| 6 | * |
| 7 | * Author: Eric Newberry <enewberry@cs.arizona.edu> |
| 8 | * |
| 9 | * Based on ndn-cxx example producer |
| 10 | */ |
| 11 | |
| 12 | #include <ndn-cxx/face.hpp> |
| 13 | #include <ndn-cxx/interest.hpp> |
| 14 | #include <ndn-cxx/interest-filter.hpp> |
| 15 | #include <ndn-cxx/lp/tags.hpp> |
| 16 | #include <ndn-cxx/security/key-chain.hpp> |
| 17 | |
| 18 | using namespace ndn; |
| 19 | |
| 20 | class TestCongestionMarkProducer |
| 21 | { |
| 22 | public: |
| 23 | void |
| 24 | run(std::string prefix) |
| 25 | { |
| 26 | m_face.setInterestFilter(prefix, |
| 27 | bind(&TestCongestionMarkProducer::onInterest, this, _1, _2), |
| 28 | RegisterPrefixSuccessCallback(), |
| 29 | bind(&TestCongestionMarkProducer::onRegisterFailed, this, _1, _2)); |
| 30 | m_face.processEvents(); |
| 31 | } |
| 32 | |
| 33 | private: |
| 34 | void |
| 35 | onInterest(const InterestFilter& filter, const Interest& i) |
| 36 | { |
| 37 | static const std::string content = "0123456789"; |
| 38 | shared_ptr<Data> data = make_shared<Data>(); |
| 39 | data->setName(i.getName()); |
| 40 | data->setFreshnessPeriod(time::seconds(10)); |
| 41 | data->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size()); |
| 42 | data->setCongestionMark(i.getCongestionMark()); |
| 43 | m_keyChain.sign(*data); |
| 44 | m_face.put(*data); |
| 45 | } |
| 46 | |
| 47 | void |
| 48 | onRegisterFailed(const Name& prefix, const std::string& reason) |
| 49 | { |
| 50 | std::cerr << "Failed to register prefix " << prefix << " (reason: " + reason + ")" << std::endl; |
| 51 | m_face.shutdown(); |
| 52 | } |
| 53 | |
| 54 | private: |
| 55 | Face m_face; |
| 56 | KeyChain m_keyChain; |
| 57 | }; |
| 58 | |
| 59 | int |
| 60 | main(int argc, char** argv) |
| 61 | { |
| 62 | if (argc != 2) { |
| 63 | return 2; |
| 64 | } |
| 65 | |
| 66 | try { |
| 67 | TestCongestionMarkProducer producer; |
| 68 | producer.run(argv[1]); |
| 69 | } |
| 70 | catch (const std::exception& e) { |
| 71 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | return 0; |
| 76 | } |