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 | |
Eric Newberry | 8b367fb | 2018-06-06 22:48:28 -0700 | [diff] [blame] | 18 | #include <iostream> |
| 19 | |
Eric Newberry | 608211e | 2017-11-27 21:59:37 -0700 | [diff] [blame] | 20 | using namespace ndn; |
| 21 | |
| 22 | class TestCongestionMarkProducer |
| 23 | { |
| 24 | public: |
| 25 | void |
| 26 | run(std::string prefix) |
| 27 | { |
| 28 | m_face.setInterestFilter(prefix, |
| 29 | bind(&TestCongestionMarkProducer::onInterest, this, _1, _2), |
| 30 | RegisterPrefixSuccessCallback(), |
| 31 | bind(&TestCongestionMarkProducer::onRegisterFailed, this, _1, _2)); |
| 32 | m_face.processEvents(); |
| 33 | } |
| 34 | |
| 35 | private: |
| 36 | void |
| 37 | onInterest(const InterestFilter& filter, const Interest& i) |
| 38 | { |
| 39 | static const std::string content = "0123456789"; |
| 40 | shared_ptr<Data> data = make_shared<Data>(); |
| 41 | data->setName(i.getName()); |
| 42 | data->setFreshnessPeriod(time::seconds(10)); |
| 43 | data->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size()); |
| 44 | data->setCongestionMark(i.getCongestionMark()); |
| 45 | m_keyChain.sign(*data); |
| 46 | m_face.put(*data); |
| 47 | } |
| 48 | |
| 49 | void |
| 50 | onRegisterFailed(const Name& prefix, const std::string& reason) |
| 51 | { |
| 52 | std::cerr << "Failed to register prefix " << prefix << " (reason: " + reason + ")" << std::endl; |
| 53 | m_face.shutdown(); |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | Face m_face; |
| 58 | KeyChain m_keyChain; |
| 59 | }; |
| 60 | |
| 61 | int |
| 62 | main(int argc, char** argv) |
| 63 | { |
| 64 | if (argc != 2) { |
| 65 | return 2; |
| 66 | } |
| 67 | |
| 68 | try { |
| 69 | TestCongestionMarkProducer producer; |
| 70 | producer.run(argv[1]); |
| 71 | } |
| 72 | catch (const std::exception& e) { |
| 73 | std::cerr << "ERROR: " << e.what() << std::endl; |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | return 0; |
| 78 | } |