From 468b445581c85ef1bb9ba151c10944911b96cca3 Mon Sep 17 00:00:00 2001 From: SunnyQJm Date: Tue, 7 Apr 2020 19:52:09 +0800 Subject: [PATCH] init --- .gitignore | 5 +++ CMakeLists.txt | 10 +++++ consumer.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++ producer.cpp | 93 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 207 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 consumer.cpp create mode 100644 producer.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7861e8e --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/ndn_consumer_producer_example.cbp +/Makefile +/FacePool.h +/CMakeCache.txt +/cmake_install.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..b7dbdf3 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,10 @@ +cmake_minimum_required(VERSION 3.5) +project(ndn_consumer_producer_example) + +set(CMAKE_CXX_STANDARD 14) + +add_executable(consumer consumer.cpp FacePool.h) +target_link_libraries(consumer pthread ndn-cxx boost_system) + +add_executable(producer producer.cpp FacePool.h) +target_link_libraries(producer pthread ndn-cxx boost_system) diff --git a/consumer.cpp b/consumer.cpp new file mode 100644 index 0000000..c7803f9 --- /dev/null +++ b/consumer.cpp @@ -0,0 +1,99 @@ +/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ +/* + * Copyright (c) 2013-2018 Regents of the University of California. + * + * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). + * + * ndn-cxx library is free software: you can redistribute it and/or modify it under the + * terms of the GNU Lesser General Public License as published by the Free Software + * Foundation, either version 3 of the License, or (at your option) any later version. + * + * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A + * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. + * + * You should have received copies of the GNU General Public License and GNU Lesser + * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see + * . + * + * See AUTHORS.md for complete list of ndn-cxx authors and contributors. + * + * @author Alexander Afanasyev + */ + +#include + +#include +#include + +// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) +namespace ndn { +// Additional nested namespaces can be used to prevent/limit name conflicts + namespace examples { + + class Consumer : noncopyable { + public: + void + run() { + Interest interest(Name("/example/testApp/randomData")); + + // 如果需要传递参数,可以通过一下方式传递 + std::string param = "{\"code\": 1, \"msg\": \"balabala\"}"; + Block block(reinterpret_cast(param.data()), param.length()); + interest.setApplicationParameters(block); + + // 设置兴趣包的生存期,超过这个生存期没有收到对应的数据包,兴趣包就会触发超时 + interest.setInterestLifetime(2_s); // 2 seconds + + // 设置如果强制刷新,则不会命中途中路由器的缓存 + interest.setMustBeFresh(true); + + // 发送兴趣包 + m_face.expressInterest(interest, + bind(&Consumer::onData, this, _1, _2), + bind(&Consumer::onNack, this, _1, _2), + bind(&Consumer::onTimeout, this, _1)); + + std::cout << "Sending " << interest << std::endl; + + // 需要调用processEvents处理NDN事件,这样才能处理收到的数据包,要不然程序就直接退出了 + // 这个操作会一直阻塞,直到处理完所有的NDN事件(比如这里只发一个兴趣包的话,兴趣包被满足或者兴趣包超时,都会结束阻塞) + // processEvents will block until the requested data received or timeout occurs + m_face.processEvents(); + } + + private: + void + onData(const Interest &interest, const Data &data) { + std::cout << data << std::endl; + } + + void + onNack(const Interest &interest, const lp::Nack &nack) { + std::cout << "received Nack with reason " << nack.getReason() + << " for interest " << interest << std::endl; + } + + void + onTimeout(const Interest &interest) { + std::cout << "Timeout " << interest << std::endl; + } + + private: + Face m_face; + }; + + } // namespace examples +} // namespace ndn + +int +main(int argc, char **argv) { + ndn::examples::Consumer consumer; + try { + consumer.run(); + } + catch (const std::exception &e) { + std::cerr << "ERROR: " << e.what() << std::endl; + } + return 0; +} diff --git a/producer.cpp b/producer.cpp new file mode 100644 index 0000000..93b1123 --- /dev/null +++ b/producer.cpp @@ -0,0 +1,93 @@ +// +// Created by sunny on 2020/4/1. +// + +#include +#include +#include +#include + +using namespace std; +// Enclosing code in ndn simplifies coding (can also use `using namespace ndn`) +namespace ndn { +// Additional nested namespaces should be used to prevent/limit name conflicts + namespace examples { + + class Producer { + public: + Producer() {} + + void + run() { + cout << "before setInterestFilter" << endl; + // 向NFD注册一个名称前缀 /example/testApp + // 如果成功,NFD会在接收到以这个前缀开头的兴趣包转发给本程序,Producer::onInterest回调会被调用 + m_face.setInterestFilter("/example/testApp", + bind(&Producer::onInterest, this, _1, _2), + nullptr, // RegisterPrefixSuccessCallback is optional + bind(&Producer::onRegisterFailed, this, _1, _2)); + cout << "before processEvents" << endl; + + // 处理NDN事件,Producer::onInterest回调是有这个函数里面调用的 + // 意味着Producer::onInterest回调会在调用 face.processEvents 的线程运行 + // 这个函数是一个阻塞函数,调用这个函数会期望处理所有相关的事件,如果向上面那样注册了前缀监听,则会一直处理事件 + m_face.processEvents(); + cout << "after processEvents" << endl; + } + + private: + void + onInterest(const InterestFilter &, const Interest &interest) { + std::cout << ">> I: " << interest << std::endl; + + // 这边可以提取出来请求的参数 + const Block &block = interest.getApplicationParameters(); + std::string s(reinterpret_cast(block.wire()), block.size()); + std::cout << "收到的参数: " << s << std::endl; + + static const std::string content("Hello, world!"); + + // Create Data packet + auto data = make_shared(interest.getName()); + data->setFreshnessPeriod(10_s); + data->setContent(reinterpret_cast(content.data()), content.size()); + + // Sign Data packet with default identity + m_keyChain.sign(*data); + // m_keyChain.sign(*data, signingByIdentity()); + // m_keyChain.sign(*data, signingByKey()); + // m_keyChain.sign(*data, signingByCertificate()); + // m_keyChain.sign(*data, signingWithSha256()); + + // Return Data packet to the requester + std::cout << "<< D: " << *data << std::endl; + m_face.put(*data); + } + + void + onRegisterFailed(const Name &prefix, const std::string &reason) { + std::cerr << "ERROR: Failed to register prefix '" << prefix + << "' with the local forwarder (" << reason << ")" << std::endl; + m_face.shutdown(); + } + + private: + Face m_face; + KeyChain m_keyChain; + }; + + } // namespace examples +} // namespace ndn + +int +main(int argc, char **argv) { + try { + ndn::examples::Producer producer; + producer.run(); + return 0; + } + catch (const std::exception &e) { + std::cerr << "ERROR: " << e.what() << std::endl; + return 1; + } +}