ping: refactor ping client to use a modular architecture
refs #2702
Change-Id: I0ee0403f84eaec35bdbf07af7b3bb52558113452
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