src: Improving consistency and correcting code style
As of this commit, all data structures can be directly constructed from
wire format.
This commit excludes full correction of code style in security/ and
tools/ndnsec*, which will be part of a different commit.
Change-Id: I121ac1f81948bc7468990df52cdefeb2988d91a1
Refs: #1403
diff --git a/tools/ndncatchunks3.cpp b/tools/ndncatchunks3.cpp
index 59a75ad..460ed52 100644
--- a/tools/ndncatchunks3.cpp
+++ b/tools/ndncatchunks3.cpp
@@ -20,27 +20,29 @@
#include "face.hpp"
+namespace ndn {
+
class Consumer
{
public:
- Consumer(const std::string& data_name,
- size_t pipe_size, size_t total_seg,
+ Consumer(const std::string& dataName,
+ size_t pipeSize, size_t nTotalSegments,
int scope = -1, bool mustBeFresh = true)
- : m_data_name(data_name)
- , m_pipe_size(pipe_size)
- , m_total_seg(total_seg)
- , m_next_seg(0)
- , m_total_size(0)
- , m_output(false)
+ : m_dataName(dataName)
+ , m_pipeSize(pipeSize)
+ , m_nTotalSegments(nTotalSegments)
+ , m_nextSegment(0)
+ , m_totalSize(0)
+ , m_isOutputEnabled(false)
, m_scope(scope)
, m_mustBeFresh(mustBeFresh)
{
}
inline void
- enable_output()
+ enableOutput()
{
- m_output = true;
+ m_isOutputEnabled = true;
}
void
@@ -48,18 +50,18 @@
private:
void
- on_data(const ndn::Interest& interest, ndn::Data& data);
+ onData(const Interest& interest, Data& data);
void
- on_timeout(const ndn::Interest& interest);
+ onTimeout(const Interest& interest);
- ndn::Face m_face;
- ndn::Name m_data_name;
- size_t m_pipe_size;
- size_t m_total_seg;
- size_t m_next_seg;
- size_t m_total_size;
- bool m_output; // set to false by default
+ Face m_face;
+ Name m_dataName;
+ size_t m_pipeSize;
+ size_t m_nTotalSegments;
+ size_t m_nextSegment;
+ size_t m_totalSize;
+ bool m_isOutputEnabled; // set to false by default
int m_scope;
bool m_mustBeFresh;
@@ -70,18 +72,18 @@
{
try
{
- for (size_t i = 0; i < m_pipe_size; i++)
- {
- ndn::Interest interest(ndn::Name(m_data_name).appendSegment(m_next_seg++));
- interest.setInterestLifetime(ndn::time::milliseconds(4000));
- if (m_scope >= 0)
+ for (size_t i = 0; i < m_pipeSize; i++)
+ {
+ Interest interest(Name(m_dataName).appendSegment(m_nextSegment++));
+ interest.setInterestLifetime(time::milliseconds(4000));
+ if (m_scope >= 0)
interest.setScope(m_scope);
- interest.setMustBeFresh(m_mustBeFresh);
+ interest.setMustBeFresh(m_mustBeFresh);
- m_face.expressInterest(interest,
- ndn::bind(&Consumer::on_data, this, _1, _2),
- ndn::bind(&Consumer::on_timeout, this, _1));
- }
+ m_face.expressInterest(interest,
+ bind(&Consumer::onData, this, _1, _2),
+ bind(&Consumer::onTimeout, this, _1));
+ }
// processEvents will block until the requested data received or timeout occurs
m_face.processEvents();
@@ -93,85 +95,86 @@
}
void
-Consumer::on_data(const ndn::Interest& interest, ndn::Data& data)
+Consumer::onData(const Interest& interest, Data& data)
{
- const ndn::Block& content = data.getContent();
- const ndn::Name& name = data.getName();
+ const Block& content = data.getContent();
+ const Name& name = data.getName();
- if (m_output)
+ if (m_isOutputEnabled)
{
std::cout.write(reinterpret_cast<const char*>(content.value()), content.value_size());
}
- m_total_size += content.value_size();
+ m_totalSize += content.value_size();
- if (name[-1].toSegment() + 1 == m_total_seg)
+ if (name[-1].toSegment() + 1 == m_nTotalSegments)
{
std::cerr << "Last segment received." << std::endl;
- std::cerr << "Total # bytes of content received: " << m_total_size << std::endl;
+ std::cerr << "Total # bytes of content received: " << m_totalSize << std::endl;
}
else
{
// Send interest for next segment
- ndn::Interest interest(ndn::Name(m_data_name).appendSegment(m_next_seg++));
+ Interest interest(Name(m_dataName).appendSegment(m_nextSegment++));
if (m_scope >= 0)
interest.setScope(m_scope);
- interest.setInterestLifetime(ndn::time::milliseconds(4000));
+ interest.setInterestLifetime(time::milliseconds(4000));
interest.setMustBeFresh(m_mustBeFresh);
m_face.expressInterest(interest,
- ndn::bind(&Consumer::on_data, this, _1, _2),
- ndn::bind(&Consumer::on_timeout, this, _1));
+ bind(&Consumer::onData, this, _1, _2),
+ bind(&Consumer::onTimeout, this, _1));
}
}
void
-Consumer::on_timeout(const ndn::Interest& interest)
+Consumer::onTimeout(const Interest& interest)
{
//XXX: currently no retrans
- std::cerr << "TIMEOUT: last interest sent for segment #" << (m_next_seg - 1) << std::endl;
+ std::cerr << "TIMEOUT: last interest sent for segment #" << (m_nextSegment - 1) << std::endl;
}
int
usage(const std::string &filename)
{
- std::cerr << "Usage: \n " << filename << " [-p pipe_size] [-c total_segment] [-o] /ndn/name\n";
+ std::cerr << "Usage: \n "
+ << filename << " [-p pipeSize] [-c nTotalSegmentsment] [-o] /ndn/name\n";
return 1;
}
int
-main(int argc, char **argv)
+main(int argc, char** argv)
{
std::string name;
- int pipe_size = 1;
- int total_seg = std::numeric_limits<int>::max();
+ int pipeSize = 1;
+ int nTotalSegments = std::numeric_limits<int>::max();
bool output = false;
int opt;
while ((opt = getopt(argc, argv, "op:c:")) != -1)
{
- switch(opt)
- {
+ switch (opt)
+ {
case 'p':
- pipe_size = atoi(optarg);
- if (pipe_size <= 0)
- pipe_size = 1;
- std::cerr << "main(): set pipe size = " << pipe_size << std::endl;
- break;
- case 'c':
- total_seg = atoi(optarg);
- if (total_seg <= 0)
- total_seg = 1;
- std::cerr << "main(): set total seg = " << total_seg << std::endl;
- break;
- case 'o':
- output = true;
- break;
+ pipeSize = atoi(optarg);
+ if (pipeSize <= 0)
+ pipeSize = 1;
+ std::cerr << "main(): set pipe size = " << pipeSize << std::endl;
+ break;
+ case 'c':
+ nTotalSegments = atoi(optarg);
+ if (nTotalSegments <= 0)
+ nTotalSegments = 1;
+ std::cerr << "main(): set total seg = " << nTotalSegments << std::endl;
+ break;
+ case 'o':
+ output = true;
+ break;
default:
- return usage(argv[0]);
+ return usage(argv[0]);
}
}
@@ -185,12 +188,20 @@
return usage(argv[0]);
}
- Consumer consumer(name, pipe_size, total_seg);
+ Consumer consumer(name, pipeSize, nTotalSegments);
if (output)
- consumer.enable_output();
+ consumer.enableOutput();
consumer.run();
return 0;
}
+
+} // namespace ndn
+
+int
+main(int argc, char** argv)
+{
+ return ndn::main(argc, argv);
+}
diff --git a/tools/ndnputchunks3.cpp b/tools/ndnputchunks3.cpp
index bb6435b..e03e6cf 100644
--- a/tools/ndnputchunks3.cpp
+++ b/tools/ndnputchunks3.cpp
@@ -21,28 +21,30 @@
#include "face.hpp"
#include "security/key-chain.hpp"
-#define MAX_SEG_SIZE 4096
+namespace ndn {
+
+const size_t MAX_SEG_SIZE = 4096;
class Producer
{
public:
Producer(const char* name)
: m_name(name)
- , m_verbose(false)
+ , m_isVerbose(false)
{
int segnum = 0;
char* buf = new char[MAX_SEG_SIZE];
do
{
std::cin.read(buf, MAX_SEG_SIZE);
- int got = std::cin.gcount ();
+ int got = std::cin.gcount();
if (got > 0)
{
- ndn::shared_ptr<ndn::Data> data =
- ndn::make_shared<ndn::Data>(ndn::Name(m_name).appendSegment(segnum));
+ shared_ptr<Data> data =
+ make_shared<Data>(Name(m_name).appendSegment(segnum));
- data->setFreshnessPeriod(ndn::time::milliseconds(10000)); // 10 sec
+ data->setFreshnessPeriod(time::milliseconds(10000)); // 10 sec
data->setContent(reinterpret_cast<const uint8_t*>(buf), got);
m_keychain.sign(*data);
@@ -52,14 +54,14 @@
}
while (static_cast<bool>(std::cin));
- if (m_verbose)
+ if (m_isVerbose)
std::cerr << "Created " << segnum << " chunks for prefix [" << m_name << "]" << std::endl;
}
void
- onInterest(const ndn::Name& name, const ndn::Interest& interest)
+ onInterest(const Name& name, const Interest& interest)
{
- if (m_verbose)
+ if (m_isVerbose)
std::cerr << "<< I: " << interest << std::endl;
size_t segnum = static_cast<size_t>(interest.getName().rbegin()->toSegment());
@@ -71,7 +73,7 @@
}
void
- onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
+ onRegisterFailed(const Name& prefix, const std::string& reason)
{
std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
m_face.shutdown();
@@ -87,23 +89,23 @@
}
m_face.setInterestFilter(m_name,
- ndn::bind(&Producer::onInterest, this, _1, _2),
- ndn::bind(&Producer::onRegisterFailed, this, _1, _2));
+ bind(&Producer::onInterest, this, _1, _2),
+ bind(&Producer::onRegisterFailed, this, _1, _2));
m_face.processEvents();
}
private:
- ndn::Name m_name;
- ndn::Face m_face;
- ndn::KeyChain m_keychain;
+ Name m_name;
+ Face m_face;
+ KeyChain m_keychain;
- std::vector< ndn::shared_ptr<ndn::Data> > m_store;
+ std::vector< shared_ptr<Data> > m_store;
- bool m_verbose;
+ bool m_isVerbose;
};
int
-main(int argc, char *argv[])
+main(int argc, char** argv)
{
if (argc < 2)
{
@@ -113,17 +115,18 @@
try
{
- ndn::time::steady_clock::TimePoint startTime = ndn::time::steady_clock::now();
+ time::steady_clock::TimePoint startTime = time::steady_clock::now();
std::cerr << "Preparing the input..." << std::endl;
Producer producer(argv[1]);
- std::cerr << "Ready... (took " << (ndn::time::steady_clock::now() - startTime) << std::endl;
+ std::cerr << "Ready... (took " << (time::steady_clock::now() - startTime) << std::endl;
while (true)
{
try
{
- producer.run(); // this will exit when daemon dies... so try to connect again if possible
+ // this will exit when daemon dies... so try to connect again if possible
+ producer.run();
}
catch (std::exception& e)
{
@@ -139,3 +142,11 @@
}
return 0;
}
+
+} // namespace ndn
+
+int
+main(int argc, char** argv)
+{
+ return ndn::main(argc, argv);
+}