ping: Code refactoring
Change-Id: I8b4e2c9dd3ba3adfae8d296a5635b048b35cf593
Refs: #3137
diff --git a/tools/ping/server/ndn-ping-server.cpp b/tools/ping/server/ndn-ping-server.cpp
index 0a02805..2b9db41 100644
--- a/tools/ping/server/ndn-ping-server.cpp
+++ b/tools/ping/server/ndn-ping-server.cpp
@@ -30,6 +30,69 @@
namespace ping {
namespace server {
+class Runner : noncopyable
+{
+public:
+ explicit
+ Runner(const Options& options)
+ : m_options(options)
+ , m_pingServer(m_face, m_keyChain, options)
+ , m_tracer(m_pingServer, options)
+ , m_signalSetInt(m_face.getIoService(), SIGINT)
+ {
+ m_signalSetInt.async_wait(bind(&Runner::afterIntSignal, this, _1));
+
+ m_pingServer.afterFinish.connect([this] {
+ this->cancel();
+ });
+ }
+
+ int
+ run()
+ {
+ try {
+ m_pingServer.start();
+ m_face.processEvents();
+ }
+ catch (std::exception& e) {
+ std::cerr << "ERROR: " << e.what() << std::endl;
+ return 1;
+ }
+
+ std::cout << "\n--- ping server " << m_options.prefix << " ---" << std::endl;
+ std::cout << m_pingServer.getNPings() << " packets processed" << std::endl;
+
+ return 0;
+ }
+
+private:
+ void
+ cancel()
+ {
+ m_signalSetInt.cancel();
+ m_pingServer.stop();
+ }
+
+ void
+ afterIntSignal(const boost::system::error_code& errorCode)
+ {
+ if (errorCode == boost::asio::error::operation_aborted) {
+ return;
+ }
+
+ cancel();
+ }
+
+private:
+ const Options& m_options;
+ Face m_face;
+ KeyChain m_keyChain;
+ PingServer m_pingServer;
+ Tracer m_tracer;
+
+ boost::asio::signal_set m_signalSetInt;
+};
+
static time::milliseconds
getMinimumFreshnessPeriod()
{
@@ -48,36 +111,6 @@
exit(2);
}
-static void
-printStatistics(PingServer& pingServer, Options& options)
-{
- std::cout << "\n--- ping server " << options.prefix << " ---" << std::endl;
- std::cout << pingServer.getNPings() << " packets processed" << std::endl;
-}
-
-/**
- * @brief SIGINT handler: exits
- * @param pingServer pingServer instance
- * @param options options
- */
-static void
-onSigInt(PingServer& pingServer, Options& options, Face& face)
-{
- printStatistics(pingServer, options);
- face.shutdown();
- face.getIoService().stop();
- exit(1);
-}
-
-/**
- * @brief outputs errors to cerr
- */
-static void
-onError(std::string msg)
-{
- std::cerr << "ERROR: " << msg << std::endl;
-}
-
int
main(int argc, char* argv[])
{
@@ -169,30 +202,8 @@
usage(visibleOptDesc);
}
- boost::asio::io_service ioService;
- Face face(ioService);
- PingServer pingServer(face, options);
- Tracer tracer(pingServer, options);
-
- boost::asio::signal_set signalSetInt(face.getIoService(), SIGINT);
- signalSetInt.async_wait(bind(&onSigInt, ref(pingServer), ref(options), ref(face)));
-
std::cout << "PING SERVER " << options.prefix << std::endl;
-
- try {
- pingServer.run();
- }
- catch (std::exception& e) {
- onError(e.what());
- face.getIoService().stop();
- return 1;
- }
-
- printStatistics(pingServer, options);
- face.shutdown();
- face.getIoService().stop();
-
- return 0;
+ return Runner(options).run();
}
} // namespace server
@@ -203,4 +214,4 @@
main(int argc, char** argv)
{
return ndn::ping::server::main(argc, argv);
-}
\ No newline at end of file
+}
diff --git a/tools/ping/server/ping-server.cpp b/tools/ping/server/ping-server.cpp
index 799fa8d..08d17bf 100644
--- a/tools/ping/server/ping-server.cpp
+++ b/tools/ping/server/ping-server.cpp
@@ -26,11 +26,13 @@
namespace ping {
namespace server {
-PingServer::PingServer(Face& face, const Options& options)
+PingServer::PingServer(Face& face, KeyChain& keyChain, const Options& options)
: m_options(options)
+ , m_keyChain(keyChain)
, m_name(options.prefix)
, m_nPings(0)
, m_face(face)
+ , m_registeredPrefixId(nullptr)
{
shared_ptr<Buffer> b = make_shared<Buffer>();
b->assign(m_options.payloadSize, 'a');
@@ -38,22 +40,22 @@
}
void
-PingServer::run()
-{
- start();
-
- m_face.getIoService().run();
-}
-
-void
PingServer::start()
{
m_name.append("ping");
- m_face.setInterestFilter(m_name,
- bind(&PingServer::onInterest,
- this, _2),
- bind(&PingServer::onRegisterFailed,
- this, _2));
+ m_registeredPrefixId = m_face.setInterestFilter(m_name,
+ bind(&PingServer::onInterest,
+ this, _2),
+ bind(&PingServer::onRegisterFailed,
+ this, _2));
+}
+
+void
+PingServer::stop()
+{
+ if (m_registeredPrefixId != nullptr) {
+ m_face.unsetInterestFilter(m_registeredPrefixId);
+ }
}
int
@@ -77,8 +79,7 @@
++m_nPings;
if (m_options.shouldLimitSatisfied && m_options.nMaxPings > 0 && m_options.nMaxPings == m_nPings) {
- m_face.shutdown();
- m_face.getIoService().stop();
+ afterFinish();
}
}
@@ -90,4 +91,4 @@
} // namespace server
} // namespace ping
-} // namespace ndn
\ No newline at end of file
+} // namespace ndn
diff --git a/tools/ping/server/ping-server.hpp b/tools/ping/server/ping-server.hpp
index 242a54a..ff8e1c7 100644
--- a/tools/ping/server/ping-server.hpp
+++ b/tools/ping/server/ping-server.hpp
@@ -48,14 +48,20 @@
class PingServer : noncopyable
{
public:
- PingServer(Face& face, const Options& options);
+ PingServer(Face& face, KeyChain& keyChain, const Options& options);
/**
- * Signals when Interest received
+ * @brief Signals when Interest received
+ *
* @param name incoming interest name
*/
signal::Signal<PingServer, Name> afterReceive;
+ /**
+ * @brief Signals when finished pinging
+ */
+ signal::Signal<PingServer> afterFinish;
+
/** @brief starts ping server
*
* If options.shouldLimitSatisfied is false, this method does not return unless there's an error.
@@ -66,11 +72,19 @@
/**
* @brief starts the Interest filter
+ *
+ * @note This method is non-blocking and caller need to call face.processEvents()
*/
void
start();
/**
+ * @brief Unregister set interest filter
+ */
+ void
+ stop();
+
+ /**
* @brief gets the number of pings received
*/
int
@@ -78,14 +92,16 @@
private:
/**
- * Called when interest received
+ * @brief Called when interest received
+ *
* @param interest incoming interest
*/
void
onInterest(const Interest& interest);
/**
- * Called when prefix registration failed
+ * @brief Called when prefix registration failed
+ *
* @param reason reason for failure
*/
void
@@ -93,15 +109,17 @@
private:
const Options& m_options;
+ KeyChain& m_keyChain;
Name m_name;
- KeyChain m_keyChain;
int m_nPings;
Face& m_face;
Block m_payload;
+
+ const RegisteredPrefixId* m_registeredPrefixId;
};
} // namespace server
} // namespace ping
} // namespace ndn
-#endif //NDN_TOOLS_PING_SERVER_PING_SERVER_HPP
\ No newline at end of file
+#endif //NDN_TOOLS_PING_SERVER_PING_SERVER_HPP
diff --git a/tools/ping/server/tracer.cpp b/tools/ping/server/tracer.cpp
index fa4e2c8..305fc5d 100644
--- a/tools/ping/server/tracer.cpp
+++ b/tools/ping/server/tracer.cpp
@@ -43,4 +43,4 @@
} // namespace server
} // namespace ping
-} // namespace ndn
\ No newline at end of file
+} // namespace ndn
diff --git a/tools/ping/server/tracer.hpp b/tools/ping/server/tracer.hpp
index 1971f50..b8b6952 100644
--- a/tools/ping/server/tracer.hpp
+++ b/tools/ping/server/tracer.hpp
@@ -39,7 +39,7 @@
Tracer(PingServer& pingServer, const Options& options);
/**
- * Prints ping information when interest received
+ * @brief Prints ping information when interest received
* @param name interest name received
*/
void
@@ -53,4 +53,4 @@
} // namespace ping
} // namespace ndn
-#endif //NDN_TOOLS_PING_SERVER_TRACER_HPP
\ No newline at end of file
+#endif // NDN_TOOLS_PING_SERVER_TRACER_HPP