Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
ping: Code refactoring
Change-Id: I8b4e2c9dd3ba3adfae8d296a5635b048b35cf593
Refs: #3137
  • Loading branch information
cawka committed Aug 31, 2015
1 parent 7f43c53 commit 1e7a7b2
Show file tree
Hide file tree
Showing 13 changed files with 240 additions and 171 deletions.
1 change: 1 addition & 0 deletions core/common.hpp
Expand Up @@ -64,6 +64,7 @@
#include <ndn-cxx/security/signing-helpers.hpp>
#include <ndn-cxx/security/signing-info.hpp>
#include <ndn-cxx/util/scheduler.hpp>
#include <ndn-cxx/util/scheduler-scoped-event-id.hpp>
#include <ndn-cxx/util/signal.hpp>

namespace ndn {
Expand Down
7 changes: 4 additions & 3 deletions tests/ping/integrated.t.cpp
Expand Up @@ -22,14 +22,15 @@
#include <ndn-cxx/util/dummy-client-face.hpp>

#include "tests/test-common.hpp"
#include "../identity-management-time-fixture.hpp"

namespace ndn {
namespace ping {
namespace tests {

using namespace ndn::tests;

class PingIntegratedFixture : public UnitTestTimeFixture
class PingIntegratedFixture : public IdentityManagementTimeFixture
{
public:
PingIntegratedFixture()
Expand Down Expand Up @@ -93,7 +94,7 @@ BOOST_FIXTURE_TEST_CASE(Normal, PingIntegratedFixture)
serverOpts.nMaxPings = 4;
serverOpts.shouldPrintTimestamp = false;
serverOpts.payloadSize = 0;
server.reset(new server::PingServer(*serverFace, serverOpts));
server.reset(new server::PingServer(*serverFace, m_keyChain, serverOpts));
BOOST_REQUIRE_EQUAL(0, server->getNPings());
server->start();

Expand Down Expand Up @@ -128,7 +129,7 @@ BOOST_FIXTURE_TEST_CASE(Timeout, PingIntegratedFixture)
serverOpts.nMaxPings = 4;
serverOpts.shouldPrintTimestamp = false;
serverOpts.payloadSize = 0;
server.reset(new server::PingServer(*serverFace, serverOpts));
server.reset(new server::PingServer(*serverFace, m_keyChain, serverOpts));
BOOST_REQUIRE_EQUAL(0, server->getNPings());
server->start();

Expand Down
5 changes: 3 additions & 2 deletions tests/ping/server/ping-server.t.cpp
Expand Up @@ -21,6 +21,7 @@
#include <ndn-cxx/util/dummy-client-face.hpp>

#include "tests/test-common.hpp"
#include "../../identity-management-time-fixture.hpp"

namespace ndn {
namespace ping {
Expand All @@ -31,13 +32,13 @@ using namespace ndn::tests;

BOOST_AUTO_TEST_SUITE(PingServerPingServer)

class CreatePingServerFixture : public UnitTestTimeFixture
class CreatePingServerFixture : public IdentityManagementTimeFixture
{
protected:
CreatePingServerFixture()
: face(util::makeDummyClientFace(io, {false, true}))
, pingOptions(makeOptions())
, pingServer(*face, pingOptions)
, pingServer(*face, m_keyChain, pingOptions)
{
}

Expand Down
144 changes: 84 additions & 60 deletions tools/ping/client/ndn-ping.cpp
Expand Up @@ -31,6 +31,89 @@ namespace ndn {
namespace ping {
namespace client {

class Runner : noncopyable
{
public:
explicit
Runner(const Options& options)
: m_ping(m_face, options)
, m_statisticsCollector(m_ping, options)
, m_tracer(m_ping, options)
, m_signalSetInt(m_face.getIoService(), SIGINT)
, m_signalSetQuit(m_face.getIoService(), SIGQUIT)
{
m_signalSetInt.async_wait(bind(&Runner::afterIntSignal, this, _1));
m_signalSetQuit.async_wait(bind(&Runner::afterQuitSignal, this, _1));

m_ping.afterFinish.connect([this] {
this->cancel();
});
}

int
run()
{
try {
m_ping.start();
m_face.processEvents();
}
catch (std::exception& e) {
m_tracer.onError(e.what());
return 2;
}

Statistics statistics = m_statisticsCollector.computeStatistics();

std::cout << statistics << std::endl;

if (statistics.nReceived == statistics.nSent) {
return 0;
}
else {
return 1;
}
}

private:
void
cancel()
{
m_signalSetInt.cancel();
m_signalSetQuit.cancel();
m_ping.stop();
}

void
afterIntSignal(const boost::system::error_code& errorCode)
{
if (errorCode == boost::asio::error::operation_aborted) {
return;
}

cancel();
}

void
afterQuitSignal(const boost::system::error_code& errorCode)
{
if (errorCode == boost::asio::error::operation_aborted) {
return;
}

m_statisticsCollector.computeStatistics().printSummary(std::cout);
m_signalSetQuit.async_wait(bind(&Runner::afterQuitSignal, this, _1));
};

private:
Face m_face;
Ping m_ping;
StatisticsCollector m_statisticsCollector;
Tracer m_tracer;

boost::asio::signal_set m_signalSetInt;
boost::asio::signal_set m_signalSetQuit;
};

static time::milliseconds
getMinimumPingInterval()
{
Expand Down Expand Up @@ -61,34 +144,6 @@ usage(const boost::program_options::options_description& options)
exit(2);
}

/**
* @brief SIGINT handler: print statistics and exit
*/
static void
onSigInt(Face& face, StatisticsCollector& statisticsCollector)
{
face.shutdown();
Statistics statistics = statisticsCollector.computeStatistics();
std::cout << statistics << std::endl;

if (statistics.nReceived == statistics.nSent) {
exit(0);
}
else {
exit(1);
}
}

/**
* @brief SIGQUIT handler: print statistics summary and continue
*/
static void
onSigQuit(StatisticsCollector& statisticsCollector, boost::asio::signal_set& signalSet)
{
statisticsCollector.computeStatistics().printSummary(std::cout);
signalSet.async_wait(bind(&onSigQuit, ref(statisticsCollector), ref(signalSet)));
}

int
main(int argc, char* argv[])
{
Expand Down Expand Up @@ -205,39 +260,8 @@ main(int argc, char* argv[])
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 signalSetInt(face.getIoService(), SIGINT);
signalSetInt.async_wait(bind(&onSigInt, ref(face), ref(statisticsCollector)));

boost::asio::signal_set signalSetQuit(face.getIoService(), SIGQUIT);
signalSetQuit.async_wait(bind(&onSigQuit, ref(statisticsCollector), ref(signalSetQuit)));

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;
}
return Runner(options).run();
}

} // namespace client
Expand Down
15 changes: 6 additions & 9 deletions tools/ping/client/ping.cpp
Expand Up @@ -33,24 +33,23 @@ Ping::Ping(Face& face, const Options& options)
, m_nOutstanding(0)
, m_face(face)
, m_scheduler(m_face.getIoService())
, m_nextPingEvent(m_scheduler)
{
if (m_options.shouldGenerateRandomSeq) {
m_nextSeq = random::generateWord64();
}
}

void
Ping::run()
Ping::start()
{
start();

m_face.getIoService().run();
performPing();
}

void
Ping::start()
Ping::stop()
{
performPing();
m_nextPingEvent.cancel();
}

void
Expand All @@ -73,7 +72,7 @@ Ping::performPing()
++m_nOutstanding;

if ((m_options.nPings < 0) || (m_nSent < m_options.nPings)) {
m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this));
m_nextPingEvent = m_scheduler.scheduleEvent(m_options.interval, bind(&Ping::performPing, this));
}
else {
finish();
Expand Down Expand Up @@ -105,9 +104,7 @@ Ping::finish()
return;
}

m_face.shutdown();
afterFinish();
m_face.getIoService().stop();
}

Name
Expand Down
36 changes: 24 additions & 12 deletions tools/ping/client/ping.hpp
Expand Up @@ -56,51 +56,61 @@ class Ping : noncopyable
Ping(Face& face, const Options& options);

/**
* Signals on the successful return of a packet
* @brief 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
* @brief Signals on timeout of a packet
*
* @param seq ping sequence number
*/
signal::Signal<Ping, uint64_t> afterTimeout;

/**
* Signals when finished pinging
* @brief Signals when finished pinging
*/
signal::Signal<Ping> afterFinish;

/**
* Runs the ping client (blocking)
* @brief Start sending ping interests
*
* @note This method is non-blocking and caller need to call face.processEvents()
*/
void
run();
start();

/**
* Runs the ping client (non-blocking)
* @brief Stop sending ping interests
*
* This method cancels any future ping interests and does not affect already pending interests.
*
* @todo Cancel pending ping interest
*/
void
start();
stop();

private:
/**
* Creates a ping Name from the sequence number
* @brief Creates a ping Name from the sequence number
*
* @param seq ping sequence number
*/
Name
makePingName(uint64_t seq) const;

/**
* Performs individual ping
* @brief Performs individual ping
*/
void
performPing();

/**
* Called when ping returned successfully
* @brief Called when ping returned successfully
*
* @param interest NDN interest
* @param data returned data
* @param seq ping sequence number
Expand All @@ -110,15 +120,16 @@ class Ping : noncopyable
onData(const Interest& interest, Data& data, uint64_t seq, const time::steady_clock::TimePoint& sendTime);

/**
* Called when ping timed out
* @brief 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
* @brief Called after ping received or timed out
*/
void
finish();
Expand All @@ -130,6 +141,7 @@ class Ping : noncopyable
int m_nOutstanding;
Face& m_face;
scheduler::Scheduler m_scheduler;
scheduler::ScopedEventId m_nextPingEvent;
};

} // namespace client
Expand Down

0 comments on commit 1e7a7b2

Please sign in to comment.