all: Refactoring work with time using boost::chrono
Now the library has two clocks: time::steady_clock and
time::system_clock, following (boost|std)::chrono. In addition to
standard now() method, the library contains several helpers to convert
to/from UnixTimestamp (microsecond resolution) and IsoString (optional
microsecond resolution). The IsoString conversions use
boost::posix_time routines, since boost::chrono supports extended IO
support only starting boost version 1.52 (Boost Chrono V2).
This commit breaks compatibility with previous API. All time-related
Data/Interest calls must explicitly use time units to specify
FreshnessPeriod/InterestLifetime.
Brief usage/conversion guide:
- creation of time units does not support double/float types. If
necessary to create time unit from double, ``ndn::duration<double>`` (for
seconds) needs to be used instead. In some cases, this would also
require ``ndn::duration_cast``, if the target is not ``ndn::nanoseconds``.
- ndn::getNow, ndn::ndn_getNowMilliseconds, ndn::time::now are all
removed in favor of the now() method in a specific clock:
* time::system_clock::now();
* time::steady_clock::now();
- When necessary to convert system_clock::TimePoint to unix timestamp,
``time::toUnixTimestamp`` can be used. This method return number of
milliseconds since UNIX epoch as ``ndn::time::milliseconds`` type.
Use count() method to obtain number as an integral value.
Change-Id: Icd688bc6766e008d60c3d2888173627874526e47
Refs: #1152
diff --git a/tools/ndncatchunks3.cpp b/tools/ndncatchunks3.cpp
index c912f26..cb4750a 100644
--- a/tools/ndncatchunks3.cpp
+++ b/tools/ndncatchunks3.cpp
@@ -18,36 +18,39 @@
* Author: Wentao Shang <wentao@cs.ucla.edu>
*/
-
#include "face.hpp"
class Consumer
{
public:
- Consumer (const std::string &data_name, int pipe_size, int total_seg, 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_scope(scope)
- , m_mustBeFresh(mustBeFresh)
+ Consumer(const std::string& data_name, int pipe_size, int total_seg, 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_scope(scope)
+ , m_mustBeFresh(mustBeFresh)
{
}
inline void
- enable_output ()
+ enable_output()
{
m_output = true;
}
void
- run ();
-
+ run();
+
private:
void
- on_data (const ndn::Interest& interest, ndn::Data& data);
+ on_data(const ndn::Interest& interest, ndn::Data& data);
void
- on_timeout (const ndn::Interest& interest);
-
+ on_timeout(const ndn::Interest& interest);
+
ndn::Face m_face;
ndn::Name m_data_name;
int m_pipe_size;
@@ -61,25 +64,25 @@
};
void
-Consumer::run ()
+Consumer::run()
{
try
{
for (int i = 0; i < m_pipe_size; i++)
{
- ndn::Interest inst (ndn::Name(m_data_name).appendSegment (m_next_seg++));
- inst.setInterestLifetime (4000);
+ ndn::Interest interest(ndn::Name(m_data_name).appendSegment (m_next_seg++));
+ interest.setInterestLifetime(ndn::time::milliseconds(4000));
if (m_scope >= 0)
- inst.setScope (m_scope);
- inst.setMustBeFresh (m_mustBeFresh);
-
- m_face.expressInterest (inst,
- ndn::bind (&Consumer::on_data, this, _1, _2),
- ndn::bind (&Consumer::on_timeout, this, _1));
+ interest.setScope(m_scope);
+ interest.setMustBeFresh(m_mustBeFresh);
+
+ m_face.expressInterest (interest,
+ ndn::bind(&Consumer::on_data, this, _1, _2),
+ ndn::bind(&Consumer::on_timeout, this, _1));
}
-
+
// processEvents will block until the requested data received or timeout occurs
- m_face.processEvents ();
+ m_face.processEvents();
}
catch (std::exception& e)
{
@@ -88,10 +91,10 @@
}
void
-Consumer::on_data (const ndn::Interest& interest, ndn::Data& data)
+Consumer::on_data(const ndn::Interest& interest, ndn::Data& data)
{
- const ndn::Block& content = data.getContent ();
- const ndn::Name& name = data.getName ();
+ const ndn::Block& content = data.getContent();
+ const ndn::Name& name = data.getName();
if (m_output)
{
@@ -99,8 +102,8 @@
}
m_total_size += content.value_size ();
-
- if ((int) (name.rbegin ()->toSegment ()) + 1 == m_total_seg)
+
+ if ((int)(name.rbegin ()->toSegment ()) + 1 == m_total_seg)
{
std::cerr << "Last segment received." << std::endl;
std::cerr << "Total # bytes of content received: " << m_total_size << std::endl;
@@ -108,21 +111,21 @@
else
{
// Send interest for next segment
- ndn::Interest inst (ndn::Name(m_data_name).appendSegment (m_next_seg++));
- if (m_scope >= 0)
- inst.setScope (m_scope);
- inst.setInterestLifetime (4000);
- inst.setMustBeFresh (m_mustBeFresh);
-
- m_face.expressInterest (inst,
- ndn::bind (&Consumer::on_data, this, _1, _2),
- ndn::bind (&Consumer::on_timeout, this, _1));
+ ndn::Interest interest(ndn::Name(m_data_name).appendSegment (m_next_seg++));
+ if (m_scope >= 0)
+ interest.setScope(m_scope);
+ interest.setInterestLifetime(ndn::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));
}
}
void
-Consumer::on_timeout (const ndn::Interest& interest)
+Consumer::on_timeout(const ndn::Interest& interest)
{
//XXX: currently no retrans
std::cerr << "TIMEOUT: last interest sent for segment #" << (m_next_seg - 1) << std::endl;
@@ -138,7 +141,7 @@
int
-main (int argc, char **argv)
+main(int argc, char **argv)
{
std::string name;
int pipe_size = 1;
@@ -165,7 +168,7 @@
case 'o':
output = true;
break;
- default:
+ default:
return usage(argv[0]);
}
}
@@ -174,17 +177,17 @@
{
name = argv[optind];
}
-
+
if (name.empty())
{
return usage(argv[0]);
}
-
+
Consumer consumer (name, pipe_size, total_seg);
if (output)
consumer.enable_output ();
-
+
consumer.run ();
return 0;
diff --git a/tools/ndnputchunks3.cpp b/tools/ndnputchunks3.cpp
index be03453..d631798 100644
--- a/tools/ndnputchunks3.cpp
+++ b/tools/ndnputchunks3.cpp
@@ -26,23 +26,23 @@
class Producer
{
public:
- Producer (const char* name)
- : m_name (name)
- , m_verbose (false)
+ Producer(const char* name)
+ : m_name(name)
+ , m_verbose(false)
{
int segnum = 0;
char* buf = new char[MAX_SEG_SIZE];
do
{
- std::cin.read (buf, MAX_SEG_SIZE);
+ std::cin.read(buf, MAX_SEG_SIZE);
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));
- data->setFreshnessPeriod (10000); // 10 sec
- data->setContent (reinterpret_cast<const uint8_t*>(buf), got);
-
+ data->setFreshnessPeriod(ndn::time::milliseconds(10000)); // 10 sec
+ data->setContent(reinterpret_cast<const uint8_t*>(buf), got);
+
m_keychain.sign(*data);
m_store.push_back(data);
segnum++;
@@ -55,76 +55,77 @@
}
void
- onInterest (const ndn::Name& name, const ndn::Interest& interest)
+ onInterest(const ndn::Name& name, const ndn::Interest& interest)
{
if (m_verbose)
std::cerr << "<< I: " << interest << std::endl;
-
- size_t segnum = static_cast<size_t>(interest.getName ().rbegin ()->toSegment ());
+
+ size_t segnum = static_cast<size_t>(interest.getName().rbegin()->toSegment());
if (segnum < m_store.size())
{
- m_face.put (*m_store[segnum]);
+ m_face.put(*m_store[segnum]);
}
}
void
- onRegisterFailed (const ndn::Name& prefix, const std::string& reason)
+ onRegisterFailed(const ndn::Name& prefix, const std::string& reason)
{
std::cerr << "ERROR: Failed to register prefix in local hub's daemon (" << reason << ")" << std::endl;
- m_face.shutdown ();
+ m_face.shutdown();
}
-
+
void
- run ()
+ run()
{
if (m_store.empty())
{
std::cerr << "Nothing to serve. Exiting." << std::endl;
return;
}
-
- m_face.setInterestFilter (m_name,
- ndn::bind (&Producer::onInterest, this, _1, _2),
- ndn::bind (&Producer::onRegisterFailed, this, _1, _2));
- m_face.processEvents ();
+
+ m_face.setInterestFilter(m_name,
+ ndn::bind(&Producer::onInterest, this, _1, _2),
+ ndn::bind(&Producer::onRegisterFailed, this, _1, _2));
+ m_face.processEvents();
}
private:
ndn::Name m_name;
ndn::Face m_face;
ndn::KeyChain m_keychain;
-
+
std::vector< ndn::shared_ptr<ndn::Data> > m_store;
bool m_verbose;
};
int
-main (int argc, char *argv[])
+main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: ./ndnputchunks [data_prefix]\n";
return -1;
}
-
+
try
{
- ndn::MillisecondsSince1970 time = ndn::getNow();
-
+ ndn::time::steady_clock::TimePoint startTime = ndn::time::steady_clock::now();
+
std::cerr << "Preparing the input..." << std::endl;
- Producer producer (argv[1]);
- std::cerr << "Ready... (took " << ((ndn::getNow() - time)/1000) << " seconds)" << std::endl;
- while(true)
+ Producer producer(argv[1]);
+ std::cerr << "Ready... (took " << (ndn::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
+ producer.run(); // this will exit when daemon dies... so try to connect again if possible
}
catch (std::exception& e)
{
- std::cerr << "ERROR: " << e.what () << std::endl;
+ std::cerr << "ERROR: " << e.what() << std::endl;
// and keep going
sleep (1);
}
@@ -132,7 +133,7 @@
}
catch (std::exception& e)
{
- std::cerr << "ERROR: " << e.what () << std::endl;
+ std::cerr << "ERROR: " << e.what() << std::endl;
}
return 0;
}
diff --git a/tools/ndnsec-cert-gen.hpp b/tools/ndnsec-cert-gen.hpp
index 023df7e..8b6e460 100644
--- a/tools/ndnsec-cert-gen.hpp
+++ b/tools/ndnsec-cert-gen.hpp
@@ -103,26 +103,26 @@
return 1;
}
- Time notBefore;
- Time notAfter;
+ time::system_clock::TimePoint notBefore;
+ time::system_clock::TimePoint notAfter;
try{
if (0 == vm.count("not-before"))
{
- notBefore = boost::posix_time::second_clock::universal_time();
+ notBefore = time::system_clock::now();
}
else
{
- notBefore = boost::posix_time::from_iso_string(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6));
+ notBefore = time::fromIsoString(notBeforeStr.substr(0, 8) + "T" + notBeforeStr.substr(8, 6));
}
if (0 == vm.count("not-after"))
{
- notAfter = notBefore + boost::posix_time::hours(24*365);
+ notAfter = notBefore + time::days(365);
}
else
{
- notAfter = boost::posix_time::from_iso_string(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6));
+ notAfter = time::fromIsoString(notAfterStr.substr(0, 8) + "T" + notAfterStr.substr(8, 6));
if(notAfter < notBefore)
{
std::cerr << "not-before is later than not-after" << std::endl;
@@ -195,8 +195,8 @@
CertificateSubjectDescription subDescryptName("2.5.4.41", sName);
IdentityCertificate certificate;
certificate.setName(certName);
- certificate.setNotBefore((notBefore-ndn::UNIX_EPOCH_TIME).total_milliseconds());
- certificate.setNotAfter((notAfter-ndn::UNIX_EPOCH_TIME).total_milliseconds());
+ certificate.setNotBefore(notBefore);
+ certificate.setNotAfter(notAfter);
certificate.setPublicKeyInfo(selfSignedCertificate->getPublicKeyInfo());
certificate.addSubjectDescription(subDescryptName);
for(int i = 0; i < otherSubDescrypt.size(); i++)