Skip to content

Instantly share code, notes, and snippets.

@pulsejet
Last active January 26, 2023 04:10
Show Gist options
  • Save pulsejet/54484fd244630fcb8c78e246e9305208 to your computer and use it in GitHub Desktop.
Save pulsejet/54484fd244630fcb8c78e246e9305208 to your computer and use it in GitHub Desktop.
ndn-cxx example
#include <ndn-cxx/face.hpp>
#include <iostream>
void
onData(const ndn::Interest&, const ndn::Data& data)
{
std::cout << "Received Data: " << data << std::endl;
}
void
onNack(const ndn::Interest&, const ndn::lp::Nack& nack)
{
std::cout << "Received Nack with reason " << nack.getReason() << std::endl;
}
void
onTimeout(const ndn::Interest& interest)
{
std::cout << "Timeout for " << interest << std::endl;
}
int main()
{
ndn::Face face;
ndn::Name interestName("/example/testApp/randomData");
interestName.appendVersion();
ndn::Interest interest(interestName);
interest.setMustBeFresh(true);
std::cout << "Sending Interest " << interest << std::endl;
face.expressInterest(interest, &onData, &onNack, &onTimeout);
face.processEvents();
}
#include <ndn-cxx/face.hpp>
#include <ndn-cxx/security/key-chain.hpp>
#include <iostream>
ndn::Face face;
ndn::KeyChain keyChain;
void
onInterest(const ndn::InterestFilter&, const ndn::Interest& interest)
{
std::cout << ">> I: " << interest << std::endl;
static const std::string content("Hello, world!");
auto data = ndn::make_shared<ndn::Data>(interest.getName());
data->setContent(ndn::make_span(reinterpret_cast<const uint8_t*>(content.data()), content.size()));
data->setFreshnessPeriod(ndn::time::milliseconds(10000));
keyChain.sign(*data);
std::cout << "<< D: " << *data << std::endl;
face.put(*data);
}
void
onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
{
std::cerr << "ERROR: Failed to register prefix '" << prefix
<< "' with the local forwarder (" << reason << ")" << std::endl;
}
int main()
{
face.setInterestFilter("/example/testApp/randomData",
&onInterest,
nullptr, // RegisterPrefixSuccessCallback is optional
&onRegisterFailed);
face.processEvents();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment