ping: refactor ping client to use a modular architecture

refs #2702

Change-Id: I0ee0403f84eaec35bdbf07af7b3bb52558113452
diff --git a/tools/ping/client/ndn-ping.cpp b/tools/ping/client/ndn-ping.cpp
new file mode 100644
index 0000000..2115b81
--- /dev/null
+++ b/tools/ping/client/ndn-ping.cpp
@@ -0,0 +1,228 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
+ * @author: Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#include "core/common.hpp"
+#include "core/version.hpp"
+
+#include "ping.hpp"
+#include "statistics-collector.hpp"
+#include "tracer.hpp"
+
+namespace ndn {
+namespace ping {
+namespace client {
+
+static time::milliseconds
+getPingMinimumInterval()
+{
+  return time::milliseconds(1000);
+}
+
+static time::milliseconds
+getDefaultPingTimeoutThreshold()
+{
+  return time::milliseconds(4000);
+}
+
+static void
+usage(const boost::program_options::options_description& options)
+{
+  std::cout << "Usage: ndnping [options] ndn:/name/prefix\n"
+      "\n"
+      "Ping a NDN name prefix using Interests with name ndn:/name/prefix/ping/number.\n"
+      "The numbers in the Interests are randomly generated unless specified.\n"
+      "\n";
+  std::cout << options;
+  exit(2);
+}
+
+static void
+signalHandler(Face& face, StatisticsCollector& statisticsCollector)
+{
+  face.shutdown();
+  Statistics statistics = statisticsCollector.computeStatistics();
+  std::cout << statistics << "\n";
+
+  if (statistics.nReceived == statistics.nSent) {
+    exit(0);
+  }
+  else {
+    exit(1);
+  }
+}
+
+int
+main(int argc, char* argv[])
+{
+  Options options;
+  options.shouldAllowStaleData = false;
+  options.nPings = -1;
+  options.interval = time::milliseconds(getPingMinimumInterval());
+  options.timeout = time::milliseconds(getDefaultPingTimeoutThreshold());
+  options.startSeq = 0;
+  options.shouldGenerateRandomSeq = true;
+  options.shouldPrintTimestamp = false;
+
+  std::string identifier;
+
+  namespace po = boost::program_options;
+
+  po::options_description visibleOptDesc("Allowed options");
+  visibleOptDesc.add_options()
+    ("help,h", "print this message and exit")
+    ("version,V", "display version and exit")
+    ("interval,i", po::value<int>(),
+                   ("set ping interval in milliseconds (minimum " +
+                   std::to_string(getPingMinimumInterval().count()) + " ms)").c_str())
+    ("timeout,o", po::value<int>(),
+                  ("set ping timeout in milliseconds (default " +
+                  std::to_string(getDefaultPingTimeoutThreshold().count()) + " ms)").c_str())
+    ("count,c", po::value<int>(&options.nPings), "set total number of pings")
+    ("start,n", po::value<uint64_t>(&options.startSeq),
+                "set the starting seq number, the number is incremented by 1 after each Interest")
+    ("identifier,p", po::value<std::string>(&identifier),
+                     "add identifier to the Interest names before the numbers to avoid conflict")
+    ("cache,a", "allows routers to return stale Data from cache")
+    ("timestamp,t", "print timestamp with messages")
+  ;
+  po::options_description hiddenOptDesc("Hidden options");
+  hiddenOptDesc.add_options()
+    ("prefix", po::value<std::string>(), "prefix to send pings to")
+  ;
+
+  po::options_description optDesc("Allowed options");
+  optDesc.add(visibleOptDesc).add(hiddenOptDesc);
+
+  try {
+    po::positional_options_description optPos;
+    optPos.add("prefix", -1);
+
+    po::variables_map optVm;
+    po::store(po::command_line_parser(argc, argv).options(optDesc).positional(optPos).run(), optVm);
+    po::notify(optVm);
+
+    if (optVm.count("help") > 0) {
+      usage(visibleOptDesc);
+    }
+
+    if (optVm.count("version") > 0) {
+      std::cout << "ndnping " << tools::VERSION << std::endl;
+      exit(0);
+    }
+
+    if (optVm.count("prefix") > 0) {
+      options.prefix = Name(optVm["prefix"].as<std::string>());
+    }
+    else {
+      std::cerr << "ERROR: No prefix specified" << std::endl;
+      usage(visibleOptDesc);
+    }
+
+    if (optVm.count("interval") > 0) {
+      options.interval = time::milliseconds(optVm["interval"].as<int>());
+
+      if (options.interval.count() < getPingMinimumInterval().count()) {
+        std::cerr << "ERROR: Specified ping interval is less than the minimum " <<
+                     getPingMinimumInterval() << std::endl;
+        usage(visibleOptDesc);
+      }
+    }
+
+    if (optVm.count("timeout") > 0) {
+      options.timeout = time::milliseconds(optVm["timeout"].as<int>());
+    }
+
+    if (optVm.count("count") > 0) {
+      if (options.nPings <= 0) {
+        std::cerr << "ERROR: Number of ping must be positive" << std::endl;
+        usage(visibleOptDesc);
+      }
+    }
+
+    if (optVm.count("start") > 0) {
+      options.shouldGenerateRandomSeq = false;
+    }
+
+    if (optVm.count("identifier") > 0) {
+      bool isIdentifierAcceptable = std::all_of(identifier.begin(), identifier.end(), &isalnum);
+      if (identifier.empty() || !isIdentifierAcceptable) {
+        std::cerr << "ERROR: Unacceptable client identifier" << std::endl;
+        usage(visibleOptDesc);
+      }
+
+      options.clientIdentifier = name::Component(identifier);
+    }
+
+    if (optVm.count("cache") > 0) {
+      options.shouldAllowStaleData = true;
+    }
+
+    if (optVm.count("timestamp") > 0) {
+      options.shouldPrintTimestamp = true;
+    }
+  }
+  catch (const po::error& e) {
+    std::cerr << "ERROR: " << e.what() << std::endl;
+    usage(visibleOptDesc);
+  }
+
+  boost::asio::io_service ioService;
+  Face face(ioService);
+  Ping ping(face, options);
+  StatisticsCollector statisticsCollector(ping, options);
+  Tracer tracer(ping, options);
+
+  boost::asio::signal_set signalSet(face.getIoService(), SIGINT);
+  signalSet.async_wait(bind(&signalHandler, ref(face), ref(statisticsCollector)));
+
+  std::cout << "PING " << options.prefix << std::endl;
+
+  try {
+    ping.run();
+  }
+  catch (std::exception& e) {
+    tracer.onError(e.what());
+    face.getIoService().stop();
+    return 2;
+  }
+
+  Statistics statistics = statisticsCollector.computeStatistics();
+
+  std::cout << statistics << std::endl;
+
+  if (statistics.nReceived == statistics.nSent) {
+    return 0;
+  }
+  else {
+    return 1;
+  }
+}
+
+} // namespace client
+} // namespace ping
+} // namespace ndn
+
+int
+main(int argc, char** argv)
+{
+  return ndn::ping::client::main(argc, argv);
+}
diff --git a/tools/ping/client/ping.cpp b/tools/ping/client/ping.cpp
new file mode 100644
index 0000000..74cbcac
--- /dev/null
+++ b/tools/ping/client/ping.cpp
@@ -0,0 +1,123 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014-2015,  Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
+ * @author: Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#include "ping.hpp"
+
+namespace ndn {
+namespace ping {
+namespace client {
+
+Ping::Ping(Face& face, const Options& options)
+  : m_options(options)
+  , m_nSent(0)
+  , m_nextSeq(options.startSeq)
+  , m_nOutstanding(0)
+  , m_face(face)
+  , m_scheduler(m_face.getIoService())
+{
+  if (m_options.shouldGenerateRandomSeq) {
+    m_nextSeq = random::generateWord64();
+  }
+}
+
+void
+Ping::run()
+{
+  performPing();
+
+  m_face.processEvents();
+}
+
+void
+Ping::performPing()
+{
+  BOOST_ASSERT((m_options.nPings < 0) || (m_nSent < m_options.nPings));
+
+  Name pingPacketName = makePingName(m_nextSeq);
+
+  Interest interest(pingPacketName);
+  interest.setMustBeFresh(!m_options.shouldAllowStaleData);
+  interest.setInterestLifetime(m_options.timeout);
+  interest.setNonce(m_nextSeq);
+
+  m_face.expressInterest(interest,
+                         bind(&Ping::onData, this, _1, _2, m_nextSeq, time::steady_clock::now()),
+                         bind(&Ping::onTimeout, this, _1, m_nextSeq));
+
+  ++m_nSent;
+  ++m_nextSeq;
+  ++m_nOutstanding;
+
+  if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) {
+    m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this));
+  }
+  else {
+    finish();
+  }
+}
+
+void
+Ping::onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime)
+{
+  time::nanoseconds rtt = time::steady_clock::now() - sendTime;
+
+  afterResponse(seq, rtt);
+
+  finish();
+}
+
+void
+Ping::onTimeout(const Interest& interest, uint64_t seq)
+{
+  afterTimeout(seq);
+
+  finish();
+}
+
+void
+Ping::finish()
+{
+  if (--m_nOutstanding >= 0) {
+    return;
+  }
+
+  m_face.shutdown();
+  afterFinish();
+  m_face.getIoService().stop();
+}
+
+Name
+Ping::makePingName(uint64_t seq) const
+{
+  Name name(m_options.prefix);
+  name.append("ping");
+  if (!m_options.clientIdentifier.empty()) {
+    name.append(m_options.clientIdentifier);
+  }
+  name.append(std::to_string(seq));
+
+  return name;
+}
+
+} // namespace client
+} // namespace ping
+} // namespace ndn
diff --git a/tools/ping/client/ping.hpp b/tools/ping/client/ping.hpp
new file mode 100644
index 0000000..9d30067
--- /dev/null
+++ b/tools/ping/client/ping.hpp
@@ -0,0 +1,133 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2015,  Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
+ * @author: Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#ifndef NDN_TOOLS_PING_CLIENT_PING_HPP
+#define NDN_TOOLS_PING_CLIENT_PING_HPP
+
+#include "core/common.hpp"
+
+namespace ndn {
+namespace ping {
+namespace client {
+
+typedef time::duration<double, time::milliseconds::period> Rtt;
+
+/**
+ * @brief options for ndnping client
+ */
+struct Options
+{
+  Name prefix;                      //!< prefix pinged
+  bool shouldAllowStaleData;        //!< allow stale Data
+  bool shouldGenerateRandomSeq;     //!< random ping sequence
+  bool shouldPrintTimestamp;        //!< print timestamp
+  int nPings;                       //!< number of pings
+  time::milliseconds interval;      //!< ping interval
+  time::milliseconds timeout;       //!< timeout threshold
+  uint64_t startSeq;                //!< start ping sequence number
+  name::Component clientIdentifier; //!< client identifier
+};
+
+/**
+ * @brief NDN modular ping client
+ */
+class Ping : noncopyable
+{
+public:
+  Ping(Face& face, const Options& options);
+
+  /**
+   * Signals on the successful return of a packet
+   * @param seq ping sequence number
+   * @param rtt round trip time
+   */
+  signal::Signal<Ping, uint64_t, Rtt> afterResponse;
+
+  /**
+   * Signals on timeout of a packet
+   * @param seq ping sequence number
+   */
+  signal::Signal<Ping, uint64_t> afterTimeout;
+
+  /**
+   * Signals when finished pinging
+   */
+  signal::Signal<Ping> afterFinish;
+
+  /**
+   * Executes the pings
+   */
+  void
+  run();
+
+private:
+  /**
+   * Creates a ping Name from the sequence number
+   * @param seq ping sequence number
+   */
+  Name
+  makePingName(uint64_t seq) const;
+
+  /**
+   * Performs individual ping
+   */
+  void
+  performPing();
+
+  /**
+   * Called when ping returned successfully
+   * @param interest NDN interest
+   * @param data returned data
+   * @param seq ping sequence number
+   * @param sendTime time ping sent
+   */
+  void
+  onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime);
+
+  /**
+   * Called when ping timed out
+   * @param interest NDN interest
+   * @param seq ping sequence number
+   */
+  void
+  onTimeout(const Interest& interest, uint64_t seq);
+
+  /**
+   * Called after ping received or timed out
+   */
+  void
+  finish();
+
+private:
+  const Options& m_options;
+  int m_nSent;
+  uint64_t m_nextSeq;
+  int m_nOutstanding;
+  Face& m_face;
+  scheduler::Scheduler m_scheduler;
+};
+
+} // namespace client
+} // namespace ping
+} // namespace ndn
+
+#endif // NDN_TOOLS_PING_CLIENT_PING_HPP
diff --git a/tools/ping/client/statistics-collector.cpp b/tools/ping/client/statistics-collector.cpp
new file mode 100644
index 0000000..b02db12
--- /dev/null
+++ b/tools/ping/client/statistics-collector.cpp
@@ -0,0 +1,108 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2015,  Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author: Eric Newberry <enewberry@email.arizona.edu>
+ * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
+ */
+
+#include "statistics-collector.hpp"
+
+namespace ndn {
+namespace ping {
+namespace client {
+
+StatisticsCollector::StatisticsCollector(Ping& ping, const Options& options)
+  : m_ping(ping)
+  , m_options(options)
+  , m_nSent(0)
+  , m_nReceived(0)
+  , m_pingStartTime(time::steady_clock::now())
+  , m_minRtt(std::numeric_limits<double>::max())
+  , m_maxRtt(0.0)
+  , m_sumRtt(0.0)
+  , m_sumRttSquared(0.0)
+{
+  m_ping.afterResponse.connect(bind(&StatisticsCollector::recordResponse, this, _2));
+  m_ping.afterTimeout.connect(bind(&StatisticsCollector::recordTimeout, this));
+}
+
+void
+StatisticsCollector::recordResponse(Rtt rtt)
+{
+  m_nSent++;
+  m_nReceived++;
+
+  double rttMs = rtt.count();
+
+  m_minRtt = std::min(m_minRtt, rttMs);
+
+  m_maxRtt = std::max(m_maxRtt, rttMs);
+
+  m_sumRtt += rttMs;
+  m_sumRttSquared += rttMs * rttMs;
+}
+
+void
+StatisticsCollector::recordTimeout()
+{
+  m_nSent++;
+}
+
+Statistics
+StatisticsCollector::computeStatistics()
+{
+  Statistics statistics;
+
+  statistics.prefix = m_options.prefix;
+  statistics.nSent = m_nSent;
+  statistics.nReceived = m_nReceived;
+  statistics.pingStartTime = m_pingStartTime;
+  statistics.minRtt = m_minRtt;
+  statistics.maxRtt = m_maxRtt;
+  statistics.packetLossRate = (double)(m_nSent - m_nReceived) / (double)m_nSent;
+  statistics.sumRtt = m_sumRtt;
+  statistics.avgRtt = m_sumRtt / m_nReceived;
+  statistics.stdDevRtt = std::sqrt((m_sumRttSquared / m_nReceived) - (statistics.avgRtt * statistics.avgRtt));
+
+  return statistics;
+}
+
+std::ostream&
+operator<<(std::ostream& os, const Statistics& statistics)
+{
+  os << "\n";
+  os << "--- " << statistics.prefix <<" ping statistics ---\n";
+  os << statistics.nSent << " packets transmitted";
+  os << ", " << statistics.nReceived << " received";
+  os << ", " << statistics.packetLossRate * 100.0 << "% packet loss";
+  os << ", time " << statistics.sumRtt << " ms";
+  if (statistics.nReceived > 0) {
+    os << "\n";
+    os << "rtt min/avg/max/mdev = ";
+    os << statistics.minRtt << "/";
+    os << statistics.avgRtt << "/";
+    os << statistics.maxRtt << "/";
+    os << statistics.stdDevRtt << " ms";
+  }
+
+  return os;
+}
+
+} // namespace client
+} // namespace ping
+} // namespace ndn
diff --git a/tools/ping/client/statistics-collector.hpp b/tools/ping/client/statistics-collector.hpp
new file mode 100644
index 0000000..6e98577
--- /dev/null
+++ b/tools/ping/client/statistics-collector.hpp
@@ -0,0 +1,101 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2015,  Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author: Eric Newberry <enewberry@email.arizona.edu>
+ * @author: Jerald Paul Abraham <jeraldabraham@email.arizona.edu>
+ */
+
+#ifndef NDN_TOOLS_PING_CLIENT_STATISTICS_COLLECTOR_HPP
+#define NDN_TOOLS_PING_CLIENT_STATISTICS_COLLECTOR_HPP
+
+#include "core/common.hpp"
+
+#include "ping.hpp"
+
+namespace ndn {
+namespace ping {
+namespace client {
+
+/**
+ * @brief statistics data
+ */
+struct Statistics
+{
+  Name prefix;                                  //!< prefix pinged
+  int nSent;                                    //!< number of pings sent
+  int nReceived;                                //!< number of pings received
+  time::steady_clock::TimePoint pingStartTime;  //!< time pings started
+  double minRtt;                                //!< minimum round trip time
+  double maxRtt;                                //!< maximum round trip time
+  double packetLossRate;                        //!< packet loss rate
+  double sumRtt;                                //!< sum of round trip times
+  double avgRtt;                                //!< average round trip time
+  double stdDevRtt;                             //!< std dev of round trip time
+};
+
+/**
+ * @brief collects statistics from ping client
+ */
+class StatisticsCollector : noncopyable
+{
+public:
+  /**
+   * @param ping NDN ping client
+   * @param options ping client options
+   */
+  StatisticsCollector(Ping& ping, const Options& options);
+
+  /**
+   * Called on ping response received
+   * @param rtt round trip time
+   */
+  void
+  recordResponse(Rtt rtt);
+
+  /**
+   * Called on ping timeout
+   */
+  void
+  recordTimeout();
+
+  /**
+   * Returns ping statistics as structure
+   */
+  Statistics
+  computeStatistics();
+
+private:
+  Ping& m_ping;
+  const Options& m_options;
+  int m_nSent;
+  int m_nReceived;
+  time::steady_clock::TimePoint m_pingStartTime;
+  double m_minRtt;
+  double m_maxRtt;
+  double m_sumRtt;
+  double m_sumRttSquared;
+};
+
+std::ostream&
+operator<<(std::ostream& os, const Statistics& statistics);
+
+} // namespace client
+} // namespace ping
+} // namespace ndn
+
+#endif // NDN_TOOLS_PING_CLIENT_STATISTICS_COLLECTOR_HPP
diff --git a/tools/ping/client/tracer.cpp b/tools/ping/client/tracer.cpp
new file mode 100644
index 0000000..c521b20
--- /dev/null
+++ b/tools/ping/client/tracer.cpp
@@ -0,0 +1,64 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2015,  Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author: Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#include "tracer.hpp"
+
+namespace ndn {
+namespace ping {
+namespace client {
+
+Tracer::Tracer(Ping& ping, const Options& options)
+  : m_options(options)
+{
+  ping.afterResponse.connect(bind(&Tracer::onResponse, this, _1, _2));
+  ping.afterTimeout.connect(bind(&Tracer::onTimeout, this, _1));
+}
+
+void
+Tracer::onResponse(uint64_t seq, Rtt rtt)
+{
+  if (m_options.shouldPrintTimestamp) {
+    std::cout << time::toIsoString(time::system_clock::now()) << " - ";
+  }
+
+  std::cout << "content from " << m_options.prefix << ": seq=" << seq << " time="
+            << rtt.count() << " ms" << std::endl;
+}
+
+void
+Tracer::onTimeout(uint64_t seq)
+{
+  if (m_options.shouldPrintTimestamp) {
+    std::cout << time::toIsoString(time::system_clock::now()) << " - ";
+  }
+
+  std::cout << "timeout from " << m_options.prefix << ": seq=" << seq << std::endl;
+}
+
+void
+Tracer::onError(std::string msg)
+{
+  std::cerr << "ERROR: " << msg << std::endl;
+}
+
+} // namespace client
+} // namespace ping
+} // namespace ndn
diff --git a/tools/ping/client/tracer.hpp b/tools/ping/client/tracer.hpp
new file mode 100644
index 0000000..5a8cd35
--- /dev/null
+++ b/tools/ping/client/tracer.hpp
@@ -0,0 +1,74 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2015,  Arizona Board of Regents.
+ *
+ * This file is part of ndn-tools (Named Data Networking Essential Tools).
+ * See AUTHORS.md for complete list of ndn-tools authors and contributors.
+ *
+ * ndn-tools is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * ndn-tools 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * ndn-tools, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * @author: Eric Newberry <enewberry@email.arizona.edu>
+ */
+
+#ifndef NDN_TOOLS_PING_CLIENT_TRACER_HPP
+#define NDN_TOOLS_PING_CLIENT_TRACER_HPP
+
+#include "core/common.hpp"
+
+#include "ping.hpp"
+
+namespace ndn {
+namespace ping {
+namespace client {
+
+/**
+ * @brief prints ping responses and timeouts
+ */
+class Tracer : noncopyable
+{
+public:
+  /**
+   * @param ping NDN ping client
+   * @param options ping client options
+   */
+  Tracer(Ping& ping, const Options& options);
+
+  /**
+   * Prints ping results when response received
+   * @param seq ping sequence number
+   * @param rtt round trip time
+   */
+  void
+  onResponse(uint64_t seq, Rtt rtt);
+
+  /**
+   * Prints ping results when timed out
+   * @param seq ping sequence number
+   */
+  void
+  onTimeout(uint64_t seq);
+
+  /**
+   * Outputs ping errors to cerr
+   */
+  void
+  onError(std::string msg);
+
+private:
+  const Options& m_options;
+};
+
+} // namespace client
+} // namespace ping
+} // namespace ndn
+
+#endif // NDN_TOOLS_PING_CLIENT_TRACER_HPP