std::shared_ptr<ndn::Data> Publisher::createData(const ndn::Name& name) { static ndn::KeyChain keyChain; static std::vector<uint8_t> content(1500, '-'); auto data = std::make_shared<ndn::Data>(); data->setName(name); data->setContent(content.data(), content.size()); keyChain.sign(*data); return data; }
void onInterest(const InterestFilter& filter, const Interest& interest){ std::cout << "<< I: " << interest << std::endl; Name dataName(interest.getName()); dataName.append("-data-").appendVersion(); static const std::string content = "this is the first data package i generated! "; shared_ptr<Data> data = make_shared<Data>(); data->setName(dataName); data->setFreshnessPeriod(time::seconds(10)); data->setContent(reinterpret_cast<const uint8_t*>(content.c_str()), content.size()); m_keyChain.sign(*data); std::cout << ">> D: " << *data << std::endl; m_face.put(*data); }
shared_ptr<Data> createDatePacket(Interest& interest){ Name dataName(interest.getName()); dataName.append("-data-").appendVersion(); std::ifstream fin; fin.open("/home/hult/acg.mkv"); fin.seekg(0, fin.end); size_t length = fin.tellg(); fin.seekg(0, fin.beg); uint8_t *buffer = new uint8_t[length]; fin.read((char*)buffer, length); shared_ptr<Data> data = make_shared<Data>(); data->setName(dataName); data->setFreshnessPeriod(time::seconds(10)); data->setContent(buffer, length); m_keyChain.sign(*data); std::cout << "\n\n======================\n"; std::cout << ">> D: " << *data << std::endl; std::cout << "\n\n======================\n"; return data; }
namespace repo { using ndn::time::milliseconds; const milliseconds DEFAULT_TIME_INTERVAL(2000); enum Mode { AUTO, READFILE }; class Publisher { public: class Error : public std::runtime_error { public: using std::runtime_error::runtime_error; }; public: Publisher() : mode(AUTO) , dataPrefix("/example/data") , timeInterval(DEFAULT_TIME_INTERVAL) , duration(0) , m_scheduler(m_face.getIoService()) , m_randomDist(200, 1000) { } void run(); void autoGenerate(); void generateFromFile(); static std::shared_ptr<ndn::Data> createData(const ndn::Name& name); public: std::ifstream insertStream; Mode mode; ndn::Name dataPrefix; milliseconds timeInterval; milliseconds duration; private: ndn::Face m_face; ndn::Scheduler m_scheduler; std::uniform_int_distribution<> m_randomDist; }; void Publisher::run() { if (mode == AUTO) { m_scheduler.schedule(timeInterval, [this] { autoGenerate(); }); } else { m_scheduler.schedule(timeInterval, [this] { generateFromFile(); }); } m_face.processEvents(duration); } void Publisher::autoGenerate() { ndn::Name name = dataPrefix; name.appendNumber(m_randomDist(ndn::random::getRandomNumberEngine())); auto data = createData(name); m_face.put(*data); m_scheduler.schedule(timeInterval, [this] { autoGenerate(); }); } void Publisher::generateFromFile() { if (insertStream.eof()) { m_face.getIoService().stop(); return; } std::string name; getline(insertStream, name); auto data = createData(ndn::Name(name)); m_face.put(*data); m_scheduler.schedule(timeInterval, [this] { generateFromFile(); }); } std::shared_ptr<ndn::Data> Publisher::createData(const ndn::Name& name) { static ndn::KeyChain keyChain; static std::vector<uint8_t> content(1500, '-'); auto data = std::make_shared<ndn::Data>(); data->setName(name); data->setContent(content.data(), content.size()); keyChain.sign(*data); return data; } static void usage() { std::cerr << " Publisher [-d dataPrefix] [-f filename] [-s duration time] [-t generate time interval] \n" << " -d: specify the data prefix publisher generate\n" << " -f: specify filename that publish would read from\n" << " -s: specify the time duration of generate data\n" << " -t: specify the time interval between two data generated\n" << std::endl; exit(1); } static int main(int argc, char* argv[]) { Publisher generator; bool isAuto = false; bool isRead = false; int opt; while ((opt = getopt(argc, argv, "d:f:s:t:")) != -1) { switch (opt) { case 'd': { generator.dataPrefix = ndn::Name(std::string(optarg)); generator.mode = AUTO; isAuto = true; } break; case 'f': { isRead = true; generator.mode = READFILE; std::string str = std::string(optarg); generator.insertStream.open(str.c_str()); if (!generator.insertStream.is_open()) { std::cerr << "ERROR: cannot open " << std::string(optarg) << std::endl; return 1; } } break; case 's': try { generator.duration = milliseconds(boost::lexical_cast<uint64_t>(optarg)); } catch (const boost::bad_lexical_cast&) { std::cerr << "-s option should be an integer" << std::endl;; return 1; } break; case 't': try { generator.timeInterval = milliseconds(boost::lexical_cast<uint64_t>(optarg)); } catch (const boost::bad_lexical_cast&) { std::cerr << "-t option should be an integer" << std::endl;; return 1; } break; default: usage(); break; } } argc -= optind; argv += optind; if (argc != 0) usage(); if (isAuto && isRead) usage(); generator.run(); return 0; } } // namespace repo