Skip to content

Commit

Permalink
code style polishing
Browse files Browse the repository at this point in the history
  • Loading branch information
tianyuan129 committed Jul 26, 2023
1 parent 184a725 commit b9d494e
Show file tree
Hide file tree
Showing 8 changed files with 337 additions and 319 deletions.
13 changes: 7 additions & 6 deletions examples/kp-consumer-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,13 @@ class Consumer
{
ndn::Name dataName("/randomData");
m_consumer.consume(m_producerCert.getIdentity().append(dataName),
[] (const auto& result) {
std::cout << "Received data: " << std::string(result.begin(), result.end()) << std::endl;
},
[] (const auto& error) {
std::cout << "Error: " << error << std::endl;
});
[] (const auto& result) {
std::cout << "Received data: " << std::string(result.begin(), result.end()) << std::endl;
},
[] (const auto& error) {
std::cout << "Error: " << error << std::endl;
}
);

m_face.processEvents();
}
Expand Down
75 changes: 34 additions & 41 deletions examples/kp-producer-example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received copies of the GNU General Public License along with
* NAC-ABE, e.g., in COPYING.md file. If not,ndn see <http://www.gnu.org/licenses/>.
* NAC-ABE, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of NAC-ABE authors and contributors.
*/
Expand All @@ -22,7 +22,6 @@
#include <ndn-cxx/security/certificate.hpp>
#include <ndn-cxx/security/key-chain.hpp>
#include <ndn-cxx/security/signing-helpers.hpp>
#include <ndn-cxx/util/time.hpp>

#include <producer.hpp> // or <nac-abe/producer.hpp>

Expand All @@ -49,51 +48,45 @@ class Producer
{
const std::string plainText = "Hello world";
const std::vector<std::string> attributes = {"attribute"};
auto longlivedData = std::make_shared<ndn::Data>();
longlivedData->setFreshnessPeriod(ndn::time::hours(1));

std::vector<std::shared_ptr<ndn::Data>> contentData, ckData;
std::tie(contentData, ckData) = m_producer.produce("/randomData", attributes,
{reinterpret_cast<const uint8_t*>(plainText.data()),
plainText.size()}, m_signingInfo,
longlivedData, longlivedData, 50);
{reinterpret_cast<const uint8_t*>(plainText.data()), plainText.size()},
m_signingInfo
);

std::cout << "Content data object name: " << contentData.at(0)->getName().getPrefix(-1) << std::endl;
auto putSegments = [=] (auto& interest, auto& segments) {
for (auto seg : segments) {
bool exactSeg = interest.getName() == seg->getName();
bool probeSeg = (interest.getName() == seg->getName().getPrefix(-1)) &&
interest.getCanBePrefix();
if (exactSeg || probeSeg) {
std::cout << "<< D: " << seg->getName() << std::endl;
m_face.put(*seg);
std::cout << seg->getContent().size() << " bytes" << std::endl;
break;
}
}
};
m_face.setInterestFilter(m_producerCert.getIdentity(),
[=] (const auto&, const auto& interest) {
std::cout << ">> I: " << interest << std::endl;
if (interest.getName().isPrefixOf(m_cert.getName())) {
m_face.put(m_cert);
}
for (auto seg : contentData) {
bool exactSeg = interest.getName() == seg->getName();
bool probeSeg = (interest.getName() == seg->getName().getPrefix(-1)) &&
interest.getCanBePrefix();
if (exactSeg || probeSeg) {
std::cout << "<< D: " << seg->getName() << std::endl;
m_face.put(*seg);
std::cout << seg->getContent().size() << " bytes" << std::endl;
break;
}
}
for (auto seg : ckData) {
bool exactSeg = interest.getName() == seg->getName();
bool probeSeg = (interest.getName() == seg->getName().getPrefix(-1)) &&
interest.getCanBePrefix();
if (exactSeg || probeSeg) {
std::cout << "<< D: " << seg->getName() << std::endl;
m_face.put(*seg);
std::cout << seg->getContent().size() << " bytes" << std::endl;
break;
}
}
},
[this] (const auto& prefix, const std::string& reason) {
std::cerr << "ERROR: Failed to register prefix '" << prefix
<< "' with the local forwarder (" << reason << ")" << std::endl;
m_face.shutdown();
});

[=] (const auto&, const auto& interest) {
std::cout << ">> I: " << interest << std::endl;
// for own certificate
if (interest.getName().isPrefixOf(m_cert.getName())) {
m_face.put(m_cert);
}
// for content data segments
putSegments(interest, contentData);
// for CK data segments
putSegments(interest, ckData);
},
[this] (const auto& prefix, const std::string& reason) {
std::cerr << "ERROR: Failed to register prefix '" << prefix
<< "' with the local forwarder (" << reason << ")" << std::endl;
m_face.shutdown();
}
);
m_face.processEvents();
}

Expand Down
25 changes: 13 additions & 12 deletions tests/unit-tests/attribute-authority.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,19 @@ BOOST_AUTO_TEST_CASE(OnPublicParams)

int count = 0;
face.onSendData.connect([&] (const Data& response) {
count++;
BOOST_CHECK(security::verifySignature(response, authorityCert));

auto block = response.getContent();
Buffer contentBuffer(block.value(), block.value_size());
algo::PublicParams pubParams;
pubParams.fromBuffer(contentBuffer);
auto buffer = pubParams.toBuffer();

BOOST_CHECK_EQUAL_COLLECTIONS(buffer.begin(), buffer.end(),
requiredBuffer.begin(), requiredBuffer.end());
});
count++;
BOOST_CHECK(security::verifySignature(response, authorityCert));

auto block = response.getContent();
Buffer contentBuffer(block.value(), block.value_size());
algo::PublicParams pubParams;
pubParams.fromBuffer(contentBuffer);
auto buffer = pubParams.toBuffer();

BOOST_CHECK_EQUAL_COLLECTIONS(buffer.begin(), buffer.end(),
requiredBuffer.begin(), requiredBuffer.end());
}
);
face.receive(request);

advanceClocks(time::milliseconds(20), 60);
Expand Down
3 changes: 2 additions & 1 deletion tests/unit-tests/consumer.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ BOOST_AUTO_TEST_CASE(Constructor)
{
bool commandReceived = false;
c2.setInterestFilter(Name(attrAuthorityPrefix).append("PUBPARAMS"),
[&] (auto&&...) { commandReceived = true; });
[&] (auto&&...) { commandReceived = true; }
);

advanceClocks(time::milliseconds(20), 60);

Expand Down
Loading

0 comments on commit b9d494e

Please sign in to comment.