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;